Skip to content

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

12.7 KB
Raw Blame
import { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { ArrowDown, ArrowDownToLine, Loader2, PackageOpen, RefreshCw, X } from "lucide-react";

import { updaterMessage, useUpdaterState, type UpdaterState } from "@/lib/updater";
import { releaseHighlights } from "@/lib/release-highlights";
import { RELEASES_URL, desktopAppVersion, openExternalLink } from "@/lib/app-links";
import { productName } from "../lib/brand";
import { BOT_BUTTON, botButtonStyle } from "@/lib/bot-button";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { useAdvancedMode } from "@/lib/interface-mode";
import { useDesktopCapabilities, windowFramed } from "./DesktopCapabilities";

export function updateIndicatorShown(state: UpdaterState | null): boolean {
  return Boolean(state && ["downloading", "preparing", "downloaded", "installing"].includes(state.status));
}

export function updateIndicatorLabel(state: UpdaterState): string {
  const app = productName();
  const version = state.version ?? "";
  if (state.status === "downloaded" || state.status === "installing") {
    return t("update.button.ready", { app, version }).replace("  ", " ").trim();
  }
  if (state.status === "preparing") return t("update.button.preparing");
  return state.percent == null
    ? t("update.button.downloadingStart")
    : t("update.button.downloading", { percent: Math.round(state.percent) });
}

/** `bar`: in the window frame's top bar, a download arrow in that bar's button look (the class given). In Advanced, a
 * framed window shows only that arrow, so the sidebar's round buttons draw nothing. In Simple the sidebar's button is
 * the one, a round blue button the height of the Connect apps pill, and the top bar leaves its arrow out while the
 * sidebar is on screen (WindowFrameBar's `sidebarUpdate`). */
export function UpdateIndicator({ className, bar }: { className?: string; bar?: string }) {
  const state = useUpdaterState();
  const framed = windowFramed(useDesktopCapabilities().capabilities);
  const advanced = useAdvancedMode();
  const [open, setOpen] = useState(false);
  const buttonRef = useRef<HTMLButtonElement>(null);
  const settled = !state || state.status === "idle" || state.status === "checking" || state.status === "handed-off";
  useEffect(() => {
    if (settled) setOpen(false);
  }, [settled]);
  if (framed && advanced && bar === undefined) return null;
  if (!state || !updateIndicatorShown(state)) {
    return open && !settled ? <UpdateCard state={state} onClose={() => setOpen(false)} returnFocusRef={buttonRef} /> : null;
  }
  const label = updateIndicatorLabel(state);
  const ready = state.status === "downloaded";
  if (bar !== undefined) return (
    <>
      <button
        ref={buttonRef}
        type="button"
        data-update-indicator={state.status}
        aria-label={label}
        aria-haspopup="dialog"
        title={label}
        onClick={() => setOpen(true)}
        className={cn(bar, "text-accent hover:text-accent", className)}
      >
        <ArrowDownToLine size={15} strokeWidth={2} aria-hidden className={cn(!ready && "animate-pulse motion-reduce:animate-none")} />
      </button>
      {open && <UpdateCard state={state} onClose={() => setOpen(false)} returnFocusRef={buttonRef} />}
    </>
  );
  if (!advanced) return (
    <>
      <button
        ref={buttonRef}
        type="button"
        data-update-indicator={state.status}
        aria-label={label}
        aria-haspopup="dialog"
        title={label}
        onClick={() => setOpen(true)}
        style={botButtonStyle("blue")}
        className={cn(BOT_BUTTON, "flex size-9 shrink-0 items-center justify-center rounded-full shadow-sm transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-focus", !ready && "animate-pulse motion-reduce:animate-none")}
      >
        <ArrowDown size={18} strokeWidth={2.25} aria-hidden />
      </button>
      {open && <UpdateCard state={state} onClose={() => setOpen(false)} returnFocusRef={buttonRef} />}
    </>
  );
  return (
    <div
      className={cn("flex size-[46px] shrink-0 items-center justify-center", className)}
    >
      <button
        ref={buttonRef}
        type="button"
        data-update-indicator={state.status}
        aria-label={label}
        aria-haspopup="dialog"
        title={label}
        onClick={() => setOpen(true)}
        className={cn(
          "relative flex size-7 shrink-0 items-center justify-center rounded-full bg-white text-neutral-900 shadow-sm ring-1 ring-black/10 transition-transform hover:scale-105 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
        )}
      >
        <ArrowDown size={16} strokeWidth={2.25} aria-hidden className={cn(!ready && "animate-pulse")} />
      </button>
      {open && <UpdateCard state={state} onClose={() => setOpen(false)} returnFocusRef={buttonRef} />}
    </div>
  );
}

const button = "inline-flex h-7 min-w-0 items-center justify-center gap-2 rounded-full px-3.5 text-[13px] font-medium leading-4 transition-colors";
const primary = `${button} bg-accent text-white hover:brightness-110 disabled:cursor-default disabled:bg-control disabled:text-ink-secondary disabled:hover:brightness-100`;
const secondary = `${button} text-ink-secondary hover:bg-control hover:text-ink disabled:opacity-50`;

export function UpdateCard({
  state,
  onClose,
  returnFocusRef,
}: {
  state: UpdaterState;
  onClose: () => void;
  returnFocusRef?: React.RefObject<HTMLElement | null>;
}) {
  const dialogRef = useRef<HTMLDivElement>(null);
  const laterRef = useRef<HTMLButtonElement>(null);
  const primaryRef = useRef<HTMLButtonElement>(null);
  const [pending, setPending] = useState(false);
  const closeRef = useRef(onClose);
  closeRef.current = onClose;
  const status = state.status;
  useEffect(() => setPending(false), [status]);

  useEffect(() => {
    const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null;
    (primaryRef.current && !primaryRef.current.disabled ? primaryRef.current : laterRef.current)?.focus();
    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape") {
        event.preventDefault();
        closeRef.current();
        return;
      }
      if (event.key !== "Tab") return;
      const controls = [...(dialogRef.current?.querySelectorAll<HTMLElement>("button:not([disabled])") ?? [])];
      if (!controls.length) return;
      const first = controls[0]!;
      const last = controls[controls.length - 1]!;
      if (event.shiftKey && (document.activeElement === first || !dialogRef.current?.contains(document.activeElement))) {
        event.preventDefault();
        last.focus();
      } else if (!event.shiftKey && (document.activeElement === last || !dialogRef.current?.contains(document.activeElement))) {
        event.preventDefault();
        first.focus();
      }
    };
    window.addEventListener("keydown", onKey);
    return () => {
      window.removeEventListener("keydown", onKey);
      const target = returnFocusRef?.current?.isConnected ? returnFocusRef.current : opener;
      target?.focus();
    };
  }, [returnFocusRef]);

  const updater = window.ogb?.updater;
  const handoff = state.installMode === "handoff";
  const highlights = releaseHighlights(state.releaseNotes);
  const downloading = status === "downloading" || status === "preparing";
  const percent = status === "downloading" && state.percent != null ? Math.max(0, Math.min(100, Math.round(state.percent))) : null;
  const installing = status === "installing" || pending;
  const failed = status === "error";

  const action = () => {
    if (!updater) return;
    if (status === "downloaded") {
      setPending(true);
      void updater.install();
    } else if (failed && state.retryable !== false) {
      setPending(true);
      void updater.check();
    }
  };

  const primaryLabel = installing
    ? handoff ? t("settings.updates.opening") : t("settings.updates.restartingShort")
    : status === "preparing"
      ? t("settings.updates.preparingShort")
      : downloading
        ? percent == null ? t("update.card.downloadingStart") : t("update.card.downloading", { percent })
        : failed
          ? t("update.card.tryAgain")
          : handoff ? t("settings.updates.install") : t("update.card.restart");
  const primaryIcon = installing || (downloading && percent == null) || status === "preparing"
    ? <Loader2 size={14} className="animate-spin" aria-hidden />
    : downloading
      ? <ArrowDown size={14} aria-hidden />
      : handoff ? <PackageOpen size={14} aria-hidden /> : <RefreshCw size={14} aria-hidden />;

  const note = failed
    ? updaterMessage(state)?.split("\n")[0]?.slice(0, 160) || t("update.card.failed")
    : downloading
      ? t("update.card.downloadingNote")
      : handoff
        ? t("update.card.handoffNote")
        : status === "downloaded" && state.installsByItself
          ? t("update.card.idleNote", { app: productName() })
          : t("update.card.restartNote", { app: productName() });

  return createPortal(
    <div
      className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
      onMouseDown={(event) => event.target === event.currentTarget && onClose()}
    >
      <div
        ref={dialogRef}
        role="dialog"
        aria-modal="true"
        aria-labelledby="update-card-title"
        aria-describedby="update-card-note"
        data-update-card
        className="animate-pop-in relative w-full max-w-[420px] rounded-2xl border border-hairline/50 bg-panel p-6 shadow-2xl shadow-black/50"
      >
        <button
          type="button"
          onClick={onClose}
          aria-label={t("update.card.close")}
          className="absolute end-3 top-3 flex size-7 items-center justify-center rounded-full text-ink-secondary hover:bg-control hover:text-ink"
        >
          <X size={16} aria-hidden />
        </button>
        <div className="flex items-center gap-3 pe-8">
          <span className="flex size-10 shrink-0 items-center justify-center rounded-full bg-white text-neutral-900 ring-1 ring-black/10">
            <ArrowDown size={20} strokeWidth={2.25} aria-hidden />
          </span>
          <div className="min-w-0">
            <h2 id="update-card-title" className="text-[17px] font-semibold leading-6 text-ink">
              {t("update.card.title")}
            </h2>
            <p className="text-[13px] leading-5 text-ink-secondary">
              {[state.version ? `${productName()} ${state.version}` : productName(), t("update.card.current", { version: desktopAppVersion() })].join(" · ")}
            </p>
          </div>
        </div>

        {highlights.length > 0 && (
          <div className="mt-5">
            <h3 className="text-[12px] font-semibold uppercase tracking-[0.06em] text-ink-secondary">{t("update.card.whatsNew")}</h3>
            <ul className="mt-2 space-y-2">
              {highlights.map((line) => (
                <li key={line} className="flex gap-2.5 text-[14px] leading-5 text-ink">
                  <span aria-hidden className="mt-2 size-1.5 shrink-0 rounded-full bg-accent" />
                  <span className="min-w-0">{line}</span>
                </li>
              ))}
            </ul>
          </div>
        )}
        <button
          type="button"
          onClick={() => void openExternalLink(RELEASES_URL)}
          className={cn(highlights.length > 0 ? "mt-3" : "mt-5", "block text-[13px] font-medium leading-5 text-accent hover:underline")}
        >
          {t("update.card.allNotes")}
        </button>

        {percent != null && (
          <div
            className="mt-5 h-1.5 overflow-hidden rounded-full bg-control"
            role="progressbar"
            aria-label={t("update.card.progress")}
            aria-valuemin={0}
            aria-valuemax={100}
            aria-valuenow={percent}
          >
            <div className="h-full rounded-full bg-accent transition-[width] duration-300" style={{ width: `${percent}%` }} />
          </div>
        )}

        <p id="update-card-note" className="mt-4 text-[13px] leading-5 text-ink-secondary">{note}</p>

        <div className="mt-5 flex items-center justify-end gap-2">
          <button
            ref={laterRef}
            type="button"
            onClick={onClose}
            disabled={status === "installing"}
            className={secondary}
          >
            {t("update.card.later")}
          </button>
          <button
            ref={primaryRef}
            type="button"
            onClick={action}
            disabled={!updater || installing || downloading || (failed && state.retryable === false)}
            className={primary}
          >
            {primaryIcon}
            <span className="truncate">{primaryLabel}</span>
          </button>
        </div>
      </div>
    </div>,
    document.body,
  );
}