From 2eacdda23ebdedbb54d474fc26ee0742085f84db Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Mon, 12 May 2025 10:37:58 +0900 Subject: [PATCH] refactor: remove alertSwitch store and simplify alert handling in EdgeProvider --- src/providers/EdgeProvider.tsx | 41 +++++++++++++++++++--------------- src/store/alertSwitch.ts | 21 ----------------- 2 files changed, 23 insertions(+), 39 deletions(-) delete mode 100644 src/store/alertSwitch.ts diff --git a/src/providers/EdgeProvider.tsx b/src/providers/EdgeProvider.tsx index 3d8ea42..176cf06 100644 --- a/src/providers/EdgeProvider.tsx +++ b/src/providers/EdgeProvider.tsx @@ -1,25 +1,22 @@ 'use client' -import { useAlertSwitch } from '@/store/alertSwitch' import { useHeaderStore } from '@/store/header' import { usePopupController } from '@/store/popupController' import { useSideNavState } from '@/store/sideNavState' import { usePathname } from 'next/navigation' import { useEffect } from 'react' -declare global { - interface Window { - alert2: (msg: string, alert2BtnYes?: () => void, alert2BtnNo?: () => void) => void - } -} - export default function EdgeProvider({ children }: React.PropsWithChildren) { const pathname = usePathname() const { setBackBtn } = useHeaderStore() const { reset } = useSideNavState() const { setAlertMsg, setAlertBtn, setAlert, setAlert2, setAlert2BtnYes, setAlert2BtnNo } = usePopupController() - const { alertKind } = useAlertSwitch() + /** + * alert 함수 - window.alert 함수 대체 + * @param msg + * @param alertBtn + */ const alertFunc = (msg: string, alertBtn: Function) => { console.log('🚀 ~ alertFunc ~ msg:', msg) setAlertMsg(msg) @@ -27,7 +24,7 @@ export default function EdgeProvider({ children }: React.PropsWithChildren) { setAlert(true) } /** - * alert2 함수 + * alert2 함수 - window.confirm 함수 대체 * @param msg alert 메시지 * @param alertBtn2Yes alert 확인 버튼 클릭시 실행되는 함수 * @param alertBtn2No alert 취소 버튼 클릭시 실행되는 함수 @@ -42,16 +39,24 @@ export default function EdgeProvider({ children }: React.PropsWithChildren) { //alert 함수 변경해서 바인딩 useEffect(() => { - if (alertKind === 'single') { - window.alert = function (msg, alertBtn = () => setAlert(false)) { - alertFunc(msg, alertBtn) - } - } else if (alertKind === 'multi') { - window.alert = function (msg, alert2BtnYes = () => setAlert2(false), alert2BtnNo = () => setAlert2(false)) { - alertFunc2(msg, alert2BtnYes, alert2BtnNo) - } + window.alert = function (msg, alertBtn = () => setAlert(false)) { + alertFunc(msg, alertBtn) } - }, [alertKind]) + window.confirm = function (msg = '', alert2BtnYes = () => setAlert2(false), alert2BtnNo = () => setAlert2(false)) { + alertFunc2( + msg, + () => { + alert2BtnYes() + return true + }, + () => { + alert2BtnNo() + return false + }, + ) + return false + } + }, []) /** * 헤더 뒤로가기 버튼 컨트롤 diff --git a/src/store/alertSwitch.ts b/src/store/alertSwitch.ts deleted file mode 100644 index 54c9d00..0000000 --- a/src/store/alertSwitch.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { create } from 'zustand' - -type AlertSwitchState = { - alertKind: string - setAlertKind: (value: string) => void - reset: () => void -} - -type InitialState = { - alertKind: string -} - -const initialState: InitialState = { - alertKind: 'single', -} - -export const useAlertSwitch = create((set) => ({ - ...initialState, - setAlertKind: (value: string) => set((state) => ({ ...state, alertKind: value })), - reset: () => set(initialState), -}))