'use client'; import React, { useEffect, useMemo, useState } from 'react'; import useContractManagement from '../hooks/useContractManagement'; type Props = { refreshKey?: number; }; type ContractTemplate = { id: string; name: string; version: number; status: 'draft' | 'published' | 'archived' | string; updatedAt?: string; }; function StatusBadge({ status }: { status: string }) { const map: Record = { draft: 'bg-gray-100 text-gray-800 border border-gray-300', published: 'bg-green-100 text-green-800 border border-green-300', archived: 'bg-yellow-100 text-yellow-800 border border-yellow-300', }; const cls = map[status] || 'bg-blue-100 text-blue-800 border border-blue-300'; return {status}; } export default function ContractTemplateList({ refreshKey = 0 }: Props) { const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [q, setQ] = useState(''); const { listTemplates, openPreviewInNewTab, generatePdf, downloadPdf, updateTemplateState, downloadBlobFile, } = useContractManagement(); const filtered = useMemo(() => { const term = q.trim().toLowerCase(); if (!term) return items; return items.filter((i) => i.name.toLowerCase().includes(term) || String(i.version).includes(term) || i.status.toLowerCase().includes(term)); }, [items, q]); const load = async () => { setLoading(true); try { const data = await listTemplates(); const mapped: ContractTemplate[] = data.map((x: any) => ({ id: x.id ?? x._id ?? x.uuid, name: x.name ?? 'Untitled', version: Number(x.version ?? 1), status: (x.state === 'active') ? 'published' : 'draft', updatedAt: x.updatedAt ?? x.modifiedAt ?? x.updated_at, })); setItems(mapped); } catch { setItems((prev) => prev.length ? prev : [ { id: 'ex1', name: 'Sample Contract A', version: 1, status: 'draft', updatedAt: new Date().toISOString() }, { id: 'ex2', name: 'NDA Template', version: 3, status: 'published', updatedAt: new Date().toISOString() }, ]); } finally { setLoading(false); } }; useEffect(() => { load(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [refreshKey]); const onToggleState = async (id: string, current: string) => { const target = current === 'published' ? 'inactive' : 'active'; try { const updated = await updateTemplateState(id, target as 'active' | 'inactive'); setItems((prev) => prev.map((i) => i.id === id ? { ...i, status: updated.state === 'active' ? 'published' : 'draft' } : i)); } catch {} }; const onPreview = (id: string) => openPreviewInNewTab(id); const onGenPdf = async (id: string) => { try { const blob = await generatePdf(id, { preview: true }); downloadBlobFile(blob, `${id}-preview.pdf`); } catch {} }; const onDownloadPdf = async (id: string) => { try { const blob = await downloadPdf(id); downloadBlobFile(blob, `${id}.pdf`); } catch {} }; return (
setQ(e.target.value)} className="w-full rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm text-gray-900 outline-none focus:ring-2 focus:ring-indigo-500/50 shadow" />
{filtered.map((c) => (

{c.name}

Version {c.version}{c.updatedAt ? ` • Updated ${new Date(c.updatedAt).toLocaleString()}` : ''}

))} {!filtered.length && (
No contracts found.
)}
); }