Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

15.6 KB
Raw Blame
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { APP_REGISTRY } from '../apps/registry';
import {
  WINDOW_RESTORE_PARAM,
  loadWorkbenchWindows,
  saveWorkbenchWindows,
  sharesWorkbenchLayout,
  stripRestoreParam,
  type PersistedWindow,
} from '../lib/workbenchPersistence';
import { useLatestRef } from '@/lib/useLatestRef';
import { DESKTOP_MEDIA_QUERY, isDesktopViewport } from '../lib/useIsDesktop';
import {
  WindowManagerContext,
  type WindowBounds,
  type WindowInstance,
  type WindowManagerValue,
} from './WindowManagerContext';

const DEFAULT_SIZE = { width: 760, height: 520 };
// Cascade each new window down-right from the last so stacks stay reachable.
const CASCADE_STEP = 32;
const CASCADE_WRAP = 6;

function highestVisibleWindow(windows: readonly WindowInstance[]): WindowInstance | null {
  return windows
    .filter((window) => !window.minimized)
    .reduce<WindowInstance | null>((top, window) => (!top || window.z > top.z ? window : top), null);
}

// Persistence is desktop-only: the window layer is `hidden md:block` and windowed apps open only
// from the desktop-only launcher/Dock, so restoring windows below md would mount invisible bodies
// and a `[]` save from a phone would clobber a real desktop layout. Read live (not frozen at mount)
// so a desktop tab that starts narrow and is widened still restores/saves once it crosses md.
/**
 * `standalone`: this document is a single-app tab (`appTabHref` + `isStandaloneAppTab`).
 * Such a tab neither RESTORES the saved layout — the app it was opened for must not
 * come up underneath a restored window — nor SAVES: its window list starts empty, so a
 * save would clobber the real workbench layout with `[]` in every other tab. Windows
 * opened later inside that tab still work (WindowLayer stays mounted); they just live
 * and die with it. The flag is frozen by the shell at mount, so it never flips mid-life.
 *
 * `onWindowForeground`: the shell can hide the whole window layer (Settings does), in
 * which case a window brought forward would arrive invisible. Rather than teach every
 * caller — launcher, Dock, deep link, a window focusing itself on restore — to ask what
 * is covering it, the manager offers `announceForeground` and lets the shell clear the
 * way. The announcement is made for the caller by `useWindowManager`, not by the methods
 * here, because whether it is warranted depends on the caller and only the caller's own
 * context can answer it — see the hook.
 */
