-
Notifications
You must be signed in to change notification settings - Fork 0
20260102 #121 토스트 전역 핸들링을 위한 커스텀 훅 use toast 추가 #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The head ref may contain hidden characters: "20260102_#121_\uD1A0\uC2A4\uD2B8_\uC804\uC5ED_\uD578\uB4E4\uB9C1\uC744_\uC704\uD55C_\uCEE4\uC2A4\uD140_\uD6C5_useToast_\uCD94\uAC00"
Changes from all commits
8f5709b
05a52d7
fdc5dfd
05612a7
168673a
815ef0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export const APP_VERSION: string = '0.4.0'; | ||
| export const APP_VERSION: string = '0.4.1'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect, useRef, useState } from "react"; | ||
|
|
||
| interface UseToastReturn { | ||
| visible: boolean; | ||
| message: string; | ||
| showToast: (message: string) => void; | ||
| } | ||
|
|
||
| /** | ||
| * 토스트 메시지 관리 훅 | ||
| * - 타이머 충돌 방지 | ||
| * - 기존 토스트가 있으면 새로운 토스트로 교체 | ||
| */ | ||
| export function useToast( | ||
| duration: number = 3000, | ||
| animationDuration: number = 200, | ||
| ): UseToastReturn { | ||
| const [visible, setVisible] = useState<boolean>(false); | ||
| const [message, setMessage] = useState<string>(""); | ||
| const timerRef = useRef<number | null>(null); | ||
| const hideTimerRef = useRef<number | null>(null); | ||
| const visibleRef = useRef<boolean>(false); | ||
|
|
||
| const showToast = useCallback((message: string) => { | ||
| // 기존 타이머 정리 | ||
| if (timerRef.current) { | ||
| window.clearTimeout(timerRef.current); | ||
| } | ||
| if (hideTimerRef.current) { | ||
| window.clearTimeout(hideTimerRef.current); | ||
| } | ||
|
|
||
| // 이미 토스트가 표시중이면 먼저 제거 | ||
| if (visibleRef.current) { | ||
| // 기존 토스트 숨김 | ||
| setVisible(false); | ||
| visibleRef.current = false; | ||
|
|
||
| // 애니메이션 완료 대기 | ||
| hideTimerRef.current = window.setTimeout(() => { | ||
| setMessage(message); | ||
| setVisible(true); | ||
| visibleRef.current = true; | ||
|
|
||
| // duration 후 토스트 숨김 | ||
| timerRef.current = window.setTimeout(() => { | ||
| setVisible(false); | ||
| visibleRef.current = false; | ||
| timerRef.current = null; | ||
| }, duration); | ||
|
|
||
| hideTimerRef.current = null; | ||
| }, animationDuration); | ||
| } else { | ||
| // 기존 토스트가 없는 경우 | ||
| setMessage(message); | ||
| setVisible(true); | ||
| visibleRef.current = true; | ||
|
|
||
| timerRef.current = window.setTimeout(() => { | ||
| setVisible(false); | ||
| visibleRef.current = false; | ||
| timerRef.current = null; | ||
| }, duration); | ||
| } | ||
|
Comment on lines
+36
to
+67
|
||
| }, [duration, animationDuration]); | ||
|
Comment on lines
+26
to
+68
|
||
|
|
||
| // 컴포넌트 언마운트 시 타이머 정리 | ||
| useEffect(() => { | ||
| return () => { | ||
| if (timerRef.current) { | ||
| window.clearTimeout(timerRef.current); | ||
| } | ||
| if (hideTimerRef.current) { | ||
| window.clearTimeout(hideTimerRef.current); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| return { visible, message, showToast }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.