import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { useAsyncFn } from "react-use"; import { SessionResponse } from "@/backend/accounts/auth"; import { base64ToBuffer, decryptData } from "@/backend/accounts/crypto"; import { removeSession } from "@/backend/accounts/sessions"; import { Button } from "@/components/buttons/Button"; import { Loading } from "@/components/layout/Loading"; import { SettingsCard } from "@/components/layout/SettingsCard"; import { SecondaryLabel } from "@/components/text/SecondaryLabel"; import { Heading2 } from "@/components/utils/Text"; import { useBackendUrl } from "@/hooks/auth/useBackendUrl"; import { useAuthStore } from "@/stores/auth"; export function Device(props: { name: string; id: string; isCurrent?: boolean; onRemove?: () => void; }) { const { t } = useTranslation(); const url = useBackendUrl(); const token = useAuthStore((s) => s.account?.token); const [result, exec] = useAsyncFn(async () => { if (!token) throw new Error("No token present"); await removeSession(url, token, props.id); props.onRemove?.(); }, [url, token, props.id]); return (
{t("settings.account.devices.deviceNameLabel")}

{props.name}

{!props.isCurrent ? ( ) : null}
); } export function DeviceListPart(props: { loading?: boolean; error?: boolean; sessions: SessionResponse[]; onChange?: () => void; }) { const { t } = useTranslation(); const seed = useAuthStore((s) => s.account?.seed); const sessions = props.sessions; const currentSessionId = useAuthStore((s) => s.account?.sessionId); const deviceListSorted = useMemo(() => { if (!seed) return []; let list = sessions.map((session) => { const decryptedName = decryptData(session.device, base64ToBuffer(seed)); return { current: session.id === currentSessionId, id: session.id, name: decryptedName, }; }); list = list.sort((a, b) => { if (a.current) return -1; if (b.current) return 1; return a.name.localeCompare(b.name); }); return list; }, [seed, sessions, currentSessionId]); if (!seed) return null; return (
{t("settings.account.devices.title")} {props.error ? (

{t("settings.account.devices.failed")}

) : props.loading ? ( ) : (
{deviceListSorted.map((session) => ( ))}
)}
); }