import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Plus, Split } from "lucide-react"; import { PageHelp } from "../components/shared/PageHelp"; import { useAdjustments } from "../hooks/useAdjustments"; import { getEntriesByAdjustmentId } from "../services/adjustmentService"; import type { AdjustmentEntryWithCategory } from "../services/adjustmentService"; import { getSplitParentTransactions, getSplitChildren, saveSplitAdjustment, deleteSplitAdjustment, } from "../services/transactionService"; import type { TransactionRow } from "../shared/types"; import AdjustmentListPanel from "../components/adjustments/AdjustmentListPanel"; import AdjustmentDetailPanel from "../components/adjustments/AdjustmentDetailPanel"; import SplitAdjustmentModal from "../components/transactions/SplitAdjustmentModal"; export default function AdjustmentsPage() { const { t } = useTranslation(); const { state, selectAdjustment, startCreating, startEditing, cancelEditing, saveAdjustment, deleteAdjustment, } = useAdjustments(); const [entriesMap, setEntriesMap] = useState>( new Map() ); const [splitTransactions, setSplitTransactions] = useState([]); const [splitRow, setSplitRow] = useState(null); const loadAllEntries = useCallback(async () => { const map = new Map(); for (const adj of state.adjustments) { try { const entries = await getEntriesByAdjustmentId(adj.id); map.set(adj.id, entries); } catch { // skip on error } } setEntriesMap(map); }, [state.adjustments]); useEffect(() => { if (state.adjustments.length > 0) { loadAllEntries(); } }, [state.adjustments, loadAllEntries]); const loadSplitTransactions = useCallback(async () => { try { const rows = await getSplitParentTransactions(); setSplitTransactions(rows); } catch { // ignore } }, []); useEffect(() => { loadSplitTransactions(); }, [loadSplitTransactions]); const handleSplitSave = async ( parentId: number, entries: Array<{ category_id: number; amount: number; description: string }> ) => { await saveSplitAdjustment(parentId, entries); await loadSplitTransactions(); }; const handleSplitDelete = async (parentId: number) => { await deleteSplitAdjustment(parentId); await loadSplitTransactions(); }; const selectedAdjustment = state.selectedAdjustmentId !== null ? state.adjustments.find((a) => a.id === state.selectedAdjustmentId) ?? null : null; return (

{t("adjustments.title")}

{state.error && (
{state.error}
)} {state.isLoading ? (

{t("common.loading")}

) : (
{state.adjustments.length > 0 && ( )} {splitTransactions.length > 0 && ( <> {state.adjustments.length > 0 && (
)}
{t("adjustments.splitTransactions")}
{splitTransactions.map((tx) => ( ))}
)} {state.adjustments.length === 0 && splitTransactions.length === 0 && (
{t("common.noResults")}
)}
)} {splitRow && ( setSplitRow(null)} /> )}
); }