"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import type { SortingState } from "@tanstack/react-table"; import Link from "next/link"; import { Plus } from "lucide-react"; import { Badge } from "@/app/components/ui/badge"; import { Button } from "@/app/components/ui/button"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/app/components/ui/table"; import { SortableHead, sortRows } from "@/app/components/ui/sortable-head"; import { PageHeader } from "@/app/components/ui/page-header"; import { EmptyState, ErrorState, TableSkeleton } from "@/app/components/ui/states"; import { RequireModule } from "@/app/lib/RequireModule"; import { listForms } from "@/app/lib/services/adminForms"; import type { CmsFormListItem } from "@/app/lib/cmsForms"; import { formatDateTime } from "@/app/lib/format"; import { useT } from "@/app/lib/i18n/MessagesProvider"; // `/forms` (add-cms-forms): every CMS form with its field and submission counts. function FormsPageContent() { const t = useT(); const [rows, setRows] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [reloadKey, setReloadKey] = useState(0); const [sorting, setSorting] = useState([]); const refetch = useCallback(() => setReloadKey((k) => k + 1), []); useEffect(() => { let cancelled = false; // eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error ahead of the reloadKey-triggered fetch below. setLoading(true); setError(null); listForms() .then((result) => { if (!cancelled) setRows(result); }) .catch((err: unknown) => { if (!cancelled) setError(err instanceof Error ? err.message : "FrmListLoadFailed"); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [reloadKey]); const sorted = useMemo( () => sortRows(rows, sorting, { name: (r) => r.name, fields: (r) => r.fieldCount, submissions: (r) => r.submissionCount, status: (r) => t(r.status === "Y" ? "Visible" : "Hidden"), updated: (r) => r.updatedAt, }), [rows, sorting, t], ); return ( <>