import { useCallback, useEffect, useState } from "react"; import { type Windows, type WindowData, type WindowElements, type WindowIconType, type NewWindowArguments, type RD_WindowID_T, SnapType, } from "./types"; import { setIcon, closeWindow, maximizeWindow, minimizeWindow, setTitle, createNewWindow, setWindowElement, setWindowSetting, } from "./functions"; import { TRANSITIONS_IN_MILLISECONDS } from "../../utils/constants"; import { useProcesses } from "../process"; import { useSignal } from "../signal"; import { generateOddNumber } from "@/utils/functions"; type WindowsSequence = RD_WindowID_T[]; type WindowsContextState = { update: ( windowID: RD_WindowID_T, settings: Partial, deepCopy?: boolean ) => void; close: (windowID: RD_WindowID_T, closing?: boolean) => void; closeWithTransition: (windowID: RD_WindowID_T) => void; icon: (windowID: RD_WindowID_T, newIcon: WindowIconType) => void; maximize: (windowID: RD_WindowID_T) => void; minimize: (windowID: RD_WindowID_T) => void; setSnap: (windowID: RD_WindowID_T, snapState: SnapType) => void; createWindow: (windowObj: NewWindowArguments) => RD_WindowID_T; linkElement: ( windowID: RD_WindowID_T, name: keyof WindowElements, element: HTMLElement ) => void; windows: Windows; title: (id: RD_WindowID_T, newTitle: string) => void; }; // TODO: Implement a sequence system, and add dragging the windows for changing the sequence. // TODO: When a window is closed, it should be cleared from the sequence array. const useWindowsContextState = (): WindowsContextState => { const [windows, setWindows] = useState( Object.create(null) as Windows ); const [sequence, setSequence] = useState( [] as WindowsSequence ); const { addWindowIDToProcess, removeWindowIDFromProcess } = useProcesses(); const { events: RD_Events, actions: RD_Actions } = useSignal(); const close = useCallback( (id: string, closing?: boolean) => setWindows(closeWindow(id, closing, removeWindowIDFromProcess)), [removeWindowIDFromProcess] ); const linkElement = useCallback( (id: string, name: keyof WindowElements, element: HTMLElement) => setWindows(setWindowElement(id, name, element)), [] ); const icon = useCallback( (id: string, newIcon: WindowIconType) => setWindows(setIcon(id, newIcon)), [] ); const update = useCallback( ( windowID: string, settings: Partial, deepCopy: boolean = true ) => setWindows(setWindowSetting(windowID, settings, deepCopy)), [] ); const setSnap = useCallback( (id: string, snapStatus: SnapType) => setWindows(setWindowSetting(id, { snap: snapStatus })), [] ); const maximize = useCallback( (id: string) => setWindows(maximizeWindow(id)), [] ); const minimize = useCallback( (id: string) => setWindows(minimizeWindow(id)), [] ); const title = useCallback( (id: string, newTitle: string) => setWindows(setTitle(id, newTitle)), [] ); const createWindow = useCallback( (windowObj: NewWindowArguments) => { const newWindowID = generateOddNumber().toString(); setWindows(createNewWindow(newWindowID, windowObj)); // Add the window to the sequence setSequence((prevSequence) => [...prevSequence, newWindowID]); // Following is only if the window successfully opened, for now it's just an API so no ifs or anything: if (windowObj.process && windowObj.process.pid) { addWindowIDToProcess(windowObj.process.pid, newWindowID); } // Create a new object without certain keys // eslint-disable-next-line @typescript-eslint/no-unused-vars const { Component: _Component, ...windowObjWithoutComponent } = windowObj; // Announce the event RD_Events.emit("event:windows/new_window", { id: newWindowID, ...windowObjWithoutComponent, } as Partial); return newWindowID; }, [addWindowIDToProcess, RD_Events] ); const closeWithTransition = useCallback( (id: string): void => { close(id, true); window.setTimeout(() => { close(id); RD_Events.emit("event:windows/close", id); }, TRANSITIONS_IN_MILLISECONDS.WINDOW); RD_Events.emit("event:windows/close_transition", id); }, [close, RD_Events] ); // On initialization: useEffect(() => { const registeredEvents = [ RD_Actions.on("action:windows/create_window", createWindow), ]; return () => { // Cleanup registeredEvents.forEach(RD_Actions.removeListenerByID); }; }, [RD_Actions, createWindow]); useEffect(() => { const registeredEvents = [ RD_Actions.on("action:windows/close", close), RD_Actions.on("action:windows/close_transition", closeWithTransition), ]; return () => { // Cleanup registeredEvents.forEach(RD_Actions.removeListenerByID); }; }, [RD_Actions, closeWithTransition, close]); return { update, linkElement, closeWithTransition, maximize, minimize, setSnap, createWindow, close, icon, title, // State: windows, }; }; export default useWindowsContextState;