import { useEffect, useLayoutEffect, type RefObject } from "react"; const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect; /** A chat's own page, where the canvas starts with the chat's header. */ export function isSessionRoutePath(pathname: string): boolean { return /\/sessions\/[^/]+/.test(pathname); } /** * Whether a chat's header is the window bar's row: only in the macOS app, in * the docked (not mobile) layout, on a chat's own page with its session open. * Everywhere else, the web app included, the header stays in the canvas. */ export function chatHeaderInWindowBar(options: { platform: string | null | undefined; mobile: boolean; session: boolean; pathname: string; }): boolean { return ( options.platform === "macos" && !options.mobile && options.session && isSessionRoutePath(options.pathname) ); } /** * While `active`, keeps `--og-window-bar-row-left` on the canvas at the * canvas's left edge in the window, so a header the host lifts into the * window bar's row (the macOS app's `[data-window-bar-row]` rules) starts * where the canvas starts: beside the rail at any width, while the rail * collapses or is resized, and in full screen. The canvas fills the window to * its right edge, so every move of its left edge changes its width. */ export function useWindowBarRowLeft( canvasRef: RefObject, active: boolean, ): void { useIsomorphicLayoutEffect(() => { const canvas = canvasRef.current; if (!active || !canvas) return; const update = () => canvas.style.setProperty( "--og-window-bar-row-left", `${canvas.getBoundingClientRect().left}px`, ); update(); const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update); observer?.observe(canvas); return () => { observer?.disconnect(); canvas.style.removeProperty("--og-window-bar-row-left"); }; }, [canvasRef, active]); }