import { useCallback, useEffect, useMemo, useRef } from "react"; import { RD_WindowID_T, WindowData, WindowIconType } from "../types"; import EventSignals, { type EventsHandlerType } from "@/utils/events"; import { RD_WindowEventName } from "./types"; import { useSignal } from "@/contexts/signal"; import { useWindows } from "@/contexts/windows"; import { RD_EventName } from "../../signal/types"; type WindowEventEmitter = EventsHandlerType; type WindowContextState = { id: RD_WindowID_T; update: (settings: Partial, deepCopy?: boolean) => void; close: () => void; // shortcut to closeWithTransition(id) setWindowIcon: (newIcon: WindowIconType) => void; setWindowTitle: (newTitle: string) => void; Events: WindowEventEmitter; }; type WindowContextProps = { rd_window_id: RD_WindowID_T; id?: RD_WindowID_T; } const useWindowContextState = ({ rd_window_id, }: WindowContextProps): WindowContextState => { const { icon: setWindowIcon, title: setWindowTitle, closeWithTransition, update, // windows: { [id]: currentWindow }, } = useWindows(); //const { events: RD_Events } = useSignal(); const eventEmitterRef = useRef( EventSignals() ); //const RD_WindowEvents = eventEmitterRef.current; const setWindowIconReady: WindowContextState["setWindowIcon"] = useCallback( (newIcon: WindowIconType) => setWindowIcon(rd_window_id, newIcon), [setWindowIcon, rd_window_id] ); const setWindowTitleReady: WindowContextState["setWindowTitle"] = useCallback( (newTitle: string) => setWindowTitle(rd_window_id, newTitle), [setWindowTitle, rd_window_id] ); const closeThisWindow: WindowContextState["close"] = useCallback( () => closeWithTransition(rd_window_id), [rd_window_id, closeWithTransition] ); const updateThisWindow: WindowContextState["update"] = useCallback( (settings: Partial, deepCopy: boolean = true) => update(rd_window_id, settings, deepCopy), [] ); const contextValue: WindowContextState = useMemo( () => ({ id: rd_window_id, setWindowIcon: setWindowIconReady, setWindowTitle: setWindowTitleReady, close: closeThisWindow, update: updateThisWindow, Events: eventEmitterRef.current, }), [rd_window_id, setWindowIconReady, setWindowTitleReady, closeThisWindow] ); return contextValue; }; export default useWindowContextState;