feat: convert to nextjs
This commit is contained in:
23
context/toast/ToastContext.ts
Normal file
23
context/toast/ToastContext.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { createContext, useContext } from 'react';
|
||||
|
||||
export interface Toast {
|
||||
id: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ToastContextType {
|
||||
showToast: (message: string) => void;
|
||||
toasts: Toast[];
|
||||
}
|
||||
|
||||
export const ToastContext = createContext<ToastContextType | undefined>(
|
||||
undefined
|
||||
);
|
||||
|
||||
export const useToast = () => {
|
||||
const context = useContext(ToastContext);
|
||||
if (!context) {
|
||||
throw new Error('useToast must be used within a ToastProvider');
|
||||
}
|
||||
return context;
|
||||
};
|
||||
23
context/toast/ToastProvider.tsx
Normal file
23
context/toast/ToastProvider.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
import React, { useState, useCallback } from 'react';
|
||||
import { Toast, ToastContext } from './ToastContext';
|
||||
|
||||
export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
children
|
||||
}) => {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const showToast = useCallback((message: string) => {
|
||||
const id = Date.now();
|
||||
setToasts(prev => [...prev, { id, message }]);
|
||||
|
||||
setTimeout(() => {
|
||||
setToasts(prev => prev.filter(toast => toast.id !== id));
|
||||
}, 3000);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ showToast, toasts }}>
|
||||
{children}
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user