'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; type Props = { userId: string; role: string; banned: boolean; isSelf: boolean; }; export function UserActions({ userId, role, banned, isSelf }: Props) { const router = useRouter(); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); async function call( label: string, url: string, init: RequestInit, confirmMsg?: string, ) { if (confirmMsg && !window.confirm(confirmMsg)) return; setBusy(label); setError(null); setSuccess(null); try { const res = await fetch(url, init); if (!res.ok) { const b = (await res.json().catch(() => ({}))) as { error?: string }; throw new Error(b.error ?? `Request failed (${res.status})`); } setSuccess(`${label} succeeded`); router.refresh(); } catch (e) { setError((e as Error).message); } finally { setBusy(null); } } const jsonInit = (body: unknown): RequestInit => ({ method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); return (
{error &&

{error}

} {success &&

{success}

}
{role === 'admin' ? ( ) : ( )}
); }