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.

22.4 KB
Raw Blame
import { useMemo, useRef, useState } from 'react';
import { Reorder } from 'framer-motion';
import { Copy, ExternalLink, LayoutGrid, PinOff, Plus, SquarePlus } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import clsx from 'clsx';

import { appLaunchIntent, appTabHref, isAppleContextClick, tabModifierLabel } from '../../apps/appLaunch';
import { APP_REGISTRY, type AppDefinition, type AppId } from '../../apps/registry';
import { showPageAvatar, showPageIconUrl } from '../../apps/showPageAvatar';
import { ShowPageAvatarContent } from '../../apps/showPageAvatarTile';
import { useDock } from '../../context/DockContext';
import { dockIdToSession } from '../../context/dockDoc';
import { useWindowManager } from '../../context/WindowManagerContext';
import { openLinkInNewContext } from '../../lib/pwaNavigation';
import { ContextMenu, ContextMenuItem } from '../ui/context-menu';
import { useShowPageInventory } from '../useShowPages';
import { isDragRelease } from './dragClick';

// A resident Dock tile resolved from a persisted Dock id: either a built-in app
// (files/terminal/editor) or a pinned Show Page (`show:<session_id>`).
type ResidentItem =
  | { kind: 'builtin'; id: string; def: AppDefinition }
  | { kind: 'showpage'; id: string; sessionId: string; title: string; iconVersion: string | null };

