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.

8.5 KB
Raw Blame
import { desktopDragRegion } from '../../lib/desktopShell';
import { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { AlertCircle, ChevronLeft, Loader2, Search, X } from 'lucide-react';

import { useMessageSearch } from '../../lib/useMessageSearch';
import { Button } from '../ui/button';
import { Input } from '../ui/input';
import { Switch } from '../ui/switch';
import { AppSearchResultSection } from './search/AppSearchResults';
import { SearchResultGroup } from './search/SearchResultGroup';
import { useAppSearchResults, useOpenSearchApp } from './search/useAppSearch';

// Mobile full-screen app + message-content search (design.pen K7Bytg "M · Search
// results"). A focused surface with its OWN header (back chevron + an active
// search field with mint glyph + clear ✕) over the grouped results — reached
// from the Inbox search field (InboxPage) / the bottom-nav is hidden on
// /search (AppShell). Results are SERVER-driven via useMessageSearch, the same
// hook the desktop ⌘K palette uses; selecting a hit routes to
// /chat/<session>?msg=<message> (the ?msg contract consumed by ChatPage P5).
// Touch surface — no keyboard arrow navigation (that is palette-only).
export const SearchPage: React.FC = () => {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const location = useLocation();
  // The query lives in the URL (?q=…), not just component state, so navigating
  // INTO a result (push /chat/…?msg=…) and back (navigate(-1) → /search?q=…)
  // restores it — otherwise Back would land on a blank search page. Seed the
  // input from the param on mount and mirror every keystroke back into it.
  const [searchParams, setSearchParams] = useSearchParams();
  const [query, setQuery] = useState(() => searchParams.get('q') ?? '');
  // The archived opt-in rides the URL for the same reason ?q= does: opening a
  // result and coming back must restore the toggle. It is deliberately NOT
  // persisted anywhere else — a silently sticky opt-in would change the default
  // result set for every later search.
  const includeArchived = searchParams.get('archived') === '1';
  const { results, loading, error } = useMessageSearch(query, { includeArchived });
  const { results: appResults, loading: appsLoading } = useAppSearchResults(query);
  const openSearchApp = useOpenSearchApp();
  const inputRef = useRef<HTMLInputElement | null>(null);

  // Drive the query (state + URL together). ``replace: true`` so each keystroke
  // updates ?q= in place instead of pushing a history entry per character (Back
  // would otherwise step through every intermediate query). Preserve any other
  // params already on the URL.
  const updateQuery = (next: string) => {
    setQuery(next);
    setSearchParams(
      (prev) => {
        const params = new URLSearchParams(prev);
        if (next) params.set('q', next);
        else params.delete('q');
        return params;
      },
      { replace: true },
    );
  };

  // Same in-place URL write as the query: flipping the opt-in must not push a
  // history entry the Back gesture would have to step through.
  const updateIncludeArchived = (next: boolean) => {
    setSearchParams(
      (prev) => {
        const params = new URLSearchParams(prev);
        if (next) params.set('archived', '1');
        else params.delete('archived');
        return params;
      },
      { replace: true },
    );
  };

  // Back returns to where the user came from (usually the inbox field); a deep
  // link / refresh has nothing to pop, so fall back to /inbox. Mirrors
  // ChatPage's goBack.
  const goBack = () => {
    if (location.key !== 'default') navigate(-1);
    else navigate('/inbox');
  };

  // The clear ✕ empties a non-empty query (keeping focus to keep typing); on an
  // already-empty field it doubles as a back affordance.
  const onClear = () => {
    if (query) {
      updateQuery('');
      inputRef.current?.focus();
    } else {
      goBack();
    }
  };

  const trimmed = query.trim();
  const sessions = results?.sessions ?? [];
  const hasResults = appResults.length > 0 || sessions.length > 0;
  const showHint = trimmed.length === 0;
  const showError = trimmed.length > 0 && !loading && !!error;
  const showEmpty = trimmed.length > 0 && !loading && !appsLoading && results !== null && !hasResults;

  return (
    // Full-screen focused surface (like ChatPage): fixed over the shell, its own
    // header is the top of the screen, and it scrolls internally. Desktop never
    // routes here (sidebar field + ⌘K own search), but keep it sane if hit.
    <div className="fixed inset-x-0 bottom-0 top-[var(--macos-stale-login-session-notice-h,0px)] z-40 flex flex-col bg-background pt-[env(safe-area-inset-top)] md:absolute">
      {/* Header — back chevron + active search field (design.pen P6Nsz/KmsNV). */}
      <header data-tauri-drag-region={desktopDragRegion()} className="flex shrink-0 items-center gap-2.5 border-b border-border bg-background/92 px-4 py-3 backdrop-blur">
        <Button
          type="button"
          variant="ghost"
          size="icon"
          onClick={goBack}
          aria-label={t('common.back')}
          className="size-9 shrink-0 rounded-lg border border-border-strong bg-foreground/[0.04] hover:bg-foreground/[0.08]"
        >
          <ChevronLeft className="size-5" />
        </Button>
        <div className="flex min-w-0 flex-1 items-center gap-2.5 rounded-xl border border-border-strong bg-foreground/[0.04] px-3 py-2.5">
          <Search className="size-4 shrink-0 text-mint-ink" />
          <Input
            ref={inputRef}
            autoFocus
            variant="bare"
            value={query}
            onChange={(e) => updateQuery(e.target.value)}
            placeholder={t('workbench.search.placeholder')}
            aria-label={t('workbench.search.placeholder')}
            className="min-w-0 flex-1 text-[15px]"
            spellCheck={false}
            autoComplete="off"
            autoCorrect="off"
            enterKeyHint="search"
          />
          {(loading || appsLoading) && <Loader2 className="size-4 shrink-0 animate-spin text-muted" />}
          <Button
            type="button"
            variant="ghost"
            size="icon"
            onClick={onClear}
            aria-label={t('common.clear')}
            className="size-5 shrink-0 rounded-md text-muted hover:bg-transparent hover:text-foreground [&_svg]:size-4"
          >
            <X className="size-4" />
          </Button>
        </div>
      </header>

      {/* Body — own scroll surface. Empty-query hint / no-results / grouped
          results, mirroring the desktop palette's states. */}
      <div className="min-h-0 flex-1 overflow-y-auto px-4 pb-[calc(1.5rem+env(safe-area-inset-bottom))] pt-3">
        {/* Archived opt-in — off by default; archived hits open read-only. */}
        <label className="flex items-center gap-2 px-2.5 pb-1 text-[12px] font-medium text-muted">
          <Switch
            checked={includeArchived}
            onCheckedChange={updateIncludeArchived}
            label={t('workbench.search.includeArchived')}
          />
          {t('workbench.search.includeArchived')}
        </label>
        {showHint && (
          <div className="px-2.5 py-12 text-center text-[13px] text-muted">
            {t('workbench.search.hint')}
          </div>
        )}
        {showEmpty && (
          <div className="px-2.5 py-12 text-center text-[13px] text-muted">
            {t('workbench.search.empty')}
          </div>
        )}
        {showError && (
          <div className="px-2.5 py-12">
            <div className="mx-auto flex max-w-sm items-center gap-2 rounded-md border border-destructive/40 bg-destructive/[0.06] px-3 py-2 text-[13px] text-destructive-ink">
              <AlertCircle className="size-4 shrink-0" />
              <span>{t('workbench.search.error')}</span>
            </div>
          </div>
        )}
        {hasResults && (
          <div className="flex flex-col gap-1.5">
            <AppSearchResultSection results={appResults} onSelect={openSearchApp} />
            {sessions.map((session) => (
              <SearchResultGroup
                key={session.session_id}
                session={session}
                onSelect={(match) =>
                  navigate(
                    `/chat/${encodeURIComponent(session.session_id)}?msg=${encodeURIComponent(match.id)}`,
                  )
                }
              />
            ))}
          </div>
        )}
      </div>
    </div>
  );
};