"use client"; import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { useTableControls } from "@/components/ui/use-table-controls"; import { TableControls, SortableTh } from "@/components/ui/table-controls"; import { AddAccountButton, EditAccountButton } from "./account-form"; import { RowActionsMenu, RowActionsItem, RowActionsDestructiveItem, RowActionsSeparator } from "@/components/ui/row-actions-menu"; import { DeleteConfirmDialog } from "@/components/ui/delete-confirm-dialog"; import { deleteAccount, toggleAccountActive } from "./actions"; export type AccountRow = { id: string; code: string; name: string; description: string | null; isActive: boolean; }; const CHIPS = ["Active", "Inactive"]; function AccountActionsMenu({ account }: { account: AccountRow }) { const router = useRouter(); const [editOpen, setEditOpen] = useState(false); const [deleteOpen, setDeleteOpen] = useState(false); const [isPending, startTransition] = useTransition(); function handleToggle() { startTransition(async () => { await toggleAccountActive(account.id); router.refresh(); }); } return ( <> setEditOpen(true)}>Edit {account.isActive ? "Deactivate" : "Activate"} setDeleteOpen(true)}>Delete deleteAccount(account.id)} /> ); } export function AccountsTable({ accounts, suggestedCode, }: { accounts: AccountRow[]; suggestedCode: string; }) { const { search, setSearch, sortKey, sortDir, toggleSort, activeFilters, toggleFilter, filtered } = useTableControls({ rows: accounts, defaultSortKey: "code", searchText: (a) => [a.code, a.name, a.description ?? "", a.isActive ? "active" : "inactive"].join(" "), chipMatch: (a, chip) => { if (chip.toLowerCase() === "active") return a.isActive; if (chip.toLowerCase() === "inactive") return !a.isActive; return false; }, sortValue: (a, key) => { if (key === "isActive") return a.isActive ? "Active" : "Inactive"; const val = a[key as keyof AccountRow]; if (val === null || val === undefined) return ""; return typeof val === "string" || typeof val === "number" || typeof val === "boolean" ? val : String(val); }, }); return (

Account Management

toggleSort(k as keyof AccountRow)}>Code toggleSort(k as keyof AccountRow)}>Name toggleSort(k as keyof AccountRow)}>Description toggleSort(k as keyof AccountRow)}>Status {filtered.length === 0 && ( )} {filtered.map((account) => ( ))}
No accounts match your search.
{account.code} {account.name} {account.description ?? "—"} {account.isActive ? "Active" : "Inactive"}
); }