diff --git a/src/routes/clients.$clientId.tsx b/src/routes/clients.$clientId.tsx index 2e539a3..4cb8fb8 100644 --- a/src/routes/clients.$clientId.tsx +++ b/src/routes/clients.$clientId.tsx @@ -28,6 +28,7 @@ function ClientDetail() { const { user, isAdmin } = useAuth(); const [client, setClient] = useState(null); const [cases, setCases] = useState([]); + const [statusEntries, setStatusEntries] = useState([]); const [loading, setLoading] = useState(true); const [editOpen, setEditOpen] = useState(false); @@ -40,12 +41,20 @@ function ClientDetail() { if (ce) toast.error("Failed to load client", { description: ce.message }); const caseIds = (cs ?? []).map((x: any) => x.id); let totals: Record = {}; + let updates: any[] = []; if (caseIds.length) { - const { data: te } = await supabase - .from("time_entries") - .select("case_id, hours, hourly_rate, billable") - .in("case_id", caseIds) - .eq("billable", true); + const [{ data: te }, { data: su }] = await Promise.all([ + supabase + .from("time_entries") + .select("case_id, hours, hourly_rate, billable") + .in("case_id", caseIds) + .eq("billable", true), + supabase + .from("status_updates") + .select("*") + .in("case_id", caseIds) + .order("title", { ascending: false }), + ]); (te ?? []).forEach((t: any) => { const h = Number(t.hours) || 0; const r = Number(t.hourly_rate) || 0; @@ -53,9 +62,22 @@ function ClientDetail() { totals[t.case_id].hours += h; totals[t.case_id].amount += h * r; }); + const ids = Array.from(new Set((su ?? []).map((d: any) => d.created_by).filter(Boolean))); + let profileMap: Record = {}; + if (ids.length) { + const { data: profs } = await supabase.from("profiles").select("id, full_name, email").in("id", ids); + profileMap = Object.fromEntries((profs ?? []).map((p: any) => [p.id, p])); + } + const caseMap = Object.fromEntries((cs ?? []).map((x: any) => [x.id, x])); + updates = (su ?? []).map((d: any) => ({ + ...d, + user: d.created_by ? profileMap[d.created_by] : null, + case: caseMap[d.case_id], + })); } setClient(c); setCases((cs ?? []).map((x: any) => ({ ...x, billed: totals[x.id] ?? { hours: 0, amount: 0 } }))); + setStatusEntries(updates); setLoading(false); };