"use client"; import { useState } from "react"; import { Button } from "../../ui/button"; import { useToast } from "../../ui/use-toast"; import type { Account } from "@/lib/db"; import { User } from "revolt-api"; import { cancelAccountDeletion, changeAccountEmail, disableAccount, queueAccountDeletion, restoreAccount, } from "@/lib/actions"; import dayjs from "dayjs"; import relativeTime from "dayjs/plugin/relativeTime"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "../../ui/alert-dialog"; import { AlertDialogDescription } from "@radix-ui/react-alert-dialog"; import { Input } from "@/components/ui/input"; dayjs.extend(relativeTime); export function AccountActions({ account, user, }: { account: Account; user?: User; }) { const { toast } = useToast(); const [accountDraft, setAccountDraft] = useState(account); const [emailDraft, setEmailDraft] = useState(""); return (
Update account email setEmailDraft(e.currentTarget.value)} value={emailDraft} /> Cancel { try { await changeAccountEmail(account._id, emailDraft); setEmailDraft(""); toast({ title: "Updated email" }); window.location.reload(); } catch (err) { toast({ title: "Failed to execute action", description: String(err), variant: "destructive", }); } }} > Change Are you sure you want to{" "} {accountDraft.disabled ? "restore" : "disable"} this account? Cancel { try { if (accountDraft.disabled) { await restoreAccount(account._id); setAccountDraft((account) => ({ ...account!, disabled: false, })); toast({ title: "Restored account", }); } else { await disableAccount(account._id); setAccountDraft((account) => ({ ...account!, disabled: true, })); toast({ title: "Disabled account", }); } } catch (err) { toast({ title: "Failed to execute action", description: String(err), variant: "destructive", }); } }} > {accountDraft.disabled ? "Restore" : "Disable"} Are you sure you want to{" "} {accountDraft.deletion?.status === "Scheduled" ? "cancel deletion of" : "queue deletion for"}{" "} this account? Cancel { try { if (accountDraft.deletion?.status === "Scheduled") { await cancelAccountDeletion(account._id); setAccountDraft((account) => ({ ...account, deletion: null, })); toast({ title: "Cancelled account deletion", }); } else { const $set = await queueAccountDeletion(account._id); setAccountDraft((account) => ({ ...account!, ...$set })); toast({ title: "Queued account for deletion", }); } } catch (err) { toast({ title: "Failed to execute action", description: String(err), variant: "destructive", }); } }} > {accountDraft.deletion?.status === "Scheduled" ? "Unqueue" : "Queue"}
); }