tahamajs/IE / Projects /front /src /context /ToastContext.tsx
tahamajs's picture
download
raw
3.67 kB
// src/context/ToastContext.tsx
import { createContext, useContext, ReactNode } from "react";
import { toast, ToastOptions, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
// ============================================
// Types
// ============================================
interface ToastContextValue {
success: (message: string, options?: ToastOptions) => void;
error: (message: string, options?: ToastOptions) => void;
info: (message: string, options?: ToastOptions) => void;
warning: (message: string, options?: ToastOptions) => void;
loading: (message: string, options?: ToastOptions) => number | string;
update: (toastId: number | string, options: ToastOptions) => void;
dismiss: (toastId?: number | string) => void;
promise: <T>(
promise: Promise<T>,
messages: { pending: string; success: string; error: string },
options?: ToastOptions
) => Promise<T>;
}
// ============================================
// Default Options
// ============================================
const DEFAULT_OPTIONS: ToastOptions = {
position: "bottom-right",
autoClose: 4000,
hideProgressBar: false,
closeOnClick: true,
pauseOnHover: true,
draggable: true,
};
// ============================================
// Context
// ============================================
const ToastContext = createContext<ToastContextValue | null>(null);
export function ToastProvider({ children }: { children: ReactNode }) {
// ============================================
// Toast Methods
// ============================================
const success = (message: string, options?: ToastOptions) => {
toast.success(message, { ...DEFAULT_OPTIONS, ...options });
};
const error = (message: string, options?: ToastOptions) => {
toast.error(message, { ...DEFAULT_OPTIONS, autoClose: 5000, ...options });
};
const info = (message: string, options?: ToastOptions) => {
toast.info(message, { ...DEFAULT_OPTIONS, autoClose: 3000, ...options });
};
const warning = (message: string, options?: ToastOptions) => {
toast.warning(message, { ...DEFAULT_OPTIONS, ...options });
};
const loading = (message: string, options?: ToastOptions) => {
return toast.loading(message, { ...DEFAULT_OPTIONS, autoClose: false, ...options });
};
const update = (toastId: number | string, options: ToastOptions) => {
toast.update(toastId, options);
};
const dismiss = (toastId?: number | string) => {
if (toastId) toast.dismiss(toastId);
else toast.dismiss();
};
const promise = <T,>(
promise: Promise<T>,
messages: { pending: string; success: string; error: string },
options?: ToastOptions
): Promise<T> => {
return toast.promise(promise, messages, { ...DEFAULT_OPTIONS, ...options }) as Promise<T>;
};
// ============================================
// Value
// ============================================
const value: ToastContextValue = {
success,
error,
info,
warning,
loading,
update,
dismiss,
promise
};
return (
<ToastContext.Provider value={value}>
{children}
<ToastContainer
stacked
limit={5}
position="bottom-right"
autoClose={4000}
hideProgressBar={false}
newestOnTop
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
theme="light"
/>
</ToastContext.Provider>
);
}
export const useToast = () => {
const ctx = useContext(ToastContext);
if (!ctx) throw new Error("useToast must be used within ToastProvider");
return ctx;
};

Xet Storage Details

Size:
3.67 kB
·
Xet hash:
7bc70dcbf938806b99d5d0554d91f0fc22257ec8707b2512f6699c6178f674d7

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.