2026-10-09 10:37:48 +08:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
|
import { ArrowUpCircle, Check, Loader2, RefreshCcw } from "lucide-react";
|
2026-09-13 18:24:01 +08:00
|
|
|
import AppLogo from "./AppLogo";
|
|
|
|
|
import AppWordmark from "./AppWordmark";
|
|
|
|
|
import { Button } from "./ui/button";
|
|
|
|
|
import { useApplicationBackend } from "../application/state/useApplicationBackend";
|
|
|
|
|
import type { UpdateState, UseUpdateCheckResult } from "../application/state/useUpdateCheck";
|
|
|
|
|
import { useI18n } from "../application/i18n/I18nProvider";
|
|
|
|
|
import { SettingsAnchor, SettingsTabContent } from "./settings/settings-ui";
|
|
|
|
|
import { toast } from "./ui/toast";
|
|
|
|
|
|
|
|
|
|
type AppInfo = {
|
|
|
|
|
name: string;
|
|
|
|
|
version: string;
|
|
|
|
|
platform?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface SettingsApplicationTabProps {
|
|
|
|
|
updateState: UpdateState;
|
|
|
|
|
checkNow: UseUpdateCheckResult['checkNow'];
|
|
|
|
|
openReleasePage: UseUpdateCheckResult['openReleasePage'];
|
|
|
|
|
installUpdate: UseUpdateCheckResult['installUpdate'];
|
|
|
|
|
startDownload: UseUpdateCheckResult['startDownload'];
|
|
|
|
|
isUpdateDemoMode: boolean;
|
2026-10-09 10:37:48 +08:00
|
|
|
productName: string;
|
|
|
|
|
setProductName: (value: string) => void;
|
2026-09-13 18:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-10-09 10:37:48 +08:00
|
|
|
export default function SettingsApplicationTab({ updateState, checkNow, openReleasePage, installUpdate, startDownload, isUpdateDemoMode, productName, setProductName }: SettingsApplicationTabProps) {
|
2026-09-13 18:24:01 +08:00
|
|
|
const { t } = useI18n();
|
|
|
|
|
const { openExternal, getApplicationInfo } = useApplicationBackend();
|
|
|
|
|
const [appInfo, setAppInfo] = useState<AppInfo>({ name: "Netcatty", version: "" });
|
|
|
|
|
const [lastCheckResult, setLastCheckResult] = useState<'none' | 'available' | 'upToDate'>('none');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
const load = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const info = await getApplicationInfo();
|
|
|
|
|
if (!cancelled && info?.name && typeof info?.version === "string") {
|
|
|
|
|
setAppInfo(info);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Ignore: running in browser/dev without Electron bridge
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
void load();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [getApplicationInfo]);
|
|
|
|
|
|
|
|
|
|
const handleOpenExternal = async (url: string) => {
|
|
|
|
|
try {
|
|
|
|
|
await openExternal(url);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.warn("[SettingsApplicationTab] openExternal failed:", err);
|
|
|
|
|
toast.error(
|
|
|
|
|
t("settings.application.openExternal.failedBody"),
|
|
|
|
|
t("settings.application.openExternal.failedTitle"),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCheckForUpdates = async () => {
|
|
|
|
|
// In demo mode, allow checking even for dev builds
|
|
|
|
|
if (!isUpdateDemoMode && (!appInfo.version || appInfo.version === '0.0.0')) {
|
|
|
|
|
// Dev build - just open releases page
|
|
|
|
|
openReleasePage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setLastCheckResult('none');
|
|
|
|
|
|
|
|
|
|
const result = await checkNow();
|
|
|
|
|
|
|
|
|
|
if (result?.hasUpdate && result.latestRelease) {
|
|
|
|
|
setLastCheckResult('available');
|
|
|
|
|
toast.info(
|
|
|
|
|
t('update.available.message', { version: result.latestRelease.version }),
|
|
|
|
|
t('update.available.title')
|
|
|
|
|
);
|
|
|
|
|
// Don't auto-open the release page here — checkNow() already triggers
|
|
|
|
|
// electron-updater on supported platforms, and the Settings > System tab
|
|
|
|
|
// shows a "Manual Download" link on unsupported platforms.
|
|
|
|
|
} else if (result) {
|
|
|
|
|
setLastCheckResult('upToDate');
|
|
|
|
|
toast.success(
|
|
|
|
|
t('update.upToDate.message', { version: appInfo.version }),
|
|
|
|
|
t('update.upToDate.title')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset the result after 3 seconds
|
|
|
|
|
setTimeout(() => setLastCheckResult('none'), 3000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<SettingsTabContent value="application">
|
|
|
|
|
<div className="flex flex-col lg:flex-row gap-10 lg:gap-14">
|
|
|
|
|
<div className="lg:w-[320px] shrink-0">
|
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
|
<AppLogo className="w-16 h-16" />
|
|
|
|
|
<div>
|
2026-10-09 10:37:48 +08:00
|
|
|
<AppWordmark accessibleLabel={productName} className="h-8 w-auto text-foreground" />
|
2026-09-13 18:24:01 +08:00
|
|
|
<div className="flex items-center gap-2 mt-1">
|
|
|
|
|
<span className="text-sm text-muted-foreground">
|
|
|
|
|
{appInfo.version ? appInfo.version : " "}
|
|
|
|
|
</span>
|
|
|
|
|
{/* Update badge - reflects auto-download state */}
|
|
|
|
|
{updateState.latestRelease && (updateState.hasUpdate || updateState.autoDownloadStatus === 'downloading' || updateState.autoDownloadStatus === 'ready') && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => updateState.autoDownloadStatus === 'ready' ? installUpdate() : updateState.autoDownloadStatus === 'downloading' ? undefined : startDownload()}
|
|
|
|
|
className={cn(
|
|
|
|
|
"inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium",
|
|
|
|
|
updateState.autoDownloadStatus === 'ready'
|
|
|
|
|
? "bg-green-100 dark:bg-green-900 text-green-700 dark:text-green-300 hover:bg-green-200 dark:hover:bg-green-800"
|
|
|
|
|
: "bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 hover:bg-blue-200 dark:hover:bg-blue-800",
|
|
|
|
|
"transition-colors cursor-pointer"
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<ArrowUpCircle size={12} />
|
|
|
|
|
v{updateState.latestRelease.version}{' '}
|
|
|
|
|
{updateState.autoDownloadStatus === 'ready'
|
|
|
|
|
? t('update.restartNow')
|
|
|
|
|
: updateState.autoDownloadStatus === 'downloading'
|
|
|
|
|
? `${updateState.downloadPercent}%`
|
|
|
|
|
: t('update.downloadNow')}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<SettingsAnchor anchorId="application-check-updates" className="mt-6">
|
|
|
|
|
<Button
|
|
|
|
|
variant="secondary"
|
|
|
|
|
className="gap-2"
|
|
|
|
|
onClick={() => void handleCheckForUpdates()}
|
|
|
|
|
disabled={updateState.isChecking || updateState.manualCheckStatus === 'checking' || updateState.autoDownloadStatus === 'downloading' || updateState.autoDownloadStatus === 'ready'}
|
|
|
|
|
>
|
|
|
|
|
{updateState.isChecking ? (
|
|
|
|
|
<Loader2 size={16} className="animate-spin" />
|
|
|
|
|
) : lastCheckResult === 'upToDate' ? (
|
|
|
|
|
<Check size={16} />
|
|
|
|
|
) : (
|
|
|
|
|
<RefreshCcw size={16} />
|
|
|
|
|
)}
|
|
|
|
|
{updateState.isChecking
|
|
|
|
|
? t("update.checking")
|
|
|
|
|
: t("settings.application.checkUpdates")
|
|
|
|
|
}
|
|
|
|
|
</Button>
|
|
|
|
|
</SettingsAnchor>
|
|
|
|
|
|
2026-10-09 10:37:48 +08:00
|
|
|
{/* 产品名称配置 */}
|
|
|
|
|
<SettingsAnchor anchorId="application-product-name" className="mt-6">
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<label className="text-sm font-medium text-foreground">
|
|
|
|
|
{t('settings.application.productName') || 'Product Name'}
|
|
|
|
|
</label>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={productName}
|
|
|
|
|
onChange={(e) => setProductName(e.target.value)}
|
|
|
|
|
onBlur={() => {
|
|
|
|
|
// 失焦时确保标题栏更新
|
|
|
|
|
const bridge = (window as any).netcatty;
|
|
|
|
|
bridge?.setWindowTitle?.(productName);
|
|
|
|
|
}}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
(e.target as HTMLInputElement).blur();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
placeholder="NetMesh"
|
|
|
|
|
className="w-full px-3 py-2 rounded-lg border border-input bg-background text-sm text-foreground focus:outline-none focus:ring-2 focus:ring-ring"
|
2026-09-13 18:24:01 +08:00
|
|
|
/>
|
2026-10-09 10:37:48 +08:00
|
|
|
<p className="text-xs text-muted-foreground">
|
|
|
|
|
{t('settings.application.productNameHint') || 'Used in title bar, sidebar, and settings'}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</SettingsAnchor>
|
2026-09-13 18:24:01 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</SettingsTabContent>
|
|
|
|
|
);
|
|
|
|
|
}
|