import { atom } from "jotai"; import { atomWithStorage } from "jotai/utils"; import { TPostData } from "../@types/PostTypes"; import { TUser } from "../@types/UserType"; export const userAtom = atom(); export const postCreationAtom = atom(); export const postCreationTitleAtom = atom(); export const themeAtom = atomWithStorage<"light" | "dark">( "theme", "light", { getItem: (key) => localStorage.getItem(key) as any, setItem: (key, value) => localStorage.setItem(key, value as any), removeItem: (key) => localStorage.removeItem(key), }, { getOnInit: true, } ); export const storagePostAtom = atomWithStorage( "draft-post", { title: "", content: "" }, { getItem: (key) => sessionStorage.getItem(key) as any, setItem: (key, value) => sessionStorage.setItem(key, value as any), removeItem: (key) => sessionStorage.removeItem(key), }, { getOnInit: true } ); export const toastAtom = atom([]); export const setToastAtom = atom(null, (get, set, value: TToast) => { let maxToastId = Math.max(...get(toastAtom).map((toast) => toast.id)); maxToastId = maxToastId >= 0 ? maxToastId : 1; let atomValueWithNewToast = get(toastAtom); atomValueWithNewToast = [ ...atomValueWithNewToast, { id: maxToastId + 1, resetFunc: (_) => { let currentToasts = get(toastAtom); let afterRemoval = currentToasts.filter( (toast) => toast.id != maxToastId + 1 ); set(toastAtom, afterRemoval); }, title: value.title, action: value.action, description: value.description, open: true, }, ]; set(toastAtom, atomValueWithNewToast); });