"use client"; import { useState } from "react"; 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 { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { deleteAccount, toggleAccountActive } from "./actions"; type AccountItem = { id: string; code: string; name: string; description: string | null; isActive: boolean; parentId: string | null; children: AccountItem[]; }; type ParentOption = { id: string; code: string; name: string; parentId: string | null }; function AccountActionsMenu({ account, allAccounts, }: { account: AccountItem; allAccounts: ParentOption[]; }) { const [editOpen, setEditOpen] = useState(false); const [deleteOpen, setDeleteOpen] = useState(false); const [toggleOpen, setToggleOpen] = useState(false); return ( <> setEditOpen(true)}>Edit setToggleOpen(true)}> {account.isActive ? "Deactivate" : "Activate"} setDeleteOpen(true)}>Delete deleteAccount(account.id)} /> toggleAccountActive(account.id)} /> > ); } function SubCategorySection({ sub, allAccounts, }: { sub: AccountItem; allAccounts: ParentOption[]; }) { const [open, setOpen] = useState(false); const isLeaf = sub.children.length === 0; return ( {/* Sub-category header row */} !isLeaf && setOpen((v) => !v)} > {!isLeaf && ( {open ? "▾" : "▸"} )} {isLeaf && } {sub.code} {sub.name} {sub.isActive ? "Active" : "Inactive"} {/* Leaf items under this sub-category */} {!isLeaf && open && ( {sub.children.map((item) => ( {item.code} {item.name} {item.description && ( {item.description} )} {item.isActive ? "Active" : "Inactive"} ))} )} ); } function TopCategorySection({ category, allAccounts, }: { category: AccountItem; allAccounts: ParentOption[]; }) { const [open, setOpen] = useState(false); const isLeaf = category.children.length === 0; return ( {/* Top-category header */} !isLeaf && setOpen((v) => !v)} > {!isLeaf && ( {open ? "▾" : "▸"} )} {isLeaf && } {category.code} {category.name} {category.children.length} sub-categories {open && ( {category.children.map((sub) => ( ))} )} ); } export function AccountsTable({ topCategories, allAccounts, }: { topCategories: AccountItem[]; allAccounts: ParentOption[]; }) { const [search, setSearch] = useState(""); // Flat filtered view when searching const flatAll = allAccounts.filter((a) => search.trim() ? a.code.includes(search) || a.name.toLowerCase().includes(search.toLowerCase()) : false ); return ( Accounting Code Management {allAccounts.length} codes across {topCategories.length} top categories setSearch(e.target.value)} placeholder="Search by code or name…" className="w-full max-w-sm rounded-lg border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20" /> {search.trim() ? ( /* Flat search results */ {flatAll.length === 0 ? ( No codes match your search. ) : ( flatAll.map((a) => ( {a.code} {a.name} )) )} ) : ( /* Hierarchical tree view */ {topCategories.map((cat) => ( ))} {topCategories.length === 0 && ( No accounting codes yet. Add a top-level category to get started. )} )} ); }
{allAccounts.length} codes across {topCategories.length} top categories
No codes match your search.
No accounting codes yet. Add a top-level category to get started.