"use client"; import { useState } from "react"; import { toast } from "sonner"; import { ArrowLeftRight, Loader2, Unlink } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { ConfirmDeleteDialog } from "@/components/confirm-delete-dialog"; import { AccountAvatar } from "@/components/profile/account-avatar"; import { removeAccountLink, toggleAccount } from "@/lib/actions/account-links"; import type { LinkedAccountDTO } from "@/types/profile"; /** * "Linked Accounts": the other accounts this one is linked to. * - "Toggle": signs this browser out and back in as that account (the * server verifies the link and mints a one-time switch token, so the * other account's password is never needed). * - "Remove Link": either account in the pair can do it; confirmed first. */ export function LinkedAccounts({ accounts }: { accounts: LinkedAccountDTO[] }) { const [togglingId, setTogglingId] = useState(null); const [removing, setRemoving] = useState(null); const [removingBusy, setRemovingBusy] = useState(false); async function handleToggle(account: LinkedAccountDTO) { if (togglingId) return; setTogglingId(account.id); try { const result = await toggleAccount(account.id); if (result?.url) { // The action already signed this browser in as the linked account; // navigate so the app shell re-renders for them. window.location.href = result.url; return; } toast.error(result?.error ?? "Couldn't switch to that account."); } catch { toast.error("Couldn't switch to that account. Try again."); } finally { setTogglingId(null); } } async function handleRemove() { if (!removing) return; const account = removing; setRemoving(null); setRemovingBusy(true); try { const result = await removeAccountLink(account.linkId); if (result?.error) { toast.error(result.error); return; } toast.success(`Link with ${account.email} removed.`); } catch { toast.error("Couldn't remove the link. Try again."); } finally { setRemovingBusy(false); } } return ( Linked Accounts Accounts this one is linked to. Toggle signs you in as that account; Remove Link works from either side. {accounts.map((account) => (

{account.email}

{account.name && (

{account.name}

)}
))}
!removingBusy && !open && setRemoving(null)} title={`Remove link with ${removing?.email}?`} description="Either linked account can remove the link. Removing it doesn't block anything -- the other account can still send a new link request, which you can accept again." confirmLabel="Remove Link" onConfirm={handleRemove} />
); }