Files
app/src/admin/context/AlertContext.tsx
BOHA 1ee59b54bc chore(release): v2.4.35 — lint cleanup (68→0) + duplicate-Zavřít modal fix
UI fix:
- Close-only modals showing a redundant second close button now use
  hideCancel: ReceivedInvoices paid-detail modal (was two identical "Zavřít"
  buttons) and PlanCellModal "Den je součástí rozsahu".
- Add modal-duplicate-close test enforcing close-only modals set hideCancel.

Lint: cleared all 68 warnings → 0.
- preserve-caught-error: attach { cause } in ai.service / exchange-rates.
- no-require-imports: package.json version read via fs (APP_VERSION) instead
  of require(), avoiding a rootDir-expanding static JSON import.
- react-hooks/exhaustive-deps (11): ref-in-cleanup copies, derived-value
  useMemo wrapping, PlanGrid field extraction, stable nextKey useCallback,
  AuthContext documented cycle-break.
- no-explicit-any (53): precise route param/Prisma types, generic enrich*()
  preserving payload shape, minimal vite module type, frontend body/query-key
  types, SystemInfo for Settings.

Refactor (test enablement): shift-form types moved to dependency-free
shiftFormTypes.ts so the print-HTML builders are unit-testable without the
component graph; characterization test pins their output.

Gates: 649 tests pass, tsc -b clean, lint 0. Verified touched flows live
via Playwright (PlanWork CRUD + optimistic cache, warehouse form keys,
Settings system info, invoice detail).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 18:35:31 +02:00

106 lines
2.9 KiB
TypeScript

import {
createContext,
useContext,
useState,
useCallback,
useMemo,
useRef,
useEffect,
type ReactNode,
} from "react";
interface Alert {
id: string;
message: string;
type: "success" | "error" | "warning" | "info";
}
interface AlertMethods {
addAlert: (message: string, type?: string, duration?: number) => string;
removeAlert: (id: string) => void;
success: (message: string, duration?: number) => string;
error: (message: string, duration?: number) => string;
warning: (message: string, duration?: number) => string;
info: (message: string, duration?: number) => string;
}
interface AlertStateValue {
alerts: Alert[];
removeAlert: (id: string) => void;
}
const AlertContext = createContext<AlertMethods | null>(null);
const AlertStateContext = createContext<AlertStateValue | null>(null);
export function AlertProvider({ children }: { children: ReactNode }) {
const [alerts, setAlerts] = useState<Alert[]>([]);
const removeAlert = useCallback((id: string) => {
setAlerts((prev) => prev.filter((alert) => alert.id !== id));
}, []);
const counterRef = useRef(0);
const timeoutsRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());
useEffect(() => {
const timeouts = timeoutsRef.current;
return () => {
timeouts.forEach(clearTimeout);
timeouts.clear();
};
}, []);
const addAlert = useCallback(
(message: string, type = "success", duration = 4000) => {
const id = `${Date.now()}-${counterRef.current++}`;
setAlerts((prev) => [
...prev,
{ id, message, type: type as Alert["type"] },
]);
if (duration > 0) {
const timeoutId = setTimeout(() => {
timeoutsRef.current.delete(timeoutId);
removeAlert(id);
}, duration);
timeoutsRef.current.add(timeoutId);
}
return id;
},
[removeAlert],
);
const methods = useMemo<AlertMethods>(
() => ({
addAlert,
removeAlert,
success: (message, duration) => addAlert(message, "success", duration),
error: (message, duration) => addAlert(message, "error", duration),
warning: (message, duration) => addAlert(message, "warning", duration),
info: (message, duration) => addAlert(message, "info", duration),
}),
[addAlert, removeAlert],
);
return (
<AlertContext.Provider value={methods}>
<AlertStateContext.Provider value={{ alerts, removeAlert }}>
{children}
</AlertStateContext.Provider>
</AlertContext.Provider>
);
}
export function useAlert(): AlertMethods {
const context = useContext(AlertContext);
if (!context)
throw new Error("useAlert must be used within an AlertProvider");
return context;
}
export function useAlertState(): AlertStateValue {
const context = useContext(AlertStateContext);
if (!context)
throw new Error("useAlertState must be used within an AlertProvider");
return context;
}