"use client"; import { useMemo } from "react"; import { Badge } from "@/components/ui/badge"; import { api } from "@/lib/trpc"; export interface OverviewViewProps { workspaceId?: string; spaceId?: string; } function formatUpdatedAt(value: Date | string): string { const date = value instanceof Date ? value : new Date(value); if (Number.isNaN(date.getTime())) return String(value); return date.toLocaleString(); } export function OverviewView({ workspaceId, spaceId }: OverviewViewProps) { const spaceQuery = api.objects.getById.useQuery( { id: spaceId! }, { enabled: Boolean(spaceId) }, ); const childrenQuery = api.objects.list.useQuery( { workspaceId: workspaceId!, parentId: spaceId ?? undefined, limit: 200, }, { enabled: Boolean(workspaceId) }, ); const statusCounts = useMemo(() => { const counts = { open: 0, in_progress: 0, done: 0 }; for (const row of childrenQuery.data?.objects ?? []) { const s = row.status; if (s === "in_progress") counts.in_progress += 1; else if (s === "done") counts.done += 1; else counts.open += 1; } return counts; }, [childrenQuery.data?.objects]); const recentChildren = useMemo(() => { const rows = childrenQuery.data?.objects ?? []; return [...rows] .sort((a, b) => { const ta = new Date(a.updatedAt).getTime(); const tb = new Date(b.updatedAt).getTime(); return tb - ta; }) .slice(0, 10); }, [childrenQuery.data?.objects]); const isLoading = spaceQuery.isLoading || childrenQuery.isLoading; const children = childrenQuery.data?.objects ?? []; const hasChildren = children.length > 0; const space = spaceQuery.data as { title: string } | undefined; return (
{spaceQuery.isLoading ? (
Loading space…
) : space ? (

{space.title}

) : null}

By status

{( [ { key: "open" as const, label: "Open" }, { key: "in_progress" as const, label: "In progress" }, { key: "done" as const, label: "Done" }, ] as const ).map(({ key, label }) => (
{label}
{isLoading ? "—" : statusCounts[key]}
))}

Recent activity

{childrenQuery.isLoading ? (

Loading…

) : !hasChildren ? (
No items in this space yet.
) : (
    {recentChildren.map((obj) => (
  • {obj.title}
    {obj.type} {formatUpdatedAt(obj.updatedAt)}
  • ))}
)}
); }