[공통컴포넌트] alert 제작

This commit is contained in:
2026-02-13 16:12:08 +09:00
parent 7c194d7452
commit b73867c65d
6 changed files with 427 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
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 "알림";
}
}