"use client"; import { animated, useTransition } from "@react-spring/web"; import { createContext, useCallback, useContext, useRef, useState, } from "react"; import { CloseTiny } from "./Icons/CloseTiny"; type Toast = { content: React.ReactNode; type: "info" | "error" | "success"; duration?: number; }; type Smoke = { position: { x: number; y: number }; text: React.ReactNode; static?: boolean; error?: boolean; alignOnMobile?: "left" | "right" | "center" | undefined; }; type Smokes = Array; let PopUpContext = createContext({ setSmokeState: (_f: (t: Smokes) => Smokes) => {}, setToastState: (_t: Toast | null) => {}, }); export const useSmoker = () => { let { setSmokeState: setState } = useContext(PopUpContext); return (smoke: Smoke) => { let key = Date.now().toString(); setState((smokes) => smokes.concat([{ ...smoke, key }])); setTimeout(() => { setState((smokes) => smokes.filter((t) => t.key !== key)); }, 2000); }; }; export const useToaster = () => { let { setToastState: toaster } = useContext(PopUpContext); return toaster; }; export const PopUpProvider: React.FC> = ( props, ) => { let [smokes, setState] = useState([]); let [toastState, setToastState] = useState(null); let toastTimeout = useRef(null); let toaster = useCallback( (toast: Toast | null) => { if (toastTimeout.current) { window.clearTimeout(toastTimeout.current); toastTimeout.current = null; } setToastState(toast); toastTimeout.current = window.setTimeout( () => { setToastState(null); }, toast?.duration ? toast.duration : 6000, ); }, [setToastState], ); return ( {props.children} {smokes.map((smoke) => ( {smoke.text} ))} ); }; const Toast = (props: { toast: Toast | null; setToast: (t: Toast | null) => void; }) => { let transitions = useTransition(props.toast ? [props.toast] : [], { from: { top: -40 }, enter: { top: 8 }, leave: { top: -40 }, config: {}, }); return transitions((style, item) => { return item ? (
{item.content}
) : null; }); }; const Smoke: React.FC< React.PropsWithChildren<{ x: number; y: number; error?: boolean; static?: boolean; alignOnMobile?: "left" | "right" | "center" | undefined; }> > = (props) => { return (
{props.children}
); };