Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

21.3 KB
Raw Blame
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, type KeyboardEvent as ReactKeyboardEvent } from "react";
import { createPortal } from "react-dom";
import { ChevronRight } from "lucide-react";
import { cn } from "@/lib/cn";
import { MENU_ENTER, useMenuMotion } from "./MenuMotion";
import { returnMenuFocus, useMenuPress } from "./menu-press";
import { popoverClosesOnKey, usePopoverDismiss } from "@/hooks/use-popover-dismiss";
import { submenuIntent, type Point, type SubmenuIntent } from "@/lib/submenu-intent";
import { MENU_HEADING, MENU_ITEM_SEPARATOR, MENU_SHEET } from "./menu-recipe";

export interface SidebarMenuItem {
  key: string;
  label: string;
  subtitle?: string;
  note?: string;
  icon?: React.ReactNode;
  active?: boolean;
  /** one choice of several: announced as chosen or not; the row draws its own mark */
  checked?: boolean;
  attention?: boolean;
  attentionTone?: "danger" | "accent";
  disabled?: boolean;
  separatorBefore?: boolean;
  heading?: string;
  trailing?: React.ReactNode;
  value?: string;
  keepOpen?: boolean;
  submenu?: SidebarMenuItem[];
  chevron?: boolean;
  /** a 32 px leading picture and a 48 px row: the account at the top of the menu */
  large?: boolean;
  /** a sentence that wraps instead of being cut short */
  wrap?: boolean;
  /** the row's tooltip, for what its one line leaves out */
  title?: string;
  onSelect: () => void;
  tourId?: string;
}

const OPEN_DELAY_MS = 80;
const CLOSE_DELAY_MS = 250;
export const MENU_MIN_WIDTH = 237;
const MENU_MAX_WIDTH = 320;
const EDGE_MARGIN = 8;
const ANCHOR_GAP = 6;

type Box = { left: number; right: number; top: number; bottom: number };
type Size = { width: number; height: number };

/** `start`: above the trigger on its start edge. `end`: below it on its end edge, for a choice at the end of a row. */
export function anchoredMenuPosition(anchor: Box, menu: Size, viewport: Size, rtl = false, align: "start" | "end" = "start"): { left: number; top: number } {
  const maxLeft = Math.max(EDGE_MARGIN, viewport.width - menu.width - EDGE_MARGIN);
  const startsOnRight = align === "end" ? !rtl : rtl;
  const left = Math.min(Math.max(startsOnRight ? anchor.right - menu.width : anchor.left, EDGE_MARGIN), maxLeft);
  const above = anchor.top - ANCHOR_GAP - menu.height;
  const below = anchor.bottom + ANCHOR_GAP;
  const fitsBelow = below + menu.height <= viewport.height - EDGE_MARGIN;
  const top = align === "end" ? (fitsBelow || above < EDGE_MARGIN ? below : above) : above >= EDGE_MARGIN ? above : below;
  const maxTop = Math.max(EDGE_MARGIN, viewport.height - menu.height - EDGE_MARGIN);
  return { left, top: Math.min(Math.max(top, EDGE_MARGIN), maxTop) };
}

export function submenuPosition(menu: Box, row: Box, submenu: Size, viewport: Size, rtl = false): { left: number; top: number } {
  const gap = 4;
  const after = rtl ? menu.left - gap - submenu.width : menu.right + gap;
  const before = rtl ? menu.right + gap : menu.left - gap - submenu.width;
  const fits = (left: number) => left >= EDGE_MARGIN && left + submenu.width <= viewport.width - EDGE_MARGIN;
  const preferred = fits(after) ? after : fits(before) ? before : after;
  const maxLeft = Math.max(EDGE_MARGIN, viewport.width - submenu.width - EDGE_MARGIN);
  const maxTop = Math.max(EDGE_MARGIN, viewport.height - submenu.height - EDGE_MARGIN);
  // Level with its row (its first item beside the row); with no room below, it flips up so its last item sits beside
  // the row instead of sliding up the screen.
  const down = row.top - 6;
  const top = down + submenu.height <= viewport.height - EDGE_MARGIN ? down : row.bottom + 6 - submenu.height;
  return {
    left: Math.min(Math.max(preferred, EDGE_MARGIN), maxLeft),
    top: Math.min(Math.max(top, EDGE_MARGIN), maxTop),
  };
}

const isRtl = (element: Element | null) =>
  typeof getComputedStyle === "function" && element ? getComputedStyle(element).direction === "rtl" : false;

const menuItems = (menu: HTMLElement | null) =>
  menu ? [...menu.querySelectorAll<HTMLElement>(':scope > div > [role^="menuitem"]:not(:disabled)')] : [];

