| 1 | import { useEffect } from "react"; |
| 2 | import { app } from "../lib/bridge"; |
| 3 | import { browserPlatformOverride, normalizeDesktopPlatform } from "../lib/desktopPlatform"; |
| 4 | import { setDesktopPlatform, setViewportSize, useWindowChromeStore } from "../store/windowChrome"; |
| 5 | import { |
| 6 | RIGHT_DOCK_TREE_MIN_WIDTH, |
| 7 | SIDEBAR_MIN_WIDTH, |
| 8 | saveRightDockTreeWidth, |
| 9 | saveSidebarWidth, |
| 10 | useLayoutStore, |
| 11 | } from "../store/layout"; |
| 12 | |
| 13 | /** |
| 14 | * Owns the desktop chrome listeners that feed the windowChrome store: the |
| 15 | * native platform probe, viewport resize, the data-platform attribute and the |
| 16 | * layout minimum-width guards. Renders nothing; App composes it once beside |
| 17 | * AppRuntimeEffects so every chrome consumer reads one store. |
| 18 | */ |
| 19 | export function WindowChromeLifecycle() { |
| 20 | const platform = useWindowChromeStore((state) => state.platform); |
| 21 | const sidebarWidth = useLayoutStore((state) => state.sidebarWidth); |
| 22 | const setSidebarWidth = useLayoutStore((state) => state.setSidebarWidth); |
| 23 | const rightDockTreeWidth = useLayoutStore((state) => state.rightDockTreeWidth); |
| 24 | const setRightDockTreeWidth = useLayoutStore((state) => state.setRightDockTreeWidth); |
| 25 | |
| 26 | useEffect(() => { |
| 27 | document.documentElement.setAttribute("data-platform", platform); |
| 28 | }, [platform]); |
| 29 | |
| 30 | useEffect(() => { |
| 31 | let cancelled = false; |
| 32 | const override = browserPlatformOverride(); |
| 33 | if (override) { |
| 34 | setDesktopPlatform(override); |
| 35 | return () => { |
| 36 | cancelled = true; |
| 37 | }; |
| 38 | } |
| 39 | void app.Platform() |
| 40 | .then((value) => { |
| 41 | if (!cancelled) setDesktopPlatform(normalizeDesktopPlatform(value)); |
| 42 | }) |
| 43 | .catch((e) => { |
| 44 | console.warn("platform probe failed", e); |
| 45 | }); |
| 46 | return () => { |
| 47 | cancelled = true; |
| 48 | }; |
| 49 | }, []); |
| 50 | |
| 51 | useEffect(() => { |
| 52 | if (typeof window === "undefined") return; |
| 53 | const onResize = () => { |
| 54 | setViewportSize(window.innerWidth, window.innerHeight); |
| 55 | }; |
| 56 | window.addEventListener("resize", onResize); |
| 57 | return () => window.removeEventListener("resize", onResize); |
| 58 | }, []); |
| 59 | |
| 60 | useEffect(() => { |
| 61 | if (sidebarWidth >= SIDEBAR_MIN_WIDTH) return; |
| 62 | setSidebarWidth(SIDEBAR_MIN_WIDTH); |
| 63 | saveSidebarWidth(SIDEBAR_MIN_WIDTH); |
| 64 | }, [setSidebarWidth, sidebarWidth]); |
| 65 | |
| 66 | useEffect(() => { |
| 67 | if (rightDockTreeWidth >= RIGHT_DOCK_TREE_MIN_WIDTH) return; |
| 68 | setRightDockTreeWidth(RIGHT_DOCK_TREE_MIN_WIDTH); |
| 69 | saveRightDockTreeWidth(RIGHT_DOCK_TREE_MIN_WIDTH); |
| 70 | }, [rightDockTreeWidth, setRightDockTreeWidth]); |
| 71 | |
| 72 | return null; |
| 73 | } |
| 74 |