// The unified Dock panel: app launcher + running indicators + minimized windows.
// Built-in apps and pinned Show Pages share one reorderable row (server-persisted
// order); a running app's tile reveals a + (and a right-click menu) to open
// another window. EVERY tile — built-ins included (§7.1c) — can be unpinned from
// the Dock via the right-click menu (it stays installed, so the Apps view can
// re-dock it), and every tile offers Open in New Tab, which a ⌘/Ctrl-click on the
// icon also performs (§7.1m). When the Dock is empty it shows an App Library
// shortcut rather than a dead surface. The minimized-window strip trails the row
// and is not reorderable.
export const Dock: React.FC = () => {
  const { t } = useTranslation();
  const wm = useWindowManager();
  const { order, pins, undock, setOrder } = useDock();
  const { pages } = useShowPageInventory();
  const { windows, focusedId } = wm;
  const minimized = windows.filter((w) => w.minimized);

  // Cursor-positioned right-click menu, on the shared ContextMenu primitive.
  const [menu, setMenu] = useState<{ x: number; y: number; item: ResidentItem } | null>(null);

  const pinBySession = useMemo(() => new Map(pins.map((pin) => [pin.session_id, pin])), [pins]);
  const pageBySession = useMemo(() => new Map(pages.map((page) => [page.session_id, page])), [pages]);

  // A Show Page's display identity from the live inventory: title prefers the live
  // session title, then the pin snapshot, then the session id; icon_version is the
  // has-icon signal + cache token. Shared by the resident tiles and the minimized
  // chips so a page reads identically wherever it appears (§7.1k item 2).
  const showpageIdentity = useMemo(
    () =>
      (sessionId: string, fallbackTitle?: string): { title: string; iconVersion: string | null } => {
        const page = pageBySession.get(sessionId);
        // live session title → pin snapshot → caller fallback (e.g. a minimized
        // window's own title, so it survives an unloaded/failed inventory) → sid.
        const title = page
          ? page.title?.trim() || t('chat.untitled')
          : pinBySession.get(sessionId)?.title_snapshot?.trim() || fallbackTitle?.trim() || sessionId;
        return { title, iconVersion: page?.icon_version ?? null };
      },
    [pageBySession, pinBySession, t],
  );

  const resolveItem = useMemo(() => {
    return (id: string): ResidentItem | null => {
      const sessionId = dockIdToSession(id);
      if (sessionId !== null) {
        const { title, iconVersion } = showpageIdentity(sessionId);
        return { kind: 'showpage', id, sessionId, title, iconVersion };
      }
      const def = APP_REGISTRY[id as AppId];
      return def ? { kind: 'builtin', id, def } : null;
    };
  }, [showpageIdentity]);

  // Local, drag-mutable copy of the order; framer's Reorder updates it live, and
  // it re-syncs whenever the server order changes (load / pin / unpin elsewhere).
  const [dragOrder, setDragOrder] = useState<string[] | null>(null);
  const dragRef = useRef<string[] | null>(null);
  // Last pointer-press point on a tile, to tell a reorder drag from a click on
  // release (§7.1e item 4b): the whole tile is BOTH draggable and click-openable.
  const pressPtRef = useRef<{ x: number; y: number } | null>(null);
  const localOrder = dragOrder ?? order;
  const reorder = (next: string[]) => {
    dragRef.current = next;
    setDragOrder(next);
  };

  // Persist on drop — only when the order actually changed (a plain click can
  // fire a spurious drag-end), so an unchanged reorder makes no request.
  const commitOrder = () => {
    const next = dragRef.current ?? order;
    dragRef.current = null;
    setDragOrder(null);
    if (next.length === order.length && next.every((id, i) => id === order[i])) return;
    void setOrder(next);
  };

  const windowsFor = (item: ResidentItem) =>
    item.kind === 'builtin'
      ? windows.filter((w) => w.appId === item.id)
      : windows.filter((w) => w.appId === 'showpage' && w.params?.sessionId === item.sessionId);

  const focusWindowDom = (id: string) =>
    (document.querySelector(`[data-window-id="${id}"]`) as HTMLElement | null)?.focus?.({ preventScroll: true });

  const openNew = (item: ResidentItem) => {
    if (item.kind === 'builtin') wm.openApp(item.id as AppId);
    else wm.openApp('showpage', { title: item.title, params: { sessionId: item.sessionId, title: item.title } });
    setMenu(null);
  };

  // Click a tile: focus its front window, else un-minimize its top window, else
  // launch one — matched per Show Page by session id (multi-window like built-ins).
  const activate = (item: ResidentItem) => {
    setMenu(null);
    const own = windowsFor(item);
    const visible = own.filter((w) => !w.minimized);
    if (visible.length > 0) {
      const top = visible.reduce((a, b) => (b.z > a.z ? b : a));
      wm.focus(top.id);
      focusWindowDom(top.id);
    } else if (own.length > 0) {
      wm.restore(own.reduce((a, b) => (b.z > a.z ? b : a)).id);
    } else {
      openNew(item);
    }
  };

  const showAll = (item: ResidentItem) => {
    const own = windowsFor(item);
    own.filter((w) => w.minimized).forEach((w) => wm.restore(w.id));
    const visible = own.filter((w) => !w.minimized);
    if (visible.length > 0) wm.focus(visible.reduce((a, b) => (b.z > a.z ? b : a)).id);
    setMenu(null);
  };

  // The app's own standalone browser surface: a Show Page's `/show/<sid>/` page,
  // a built-in's `/apps/<id>` route (§7.1m, shared pure mapping).
  const tabHrefFor = (item: ResidentItem) =>
    item.kind === 'builtin' ? appTabHref({ appId: item.id }) : appTabHref({ appId: 'showpage', sessionId: item.sessionId });

  // The gesture is invisible otherwise, so it is spelled out in the two places a user
  // already looks: the tile's hover tooltip and the right-click menu row it mirrors.
  const tabChord = t('apps.dock.newTabChord', { key: tabModifierLabel() });

  // Hand the app to the browser as a real tab. Returns false when the app has no
  // standalone surface, so a ⌘-click can fall back to a workbench window.
  const openInTab = (item: ResidentItem) => {
    const href = tabHrefFor(item);
    setMenu(null);
    if (!href) return false;
    openLinkInNewContext(href, 'noopener,noreferrer');
    return true;
  };

  // Unpin from the Dock = undock (remove from `order`) for ANY tile, built-ins
  // included. It keeps the app installed (built-ins always; a Show Page stays in
  // the Apps list), so the tile can be re-docked from the Library — uninstalling
  // a page is the separate 移出 action there.
  const undockItem = (dockId: string) => {
    void undock(dockId);
    setMenu(null);
  };

  const focusedApp = windows.find((w) => w.id === focusedId);
  const isActive = (item: ResidentItem) =>
    !!focusedApp &&
    (item.kind === 'builtin'
      ? focusedApp.appId === item.id
      : focusedApp.appId === 'showpage' && focusedApp.params?.sessionId === item.sessionId);

  const menuItemCount = (item: ResidentItem) => {
    const running = windowsFor(item).length > 0;
    // New Window (+ Show All Windows if running) + Open in New Tab (any tile with
    // a standalone surface) + Unpin from Dock (every tile).
    return 1 + (running ? 1 : 0) + (tabHrefFor(item) ? 1 : 0) + 1;
  };

  return (
    <div className="relative" data-settings-interaction-owner="true">
      {/* Bound the panel to the viewport and scroll horizontally: with many pinned
          apps the resident row would otherwise run off-screen (the popover sits at
          the bottom-left), leaving later tiles unreachable for open/reorder/unpin. */}
      <div className="relative flex max-w-[min(88vw,880px)] items-end gap-2 overflow-x-auto overscroll-x-contain rounded-2xl border border-border-strong bg-surface-2/95 p-2 shadow-[0_24px_64px_-12px_rgba(0,0,0,0.65)] backdrop-blur-xl">
        <Reorder.Group axis="x" values={localOrder} onReorder={reorder} as="div" className="flex items-end gap-2">
          {localOrder.map((id, index) => {
            const item = resolveItem(id);
            if (!item) return null;
            const running = windowsFor(item).length > 0;
            const active = isActive(item);
            const label = item.kind === 'builtin' ? t(item.def.titleKey) : item.title;
            const avatar = item.kind === 'showpage' ? showPageAvatar(item.sessionId, item.title) : null;
            const BuiltinIcon = item.kind === 'builtin' ? item.def.icon : null;
            const dotColor = item.kind === 'builtin' ? `var(${item.def.accent})` : `var(${avatar!.accentVar})`;

            return (
              <Reorder.Item
                key={id}
                value={id}
                as="div"
                onDragEnd={commitOrder}
                className="group/tile relative flex w-[60px] cursor-grab flex-col items-center gap-1 active:cursor-grabbing"
              >
                {/* Icon wrapper positions the + affordance to the icon's corner. */}
                <div className="relative">
                  <button
                    type="button"
                    title={[
                      label,
                      index < 9 ? t('apps.dock.switchShortcut', { number: index + 1 }) : null,
                      tabHrefFor(item) ? tabChord : null,
                    ]
                      .filter(Boolean)
                      .join(' · ')}
                    aria-label={label}
                    onPointerDown={(e) => {
                      pressPtRef.current = { x: e.clientX, y: e.clientY };
                    }}
                    onClick={(e) => {
                      // After a reorder drag the browser still fires a click on
                      // release; swallow it past the drag threshold so the tile
                      // doesn't spuriously open. Read-and-null the press point, and
                      // skip the check for keyboard/synthetic activation (detail 0,
                      // clientX/Y 0, no fresh pointerdown) so Enter/Space still opens.
                      const press = pressPtRef.current;
                      pressPtRef.current = null;
                      if (e.detail !== 0 && isDragRelease(press, { x: e.clientX, y: e.clientY })) return;
                      // Modifier click (§7.1m): the platform tab modifier (⌘ on Apple,
                      // Ctrl elsewhere) → a real browser tab, Alt/Shift → another
                      // workbench window. An app with no standalone surface falls back
                      // to a window rather than doing nothing. A macOS Ctrl-click is the
                      // right-click gesture — onContextMenu owns it, so do nothing here.
                      if (isAppleContextClick(e)) return;
                      const intent = appLaunchIntent(e);
                      if (intent === 'newTab' && openInTab(item)) return;
                      if (intent === 'activate') activate(item);
                      else openNew(item);
                    }}
                    onContextMenu={(e) => {
                      e.preventDefault();
                      setMenu({ x: e.clientX, y: e.clientY, item });
                    }}
                    className={clsx(
                      // §7.1k: 12px radius (rounded-lg = --radius-lg = 12px in this theme;
                      // rounded-xl is 16px here) unified with the App Library / ⌘K tiles.
                      // No shared `border` here: built-in tiles carry their own neutral
                      // border (accent only when focused); pinned Show Page tiles are
                      // borderless — the accent lives in the tint + letter color.
                      'grid size-10 place-items-center overflow-hidden rounded-lg text-[15px] font-bold leading-none transition',
                      item.kind === 'builtin' &&
                        (active ? 'border-2 bg-foreground/[0.07]' : 'border border-border bg-foreground/[0.03] hover:bg-foreground/[0.07]'),
                    )}
                    style={
                      item.kind === 'builtin'
                        ? active
                          ? { borderColor: `var(${item.def.accent})` }
                          : undefined
                        : {
                            // Pinned Show Page: the page's favicon or a letter avatar on a
                            // hashed accent tint (no icon pipeline). §7.1k: the per-session
                            // accent RESTING border is gone (it read as noisy across many
                            // pinned tiles); the accent survives as the 16% tint + letter
                            // color. The FOCUSED tile shows an accent ring (box-shadow, not a
                            // resting border) — visible even over an opaque favicon, where a
                            // tint change would be hidden; only one tile is focused at a time,
                            // so it is not the noisy multi-color resting look §7.1k removed.
                            color: `var(${avatar!.accentVar})`,
                            backgroundColor: `color-mix(in srgb, var(${avatar!.accentVar}) 16%, transparent)`,
                            boxShadow: active ? `0 0 0 2px var(${avatar!.accentVar})` : undefined,
                          }
                    }
                  >
                    {item.kind === 'builtin' && BuiltinIcon ? (
                      <BuiltinIcon className="size-5" style={{ color: `var(${item.def.accent})` }} />
                    ) : (
                      <ShowPageAvatarContent
                        iconUrl={item.kind === 'showpage' ? showPageIconUrl(item.sessionId, item.iconVersion) : null}
                        letter={avatar!.letter}
                      />
                    )}
                  </button>

                  {/* + reveals on hover and opens another window directly. */}
                  <button
                    type="button"
                    title={t('apps.dock.newWindow')}
                    aria-label={t('apps.dock.newWindow')}
                    onClick={(e) => {
                      e.stopPropagation();
                      openNew(item);
                    }}
                    // Don't let a press on + start a tile drag.
                    onPointerDown={(e) => e.stopPropagation()}
                    className="absolute -right-1 -top-1 grid size-4 place-items-center rounded-full border border-border-strong bg-surface-2 text-muted opacity-0 shadow transition hover:text-foreground group-hover/tile:opacity-100"
                  >
                    <Plus className="size-2.5" strokeWidth={3} />
                  </button>
                </div>

                <span className="max-w-full truncate text-[10px] leading-tight text-muted">{label}</span>
                <span className="size-1 rounded-full" style={{ backgroundColor: running ? dotColor : 'transparent' }} />
              </Reorder.Item>
            );
          })}
        </Reorder.Group>

        {/* Empty Dock (every tile undocked) is valid — offer a way back to the App
            Library instead of a dead, empty surface (§7.1c). */}
        {localOrder.length === 0 && (
          <button
            type="button"
            onClick={() => {
              wm.openApp('library');
              setMenu(null);
            }}
            className="flex items-center gap-2 rounded-xl border border-dashed border-border px-3 py-2.5 text-[12px] font-medium text-muted transition-colors hover:border-cyan/60 hover:text-foreground"
          >
            <LayoutGrid className="size-4 shrink-0 text-cyan-ink" />
            <span className="whitespace-nowrap">{t('apps.dock.emptyHint')}</span>
          </button>
        )}

        {minimized.length > 0 && <div className="mx-1 h-11 w-px shrink-0 self-center bg-border-strong" />}

        {/* Minimized windows render as a small window-style thumbnail (a mini titlebar with traffic
            lights + the app icon) with the title stacked BELOW it — matching the app tiles' layout so
            the whole row stays vertically consistent. Not reorderable. */}
        {minimized.map((w) => {
          const def = APP_REGISTRY[w.appId];
          const Icon = def.icon;
          // A showpage window's chip mirrors its Dock tile: the page's OWN icon
          // (favicon → letter avatar with its accent) and its live title. Every
          // other app keeps its registry glyph — that IS its icon (§7.1k item 2).
          // Resolve the sessionId param via a local first (the registry idiom) so
          // the typeof check narrows without a possibly-undefined property access.
          const rawSessionId = w.appId === 'showpage' ? w.params?.sessionId : undefined;
          const sessionId = typeof rawSessionId === 'string' && rawSessionId ? rawSessionId : null;
          // Pass w.title as the identity fallback so a persisted minimized window keeps
          // its real title if the inventory hasn't loaded / failed (Codex P3); the
          // live/pinned title still wins when available.
          const identity = sessionId ? showpageIdentity(sessionId, w.title) : null;
          const avatar = sessionId && identity ? showPageAvatar(sessionId, identity.title) : null;
          const label = identity ? identity.title : (w.title ?? t(def.titleKey));
          return (
            <button
              key={w.id}
              type="button"
              title={label}
              aria-label={t('apps.window.restore')}
              onClick={() => wm.restore(w.id)}
              className="group/min flex w-[64px] shrink-0 flex-col items-center gap-1"
            >
              <span className="flex h-9 w-14 flex-col overflow-hidden rounded-md border border-border-strong bg-surface shadow-sm transition group-hover/min:border-foreground/40">
                {/* Faux window titlebar with the three traffic lights. */}
                <span className="flex h-2.5 shrink-0 items-center gap-[3px] border-b border-border/70 bg-surface-2 px-1">
                  <span className="size-1 rounded-full" style={{ backgroundColor: '#ff5f57' }} />
                  <span className="size-1 rounded-full" style={{ backgroundColor: '#febc2e' }} />
                  <span className="size-1 rounded-full" style={{ backgroundColor: '#28c840' }} />
                </span>
                <span className="grid flex-1 place-items-center">
                  {sessionId && avatar ? (
                    // The page's favicon (else letter avatar on its accent tint) — the
                    // same shared chokepoint the Dock tile uses, scaled to the chip body.
                    <span
                      aria-hidden
                      className="grid size-5 place-items-center overflow-hidden rounded-md text-[11px] font-bold leading-none"
                      style={{
                        color: `var(${avatar.accentVar})`,
                        backgroundColor: `color-mix(in srgb, var(${avatar.accentVar}) 16%, transparent)`,
                      }}
                    >
                      <ShowPageAvatarContent
                        iconUrl={showPageIconUrl(sessionId, identity!.iconVersion)}
                        letter={avatar.letter}
                      />
                    </span>
                  ) : (
                    <Icon className="size-4" style={{ color: `var(${def.accent})` }} />
                  )}
                </span>
              </span>
              <span className="max-w-full truncate text-[10px] leading-tight text-muted">{label}</span>
            </button>
          );
        })}
      </div>

      {menu &&
        (() => {
          // Bind the target to a const so the discriminated-union narrowing
          // survives into the item onClick closures (a mutable `menu.item` would
          // widen back to ResidentItem inside them).
          const item = menu.item;
          const hasTab = !!tabHrefFor(item);
          return (
            <ContextMenu x={menu.x} y={menu.y} onClose={() => setMenu(null)} width={hasTab ? 268 : 196} itemCount={menuItemCount(item)}>
              <ContextMenuItem
                icon={<SquarePlus className="size-[15px] text-cyan-ink" />}
                label={t('apps.dock.newWindow')}
                onClick={() => openNew(item)}
              />
              {windowsFor(item).length > 0 && (
                <ContextMenuItem
                  icon={<Copy className="size-[15px]" />}
                  label={t('apps.dock.showAllWindows')}
                  onClick={() => showAll(item)}
                />
              )}
              {/* Open in New Tab is no longer Show-Page-only: a built-in has an
                  `/apps/<id>` surface too, and this is the discoverable twin of the
                  ⌘/Ctrl-click on the icon (§7.1m). */}
              {hasTab && (
                <ContextMenuItem
                  icon={<ExternalLink className="size-[15px]" />}
                  label={t('apps.dock.openInNewTab')}
                  shortcut={tabChord}
                  onClick={() => openInTab(item)}
                />
              )}
              {/* Unpin (undock) is available on EVERY tile now, built-ins included. */}
              <ContextMenuItem
                icon={<PinOff className="size-[15px]" />}
                label={t('apps.dock.unpin')}
                danger
                onClick={() => undockItem(item.id)}
              />
            </ContextMenu>
          );
        })()}
    </div>
  );
};