Linked transactions now show first in "Link transfers", checked, with no date bound; a save diffs the selection and calls unlinkTransfer for the unchecked ones. A delete of the import history refused for linked transfers keeps a structured state instead of the English error text: the panel names the accounts, counts the transfers with a COUNT(*), links to the account's window when the edition opens the Balance sheet (/balance?linkTransfers=<id>), and offers "Unlink and delete" in every edition, one SQL transaction, the Journal line carrying numbers only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
5.6 KiB
TypeScript
179 lines
5.6 KiB
TypeScript
import { useReducer, useCallback, useEffect, useRef } from "react";
|
|
import type { ImportedFileWithSource } from "../shared/types";
|
|
import {
|
|
getAllImportedFiles,
|
|
deleteImportWithTransactions,
|
|
deleteAllImportsWithTransactions,
|
|
getLinkedTransfersSummary,
|
|
unlinkAndDeleteImport,
|
|
unlinkAndDeleteAllImports,
|
|
type ImportDeletionScope,
|
|
type LinkedTransfersSummary,
|
|
} from "../services/importedFileService";
|
|
import { TransactionLinkedToBalanceError } from "../services/transactionService";
|
|
|
|
/**
|
|
* A delete refused because transactions of the import are linked to the
|
|
* Balance sheet (#522). Kept structured, never as the error's English text:
|
|
* the panel names the accounts, counts the transfers and offers "Unlink and
|
|
* delete". `summary` is null when the count itself failed (generic message).
|
|
*/
|
|
export interface BlockedDeletion {
|
|
scope: ImportDeletionScope;
|
|
summary: LinkedTransfersSummary | null;
|
|
}
|
|
|
|
interface ImportHistoryState {
|
|
files: ImportedFileWithSource[];
|
|
isLoading: boolean;
|
|
isDeleting: boolean;
|
|
error: string | null;
|
|
blocked: BlockedDeletion | null;
|
|
/** The unlink-and-delete itself failed: a translated message, nothing moved. */
|
|
unlinkFailed: boolean;
|
|
}
|
|
|
|
type ImportHistoryAction =
|
|
| { type: "SET_LOADING"; payload: boolean }
|
|
| { type: "SET_DELETING"; payload: boolean }
|
|
| { type: "SET_ERROR"; payload: string | null }
|
|
| { type: "SET_FILES"; payload: ImportedFileWithSource[] }
|
|
| { type: "SET_BLOCKED"; payload: BlockedDeletion | null }
|
|
| { type: "SET_UNLINK_FAILED"; payload: boolean };
|
|
|
|
const initialState: ImportHistoryState = {
|
|
files: [],
|
|
isLoading: false,
|
|
isDeleting: false,
|
|
error: null,
|
|
blocked: null,
|
|
unlinkFailed: false,
|
|
};
|
|
|
|
function reducer(
|
|
state: ImportHistoryState,
|
|
action: ImportHistoryAction
|
|
): ImportHistoryState {
|
|
switch (action.type) {
|
|
case "SET_LOADING":
|
|
return { ...state, isLoading: action.payload };
|
|
case "SET_DELETING":
|
|
return { ...state, isDeleting: action.payload };
|
|
case "SET_ERROR":
|
|
return { ...state, error: action.payload };
|
|
case "SET_FILES":
|
|
return { ...state, files: action.payload };
|
|
case "SET_BLOCKED":
|
|
return { ...state, blocked: action.payload, unlinkFailed: false };
|
|
case "SET_UNLINK_FAILED":
|
|
return { ...state, unlinkFailed: action.payload };
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
export function useImportHistory(onChanged?: () => void) {
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
const fetchIdRef = useRef(0);
|
|
|
|
const loadHistory = useCallback(async () => {
|
|
const fetchId = ++fetchIdRef.current;
|
|
dispatch({ type: "SET_LOADING", payload: true });
|
|
dispatch({ type: "SET_ERROR", payload: null });
|
|
try {
|
|
const files = await getAllImportedFiles();
|
|
if (fetchId !== fetchIdRef.current) return;
|
|
dispatch({ type: "SET_FILES", payload: files });
|
|
} catch (err) {
|
|
if (fetchId !== fetchIdRef.current) return;
|
|
dispatch({ type: "SET_ERROR", payload: String(err) });
|
|
} finally {
|
|
if (fetchId === fetchIdRef.current) {
|
|
dispatch({ type: "SET_LOADING", payload: false });
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
/** Runs one delete; a refusal for linked transfers becomes `blocked`. */
|
|
const runDelete = useCallback(
|
|
async (scope: ImportDeletionScope, remove: () => Promise<unknown>) => {
|
|
dispatch({ type: "SET_DELETING", payload: true });
|
|
dispatch({ type: "SET_ERROR", payload: null });
|
|
dispatch({ type: "SET_BLOCKED", payload: null });
|
|
try {
|
|
await remove();
|
|
await loadHistory();
|
|
onChanged?.();
|
|
} catch (err) {
|
|
if (err instanceof TransactionLinkedToBalanceError) {
|
|
let summary: LinkedTransfersSummary | null = null;
|
|
try {
|
|
summary = await getLinkedTransfersSummary(scope);
|
|
} catch {
|
|
// The panel falls back to a message without names nor count.
|
|
}
|
|
dispatch({ type: "SET_BLOCKED", payload: { scope, summary } });
|
|
} else {
|
|
dispatch({ type: "SET_ERROR", payload: String(err) });
|
|
}
|
|
} finally {
|
|
dispatch({ type: "SET_DELETING", payload: false });
|
|
}
|
|
},
|
|
[loadHistory, onChanged]
|
|
);
|
|
|
|
const handleDelete = useCallback(
|
|
(fileId: number) =>
|
|
runDelete({ kind: "file", fileId }, () => deleteImportWithTransactions(fileId)),
|
|
[runDelete]
|
|
);
|
|
|
|
const handleDeleteAll = useCallback(
|
|
() => runDelete({ kind: "all" }, () => deleteAllImportsWithTransactions()),
|
|
[runDelete]
|
|
);
|
|
|
|
/**
|
|
* "Unlink and delete" the refused scope, after the panel's confirmation. On
|
|
* failure nothing moved (one SQL transaction): the notice stays, with a
|
|
* translated failure line.
|
|
*/
|
|
const handleUnlinkAndDelete = useCallback(async () => {
|
|
const blocked = state.blocked;
|
|
if (!blocked) return;
|
|
dispatch({ type: "SET_DELETING", payload: true });
|
|
dispatch({ type: "SET_UNLINK_FAILED", payload: false });
|
|
try {
|
|
if (blocked.scope.kind === "file") {
|
|
await unlinkAndDeleteImport(blocked.scope.fileId);
|
|
} else {
|
|
await unlinkAndDeleteAllImports();
|
|
}
|
|
dispatch({ type: "SET_BLOCKED", payload: null });
|
|
await loadHistory();
|
|
onChanged?.();
|
|
} catch {
|
|
dispatch({ type: "SET_UNLINK_FAILED", payload: true });
|
|
} finally {
|
|
dispatch({ type: "SET_DELETING", payload: false });
|
|
}
|
|
}, [state.blocked, loadHistory, onChanged]);
|
|
|
|
const dismissBlocked = useCallback(() => {
|
|
dispatch({ type: "SET_BLOCKED", payload: null });
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
loadHistory();
|
|
}, [loadHistory]);
|
|
|
|
return {
|
|
state,
|
|
loadHistory,
|
|
handleDelete,
|
|
handleDeleteAll,
|
|
handleUnlinkAndDelete,
|
|
dismissBlocked,
|
|
};
|
|
}
|