Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.7 KB
Raw Blame
"use client";

import { useCallback, useEffect, useState } from "react";
import { useTranslations } from "next-intl";
import { Toggle } from "@/shared/components";

type FlagEntry = { key: string; effectiveValue: string };

const CODEX_KEY = "OMNIROUTE_AUTO_SYNC_CODEX_PROFILES";
const CLAUDE_KEY = "OMNIROUTE_AUTO_SYNC_CLAUDE_PROFILES";

function isOn(value: string | undefined): boolean {
  return value === "true" || value === "1" || value === "yes" || value === "on";
}

/**
 * Toggle card for the opt-in "auto-sync CLI profiles after model discovery" feature.
 * Reads/writes the OMNIROUTE_AUTO_SYNC_{CODEX,CLAUDE}_PROFILES feature flags via
 * /api/settings/feature-flags. Both default off; enabling one makes a provider model
 * sync regenerate that tool's profile files from the live catalog.
 */
export default function CliProfileAutoSyncToggles() {
  const t = useTranslations("cliTools");
  const [codexOn, setCodexOn] = useState(false);
  const [claudeOn, setClaudeOn] = useState(false);
  const [loading, setLoading] = useState(true);
  const [savingKey, setSavingKey] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);

  const load = useCallback(async () => {
    setLoading(true);
    try {
      const res = await fetch("/api/settings/feature-flags");
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const data = await res.json();
      const flags: FlagEntry[] = Array.isArray(data.flags) ? data.flags : [];
      setCodexOn(isOn(flags.find((f) => f.key === CODEX_KEY)?.effectiveValue));
      setClaudeOn(isOn(flags.find((f) => f.key === CLAUDE_KEY)?.effectiveValue));
      setError(null);
    } catch (err) {
      setError(err instanceof Error ? err.message : t("profileSyncLoadFailed"));
    } finally {
      setLoading(false);
    }
  }, [t]);

  useEffect(() => {
    // Load in an async continuation so every setState happens after an await
    // (react-hooks/set-state-in-effect: no synchronous setState in effect bodies).
    void (async () => {
      await load();
    })();
  }, [load]);

  const persist = useCallback(
    async (key: string, next: boolean, previous: boolean, apply: (v: boolean) => void) => {
      apply(next); // optimistic
      setSavingKey(key);
      setError(null);
      try {
        const res = await fetch("/api/settings/feature-flags", {
          method: "PUT",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ key, value: next ? "true" : "false" }),
        });
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
      } catch (err) {
        apply(previous); // revert on failure
        setError(err instanceof Error ? err.message : t("failedSave"));
      } finally {
        setSavingKey(null);
      }
    },
    [t]
  );

  return (
    <div className="rounded-xl border border-border bg-surface/40 p-4">
      <div className="mb-3">
        <h3 className="text-sm font-semibold text-text-main">{t("profileSyncTitle")}</h3>
        <p className="text-xs text-text-muted">{t("profileSyncDescription")}</p>
      </div>
      <div className="flex flex-col gap-3">
        <Toggle
          checked={codexOn}
          disabled={loading || savingKey === CODEX_KEY}
          onChange={(v) => persist(CODEX_KEY, v, codexOn, setCodexOn)}
          label={t("codexProfiles")}
          description={t("codexProfilesDescription")}
        />
        <Toggle
          checked={claudeOn}
          disabled={loading || savingKey === CLAUDE_KEY}
          onChange={(v) => persist(CLAUDE_KEY, v, claudeOn, setClaudeOn)}
          label={t("claudeProfiles")}
          description={t("claudeProfilesDescription")}
        />
      </div>
      {error ? <p className="mt-2 text-xs text-red-500">{error}</p> : null}
    </div>
  );
}