export const WindowManagerProvider: React.FC<{
  children: React.ReactNode;
  standalone?: boolean;
  onWindowForeground?: () => void;
}> = ({
  children,
  standalone = false,
  onWindowForeground,
}) => {
  // Through a ref: `focus` and `openApp` are part of the context value every window
  // consumes, so their identity must not change each time the shell recomputes its
  // exit route.
  const foregroundRef = useLatestRef(onWindowForeground);
  const idSeq = useRef(0);
  const zSeq = useRef(0);
  const openCount = useRef(0);
  // Whether the persisted layout has been restored yet — set the first time we're on desktop, so a
  // desktop-at-mount restore and a restore-after-widen never both fire.
  const restoredRef = useRef(false);

  // Load the persisted layout and seed the id/z/open sequences past the restored maxima (idempotent)
  // so new windows don't collide with, or open behind, restored ones.
  const loadAndSeed = (): WindowInstance[] => {
    const restored = loadWorkbenchWindows();
    for (const w of restored) {
      const n = Number.parseInt(w.id.slice(4), 10); // strip the validated "win-" prefix
      if (Number.isFinite(n)) idSeq.current = Math.max(idSeq.current, n);
      zSeq.current = Math.max(zSeq.current, w.z);
    }
    openCount.current = Math.max(openCount.current, restored.length);
    return restored;
  };

  // Restore in the lazy initializer when the shell mounts at desktop width, so `windows` never
  // starts empty there (which would let the first debounced save wipe the stored layout). A shell
  // that mounts narrow restores later, on crossing the breakpoint (see the effect below).
  //
  // The initializer marks `restoredRef` and seeds the id/z/open sequences, which is a ref write
  // during render. It runs exactly once, before anything is mounted, and is the same bootstrap the
  // after-widen path performs from an effect; moving it out would put the first debounced save back
  // in a position to wipe the stored layout.
  // eslint-disable-next-line react-hooks/refs -- Mount-time bootstrap; see above.
  const [windows, setWindows] = useState<WindowInstance[]>(() => {
    if (!sharesWorkbenchLayout(standalone, isDesktopViewport())) {
      // A standalone tab marks itself restored so the after-widen path below stays inert
      // too: "no restore" must hold for its whole life, not just at mount. A narrow shell
      // leaves the flag clear, so widening to desktop still restores (unchanged).
      if (standalone) restoredRef.current = true;
      return [];
    }
    restoredRef.current = true;
    return loadAndSeed();
  });
  const [focusedId, setFocusedId] = useState<string | null>(() => {
    const visible = windows.filter((window) => !window.minimized);
    return visible.length > 0
      ? visible.reduce((top, window) => (window.z > top.z ? window : top)).id
      : null;
  });

  // Latest windows for the debounced/pagehide save closures (which fire after render).
  const windowsRef = useLatestRef(windows);

  // Per-window close guards: a body (e.g. a dirty editor) registers a getter that
  // returns a confirm message when closing would lose work. Held in a ref so it
  // never triggers re-renders.
  const closeGuards = useRef(new Map<string, () => string | null>());
  // Per-window state providers: a body registers a getter returning its JSON-able snapshot
  // (Editor tabs, Terminal tab titles), read when the layout is saved. Same ref pattern.
  const stateProviders = useRef(new Map<string, () => unknown>());
  // Windows whose close exit animation has started but which are still mounted (removed only at
  // animationEnd). Excluded from the snapshot so a reload mid-animation doesn't re-persist them.
  const closingIds = useRef(new Set<string>());

  const setCloseGuard = useCallback((id: string, getMessage: (() => string | null) | null) => {
    if (getMessage) closeGuards.current.set(id, getMessage);
    else closeGuards.current.delete(id);
  }, []);

  const setStateProvider = useCallback((id: string, getState: (() => unknown) | null) => {
    if (getState) stateProviders.current.set(id, getState);
    else stateProviders.current.delete(id);
  }, []);

  const markClosing = useCallback((id: string) => {
    closingIds.current.add(id);
  }, []);

  // Snapshot every window plus its body's state (from its provider). A provider that throws
  // contributes no appState rather than failing the whole save. Drop the injected restore payload
  // from params so a re-save keeps only original launch params (no nested stale snapshots).
  const buildSnapshot = useCallback((): PersistedWindow[] =>
    windowsRef.current
      .filter((w) => !closingIds.current.has(w.id))
      .map((w) => {
        let appState: unknown;
        try {
          appState = stateProviders.current.get(w.id)?.();
        } catch {
          appState = undefined;
        }
        // A restored window's lazy body may not have mounted + registered its provider yet. Until
        // it does, carry the payload it was restored WITH (still in params) so a save during that
        // gap doesn't drop the very state we're persisting for.
        if (appState === undefined) appState = w.params?.[WINDOW_RESTORE_PARAM];
        return { ...w, params: stripRestoreParam(w.params), appState };
      }), []);

  // Debounced persistence: coalesce bursts of geometry/z/open-close changes into one write.
  const saveTimer = useRef<number | null>(null);
  const flushSave = useCallback(() => {
    if (saveTimer.current !== null) {
      clearTimeout(saveTimer.current);
      saveTimer.current = null;
    }
    if (!sharesWorkbenchLayout(standalone, isDesktopViewport())) return;
    saveWorkbenchWindows(buildSnapshot());
  }, [buildSnapshot, standalone]);
  const scheduleSave = useCallback(() => {
    if (!sharesWorkbenchLayout(standalone, isDesktopViewport())) return;
    if (saveTimer.current !== null) clearTimeout(saveTimer.current);
    saveTimer.current = window.setTimeout(() => {
      saveTimer.current = null;
      saveWorkbenchWindows(buildSnapshot());
    }, 400);
  }, [buildSnapshot, standalone]);

  const confirmClose = useCallback((id: string): boolean => {
    const message = closeGuards.current.get(id)?.();
    return !message || window.confirm(message);
  }, []);

  const announceForeground = useCallback(() => {
    foregroundRef.current?.();
  }, [foregroundRef]);

  const focus = useCallback((id: string) => {
    setFocusedId(id);
    setWindows((prev) => {
      const target = prev.find((w) => w.id === id);
      if (!target) return prev;
      const nextZ = ++zSeq.current;
      // Already on top → no churn.
      if (target.z === nextZ - 1 && prev.every((w) => w.id === id || w.z < target.z)) return prev;
      return prev.map((w) => (w.id === id ? { ...w, z: nextZ } : w));
    });
  }, []);

  const focusCanvas = useCallback(() => {
    setFocusedId(null);
    const active = typeof document === 'undefined' ? null : document.activeElement;
    if (active instanceof HTMLElement && active.closest('[data-window-id]')) active.blur();
  }, []);

  const openApp = useCallback<WindowManagerValue['openApp']>((appId, opts) => {
    const def = APP_REGISTRY[appId];
    const size = { ...DEFAULT_SIZE, ...def?.defaultSize };
    const i = openCount.current++ % CASCADE_WRAP;
    const id = `win-${++idSeq.current}`;
    const z = ++zSeq.current;
    const bounds: WindowBounds = {
      // Cascade starts clear of the 248px sidebar so a new window doesn't open over it
      // (the layer now spans the full viewport); it can still be dragged/maximized over it.
      x: 264 + i * CASCADE_STEP,
      y: 32 + i * CASCADE_STEP,
      width: size.width,
      height: size.height,
      ...opts?.bounds,
    };
    setWindows((prev) => [
      ...prev,
      {
        id,
        appId,
        title: opts?.title,
        params: opts?.params,
        bounds,
        z,
        minimized: false,
        maximized: false,
      },
    ]);
    setFocusedId(id);
    return id;
  }, []);

  const close = useCallback((id: string) => {
    closeGuards.current.delete(id);
    stateProviders.current.delete(id);
    closingIds.current.delete(id);
    const nextWindows = windowsRef.current.filter((window) => window.id !== id);
    setFocusedId((current) => (current === id ? highestVisibleWindow(nextWindows)?.id ?? null : current));
    setWindows(nextWindows);
  }, [windowsRef]);

  const minimize = useCallback((id: string) => {
    const nextWindows = windowsRef.current.map((window) => (window.id === id ? { ...window, minimized: true } : window));
    setFocusedId((current) => (current === id ? highestVisibleWindow(nextWindows)?.id ?? null : current));
    setWindows(nextWindows);
  }, [windowsRef]);

  const restore = useCallback(
    (id: string) => {
      setWindows((prev) => prev.map((w) => (w.id === id ? { ...w, minimized: false } : w)));
      focus(id);
    },
    [focus],
  );

  const toggleMaximize = useCallback((id: string) => {
    setWindows((prev) =>
      prev.map((w) => {
        if (w.id !== id) return w;
        if (w.maximized) {
          return { ...w, maximized: false, bounds: w.restoreBounds ?? w.bounds, restoreBounds: undefined };
        }
        return { ...w, maximized: true, restoreBounds: w.bounds };
      }),
    );
    focus(id);
  }, [focus]);

  const setBounds = useCallback((id: string, patch: Partial<WindowBounds>) => {
    setWindows((prev) => prev.map((w) => (w.id === id ? { ...w, bounds: { ...w.bounds, ...patch } } : w)));
  }, []);

  const setTitle = useCallback((id: string, title: string | undefined) => {
    // Return the SAME array reference when nothing changes so a body that calls this every render
    // (the Editor, on active-tab change) can't trigger a re-render loop.
    setWindows((prev) => {
      const w = prev.find((x) => x.id === id);
      if (!w || w.title === title) return prev;
      return prev.map((x) => (x.id === id ? { ...x, title } : x));
    });
  }, []);

  const setParams = useCallback((id: string, patch: Record<string, unknown>) => {
    setWindows((prev) => prev.map((w) => (w.id === id ? { ...w, params: { ...w.params, ...patch } } : w)));
  }, []);

  // If the shell mounted below md (initializer skipped restore), restore the saved layout the first
  // time the viewport crosses to desktop — BEFORE the now-enabled saves could persist an empty list
  // over it. The window list is still empty while narrow (windowed apps open only from the
  // desktop-only launcher), so replacing it here can't discard anything the user opened.
  useEffect(() => {
    if (restoredRef.current) return;
    if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return;
    const mql = window.matchMedia(DESKTOP_MEDIA_QUERY);
    const restoreOnce = () => {
      if (restoredRef.current || !mql.matches) return;
      restoredRef.current = true;
      const restored = loadAndSeed();
      setWindows((cur) => (cur.length ? cur : restored));
      setFocusedId((current) => {
        if (current) return current;
        const visible = restored.filter((window) => !window.minimized);
        return visible.length > 0
          ? visible.reduce((top, window) => (window.z > top.z ? window : top)).id
          : null;
      });
    };
    restoreOnce(); // may have crossed to desktop between the initial render and this effect
    mql.addEventListener('change', restoreOnce);
    return () => mql.removeEventListener('change', restoreOnce);
  }, []);

  // Persist on any window-list change (open/close, geometry, z, min/max, title). Body-only state
  // (Editor tabs, Terminal titles) that doesn't touch the window list is captured by the same
  // provider read on the next save this triggers, and — authoritatively — by the pagehide flush.
  // scheduleSave itself no-ops below md, so this stays inert until the viewport is desktop-sized.
  useEffect(() => {
    scheduleSave();
  }, [windows, scheduleSave]);

  // A reload fires pagehide (not React unmount): flush a final synchronous snapshot so the very
  // latest body state (a just-renamed terminal tab, a just-opened editor file) is captured.
  // flushSave no-ops below md, so no phone/narrow viewport ever writes.
  useEffect(() => {
    window.addEventListener('pagehide', flushSave);
    return () => {
      window.removeEventListener('pagehide', flushSave);
      if (saveTimer.current !== null) clearTimeout(saveTimer.current);
    };
  }, [flushSave]);

  // True while ANY window is mid drag/resize. A transient shared flag (not persisted)
  // that arms the per-window iframe shield during a gesture (§7.1i).
  const [gestureActive, setGestureActive] = useState(false);

  const value = useMemo<WindowManagerValue>(
    () => ({
      windows,
      focusedId,
      announceForeground,
      openApp,
      close,
      focus,
      focusCanvas,
      minimize,
      restore,
      toggleMaximize,
      setBounds,
      setTitle,
      setParams,
      setCloseGuard,
      setStateProvider,
      markClosing,
      confirmClose,
      gestureActive,
      setGestureActive,
    }),
    [windows, focusedId, announceForeground, openApp, close, focus, focusCanvas, minimize, restore, toggleMaximize, setBounds, setTitle, setParams, setCloseGuard, setStateProvider, markClosing, confirmClose, gestureActive],
  );

  return <WindowManagerContext.Provider value={value}>{children}</WindowManagerContext.Provider>;
};