import { ShowToastOption } from './ToastManageView'; /** * `ToastManageView` 컴포넌트와 연동하여 토스트를 표시하거나 숨기는 기능을 제공합니다. */ class ToastManagerClass { private static instance: ToastManagerClass; private showFunc?: (text: string, option?: ShowToastOption) => void; private hideAllFunc?: () => void; private constructor() {} static getInstance() { if (!ToastManagerClass.instance) { ToastManagerClass.instance = new ToastManagerClass(); } return ToastManagerClass.instance; } /** * ToastManageView 리액트 컴포넌트에 연동시킵니다. */ init = (show: typeof this.showFunc, hideAll: typeof this.hideAllFunc) => { this.showFunc = show; this.hideAllFunc = hideAll; }; /** * ToastManageView 리액트 컴포넌트와의 연결을 제거합니다. */ destroy = () => { this.showFunc = undefined; this.hideAllFunc = undefined; }; /** * 토스트를 표시합니다. */ show = (text: string, option?: ShowToastOption) => { if (this.showFunc) { this.showFunc(text, option); } else { console.warn('ToastManager is not initialized. Call init() first.'); } }; /** * 토스트를 모두 닫습니다. */ hideAll = () => { if (this.hideAllFunc) { this.hideAllFunc(); } else { console.warn('ToastManager is not initialized. Call init() first.'); } }; } const toastManager = ToastManagerClass.getInstance(); /** * `ToastManageView` 컴포넌트와 연동하는 용도 */ export const ToastManagerConnector = { init: toastManager.init, destroy: toastManager.destroy, }; /** * 토스트를 표시하거나 숨기는 기능을 제공합니다. */ export const ToastManager = { /** * 토스트를 표시합니다. * @param text 표시할 텍스트 * @param option 토스트 옵션 * @param option.state 토스트 상태 * @param option.buttonLabel 버튼 텍스트 * @param option.onPressButton 버튼 클릭 시 실행할 함수 * @param option.offset 토스트의 수직 위치 조정 * @param option.duration 토스트 표시 시간 * @see ShowToastOption * * @example * ```ts * ToastManager.show('토스트 메시지'); * ToastManager.show('토스트 메시지', { state: 'success', duration: 10000 }); * ToastManager.show('토스트 메시지', { buttonLabel: '확인', onPressButton: handler, offset: 60 }); * ``` */ show: toastManager.show, /** * 모든 토스트를 닫습니다. */ hideAll: toastManager.hideAll, };