import { useCallback, useEffect, useLayoutEffect, useMemo, useState, } from "react"; import { useWindows } from "@/contexts/windows"; import { useWindow } from "@/contexts/windows/window"; import { useSession } from "@/contexts/session"; import { FOCUSABLE_ELEMENT, PREVENT_SCROLL, TRANSITIONS_IN_MILLISECONDS, } from "@/utils/constants"; import { useSignal } from "@/contexts/signal"; type Events = { onBlurCapture: (event: React.FocusEvent) => void; onClickCapture: (event?: React.MouseEvent) => void; onFocusCapture: (event?: React.FocusEvent) => void; }; type Focusable = Events & typeof FOCUSABLE_ELEMENT & { zIndex: number; }; const useFocusable = ( id: string, callbackEvents?: Partial, focusElement?: HTMLElement | null ): Focusable => { const { foregroundId, previewID, prependToStack, setForegroundId, stackOrder = [], } = useSession(); const { events: RD_Events } = useSignal(); const { windows: { [id]: currentRDWindow }, } = useWindows(); const [underPreview, setUnderPreview] = useState(false); const { Events: RD_WindowEvents } = useWindow(); const { closing, componentWindow = focusElement, minimized, taskbarEntry, } = currentRDWindow || {}; const zIndex = useMemo( () => previewID !== false && previewID == id ? 1000 : underPreview && minimized ? 1 : stackOrder.length + (minimized ? 1 : -stackOrder.indexOf(id)) + 1, [previewID, id, underPreview, minimized, stackOrder] ); useEffect(() => { let timeoutId; if (!previewID) { timeoutId = setTimeout(() => { setUnderPreview(false); }, TRANSITIONS_IN_MILLISECONDS.WINDOW + 100); } else { setUnderPreview(true); } return () => { if (timeoutId) { clearTimeout(timeoutId); } }; }, [previewID]); const onBlurCapture: React.FocusEventHandler = useCallback( (event) => { const { relatedTarget } = event; const focusedElement = relatedTarget as HTMLElement | null; const focusedOnTaskbarEntry = relatedTarget === taskbarEntry; const focusedOnTaskbarPeek = focusedElement && taskbarEntry?.previousSibling?.contains(focusedElement); const focusedOnInsideWindow = focusedElement && componentWindow?.contains(focusedElement); setForegroundId((currentForegroundId) => { if ( currentForegroundId === id && !focusedOnTaskbarEntry && !focusedOnInsideWindow ) { if (focusedOnTaskbarPeek) { componentWindow?.focus(PREVENT_SCROLL); } else { callbackEvents?.onBlurCapture?.(event); RD_WindowEvents.emit("event:window/unfocus", id, event); RD_Events.emit( ["event:windows/unfocus", `event:windows/unfocus:${id}`], id, event ); } return focusedOnTaskbarPeek ? currentForegroundId : ""; } return currentForegroundId; }); }, [ callbackEvents, componentWindow, id, setForegroundId, taskbarEntry, RD_Events, RD_WindowEvents, ] ); const moveToFront = useCallback( (event?: React.FocusEvent | React.MouseEvent) => { const { relatedTarget } = event || {}; if (componentWindow?.contains(document.activeElement)) { prependToStack(id); setForegroundId(id); } else if (!relatedTarget || document.activeElement === taskbarEntry) { componentWindow?.focus(PREVENT_SCROLL); callbackEvents?.onFocusCapture?.( event as React.FocusEvent ); } }, [ callbackEvents, componentWindow, id, prependToStack, setForegroundId, taskbarEntry, ] ); useLayoutEffect(() => { if (id === foregroundId) { RD_WindowEvents.emit("event:window/focus", id); RD_Events.emit(["event:windows/focus", `event:windows/focus:${id}`], id); } }, [foregroundId, id, RD_Events, RD_WindowEvents]); useLayoutEffect(() => { if (id === foregroundId) moveToFront(); }, [foregroundId, id, moveToFront]); useLayoutEffect(() => { if (componentWindow && !closing && !minimized) { setForegroundId(id); } }, [closing, componentWindow, id, minimized, setForegroundId]); useLayoutEffect(() => { prependToStack(id); setForegroundId(id); }, [id, prependToStack, setForegroundId]); return useMemo( () => ({ onBlurCapture, onClickCapture: moveToFront, onFocusCapture: moveToFront, zIndex, ...FOCUSABLE_ELEMENT, }), [moveToFront, onBlurCapture, zIndex] ); }; export default useFocusable;