Skip to content

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

2.8 KB
Raw Blame
import { useId, useState } from "react";
import { ArrowUpRight, ChartNoAxesCombined, ChevronRight, Table2 } from "lucide-react";
import type { Message } from "@/state/store";
import { useStoreDispatch, useStoreSelector } from "@/state/store-select";
import { t } from "@/lib/i18n";
import { InlineDataChart } from "./data/InlineDataChart";

export function DataResultChip({ message }: { message: Message }) {
  const dispatch = useStoreDispatch();
  const [expanded, setExpanded] = useState(false);
  const queryId = useId();
  const result = message.dataResult;
  const companion = window.ogb?.remoteClient?.active === true;
  const available = useStoreSelector((state) => !companion && Boolean(result) && state.bots.some((bot) => bot.id === result!.botId));
  if (!result) return null;
  const chart = result.kind === "chart";
  const Icon = chart ? ChartNoAxesCombined : Table2;
  return (
    <div data-data-result={result.cardId} className="w-full max-w-[min(var(--chat-prose-max),var(--chat-card-pct))] overflow-hidden rounded-xl border border-hairline/40 bg-panel text-sm">
      <div className="flex items-center gap-3 px-3 py-2">
        <Icon size={16} className="shrink-0 text-ink-tertiary" aria-hidden="true" />
        {result.sql ? <button type="button" aria-expanded={expanded} aria-controls={queryId}
          onClick={() => setExpanded(!expanded)} className="flex min-w-0 items-center gap-1.5 rounded text-left text-ink hover:text-accent focus-visible:outline-accent">
          <span className="truncate" title={result.title}>{result.title}</span>
          <ChevronRight size={13} className={`shrink-0 text-ink-tertiary ${expanded ? "rotate-90" : ""}`} aria-hidden="true" />
        </button> : <span className="min-w-0 truncate text-ink" title={result.title}>{result.title}</span>}
        <button
          type="button"
          disabled={!available}
          title={companion ? t("data.openInWorkspace") : undefined}
          onClick={() => dispatch({ type: "openDataResult", botId: result.botId, cardId: result.cardId })}
          className={`ml-auto flex shrink-0 items-center gap-1 text-xs hover:underline disabled:opacity-40 ${chart ? "text-ink-tertiary hover:text-accent" : "text-accent"}`}
        >
          {t("data.openResult")}<ArrowUpRight size={13} aria-hidden="true" />
        </button>
      </div>
      {result.sql && expanded && <div id={queryId} className="border-t border-hairline/40 px-3 py-2">
        <div className="mb-1 text-[11px] font-medium text-ink-secondary">{t("data.card.sql")}</div>
        <pre dir="ltr" className="max-h-64 overflow-auto rounded-lg bg-inset p-2.5 font-mono text-xs whitespace-pre-wrap break-words text-ink"><code>{result.sql}</code></pre>
      </div>}
      {chart && <InlineDataChart botId={result.botId} cardId={result.cardId} title={result.title} />}
    </div>
  );
}