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.

3.2 KB
Raw Blame
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation, useNavigate } from 'react-router-dom';

import { appLaunchIntent, appTabHref, type LaunchModifiers } from '../../../apps/appLaunch';
import { APP_LIST } from '../../../apps/registry';
import { showAppRoutePath } from '../../apps/mobileDock';
import { useShowPageInventory } from '../../useShowPages';
import { useWindowManager } from '../../../context/WindowManagerContext';
import { openLinkInNewContext } from '../../../lib/pwaNavigation';
import { isDesktopViewport } from '../../../lib/useIsDesktop';
import { filterAppSearchResults, type AppSearchResult } from './appSearch';

export function useAppSearchResults(query: string, enabled = true) {
  const { t } = useTranslation();
  const { pages, loading, loaded } = useShowPageInventory(enabled);
  const candidates = useMemo<AppSearchResult[]>(
    () => [
      ...APP_LIST.map((def) => {
        const title = t(def.titleKey);
        return {
          key: `builtin:${def.id}`,
          kind: 'builtin' as const,
          appId: def.id,
          title,
          searchTitle: title,
        };
      }),
      ...pages.map((page) => {
        const liveTitle = page.title?.trim() ?? '';
        return {
          key: `show:${page.session_id}`,
          kind: 'showpage' as const,
          appId: 'showpage' as const,
          title: liveTitle || t('chat.untitled'),
          searchTitle: liveTitle,
          sessionId: page.session_id,
          iconVersion: page.icon_version,
        };
      }),
    ],
    [pages, t],
  );
  const results = useMemo(() => filterAppSearchResults(candidates, query), [candidates, query]);

  return { results, loading: enabled && (loading || !loaded) };
}

export function useOpenSearchApp() {
  const wm = useWindowManager();
  const navigate = useNavigate();
  const location = useLocation();

  return useCallback(
    (result: AppSearchResult, launch?: LaunchModifiers | null) => {
      // Tab modifier held (click OR Enter) → hand the app to the browser as a real
      // tab, the same rule the Dock and the App Library rows follow (§7.1m).
      if (launch && appLaunchIntent(launch) === 'newTab') {
        const href = appTabHref({ appId: result.appId, sessionId: result.kind === 'showpage' ? result.sessionId : null });
        if (href) {
          openLinkInNewContext(href, 'noopener,noreferrer');
          return;
        }
      }
      const desktop = isDesktopViewport();
      if (desktop) {
        wm.openApp(result.appId, {
          title: result.kind === 'showpage' ? result.title : undefined,
          params:
            result.kind === 'showpage'
              ? { sessionId: result.sessionId, title: result.title }
              : undefined,
        });
        if (location.pathname === '/search') {
          if (location.key === 'default') navigate('/inbox', { replace: true });
          else navigate(-1);
        }
        return;
      }
      if (result.kind === 'showpage') {
        if (result.sessionId) navigate(showAppRoutePath(result.sessionId));
      } else {
        navigate(`/apps/${result.appId}`);
      }
    },
    [location.key, location.pathname, navigate, wm],
  );
}