Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

4.8 KB
Raw Blame
import { useState } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { getVersion } from "@tauri-apps/api/app";
import { Sparkles } from "lucide-react";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { providersApi, settingsApi } from "@/lib/api";
import { useSettingsQuery } from "@/lib/query";
import { markSeen } from "@/lib/whatsNew";
import type { Settings } from "@/types";
import { APP_IDS } from "@/config/appSpecs";

const RELEASES_URL = "https://github.com/farion1231/cc-switch/releases";

/** 任一应用的数据库里已经有供应商(找到一家就停) */
async function hasAnyProvider(): Promise<boolean> {
  for (const app of APP_IDS) {
    try {
      const providers = await providersApi.getAll(app);
      if (Object.keys(providers).length > 0) return true;
    } catch {
      // 某个应用读失败不影响其他应用
    }
  }
  return false;
}

/**
 * 「界面改版了」弹窗这次会不会出现:true 会,false 不会,undefined 还在查供应商。
 * - 必须已经看完或跳过使用教程。全新安装时后端会先导入当前配置、再预置官方
 *   供应商,单看「有没有供应商」分不出新老用户;教程结束时会把这个字段一起写成
 *   true,所以看过教程的用户(新老都是)不会再看到这里。
 * - 数据库里已经有供应商(任一应用)。
 * 更新摘要弹窗也读它,排在这个弹窗后面。
 */
export function useNewLayoutNoticePending(
  settings: Settings | undefined,
): boolean | undefined {
  const eligible =
    settings != null &&
    settings.onboardingTourSeen === true &&
    settings.newLayoutNoticeConfirmed !== true;

  const { data: hasProviders } = useQuery({
    queryKey: ["new-layout-notice", "has-providers"],
    queryFn: hasAnyProvider,
    enabled: eligible,
    staleTime: Infinity,
  });

  return eligible ? hasProviders : false;
}

/**
 * 「界面改版了」一次性弹窗:只给升级上来的老用户看,确认后写 newLayoutNoticeConfirmed。
 * 弹窗盖在页面上,不占布局,晚一点出来也不会推动内容区。
 */
export function NewLayoutDialog() {
  const { t } = useTranslation();
  const queryClient = useQueryClient();
  const { data: settings } = useSettingsQuery();
  // 点了就先关,不等保存回来
  const [closed, setClosed] = useState(false);
  const pending = useNewLayoutNoticePending(settings);

  const isOpen = !closed && pending === true;

  const handleAcknowledge = async () => {
    setClosed(true);
    if (!settings) return;
    try {
      const { webdavSync: _, ...rest } = settings;
      // 更新摘要一并记成已看,不再接着弹第二个。看到这里的是从 3.x 升上来的用户,
      // 之前没有记录,摘要本来也只会显示当前这一版;那几条是相对上一个 4.x 预览版
      // 的改动,对他们不如这个弹窗和「查看更新说明」(正式版页面是 4.0 完整说明)有用。
      const version = await getVersion().catch(() => undefined);
      await settingsApi.save({
        ...rest,
        newLayoutNoticeConfirmed: true,
        whatsNewSeenVersion: markSeen(settings.whatsNewSeenVersion, version),
      });
      await queryClient.invalidateQueries({ queryKey: ["settings"] });
    } catch (error) {
      console.error("Failed to save newLayoutNoticeConfirmed:", error);
    }
  };

  const handleViewReleaseNotes = async () => {
    try {
      const version = await getVersion();
      await settingsApi.openExternal(
        version ? `${RELEASES_URL}/tag/v${version}` : RELEASES_URL,
      );
    } catch (error) {
      console.error("Failed to open release notes:", error);
    }
    await handleAcknowledge();
  };

  return (
    <Dialog
      open={isOpen}
      onOpenChange={(open) => {
        if (!open) void handleAcknowledge();
      }}
    >
      <DialogContent className="max-w-md" zIndex="top">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <Sparkles className="h-5 w-5 text-fg-1" />
            {t("newLayoutNotice.title")}
          </DialogTitle>
        </DialogHeader>
        <div className="px-6 py-5">
          <DialogDescription className="whitespace-pre-line leading-relaxed">
            {t("newLayoutNotice.body")}
          </DialogDescription>
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={handleViewReleaseNotes}>
            {t("newLayoutNotice.viewReleaseNotes")}
          </Button>
          <Button onClick={handleAcknowledge}>
            {t("newLayoutNotice.confirm")}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}