import { dirname, extname } from "path"; import { type SetStateAction, useCallback, useEffect, useRef, useState, } from "react"; import { WallpaperState, type SessionContextState, type SessionData, type WallpaperFit, type WindowStates, } from "./types"; // import defaultSession from "public/session.json"; import { DEFAULT_ASCENDING, DEFAULT_CLOCK_SOURCE, DEFAULT_THEME, DEFAULT_WALLPAPER, DEFAULT_WALLPAPER_FIT, DESKTOP_PATH, SESSION_FILE, SYSTEM_FILES, TRANSITIONS_IN_MILLISECONDS, } from "../../utils/constants"; import { RD_WindowID_T } from "../windows/types"; // import { updateIconPositionsIfEmpty } from "../../utils/functions"; const DEFAULT_SESSION = /*defaultSession || */ {} as unknown as SessionData; const useSessionContextState = (data): SessionContextState => { const [sessionLoaded, setSessionLoaded] = useState(false); const [previewID, setPreviewID] = useState(false); const [foregroundId, setForegroundId] = useState(""); const [stackOrder, setStackOrder] = useState([]); const [themeName, setThemeName] = useState(DEFAULT_THEME); // const [layoutType, setLayout] = useState(DEFAULT_THEME); const [clockSource, setClockSource] = useState(DEFAULT_CLOCK_SOURCE); const [cursor, setCursor] = useState(""); const [windowStates, setWindowStates] = useState( Object.create(null) as WindowStates ); const [wallpaperFit, setWallpaperFit] = useState(DEFAULT_WALLPAPER_FIT); const [wallpaperData, setWallpaperData] = useState(false); const prependToStack = useCallback( (id: string) => setStackOrder((currentStackOrder) => currentStackOrder[0] === id ? currentStackOrder : [id, ...currentStackOrder.filter((stackId) => stackId !== id)] ), [] ); const removeFromStack = useCallback( (id: string) => setStackOrder((currentStackOrder) => currentStackOrder.filter((stackId) => stackId !== id) ), [] ); const setWallpaper = useCallback( (newWallpaperData: WallpaperState, fit?: WallpaperFit): void => { if (fit) setWallpaperFit(fit); setWallpaperData(newWallpaperData); }, [] ); const [haltSession, setHaltSession] = useState(false); // const initializedSession = useRef(false); const loadingDebounceRef = useRef(0); useEffect(() => { if (!loadingDebounceRef.current && sessionLoaded && !haltSession) { const updateSessionFile = (): void => { /* writeFile( SESSION_FILE, JSON.stringify({ clockSource, cursor, themeName, wallpaperFit, wallpaperImage, windowStates, }), true ); */ }; if ( "requestIdleCallback" in window && typeof window.requestIdleCallback === "function" ) { requestIdleCallback(updateSessionFile); } else { updateSessionFile(); } } }, [ clockSource, cursor, haltSession, sessionLoaded, themeName, wallpaperFit, wallpaperData, windowStates, ]); return { clockSource, cursor, previewID, foregroundId, prependToStack, removeFromStack, stackOrder, themeName, wallpaperFit, wallpaper: wallpaperData, windowStates, sessionLoaded, //--------------- // Functions: setClockSource, setCursor, setPreviewID, setForegroundId, setHaltSession, setThemeName, setWallpaper, setWindowStates, }; }; export default useSessionContextState;