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.

954 B
Raw Blame
import { createContext, useContext, useEffect, useLayoutEffect, useRef } from 'react';

export const RouteSurfaceActiveContext = createContext(true);

export const useRouteSurfaceActive = (): boolean => useContext(RouteSurfaceActiveContext);

export const useRouteSurfaceWindowEvent = <K extends keyof WindowEventMap>(
  type: K,
  listener: (event: WindowEventMap[K]) => void,
  enabled = true,
  options?: boolean | AddEventListenerOptions,
): void => {
  const active = useRouteSurfaceActive();
  const listenerRef = useRef(listener);

  useLayoutEffect(() => {
    listenerRef.current = listener;
  }, [listener]);

  useEffect(() => {
    if (!active || !enabled) return;
    const handler = (event: WindowEventMap[K]) => listenerRef.current(event);
    window.addEventListener(type, handler as EventListener, options);
    return () => window.removeEventListener(type, handler as EventListener, options);
  }, [active, enabled, options, type]);
};