85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import { ReactNode } from "react";
|
|
import {
|
|
AlertType,
|
|
useGlobalAlertStore,
|
|
} from "@/features/layout/stores/use-global-alert-store";
|
|
|
|
interface AlertOptions {
|
|
title?: ReactNode;
|
|
confirmLabel?: string;
|
|
cancelLabel?: string;
|
|
onConfirm?: () => void;
|
|
onCancel?: () => void;
|
|
type?: AlertType;
|
|
}
|
|
|
|
export function useGlobalAlert() {
|
|
const openAlert = useGlobalAlertStore((state) => state.openAlert);
|
|
const closeAlert = useGlobalAlertStore((state) => state.closeAlert);
|
|
|
|
const show = (
|
|
message: ReactNode,
|
|
type: AlertType = "info",
|
|
options?: AlertOptions,
|
|
) => {
|
|
openAlert({
|
|
message,
|
|
type,
|
|
title: options?.title || getDefaultTitle(type),
|
|
confirmLabel: options?.confirmLabel || "확인",
|
|
cancelLabel: options?.cancelLabel,
|
|
onConfirm: options?.onConfirm,
|
|
onCancel: options?.onCancel,
|
|
isSingleButton: true,
|
|
});
|
|
};
|
|
|
|
const confirm = (
|
|
message: ReactNode,
|
|
type: AlertType = "warning",
|
|
options?: AlertOptions,
|
|
) => {
|
|
openAlert({
|
|
message,
|
|
type,
|
|
title: options?.title || "확인",
|
|
confirmLabel: options?.confirmLabel || "확인",
|
|
cancelLabel: options?.cancelLabel || "취소",
|
|
onConfirm: options?.onConfirm,
|
|
onCancel: options?.onCancel,
|
|
isSingleButton: false,
|
|
});
|
|
};
|
|
|
|
return {
|
|
alert: {
|
|
success: (message: ReactNode, options?: AlertOptions) =>
|
|
show(message, "success", options),
|
|
warning: (message: ReactNode, options?: AlertOptions) =>
|
|
show(message, "warning", options),
|
|
error: (message: ReactNode, options?: AlertOptions) =>
|
|
show(message, "error", options),
|
|
info: (message: ReactNode, options?: AlertOptions) =>
|
|
show(message, "info", options),
|
|
confirm: (message: ReactNode, options?: AlertOptions) =>
|
|
confirm(message, options?.type || "warning", options),
|
|
},
|
|
close: closeAlert,
|
|
};
|
|
}
|
|
|
|
function getDefaultTitle(type: AlertType) {
|
|
switch (type) {
|
|
case "success":
|
|
return "성공";
|
|
case "error":
|
|
return "오류";
|
|
case "warning":
|
|
return "주의";
|
|
case "info":
|
|
return "알림";
|
|
default:
|
|
return "알림";
|
|
}
|
|
}
|