import { useEffect, useState } from "react";
import { del, get, post, put } from "../api";
import { useT } from "../i18n";
import { useStore } from "../store";
import { toast } from "../dialogs";
import { fmtDate } from "../i18n";
interface DirUser {
id: string;
username: string;
display_name: string;
}
interface Link {
token: string;
url: string;
expires_at: number;
}
/** Read-only links for people without an account. */
function Links({ sessionId }: { sessionId: string }) {
const t = useT();
const [links, setLinks] = useState([]);
const [days, setDays] = useState(7);
const load = () => get(`/sessions/${sessionId}/links`).then(setLinks).catch(() => undefined);
useEffect(() => void load(), [sessionId]);
const create = async () => {
try {
const l = await post(`/sessions/${sessionId}/links`, { days });
await navigator.clipboard?.writeText(l.url).then(() => toast(t("link.copied"), "success"), () => undefined);
void load();
} catch (e: any) {
toast(e.message);
}
};
return (
{t("link.title")}
{t("link.hint")}
{links.map((l) => (
e.currentTarget.select()} aria-label={t("link.title")} />
{t("link.until", { date: fmtDate(l.expires_at, false) })}
))}
);
}
/** Choose users a session is shared with (read-only for them). */
export function ShareDialog({ sessionId, onClose }: { sessionId: string; onClose: () => void }) {
const t = useT();
const me = useStore((s) => s.me);
const owner = useStore((s) => s.sessionById[sessionId]?.owner_id);
const [users, setUsers] = useState([]);
const [selected, setSelected] = useState>(new Set());
const [error, setError] = useState();
useEffect(() => {
Promise.all([get("/users/directory"), get(`/sessions/${sessionId}/shares`)])
.then(([u, s]) => {
setUsers(u.filter((x) => x.id !== owner && x.id !== me?.id));
setSelected(new Set(s));
})
.catch((e) => setError(e.message));
}, [sessionId, owner, me?.id]);
const save = async () => {
try {
await put(`/sessions/${sessionId}/shares`, { users: [...selected] });
onClose();
} catch (e: any) {
setError(e.message);
}
};
return (
e.stopPropagation()}>
{t("share.title")}
{t("share.hint")}
{users.length === 0 &&
{t("share.noUsers")}
}
{users.map((u) => (
))}
{error &&
{error}
}
);
}