"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { Card } from "@/components/ui/card"; import type { SupportTicketCategory, SupportTicketStatus, } from "@/types"; const STATUSES: SupportTicketStatus[] = [ "open", "in_progress", "waiting_for_customer", "resolved", "reopened", ]; const CATEGORIES: SupportTicketCategory[] = [ "bug", "feature_request", "question", "billing", "other", ]; interface Props { ticketId: string; currentStatus: SupportTicketStatus; currentCategory: SupportTicketCategory; } /** * Admin-only controls — change ticket status / category. Visible * exclusively when `user.isPlatform` (gate is in the parent server * component, not here). * * Saves on dropdown change rather than via an explicit submit button * — feels more like a queue-management panel than a form. Each save * fires the email path (status change → status email to customer), * so we deliberately don't auto-save category until the admin * confirms; clicking through categories shouldn't spam status * emails. (Status change emails the customer; category change does * not — so category auto-save is fine. Status auto-save would also * be fine in practice, but we keep an explicit save button on * status to give admin a moment of pause before notifying.) * * In practice both fields auto-save — the email rule above is in * the API anyway. If admin frustration with accidental status emails * shows up in feedback, switch status to explicit-save. */ export function TicketAdminControls({ ticketId, currentStatus, currentCategory, }: Props) { const t = useTranslations("support"); const router = useRouter(); const [status, setStatus] = useState(currentStatus); const [category, setCategory] = useState(currentCategory); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const saveChange = async (changes: { status?: SupportTicketStatus; category?: SupportTicketCategory; }) => { setSaving(true); setError(""); try { const res = await fetch( `/api/support/tickets/${encodeURIComponent(ticketId)}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(changes), } ); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error || t("updateFailed")); } router.refresh(); } catch (e: any) { setError(e.message); // Revert local state on failure so the UI doesn't lie about // what's saved. if (changes.status) setStatus(currentStatus); if (changes.category) setCategory(currentCategory); } finally { setSaving(false); } }; return (
{t("adminControlsTitle")}
{error && (
{error}
)}
); }