export function nextMenuIndex(key: string, current: number, count: number): number | null {
  if (count === 0) return null;
  switch (key) {
    case "ArrowDown": return current < 0 ? 0 : (current + 1) % count;
    case "ArrowUp": return current < 0 ? count - 1 : (current - 1 + count) % count;
    case "Home": return 0;
    case "End": return count - 1;
    default: return null;
  }
}

function MenuRows({
  items,
  openSubmenu,
  engaged = false,
  dense = false,
  onItem,
  onRowEnter,
  onRowLeave,
}: {
  /** the account menu: rows on a 32 px pitch, 4 px around each divider */
  dense?: boolean;
  items: SidebarMenuItem[];
  openSubmenu?: string | null;
  engaged?: boolean;
  onItem: (item: SidebarMenuItem, element: HTMLElement, keyboard: boolean) => void;
  onRowEnter?: (item: SidebarMenuItem, element: HTMLElement, point: Point) => void;
  onRowLeave?: (item: SidebarMenuItem) => void;
}) {
  return (
    <>
      {items.map((item) => (
        <div key={item.key} className={cn("flex flex-col", !dense || item.separatorBefore || item.heading ? "gap-1" : undefined, dense && item.separatorBefore && "pt-1")}>
          {item.separatorBefore && <div role="separator" className={MENU_ITEM_SEPARATOR} />}
          {item.heading && <div className={MENU_HEADING}>{item.heading}</div>}
          <button
            type="button"
            role={item.checked === undefined ? "menuitem" : "menuitemradio"}
            aria-checked={item.checked}
            data-tour={item.tourId}
            data-menu-key={item.key}
            title={item.title}
            disabled={item.disabled}
            aria-haspopup={item.submenu ? "menu" : undefined}
            aria-expanded={item.submenu ? openSubmenu === item.key : undefined}
            onClick={(event) => onItem(item, event.currentTarget, event.detail === 0)}
            onPointerEnter={(event) => onRowEnter?.(item, event.currentTarget, { x: event.clientX, y: event.clientY })}
            onPointerLeave={() => onRowLeave?.(item)}
            className={cn(
              "flex min-h-8 w-full items-center gap-2 rounded-lg px-2 py-1.5 text-start text-[14px] leading-5 disabled:opacity-60 focus-visible:bg-menu-hover focus-visible:outline-none",
              item.large && "min-h-12 py-1.5",
              item.active || (item.submenu && openSubmenu === item.key && (engaged || dense)) ? "bg-menu-active text-ink" : "text-ink hover:bg-menu-hover",
            )}
          >
            {item.icon && (
              <span
                className={cn("flex shrink-0 items-center justify-center", item.large ? "size-8" : "size-4 [&>svg]:size-4", item.wrap && (dense ? "h-5 self-start" : "h-[18px] self-start"), item.active ? "text-accent" : "text-ink-secondary")}
              >
                {item.icon}
              </span>
            )}
            {item.subtitle || item.note ? (
              <span className="flex min-w-0 flex-1 flex-col">
                <span className={cn(item.wrap ? "break-words" : "truncate", item.large && "font-medium leading-[18px]")}>{item.label}</span>
                {item.subtitle && <span className="truncate text-[12px] font-normal leading-4 text-ink-secondary">{item.subtitle}</span>}
                {item.note && <span className="text-[12px] font-normal leading-snug text-ink-tertiary">{item.note}</span>}
              </span>
            ) : (
              <span className={cn("flex-1", item.wrap ? "min-w-0 break-words" : "truncate")}>{item.label}</span>
            )}
            {item.value && <span className="shrink-0 text-[13px] font-normal leading-[18px] tabular-nums text-ink-secondary">{item.value}</span>}
            {item.trailing}
            {item.attention && (
              <span
                className={cn(
                  "size-2 shrink-0 rounded-full",
                  item.attentionTone === "accent" ? "bg-accent" : "bg-danger",
                )}
              />
            )}
            {(item.submenu || item.chevron) && <ChevronRight size={14} aria-hidden className="-me-0.5 shrink-0 text-ink-secondary rtl:-scale-x-100" />}
          </button>
        </div>
      ))}
    </>
  );
}

const closesOnEscape = (event: ReactKeyboardEvent) =>
  popoverClosesOnKey({ key: event.key, defaultPrevented: event.defaultPrevented, isComposing: event.nativeEvent.isComposing });

