Files
projet_gestion_commande/frontend-admin/src/hooks/useAlert.ts
T
2026-02-07 22:33:02 +01:00

58 lines
1.4 KiB
TypeScript

import { useState, useCallback } from "react";
interface AlertState {
visible: boolean;
type: "success" | "error" | "confirm";
title: string;
message: string;
onConfirm?: () => void;
confirmText?: string;
cancelText?: string;
}
const INITIAL: AlertState = {
visible: false,
type: "error",
title: "",
message: "",
};
export function useAlert() {
const [alert, setAlert] = useState<AlertState>(INITIAL);
const showError = useCallback((title: string, message: string) => {
setAlert({ visible: true, type: "error", title, message });
}, []);
const showSuccess = useCallback((title: string, message: string) => {
setAlert({ visible: true, type: "success", title, message });
}, []);
const showConfirm = useCallback(
(
title: string,
message: string,
onConfirm: () => void,
confirmText?: string,
cancelText?: string,
) => {
setAlert({
visible: true,
type: "confirm",
title,
message,
onConfirm,
confirmText,
cancelText,
});
},
[],
);
const hideAlert = useCallback(() => {
setAlert(INITIAL);
}, []);
return { alert, showError, showSuccess, showConfirm, hideAlert };
}