Skip to content

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

2.6 KB
Raw Blame
import { FIELD_EDGE } from "./SettingsPrimitives";
import { useEffect, useSyncExternalStore } from "react";
import { api, type ConfigStatus } from "@/state/store";
import { t } from "@/lib/i18n";
import { createAboutMeDraft } from "./about-me-draft";
import { sameFields, useStoreDispatch, useStoreSelector } from "@/state/store-select";

const drafts = new WeakMap<object, ReturnType<typeof createAboutMeDraft>>();

export function EveryBotRules() {
  const state = useStoreSelector((s) => ({ config: s.config }), sameFields), dispatch = useStoreDispatch();
  const confirmed = state.config?.everyBotRules;
  let controller = drafts.get(dispatch);
  if (!controller) {
    controller = createAboutMeDraft(confirmed ?? "", async (sent) => {
      const config = await api<ConfigStatus>("/api/config", { method: "PUT", body: JSON.stringify({ everyBotRules: sent }), timeoutMs: 10_000 });
      dispatch({ type: "configStatus", config });
    });
    drafts.set(dispatch, controller);
  }
  const { value, status } = useSyncExternalStore(controller.subscribe, controller.getSnapshot, controller.getSnapshot);
  const { confirm, edit, flush } = controller;
  useEffect(() => { confirm(confirmed ?? ""); }, [confirm, confirmed]);
  useEffect(() => () => { void flush(); }, [flush]);
  // The server sends the rules only to someone who may change them.
  if (confirmed === undefined) return null;

  return (
    <div className="flex flex-col gap-1.5">
      <label htmlFor="profile-every-bot-rules" className="text-[13px] font-medium leading-[18px] text-ink">{t("settings.profile.everyBotRules")}</label>
      <textarea id="profile-every-bot-rules" value={value} rows={4} maxLength={24_000}
        placeholder={t("settings.profile.everyBotRulesPlaceholder")}
        onChange={(event) => edit(event.target.value)}
        onBlur={() => void flush()}
        className={`min-h-[96px] w-full resize-none rounded-lg ${FIELD_EDGE} bg-app px-2 py-1.5 text-[13px] leading-5 text-ink placeholder:text-ink-tertiary focus:outline-none focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-focus`}
      />
      <div className="text-[12px] leading-4 empty:hidden" role="status">
        {status === "saving" && <span className="text-ink-secondary">{t("settings.profile.saving")}</span>}
        {status === "saved" && <span className="text-success">{t("settings.profile.saved")}</span>}
        {status === "error" && <span className="text-danger">{t("settings.profile.saveError")} {" "}
          <button type="button" onClick={() => void flush()} className="underline">{t("settings.profile.retry")}</button>
        </span>}
      </div>
    </div>
  );
}