Skip to content

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

9.5 KB
Raw Blame
import { useEffect, useState, type ReactNode } from "react";
import { AppWindow, CircleAlert, CircleCheck, Info, Loader2, Maximize2, RotateCw, X } from "lucide-react";
import { cn } from "@/lib/cn";
import { intlLocale, t } from "@/lib/i18n";
import { openExternalLink } from "@/lib/app-links";
import type { AppDoneState, AppNetwork } from "../../../shared/app";
import { ASK_CHIP, ASK_CHIP_IDLE, ASK_QUIET_BUTTON } from "../AskCard";

const PAGE_ICON = "flex shrink-0 text-ink-secondary";
export const APP_SURFACE = "w-full overflow-hidden rounded-2xl bg-composer ring-1 ring-composer-ring";
const ICON_BUTTON =
  "flex size-7 shrink-0 items-center justify-center rounded-full text-ink-tertiary transition-colors hover:bg-ink/[0.07] hover:text-ink focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus disabled:opacity-40";

const decimal = (value: number, digits: number) =>
  new Intl.NumberFormat(intlLocale(), { minimumFractionDigits: digits, maximumFractionDigits: digits, useGrouping: false }).format(value);

export function formatAppBytes(bytes: number): string {
  if (!Number.isFinite(bytes) || bytes < 0) return "";
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${decimal(bytes / 1024, bytes < 10 * 1024 ? 1 : 0)} KB`;
  return `${decimal(bytes / (1024 * 1024), 1)} MB`;
}

export function AppHeader({
  title,
  origin,
  infoId,
  infoOpen,
  onInfo,
  onReload,
  reloading = false,
  bordered,
}: {
  title: string;
  origin: string;
  infoId: string;
  infoOpen: boolean;
  onInfo(): void;
  onReload?: () => void;
  reloading?: boolean;
  bordered: boolean;
}) {
  return (
    <div data-app-header className={cn("flex items-start gap-3", bordered ? "px-4 pb-3 pt-3.5" : "pb-2")}>
      <div className="min-w-0 flex-1">
        <div title={title} className="truncate text-[15px] font-semibold leading-6 text-ink"><bdi dir="auto">{title}</bdi></div>
        <div className="mt-0.5 flex min-w-0 items-center gap-1.5 text-[13px] leading-snug text-ink-secondary">
          <AppWindow aria-hidden="true" size={13} className="shrink-0 text-ink-tertiary" />
          <span dir="auto" className="min-w-0 truncate">{origin}</span>
        </div>
      </div>
      <div className="-me-1.5 mt-0.5 flex shrink-0 items-center gap-0.5">
        <button
          type="button"
          aria-label={t("app.info")}
          title={t("app.info")}
          aria-expanded={infoOpen}
          aria-controls={infoId}
          onClick={onInfo}
          className={cn(ICON_BUTTON, infoOpen && "bg-ink/[0.07] text-ink")}
        >
          <Info size={15} />
        </button>
        {onReload && (
          <button type="button" aria-label={t("app.reload")} title={t("app.reload")} onClick={onReload} disabled={reloading} className={ICON_BUTTON}>
            <RotateCw size={14} className={cn(reloading && "animate-spin motion-reduce:animate-none")} />
          </button>
        )}
      </div>
    </div>
  );
}

export function PageCardFooter({ title, detail, updated, infoId, infoOpen, onInfo, onReload, reloading = false, open }: {
  title: string;
  detail: string;
  updated: boolean;
  infoId: string;
  infoOpen: boolean;
  onInfo(): void;
  onReload?: () => void;
  reloading?: boolean;
  open?: { pressed: boolean; onClick(): void };
}) {
  return (
    <div data-page-footer className="flex min-h-18 items-center gap-3 border-t border-ink/[0.08] px-5 py-4">
      <span data-page-icon className={PAGE_ICON}>
        <AppWindow aria-hidden="true" size={16} />
      </span>
      <div className="min-w-0 flex-1">
        <div title={title} className="truncate text-sm font-medium leading-[18px] text-ink"><bdi dir="auto">{title}</bdi></div>
        <div className="truncate text-[13px] leading-[18px] text-ink-secondary">{detail}</div>
      </div>
      {updated && <span role="status" data-page-updated className="shrink-0 rounded-full bg-accent/15 px-2 py-px text-[13px] font-medium text-accent-text">{t("chat.preview.updated")}</span>}
      <div className={cn(
        "relative z-[1] -me-1.5 flex shrink-0 items-center gap-0.5 transition-opacity duration-[120ms] motion-reduce:transition-none",
        "group-hover/page:opacity-100 group-focus-within/page:opacity-100 touch:opacity-100",
        !infoOpen && "opacity-0",
      )}>
        <button type="button" aria-label={t("app.info")} title={t("app.info")} aria-expanded={infoOpen} aria-controls={infoId} onClick={onInfo}
          className={cn(ICON_BUTTON, infoOpen && "bg-ink/[0.07] text-ink")}>
          <Info size={15} />
        </button>
        {onReload && (
          <button type="button" aria-label={t("app.reload")} title={t("app.reload")} onClick={onReload} disabled={reloading} className={ICON_BUTTON}>
            <RotateCw size={14} className={cn(reloading && "animate-spin motion-reduce:animate-none")} />
          </button>
        )}
      </div>
      {open && (
        <button type="button" data-page-open aria-pressed={open.pressed} onClick={open.onClick}
          aria-label={t(open.pressed ? "chat.preview.closePage" : "chat.preview.openPage")}
          title={t(open.pressed ? "chat.preview.closePage" : "chat.preview.openPage")}
          className={cn(ICON_BUTTON, "-me-1.5", open.pressed && "bg-ink/[0.07] text-ink")}>
          {open.pressed ? <X aria-hidden="true" size={16} /> : <Maximize2 aria-hidden="true" size={14} />}
        </button>
      )}
    </div>
  );
}

function OriginList({ origins }: { origins: readonly string[] }) {
  if (!origins.length) return <span className="text-ink-tertiary">{t("app.info.nothing")}</span>;
  return <span className="[overflow-wrap:anywhere]">{origins.join(", ")}</span>;
}

export function AppInfo({ id, network, bytes, bordered }: { id: string; network: AppNetwork; bytes: number; bordered: boolean }) {
  const rows: Array<[string, ReactNode]> = [
    [t("app.info.loads"), <OriginList key="r" origins={network.resources} />],
    [t("app.info.connects"), <OriginList key="c" origins={network.connect} />],
  ];
  if (network.refused?.length) rows.push([t("app.info.refused"), <span key="x" className="[overflow-wrap:anywhere]">{network.refused.join(", ")}</span>]);
  if (bytes > 0) rows.push([t("app.info.size"), formatAppBytes(bytes)]);
  rows.push([t("app.info.off"), t("app.info.offList")]);
  return (
    <div id={id} data-app-info className={cn("rounded-2xl bg-inset px-3.5 py-3 text-[13px] leading-[18px]", bordered ? "mx-4 mb-3" : "mb-2")}>
      <p className="mb-2 text-ink-secondary">{t("app.info.sandbox")}</p>
      <dl className="grid grid-cols-[minmax(0,auto)_minmax(0,1fr)] gap-x-4 gap-y-1">
        {rows.map(([label, value]) => (
          <div key={label} className="contents">
            <dt className="whitespace-nowrap text-ink-tertiary">{label}</dt>
            <dd className="min-w-0 text-ink">{value}</dd>
          </div>
        ))}
      </dl>
    </div>
  );
}

export function AppStrip({ text, action, bordered, tone = "warning" }: {
  text: string;
  action?: { label: string; onClick(): void };
  bordered: boolean;
  tone?: "warning" | "quiet";
}) {
  return (
    <div role="status" data-app-strip className={cn("flex items-center gap-2 border-t border-ink/[0.12] py-2.5 text-[13px] leading-[18px] text-ink-secondary", bordered ? "mx-4" : "mt-2")}>
      <CircleAlert aria-hidden="true" size={14} className={cn("shrink-0", tone === "warning" ? "text-warning" : "text-ink-tertiary")} />
      <span dir="auto" className="min-w-0 flex-1 break-words">{text}</span>
      {action && <button type="button" onClick={action.onClick} className={ASK_QUIET_BUTTON}>{action.label}</button>}
    </div>
  );
}

/** The last change the card made itself, kept on the row: a reopened conversation shows it was sent. */
export function AppDone({ done, bordered }: { done: AppDoneState; bordered: boolean }) {
  return (
    <div role="status" data-app-done className={cn("flex items-center gap-2 border-t border-ink/[0.12] py-2.5 text-[13px] leading-[18px] text-ink-secondary", bordered ? "mx-4" : "mt-2")}>
      <CircleCheck aria-hidden="true" size={14} className="shrink-0 text-success" />
      <span dir="auto" className="min-w-0 flex-1 break-words">{t("app.done", { tool: done.tool })}{done.text ? ` · ${done.text}` : ""}</span>
      {done.url && <button type="button" onClick={() => openExternalLink(done.url!)} className={ASK_QUIET_BUTTON}>{t("app.doneOpen")}</button>}
    </div>
  );
}

/** Keeps the frame's height while no frame runs, so the transcript does not jump. */
export function AppPlaceholder({ height, loading, open, label, children }: {
  height: number;
  loading?: boolean;
  open?: () => void;
  label?: string;
  children?: ReactNode;
}) {
  const [slow, setSlow] = useState(false);
  useEffect(() => {
    if (!loading) { setSlow(false); return; }
    const timer = setTimeout(() => setSlow(true), 400);
    return () => clearTimeout(timer);
  }, [loading]);
  return (
    <div data-app-placeholder className="flex w-full flex-col items-center justify-center gap-2 px-4 text-center" style={{ height }}>
      {children}
      {loading && slow && (
        <span role="status" className="flex items-center gap-1.5 text-[13px] text-ink-tertiary">
          <Loader2 aria-hidden="true" size={14} className="animate-spin motion-reduce:animate-none" />
          {t("app.loading")}
        </span>
      )}
      {!loading && open && <AppOpenButton label={label ?? t("app.open")} onClick={open} />}
    </div>
  );
}

export function AppOpenButton({ label, onClick }: { label: string; onClick(): void }) {
  return (
    <button type="button" onClick={onClick} className={cn(ASK_CHIP, ASK_CHIP_IDLE)}>
      <AppWindow aria-hidden="true" size={14} />
      {label}
    </button>
  );
}