'use client' import { useState } from 'react' import Link from 'next/link' import { ArrowLeft, Download } from 'lucide-react' import { PageHeader, PageTitle } from '@/components/ui/page-title' import { Button } from '@/components/ui/button' import { Empty, EmptyHeader, EmptyTitle, EmptyDescription } from '@/components/ui/empty' import { toast } from 'sonner' import { exportFormResultsCsv } from '@/lib/actions' import type { FormField } from '@/lib/forms-schema' type Submission = { id: string member_id: string | null submitter_email: string | null answers: Record | null form_version: number created_at: string } function cell(v: unknown): string { if (v === null || v === undefined) return '' return typeof v === 'string' ? v : JSON.stringify(v) } export function ResultsClient({ formId, title, fields, submissions, }: { formId: string title: string fields: FormField[] submissions: Submission[] }) { const [exporting, setExporting] = useState(false) async function exportCsv() { setExporting(true) const res = await exportFormResultsCsv({ formId }) setExporting(false) if ('error' in res && res.error) { toast.error(res.error || 'Export failed') return } const { filename, csv } = res.data const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = filename a.click() URL.revokeObjectURL(url) } return ( <>
{title} — responses
{submissions.length === 0 ? ( No responses yet Submissions will appear here as they come in. ) : (
{fields.map(f => ( ))} {submissions.map(s => { const a = s.answers ?? {} return ( {fields.map(f => ( ))} ) })}
Submitted Submitter {f.label}
{new Date(s.created_at).toLocaleString()} {s.member_id ? 'Member' : s.submitter_email || 'Anonymous'} {cell(a[f.key])}
)}
) }