"use client"; import * as React from "react"; import { ExternalLink, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { api } from "@/lib/trpc"; import { usePanelStore } from "@/lib/stores/panel-store"; import { cn } from "@/lib/utils"; import type { FormField } from "./form-renderer"; function formatCellValue(value: unknown): string { if (value === undefined || value === null) return "—"; if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { return String(value); } if (Array.isArray(value)) { return value.map((x) => (typeof x === "string" ? x : JSON.stringify(x))).join(", "); } try { return JSON.stringify(value); } catch { return "—"; } } function tableColumns(fields: FormField[]): FormField[] { return fields.filter((f) => f.type !== "section_header" && f.type !== "divider"); } export interface FormResponsesProps { formId: string; workspaceHandle: string; fields: FormField[]; className?: string; } export function FormResponses({ formId, workspaceHandle, fields, className }: FormResponsesProps) { const open = usePanelStore((s) => s.open); const cols = React.useMemo(() => tableColumns(fields), [fields]); const listQuery = api.forms.listResponses.useQuery( { workspace: workspaceHandle, formId }, { enabled: Boolean(formId) && Boolean(workspaceHandle) }, ); if (listQuery.isPending) { return (
Loading responses
); } if (listQuery.isError) { return (

Could not load responses.

); } const rows = listQuery.data?.responses ?? []; if (rows.length === 0) { return (

No responses yet.

); } return (
{cols.map((f) => ( ))} {rows.map((row) => { const data = row.data && typeof row.data === "object" && row.data !== null ? (row.data as Record) : {}; const submitted = row.submittedAt instanceof Date ? row.submittedAt.toLocaleString() : String(row.submittedAt ?? "—"); return ( {cols.map((f) => ( ))} ); })}
{f.label} Submitted Task
{formatCellValue(data[f.id])} {submitted} {row.createdObjectId ? ( ) : ( )}
); }