import { useQuery } from '@tanstack/react-query'; import { useState } from 'react'; import { listConsentRecords } from '../api/consent'; import type { ConsentRecord } from '../types/api'; import { Badge } from './ui/badge'; import { Button } from './ui/button'; import { Card } from './ui/card'; import { LoadingState } from './ui/loading-state'; interface Props { siteId: string; } function actionVariant(action: string): 'success' | 'error' | 'warning' | 'neutral' { const map: Record = { accept_all: 'success', reject_all: 'error', custom: 'warning', withdraw: 'error', }; return map[action] ?? 'neutral'; } function actionLabel(action: string): string { const map: Record = { accept_all: 'Accept all', reject_all: 'Reject all', custom: 'Custom', withdraw: 'Withdrawn', }; return map[action] ?? action; } function RecordDetail({ record }: { record: ConsentRecord }) { return (
Visitor ID

{record.visitor_id}

Page URL

{record.page_url ?? '—'}

Accepted

{record.categories_accepted.join(', ') || '—'}

Rejected

{record.categories_rejected?.join(', ') || '—'}

{record.country_code && (
Location

{record.region_code ? `${record.country_code}-${record.region_code}` : record.country_code}

)} {record.tc_string && (
TC String

{record.tc_string}

)} {record.gpc_detected != null && (
GPC

Detected: {record.gpc_detected ? 'Yes' : 'No'} {record.gpc_honoured != null && ` · Honoured: ${record.gpc_honoured ? 'Yes' : 'No'}`}

)}
); } export default function SiteConsentTab({ siteId }: Props) { const [search, setSearch] = useState(''); const [activeSearch, setActiveSearch] = useState(''); const [page, setPage] = useState(1); const [expandedId, setExpandedId] = useState(null); const pageSize = 25; const { data, isLoading } = useQuery({ queryKey: ['consent', siteId, activeSearch, page], queryFn: () => listConsentRecords(siteId, { visitor_id: activeSearch || undefined, page, page_size: pageSize, }), }); const handleSearch = () => { setActiveSearch(search.trim()); setPage(1); }; const totalPages = data ? Math.ceil(data.total / pageSize) : 0; return (
{/* Search */}

Search Consent Records

setSearch(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSearch()} /> {activeSearch && ( )}
{activeSearch && (

Showing results for visitor: {activeSearch}

)}
{/* Results */} {isLoading ? ( ) : !data || data.items.length === 0 ? (
{activeSearch ? 'No consent records found for this visitor.' : 'No consent records yet.'}
) : ( <>
{data.total} record{data.total !== 1 ? 's' : ''} Page {page} of {totalPages}
{data.items.map((record) => ( <> setExpandedId(expandedId === record.id ? null : record.id)} > {expandedId === record.id && ( )} ))}
Visitor Action Categories Date
{record.visitor_id.length > 16 ? record.visitor_id.slice(0, 8) + '…' + record.visitor_id.slice(-8) : record.visitor_id} {actionLabel(record.action)} {record.categories_accepted.join(', ')} {new Date(record.consented_at).toLocaleString()} {expandedId === record.id ? '▲' : '▼'}
{/* Pagination */} {totalPages > 1 && (
{page} / {totalPages}
)} )}
); }