Files
app/src/admin/context/AlertContext.tsx
BOHA d7c7fbad88 fix: security, validation, and data integrity fixes across 53 files
- Auth: HS256 algorithm restriction on JWT verify, timing-safe bcrypt
  for inactive/locked users, locked_until check in loadAuthData, TOTP
  fixes (async bcrypt, BigInt conversion, future-code counter fix)
- Validation: Zod enums for leave_type/status, numeric transforms on
  foreign keys, VAT 0% coercion fix (Number(v)||21 → v!=null checks)
- Permissions: requirePermission on attendance PUT, attendance_users
  and project_logs access checks, trips users filtered by trips.record
- Prisma queries: fixed roles.is:{OR} pattern (doesn't work on to-one
  relations), attendance_users now filters by attendance.record only
- Transactions: wrapped deleteOrder, createOrder, updateUser, deleteUser,
  duplicateOffer, bulkCreateAttendance, createLeave, scope-templates,
  leave-requests, company-settings, profile updates
- Frontend: mountedRef reset in useListData, blob URL cleanup on unmount,
  null checks on date fields, AdminDatePicker min/max for HH:mm
- Security headers: COOP, CORP, CSP frame-ancestors/form-action/base-uri
- Other: exchange-rate cache TTL, invoice-alert midnight comparison fix,
  numbering.service releaseSequence no-op, nas-offers filename sanitize,
  Content-Disposition header injection fix, mojibake Czech strings

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 08:40:38 +02:00

105 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(() => {
return () => {
timeoutsRef.current.forEach(clearTimeout);
timeoutsRef.current.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;
}