feat(inquiries): triage workflow on the inbox (R2-M2)
The inquiry inbox was read-only — every inquiry stayed there forever
with no way to mark "I handled this" or "this is spam." Now:
- Migration 0045 adds triage_state ('open' | 'assigned' | 'converted'
| 'dismissed' default 'open') + triaged_at + triaged_by columns to
website_submissions, plus a (port_id, triage_state, received_at)
index for the inbox query.
- New PATCH /api/v1/admin/website-submissions/[id]/triage flips the
state with audit log entry.
- List endpoint takes a `state` filter (default 'inbox' = open +
assigned, hides converted + dismissed).
- UI: per-row Convert / Assign / Dismiss / Reopen actions; second
filter row for state; triage badge per card. "Convert" jumps to
/clients with prefill_name / prefill_email / prefill_phone /
prefill_source / prefill_inquiry_id query params + marks the row
converted (the client-create form will read those — same prefill
pattern other entry points use).
1175/1175 vitest passing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
'use client';
|
||||
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useParams, useRouter } from 'next/navigation';
|
||||
import { formatDistanceToNow } from 'date-fns';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
import { PageHeader } from '@/components/shared/page-header';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
@@ -10,6 +12,9 @@ import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
|
||||
type TriageState = 'open' | 'assigned' | 'converted' | 'dismissed';
|
||||
type StateFilter = 'inbox' | 'open' | 'assigned' | 'converted' | 'dismissed' | 'all';
|
||||
|
||||
interface Submission {
|
||||
id: string;
|
||||
portId: string;
|
||||
@@ -20,8 +25,18 @@ interface Submission {
|
||||
sourceIp: string | null;
|
||||
userAgent: string | null;
|
||||
receivedAt: string;
|
||||
triageState: TriageState;
|
||||
triagedAt: string | null;
|
||||
triagedBy: string | null;
|
||||
}
|
||||
|
||||
const TRIAGE_BADGE: Record<TriageState, string> = {
|
||||
open: 'bg-blue-100 text-blue-800',
|
||||
assigned: 'bg-amber-100 text-amber-900',
|
||||
converted: 'bg-emerald-100 text-emerald-800',
|
||||
dismissed: 'bg-slate-100 text-slate-600',
|
||||
};
|
||||
|
||||
interface ListResponse {
|
||||
data: Submission[];
|
||||
pagination: { nextCursor: { receivedAt: string; id: string } | null };
|
||||
@@ -64,14 +79,21 @@ function pickPhone(payload: Record<string, unknown> | null): string {
|
||||
|
||||
export function InquiryInbox() {
|
||||
const [kind, setKind] = useState<Submission['kind'] | 'all'>('all');
|
||||
const [state, setState] = useState<StateFilter>('inbox');
|
||||
const [expanded, setExpanded] = useState<string | null>(null);
|
||||
const qc = useQueryClient();
|
||||
const router = useRouter();
|
||||
const params = useParams<{ portSlug: string }>();
|
||||
const portSlug = params?.portSlug ?? '';
|
||||
|
||||
const { data, isLoading, error } = useQuery({
|
||||
queryKey: ['inquiry-inbox', kind],
|
||||
queryFn: () =>
|
||||
apiFetch<ListResponse>(
|
||||
`/api/v1/admin/website-submissions${kind === 'all' ? '' : `?kind=${kind}`}`,
|
||||
),
|
||||
queryKey: ['inquiry-inbox', kind, state],
|
||||
queryFn: () => {
|
||||
const qs = new URLSearchParams();
|
||||
if (kind !== 'all') qs.set('kind', kind);
|
||||
qs.set('state', state);
|
||||
return apiFetch<ListResponse>(`/api/v1/admin/website-submissions?${qs}`);
|
||||
},
|
||||
});
|
||||
|
||||
const counts = data?.counts ?? {};
|
||||
@@ -79,6 +101,39 @@ export function InquiryInbox() {
|
||||
|
||||
const rows = data?.data ?? [];
|
||||
|
||||
const triageMutation = useMutation({
|
||||
mutationFn: (args: { id: string; state: TriageState }) =>
|
||||
apiFetch(`/api/v1/admin/website-submissions/${args.id}/triage`, {
|
||||
method: 'PATCH',
|
||||
body: { state: args.state },
|
||||
}),
|
||||
onSuccess: (_data, vars) => {
|
||||
qc.invalidateQueries({ queryKey: ['inquiry-inbox'] });
|
||||
toast.success(`Marked ${vars.state}.`);
|
||||
},
|
||||
onError: (err: unknown) => {
|
||||
toast.error(err instanceof Error ? err.message : 'Triage update failed');
|
||||
},
|
||||
});
|
||||
|
||||
function convertToClient(row: Submission) {
|
||||
// Mark converted then jump to /clients with prefilled query params.
|
||||
// The /clients page reads ?prefill_* on mount and opens the New
|
||||
// Client form with the values populated. Final form submission is
|
||||
// entirely operator-driven; this just hands the data over.
|
||||
const name = pickName(row.payload);
|
||||
const email = pickEmail(row.payload);
|
||||
const phone = pickPhone(row.payload);
|
||||
triageMutation.mutate({ id: row.id, state: 'converted' });
|
||||
const qs = new URLSearchParams();
|
||||
if (name) qs.set('prefill_name', name);
|
||||
if (email) qs.set('prefill_email', email);
|
||||
if (phone) qs.set('prefill_phone', phone);
|
||||
qs.set('prefill_source', 'website');
|
||||
qs.set('prefill_inquiry_id', row.id);
|
||||
router.push(`/${portSlug}/clients?${qs.toString()}`);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
@@ -88,7 +143,7 @@ export function InquiryInbox() {
|
||||
|
||||
<div className="flex items-center gap-2 mt-6 flex-wrap">
|
||||
<FilterChip
|
||||
label={`All (${totalAll})`}
|
||||
label={`All kinds (${totalAll})`}
|
||||
active={kind === 'all'}
|
||||
onClick={() => setKind('all')}
|
||||
/>
|
||||
@@ -109,6 +164,32 @@ export function InquiryInbox() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 mt-3 flex-wrap">
|
||||
<span className="text-xs text-muted-foreground mr-1">State:</span>
|
||||
<FilterChip
|
||||
label="Inbox (open + assigned)"
|
||||
active={state === 'inbox'}
|
||||
onClick={() => setState('inbox')}
|
||||
/>
|
||||
<FilterChip label="Open" active={state === 'open'} onClick={() => setState('open')} />
|
||||
<FilterChip
|
||||
label="Assigned"
|
||||
active={state === 'assigned'}
|
||||
onClick={() => setState('assigned')}
|
||||
/>
|
||||
<FilterChip
|
||||
label="Converted"
|
||||
active={state === 'converted'}
|
||||
onClick={() => setState('converted')}
|
||||
/>
|
||||
<FilterChip
|
||||
label="Dismissed"
|
||||
active={state === 'dismissed'}
|
||||
onClick={() => setState('dismissed')}
|
||||
/>
|
||||
<FilterChip label="All states" active={state === 'all'} onClick={() => setState('all')} />
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
{isLoading ? (
|
||||
<p className="text-sm text-muted-foreground py-6">Loading…</p>
|
||||
@@ -135,8 +216,9 @@ export function InquiryInbox() {
|
||||
<CardHeader className="pb-3">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Badge className={KIND_COLORS[row.kind]}>{KIND_LABELS[row.kind]}</Badge>
|
||||
<Badge className={TRIAGE_BADGE[row.triageState]}>{row.triageState}</Badge>
|
||||
<span
|
||||
className="text-xs text-muted-foreground"
|
||||
title={new Date(row.receivedAt).toISOString()}
|
||||
@@ -163,6 +245,48 @@ export function InquiryInbox() {
|
||||
{isOpen ? 'Hide payload' : 'View payload'}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{row.triageState !== 'converted' && row.triageState !== 'dismissed' && (
|
||||
<div className="mt-3 flex items-center gap-2 flex-wrap">
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => convertToClient(row)}
|
||||
disabled={triageMutation.isPending}
|
||||
>
|
||||
Convert to client
|
||||
</Button>
|
||||
{row.triageState === 'open' && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => triageMutation.mutate({ id: row.id, state: 'assigned' })}
|
||||
disabled={triageMutation.isPending}
|
||||
>
|
||||
Assign to me
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => triageMutation.mutate({ id: row.id, state: 'dismissed' })}
|
||||
disabled={triageMutation.isPending}
|
||||
>
|
||||
Dismiss
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{(row.triageState === 'converted' || row.triageState === 'dismissed') && (
|
||||
<div className="mt-3">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => triageMutation.mutate({ id: row.id, state: 'open' })}
|
||||
disabled={triageMutation.isPending}
|
||||
>
|
||||
Reopen
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</CardHeader>
|
||||
{isOpen && (
|
||||
<CardContent>
|
||||
|
||||
Reference in New Issue
Block a user