'use client' import { Pencil } from 'lucide-react' import Link from 'next/link' import { toggleCatalogPage } from '@/actions/catalog' import { toggleBcPage } from '@/actions/catalog-bc' import { DataTable } from '@/components/admin/data-table' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { useServerAction } from '@/hooks/use-server-action' import type { DataTableColumn, PaginatedResult } from '@/types' interface CatalogPageRow { id: number caption: string parentId: number parentCaption: string pageLayout: string enabled: string visible: string minRank: number orderNum: number itemCount: number childCount: number } interface CatalogTableProps { data: PaginatedResult canEdit: boolean catalogType?: 'normal' | 'bc' } export function CatalogTable({ data, canEdit, catalogType = 'normal' }: CatalogTableProps) { const { run, isPending } = useServerAction() const isBc = catalogType === 'bc' // Parent URLs stay in the same catalog; root takes you to the catalog index. const detailHref = (id: number) => isBc ? `/admin/catalog/builder-club/${id}` : `/admin/catalog/${id}` function handleToggle(id: number, action: 'toggleEnabled' | 'toggleVisible') { if (isBc) { const field = action === 'toggleEnabled' ? 'enabled' : 'visible' run(() => toggleBcPage({ id, field }), { successMessage: 'Updated.' }) } else { run(() => toggleCatalogPage({ id, action }), { successMessage: 'Updated.' }) } } const columns: DataTableColumn[] = [ { key: 'id', label: 'ID', sortable: true }, { key: 'caption', label: 'Caption', sortable: true, render: (value, row) => ( {String(value)} ), }, { key: 'parentId', label: 'Parent', sortable: true, render: (_, row) => row.parentId === -1 ? ( Root ) : ( {row.parentCaption} ), }, { key: 'pageLayout', label: 'Layout', render: (value) => {String(value)}, }, { key: 'enabled', label: 'Enabled', sortable: true, filterKey: 'filter_enabled', filterOptions: [ { label: 'Enabled', value: '1' }, { label: 'Disabled', value: '0' }, ], render: (value, row) => canEdit ? ( ) : ( {value === '1' ? 'Yes' : 'No'} ), }, { key: 'visible', label: 'Visible', sortable: true, filterKey: 'filter_visible', filterOptions: [ { label: 'Visible', value: '1' }, { label: 'Hidden', value: '0' }, ], render: (value, row) => canEdit ? ( ) : ( {value === '1' ? 'Yes' : 'No'} ), }, ...(isBc ? [] : [ { key: 'minRank' as const, label: 'Min Rank', sortable: true, } satisfies DataTableColumn, ]), { key: 'itemCount', label: 'Items', render: (value) => {String(value)}, }, { key: 'childCount', label: 'Children', render: (value) => {String(value)}, }, { key: 'actions', label: 'Actions', render: (_, row) => ( ), }, ] return ( ) }