Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-17 02:08:09 +00:00
co-authored by renee-png
parent 7fe3e92254
commit 5fe3d744d1
+27 -5
View File
@@ -28,6 +28,7 @@ function ClientDetail() {
const { user, isAdmin } = useAuth();
const [client, setClient] = useState<any>(null);
const [cases, setCases] = useState<any[]>([]);
const [statusEntries, setStatusEntries] = useState<any[]>([]);
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<string, { hours: number; amount: number }> = {};
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<string, any> = {};
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);
};