'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { toast } from 'sonner' import { UserCog } from 'lucide-react' import { createAreaLeadRole, assignAreaLead, unassignAreaLead, deleteAreaLeadRole } from '@/lib/actions' import { Card } from './card' import { useConfirm } from '@/components/ui/confirm' import { Empty, EmptyHeader, EmptyMedia, EmptyTitle, EmptyDescription } from '@/components/ui/empty' interface AreaLead { id: string area_code: string | null area_name: string lead_id: string | null lead_handle: string | null status: string } interface Member { id: string; display_name: string | null; handle: string | null } interface Props { isAdmin: boolean areaLeads: AreaLead[] members: Member[] } export function AreaLeadsPanel({ isAdmin, areaLeads: initial, members }: Props) { const router = useRouter() const confirm = useConfirm() const [rows, setRows] = useState(initial) const [showNew, setShowNew] = useState(false) const [d, setD] = useState({ area_code: '', name: '' }) function memberLabel(id: string | null) { if (!id) return null const m = members.find(x => x.id === id) return m ? (m.display_name || m.handle || 'Member') : 'Member' } return ( setShowNew(v => !v)} className="font-mono text-[10px] border border-border px-3 py-1.5 rounded hover:border-primary hover:text-primary transition whitespace-nowrap"> {showNew ? 'Cancel' : '+ Area-lead role'} ) : undefined} > {showNew && (
{ e.preventDefault() const result = await createAreaLeadRole({ area_code: d.area_code.trim().toLowerCase(), name: d.name.trim() }) if ('error' in result && result.error) { toast.error(result.error); return } toast.success('Area-lead role created') const created = (result as { data?: AreaLead }).data if (created) setRows(prev => [...prev, created]) setShowNew(false); setD({ area_code: '', name: '' }) }} className="mb-4 p-4 border border-border rounded bg-background grid grid-cols-1 md:grid-cols-2 gap-3" > setD({ ...d, area_code: e.target.value.toLowerCase().replace(/[^a-z0-9_-]/g, '-') })} placeholder="area code (e.g. woodshop)" className="bg-background border border-border text-foreground font-mono text-xs rounded px-2 py-1.5 focus:outline-none focus:border-primary" /> setD({ ...d, name: e.target.value })} placeholder="Role name (e.g. Woodshop Lead)" className="bg-background border border-border text-foreground font-sans text-sm rounded px-2 py-1.5 focus:outline-none focus:border-primary" />
)} {rows.length === 0 ? ( No area-lead roles yet Create a role per shop area, then assign a member to it. Roles without a member show as Vacant. ) : ( )}
) }