fix(update): recover from GitHub rate limits with configurable mirrors
This commit is contained in:
1 parent
68deb48811
commit
20cf7394e5
21 files changed
+1423
-93
No files matched your search
@@ -95,6 +95,7 @@ import { NotificationsPanel } from '@/components/settings/notifications-panel';
|
||||
import { GlobalConfigPanel } from '@/components/settings/global-config-panel';
|
||||
import { SystemPanel } from '@/components/settings/system-panel';
|
||||
import { UpdateInstaller } from '@/components/settings/update-installer';
|
||||
import { UpdateSources } from '@/components/settings/update-sources';
|
||||
import { StoragePanel } from '@/components/settings/storage-panel';
|
||||
import { DeveloperCrashProbe, developerCrashReducer } from '@/components/settings/developer-tools';
|
||||
|
||||
@@ -2000,6 +2001,12 @@ function UpdateGroup() {
|
||||
</div>
|
||||
</SettingRow>
|
||||
{detail}
|
||||
{updateInfo?.error && !disabledCheck && (
|
||||
<p role="alert" className="px-5 py-3 text-xs text-destructive">
|
||||
{updateInfo.error}
|
||||
</p>
|
||||
)}
|
||||
<UpdateSources />
|
||||
<UpdateInstaller version={updateInfo?.hasUpdate ? updateInfo.latest : null} />
|
||||
</Group>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
import { useEffect, useId, useState } from 'react';
|
||||
import { normalizeUpdateSourceConfig } from '@snowluma/common/update-source';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { DropdownSelect } from '@/components/ui/dropdown-select';
|
||||
import { SkeletonSwap } from '@/components/interior/skeleton-swap';
|
||||
import { useActionFeedback } from '@/contexts/ActionFeedbackContext';
|
||||
import { useApi } from '@/lib/api';
|
||||
import type { UpdateSourceConfig, UpdateSourceProbe } from '@/types';
|
||||
|
||||
export function UpdateSources() {
|
||||
const api = useApi();
|
||||
const { runAction } = useActionFeedback();
|
||||
const id = useId();
|
||||
const [config, setConfig] = useState<UpdateSourceConfig | null>(null);
|
||||
const [mirrors, setMirrors] = useState('');
|
||||
const [results, setResults] = useState<UpdateSourceProbe[]>([]);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [ready, setReady] = useState(false);
|
||||
const [reload, setReload] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
let disposed = false;
|
||||
setReady(false);
|
||||
void api.update
|
||||
.sources()
|
||||
.then(({ config: next }) => {
|
||||
if (!disposed) {
|
||||
setConfig(next);
|
||||
setMirrors(next.mirrors.join('\n'));
|
||||
setError(null);
|
||||
}
|
||||
})
|
||||
.catch((caught: unknown) => {
|
||||
if (!disposed) {
|
||||
setError(caught instanceof Error ? caught.message : String(caught));
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (!disposed) {
|
||||
setReady(true);
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
disposed = true;
|
||||
};
|
||||
}, [api, reload]);
|
||||
|
||||
const act = async (testing: boolean) => {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
await runAction(
|
||||
{
|
||||
title: testing ? '正在检测更新来源' : '正在保存更新来源',
|
||||
successTitle: testing ? '来源检测完成' : '更新来源已保存',
|
||||
successDetail: testing
|
||||
? '检测从 SnowLuma 所在服务器发起'
|
||||
: '下次检查和下载时生效',
|
||||
errorTitle: testing ? '来源检测失败' : '保存失败',
|
||||
},
|
||||
async () => {
|
||||
const next = normalizeUpdateSourceConfig({
|
||||
...config,
|
||||
mirrors: mirrors
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean),
|
||||
});
|
||||
|
||||
if (testing) {
|
||||
setResults((await api.update.testSources(next)).results);
|
||||
} else {
|
||||
const saved = await api.update.saveSources(next);
|
||||
|
||||
if (!saved.saved || !saved.applied) {
|
||||
throw new Error(saved.errors.join(';') || saved.message);
|
||||
}
|
||||
|
||||
setConfig(saved.config);
|
||||
setMirrors(saved.config.mirrors.join('\n'));
|
||||
setResults([]);
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (caught) {
|
||||
setError(caught instanceof Error ? caught.message : String(caught));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-3 px-5 py-4">
|
||||
<p className="text-sm font-medium">更新来源</p>
|
||||
{error && (
|
||||
<p role="alert" className="break-words text-xs text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<SkeletonSwap ready={ready} reserve={100} lines={3} label="更新来源">
|
||||
{config ? (
|
||||
<fieldset
|
||||
disabled={busy}
|
||||
className="flex min-w-0 flex-col gap-3 disabled:opacity-60"
|
||||
>
|
||||
<DropdownSelect
|
||||
value={config.mode}
|
||||
ariaLabel="下载来源模式"
|
||||
options={[
|
||||
{ value: 'auto', label: '自动切换' },
|
||||
{ value: 'direct', label: 'GitHub 直连' },
|
||||
{ value: 'mirror', label: '指定镜像' },
|
||||
]}
|
||||
onChange={(mode) => {
|
||||
setConfig({ ...config, mode });
|
||||
setResults([]);
|
||||
}}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
自动模式先尝试
|
||||
GitHub,失败后按列表顺序切换。镜像用于文件下载,版本查询也会尝试 GitHub
|
||||
发布页面。
|
||||
</p>
|
||||
{config.mode === 'mirror' && (
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<label htmlFor={`${id}-mirror`} className="text-xs">
|
||||
指定镜像地址
|
||||
</label>
|
||||
<Input
|
||||
id={`${id}-mirror`}
|
||||
value={config.mirror}
|
||||
placeholder="https://ghfast.top"
|
||||
onChange={(event) => {
|
||||
setConfig({ ...config, mirror: event.target.value });
|
||||
setResults([]);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<details>
|
||||
<summary className="cursor-pointer text-xs">
|
||||
编辑候选镜像(每行一个地址)
|
||||
</summary>
|
||||
<Textarea
|
||||
aria-label="候选镜像地址"
|
||||
className="mt-2 min-h-32 font-mono text-xs"
|
||||
value={mirrors}
|
||||
onChange={(event) => {
|
||||
setMirrors(event.target.value);
|
||||
setResults([]);
|
||||
}}
|
||||
/>
|
||||
<p className="mt-1 text-xs text-muted-foreground">
|
||||
地址后会追加完整 GitHub 链接;支持带路径的代理前缀。
|
||||
</p>
|
||||
</details>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
void act(true);
|
||||
}}
|
||||
>
|
||||
检测可用性
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
void act(false);
|
||||
}}
|
||||
>
|
||||
保存来源
|
||||
</Button>
|
||||
</div>
|
||||
{results.length > 0 && (
|
||||
<div className="flex flex-col gap-2">
|
||||
<p className="text-xs text-muted-foreground">
|
||||
按可用性和响应延迟排序;检测结果不代表下载速度。
|
||||
</p>
|
||||
<ul className="flex max-h-64 flex-col gap-2 overflow-auto">
|
||||
{results.map((result) => (
|
||||
<li
|
||||
key={result.mirror}
|
||||
className="flex items-center gap-2 text-xs"
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="break-all">
|
||||
{result.mirror || 'GitHub 直连'}
|
||||
</p>
|
||||
<p
|
||||
className={
|
||||
result.available
|
||||
? 'text-success'
|
||||
: 'text-destructive'
|
||||
}
|
||||
>
|
||||
{result.available
|
||||
? `可用 · ${result.latencyMs} ms`
|
||||
: result.error || '不可用'}
|
||||
</p>
|
||||
</div>
|
||||
{result.available && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setConfig({
|
||||
...config,
|
||||
mode: result.mirror
|
||||
? 'mirror'
|
||||
: 'direct',
|
||||
mirror: result.mirror,
|
||||
});
|
||||
}}
|
||||
>
|
||||
选用
|
||||
</Button>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="self-start"
|
||||
onClick={() => {
|
||||
setMirrors(
|
||||
results
|
||||
.filter(
|
||||
(result) => result.available && result.mirror,
|
||||
)
|
||||
.map((result) => result.mirror)
|
||||
.join('\n'),
|
||||
);
|
||||
setConfig({ ...config, mode: 'auto' });
|
||||
setResults([]);
|
||||
}}
|
||||
>
|
||||
仅保留可用镜像
|
||||
</Button>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
选用或筛选后,点击「保存来源」生效。
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</fieldset>
|
||||
) : (
|
||||
ready && (
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => setReload((value) => value + 1)}
|
||||
>
|
||||
重试
|
||||
</Button>
|
||||
)
|
||||
)}
|
||||
</SkeletonSwap>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -24,6 +24,9 @@ import type {
|
||||
UiConfig,
|
||||
UpdateInfo,
|
||||
UpdateStatus,
|
||||
UpdateSourceConfig,
|
||||
UpdateSourceProbe,
|
||||
UpdateSourceSaveResult,
|
||||
} from '@/types';
|
||||
import type { PasswordRule } from '@/components/pages/change-password-form';
|
||||
import { normalizeOneBotConfig } from '@/lib/onebot-config';
|
||||
@@ -175,6 +178,11 @@ class HttpApiClient implements ApiClient {
|
||||
check: (force) =>
|
||||
this.getJson<UpdateInfo>(`/api/update/check${force ? '?force=true' : ''}`),
|
||||
status: () => this.getJson<UpdateStatus>('/api/update/status'),
|
||||
sources: () => this.getJson<{ config: UpdateSourceConfig }>('/api/update/sources'),
|
||||
saveSources: (config) =>
|
||||
this.postJson<UpdateSourceSaveResult>('/api/update/sources', config),
|
||||
testSources: (config) =>
|
||||
this.postJson<{ results: UpdateSourceProbe[] }>('/api/update/sources/test', config),
|
||||
download: (version) => this.postJson<UpdateStatus>('/api/update/download', { version }),
|
||||
restart: () => this.postJson<UpdateStatus>('/api/update/restart'),
|
||||
};
|
||||
|
||||
@@ -28,6 +28,9 @@ import type {
|
||||
UiConfig,
|
||||
UpdateInfo,
|
||||
UpdateStatus,
|
||||
UpdateSourceConfig,
|
||||
UpdateSourceProbe,
|
||||
UpdateSourceSaveResult,
|
||||
} from '@/types';
|
||||
import type { PasswordRule } from '@/components/pages/change-password-form';
|
||||
|
||||
@@ -277,6 +280,9 @@ export interface ApiClient {
|
||||
/** Advisory check for a newer stable release. Read-only — never downloads. */
|
||||
check(force?: boolean): Promise<UpdateInfo>;
|
||||
status(): Promise<UpdateStatus>;
|
||||
sources(): Promise<{ config: UpdateSourceConfig }>;
|
||||
saveSources(config: UpdateSourceConfig): Promise<UpdateSourceSaveResult>;
|
||||
testSources(config: UpdateSourceConfig): Promise<{ results: UpdateSourceProbe[] }>;
|
||||
download(version: string): Promise<UpdateStatus>;
|
||||
restart(): Promise<UpdateStatus>;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
export const APP_NAME = 'SnowLuma';
|
||||
// Injected at build time by Vite from the monorepo root package.json.
|
||||
export { APP_VERSION } from '@snowluma/common/version';
|
||||
import type { UpdateSourceConfig } from '@snowluma/common/update-source';
|
||||
export type { UpdateSourceConfig } from '@snowluma/common/update-source';
|
||||
|
||||
export interface UpdateSourceProbe {
|
||||
mirror: string;
|
||||
available: boolean;
|
||||
latencyMs: number;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface UpdateSourceSaveResult {
|
||||
config: UpdateSourceConfig;
|
||||
saved: boolean;
|
||||
applied: boolean;
|
||||
online: boolean;
|
||||
errors: string[];
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type UpdateStatus = { current: string } & (
|
||||
| { phase: 'unavailable' | 'error'; message: string }
|
||||
|
||||
Reference in new issue
Block a user