export function SidebarPopoverMenu({
  items,
  ariaLabel,
  openOnHover = false,
  placement = "above",
  align = "start",
  triggerClassName = "w-full",
  triggerTitle,
  triggerLabel,
  triggerTour,
  onOpenChange,
  renderTrigger,
  width,
}: {
  /** a fixed width (the account menu's 272) in place of the content's */
  width?: number;
  placement?: "above" | "below";
  align?: "start" | "end";
  items: SidebarMenuItem[];
  ariaLabel: string;
  openOnHover?: boolean;
  triggerClassName?: string;
  triggerTitle?: string;
  /** the trigger's own name, when it says more than the menu's */
  triggerLabel?: string;
  triggerTour?: string;
  onOpenChange?: (open: boolean) => void;
  renderTrigger: (state: { open: boolean }) => React.ReactNode;
}) {
  const [open, setOpen] = useState(false);
  const [pinned, setPinned] = useState(false);
  const [submenu, setSubmenu] = useState<string | null>(null);
  const [position, setPosition] = useState<{ left: number; top: number; fit?: number; origin?: string } | null>(null);
  const [subPosition, setSubPosition] = useState<{ left: number; top: number } | null>(null);
  const rootRef = useRef<HTMLDivElement>(null);
  const triggerRef = useRef<HTMLButtonElement>(null);
  const menuRef = useRef<HTMLDivElement>(null);
  const subRef = useRef<HTMLDivElement>(null);
  const subRowRef = useRef<HTMLElement | null>(null);
  const rowsByKey = useRef(new Map<string, HTMLElement>());
  const [engaged, setEngaged] = useState(false);
  const intentRef = useRef<SubmenuIntent | null>(null);
  intentRef.current ??= submenuIntent({
    open: (key) => {
      subRowRef.current = rowsByKey.current.get(key) ?? null;
      setSubPosition(null);
      setSubmenu(key);
    },
    close: () => { setSubmenu(null); setEngaged(false); },
    submenuBox: () => subRef.current?.getBoundingClientRect() ?? null,
  });
  const intent = intentRef.current;
  const focusFirst = useRef(false);
  const openTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const closeTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
  const menuId = useId();
  const motion = useMenuMotion(open);
  const anchored = placement === "above";
  // The mouse opens the menu on the press; a tap, Enter and Space on the click (menu-press.ts).
  const press = useMenuPress((how) => {
    clearTimers();
    if (open && (pinned || !openOnHover)) close();
    else {
      // A click from Enter or Space lands on the first item.
      focusFirst.current = anchored && how === "keyboard";
      setPinned(true);
      setOpen(true);
    }
  });
  const pressRef = useRef(press);
  pressRef.current = press;
  const reportOpen = useRef(onOpenChange);
  reportOpen.current = onOpenChange;
  useEffect(() => { reportOpen.current?.(open); }, [open]);

  const clearTimers = () => {
    if (openTimer.current) clearTimeout(openTimer.current);
    if (closeTimer.current) clearTimeout(closeTimer.current);
    openTimer.current = null;
    closeTimer.current = null;
  };
  useEffect(() => () => { clearTimers(); intentRef.current?.reset(); }, []);

  const hoverOpen = () => {
    if (!openOnHover || pinned) return;
    clearTimers();
    openTimer.current = setTimeout(() => setOpen(true), OPEN_DELAY_MS);
  };
  const hoverClose = () => {
    if (!openOnHover || pinned) return;
    clearTimers();
    closeTimer.current = setTimeout(() => setOpen(false), CLOSE_DELAY_MS);
  };
  const close = useCallback((refocus = false) => {
    clearTimers();
    intentRef.current?.reset();
    setPinned(false);
    setOpen(false);
    setSubmenu(null);
    setEngaged(false);
    if (refocus) returnMenuFocus(triggerRef.current, pressRef.current.last.current, menuRef.current);
  }, []);

  const insideRef = useRef({
    contains: (target: unknown) =>
      [rootRef.current, menuRef.current, subRef.current].some((node) => node?.contains(target as Node)),
  });
  usePopoverDismiss(open, insideRef, () => close(anchored));

  useLayoutEffect(() => {
    if (!motion.shown) return;
    const place = () => {
      const trigger = triggerRef.current, menu = menuRef.current;
      if (!trigger || !menu) return;
      const viewport = { width: window.innerWidth, height: window.innerHeight };
      // The window's rail of places is drawn over the page's left edge: a menu never opens under it.
      const rail = Number.parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--rail-beside")) || 0;
      if (anchored) {
        const at = anchoredMenuPosition(trigger.getBoundingClientRect(), { width: menu.offsetWidth, height: menu.offsetHeight }, viewport, isRtl(trigger), align);
        let left = Math.max(at.left, rail + EDGE_MARGIN);
        let fit: number | undefined;
        // The account menu stays inside the sidebar it opens from: its right edge 8 in from the sidebar's edge, and a
        // narrow sidebar takes a little off the menu's width rather than let it hang over the chat.
        const side = width && !isRtl(trigger) ? trigger.closest("[data-sidebar-root]")?.getBoundingClientRect() : undefined;
        if (width && side && side.width >= 240 + EDGE_MARGIN * 2) {
          const edge = side.right - EDGE_MARGIN;
          if (left + width > edge) {
            left = Math.max(rail + EDGE_MARGIN, edge - width);
            if (left + width > edge) fit = Math.max(240, edge - left);
          }
        }
        // It grows out of the trigger: from its edge nearest the trigger, on the trigger's side.
        const box = trigger.getBoundingClientRect();
        const origin = `${at.top < box.top ? "bottom" : "top"} ${(align === "end") !== isRtl(trigger) ? "right" : "left"}`;
        setPosition({ ...at, left, fit, origin });
      }
      const sub = subRef.current, row = subRowRef.current;
      if (sub && row) {
        setSubPosition(submenuPosition(menu.getBoundingClientRect(), row.getBoundingClientRect(), { width: sub.offsetWidth, height: sub.offsetHeight }, viewport, isRtl(trigger)));
      }
    };
    place();
    window.addEventListener("resize", place);
    window.addEventListener("scroll", place, true);
    return () => {
      window.removeEventListener("resize", place);
      window.removeEventListener("scroll", place, true);
    };
  }, [anchored, align, motion.shown, submenu, items.length, width]);

  // Focus waits until the anchored sheet is placed: a hidden sheet cannot take focus.
  useEffect(() => {
    if (!open || !focusFirst.current || (anchored && !position)) return;
    focusFirst.current = false;
    menuItems(menuRef.current)[0]?.focus();
  }, [open, anchored, position]);

  const openSub = (item: SidebarMenuItem, row: HTMLElement, focus = false) => {
    rowsByKey.current.set(item.key, row);
    intent.openNow(item.key);
    if (focus) requestAnimationFrame(() => menuItems(subRef.current)[0]?.focus());
  };
  const closeSub = (focusRow = false) => {
    intent.closeNow();
    if (focusRow) subRowRef.current?.focus();
  };

  const selectItem = (item: SidebarMenuItem, row: HTMLElement, keyboard: boolean) => {
    if (item.submenu) {
      if (submenu === item.key) closeSub();
      else openSub(item, row, keyboard);
      return;
    }
    item.onSelect();
    if (!item.keepOpen) close(anchored);
  };

  const onMenuKey = (event: ReactKeyboardEvent<HTMLDivElement>, inSubmenu: boolean) => {
    const menu = event.currentTarget;
    const list = menuItems(menu);
    const current = list.indexOf(document.activeElement as HTMLElement);
    const next = nextMenuIndex(event.key, current, list.length);
    if (next != null) {
      event.preventDefault();
      list[next]?.focus();
      return;
    }
    const rtl = isRtl(menu);
    const forward = rtl ? "ArrowLeft" : "ArrowRight";
    const back = rtl ? "ArrowRight" : "ArrowLeft";
    const focused = list[current];
    const item = focused ? items.find((entry) => entry.key === focused.dataset.menuKey) : undefined;
    if (!inSubmenu && event.key === forward && item?.submenu && focused) {
      event.preventDefault();
      openSub(item, focused, true);
    } else if (inSubmenu && (event.key === back || event.key === "Escape")) {
      // preventDefault tells the window's Escape handler this press is handled.
      event.preventDefault();
      event.stopPropagation();
      closeSub(true);
    } else if (!inSubmenu && closesOnEscape(event)) {
      // The menu, not the panel or dialog around it, owns this Escape: an anchored sheet's keys bubble through the
      // portal to the trigger's ancestors before the window ever sees them.
      event.preventDefault();
      event.stopPropagation();
      close(true);
    } else if (event.key === "Tab" && anchored) {
      event.preventDefault();
      close(true);
    }
  };

  const openItem = submenu ? items.find((item) => item.key === submenu) : undefined;
  // The account menu (a fixed width) is dense: rows on a 32 px pitch with 4 px padding (§6.13).
  const dense = width !== undefined;
  const sheetClass = cn(MENU_SHEET, "overflow-hidden", dense && "gap-0 p-1");

  const sheet = motion.shown && (
    <div
      ref={menuRef}
      id={menuId}
      role="menu"
      aria-label={ariaLabel}
      data-sidebar-popover-menu={anchored ? "anchored" : undefined}
      {...motion.exitProps}
      onKeyDown={(event) => onMenuKey(event, false)}
      onPointerMove={(event) => intent.move({ x: event.clientX, y: event.clientY }, Boolean(submenu && subRowRef.current?.contains(event.target as Node)))}
      onPointerLeave={() => intent.leaveMenu()}
      style={anchored ? { position: "fixed", left: position?.left ?? 0, top: position?.top ?? 0, minWidth: width ? undefined : MENU_MIN_WIDTH, width: width ? `min(${position?.fit ?? width}px, calc(100vw - var(--rail-beside, 0px) - ${EDGE_MARGIN * 2}px))` : "max-content", maxWidth: width ? undefined : `min(${MENU_MAX_WIDTH}px, calc(100vw - ${EDGE_MARGIN * 2}px))`, maxHeight: `calc(100vh - ${EDGE_MARGIN * 2}px)`, visibility: position ? undefined : "hidden", transformOrigin: position?.origin } : undefined}
      className={cn(
        sheetClass,
        anchored ? "z-[60] overflow-y-auto" : "absolute z-40",
        placement === "below" && "top-full right-0 mt-1 w-72 max-w-[calc(100vw-2rem)] origin-top-right",
        motion.className,
      )}
    >
      <MenuRows
        items={items}
        dense={dense}
        openSubmenu={submenu}
        engaged={engaged}
        onItem={selectItem}
        onRowEnter={(item, row, point) => {
          if (item.submenu) rowsByKey.current.set(item.key, row);
          intent.enterRow(item.key, Boolean(item.submenu), point);
        }}
        onRowLeave={(item) => intent.leaveRow(item.key)}
      />
    </div>
  );

  const subSheet = motion.shown && openItem?.submenu && (
    <div
      ref={subRef}
      role="menu"
      aria-label={openItem.label}
      data-sidebar-popover-submenu
      onKeyDown={(event) => onMenuKey(event, true)}
      onPointerEnter={() => { intent.enterSubmenu(); setEngaged(true); }}
      onPointerLeave={() => setEngaged(false)}
      onFocus={() => setEngaged(true)}
      onBlur={(event) => { if (!(event.relatedTarget instanceof Node && event.currentTarget.contains(event.relatedTarget))) setEngaged(false); }}
      style={{ position: "fixed", left: subPosition?.left ?? 0, top: subPosition?.top ?? 0, minWidth: 200, width: dense ? 240 : "max-content", maxWidth: `min(${MENU_MAX_WIDTH}px, calc(100vw - ${EDGE_MARGIN * 2}px))`, maxHeight: `calc(100vh - ${EDGE_MARGIN * 2}px)`, visibility: subPosition ? undefined : "hidden" }}
      className={cn(sheetClass, "z-[61] overflow-y-auto", MENU_ENTER)}
    >
      <MenuRows
        dense={dense}
        items={openItem.submenu}
        onItem={(item) => {
          item.onSelect();
          if (!item.keepOpen) close(true);
        }}
      />
    </div>
  );

  return (
    <div
      ref={rootRef}
      className="relative"
      onPointerEnter={hoverOpen}
      onPointerLeave={hoverClose}
      onFocus={() => openOnHover && setOpen(true)}
      onBlur={(event) => {
        if (pinned) return;
        if (!event.relatedTarget || !insideRef.current.contains(event.relatedTarget)) setOpen(false);
      }}
    >
      <button
        ref={triggerRef}
        type="button"
        aria-haspopup="menu"
        aria-expanded={open}
        aria-controls={open ? menuId : undefined}
        aria-label={triggerLabel ?? ariaLabel}
        title={triggerTitle}
        data-tour={triggerTour}
        {...press.triggerProps}
        onKeyDown={(event) => {
          if (open && closesOnEscape(event)) {
            // A menu opened with the mouse leaves focus here; Escape closes it before anything around it.
            event.preventDefault();
            event.stopPropagation();
            close(true);
            return;
          }
          if (!anchored || open || (event.key !== "ArrowUp" && event.key !== "ArrowDown")) return;
          event.preventDefault();
          press.last.current = "keyboard";
          focusFirst.current = true;
          setPinned(true);
          setOpen(true);
        }}
        className={triggerClassName}
      >
        {renderTrigger({ open })}
      </button>

      {anchored
        ? typeof document !== "undefined" && sheet ? createPortal(<>{sheet}{subSheet}</>, document.body) : null
        : <>{sheet}{typeof document !== "undefined" && subSheet ? createPortal(subSheet, document.body) : null}</>}
    </div>
  );
}