refactor(endpoint): separate edit and create mode endpoint management (#192)

Optimize custom endpoint management logic to distinguish between edit and create modes:
- Edit mode: endpoints are read/written directly to backend via API
- Create mode: use draftCustomEndpoints to stage, save on submit
- Remove duplicate endpoint loading in useSpeedTestEndpoints
- Add isSaving state and initialCustomUrls tracking
This commit is contained in:
YoVinchen
2025-11-12 11:02:43 +08:00
committed by GitHub
parent 8a05e7bd3d
commit 346f916048
5 changed files with 185 additions and 158 deletions

View File

@@ -25,10 +25,12 @@ interface UseSpeedTestEndpointsProps {
* 收集端点测速弹窗的初始端点列表
*
* 收集来源:
* 1. 编辑模式:从 meta.custom_endpoints 读取已保存的端点(优先)
* 2. 当前选中的 Base URL
* 3. 编辑模式下的初始数据 URL
* 4. 预设中的 endpointCandidates
* 1. 当前选中的 Base URL
* 2. 编辑模式下的初始数据 URL
* 3. 预设中的 endpointCandidates
*
* 注意:已保存的自定义端点通过 getCustomEndpoints API 在 EndpointSpeedTest 组件中加载,
* 不在此处读取,避免重复导入。
*/
export function useSpeedTestEndpoints({
appId,
@@ -43,28 +45,21 @@ export function useSpeedTestEndpoints({
if (appId !== "claude" && appId !== "gemini") return [];
const map = new Map<string, EndpointCandidate>();
// 所有端点标记为 isCustom: true给用户完全的管理自由
const add = (url?: string) => {
// 候选端点标记为 isCustom: false表示来自预设或配置
// 已保存的自定义端点会在 EndpointSpeedTest 组件中通过 API 加载
const add = (url?: string, isCustom = false) => {
if (!url) return;
const sanitized = url.trim().replace(/\/+$/, "");
if (!sanitized || map.has(sanitized)) return;
map.set(sanitized, { url: sanitized, isCustom: true });
map.set(sanitized, { url: sanitized, isCustom });
};
// 1. 编辑模式:从 meta.custom_endpoints 读取已保存的端点(优先)
if (initialData?.meta?.custom_endpoints) {
const customEndpoints = initialData.meta.custom_endpoints;
for (const url of Object.keys(customEndpoints)) {
add(url);
}
}
// 2. 当前 Base URL
// 1. 当前 Base URL
if (baseUrl) {
add(baseUrl);
}
// 3. 编辑模式:初始数据中的 URL
// 2. 编辑模式:初始数据中的 URL
if (initialData && typeof initialData.settingsConfig === "object") {
const configEnv = initialData.settingsConfig as {
env?: { ANTHROPIC_BASE_URL?: string; GOOGLE_GEMINI_BASE_URL?: string };
@@ -78,7 +73,7 @@ export function useSpeedTestEndpoints({
});
}
// 4. 预设中的 endpointCandidates(也允许用户删除)
// 3. 预设中的 endpointCandidates
if (selectedPresetId && selectedPresetId !== "custom") {
const entry = presetEntries.find((item) => item.id === selectedPresetId);
if (entry) {
@@ -112,28 +107,21 @@ export function useSpeedTestEndpoints({
if (appId !== "codex") return [];
const map = new Map<string, EndpointCandidate>();
// 所有端点标记为 isCustom: true给用户完全的管理自由
const add = (url?: string) => {
// 候选端点标记为 isCustom: false表示来自预设或配置
// 已保存的自定义端点会在 EndpointSpeedTest 组件中通过 API 加载
const add = (url?: string, isCustom = false) => {
if (!url) return;
const sanitized = url.trim().replace(/\/+$/, "");
if (!sanitized || map.has(sanitized)) return;
map.set(sanitized, { url: sanitized, isCustom: true });
map.set(sanitized, { url: sanitized, isCustom });
};
// 1. 编辑模式:从 meta.custom_endpoints 读取已保存的端点(优先)
if (initialData?.meta?.custom_endpoints) {
const customEndpoints = initialData.meta.custom_endpoints;
for (const url of Object.keys(customEndpoints)) {
add(url);
}
}
// 2. 当前 Codex Base URL
// 1. 当前 Codex Base URL
if (codexBaseUrl) {
add(codexBaseUrl);
}
// 3. 编辑模式:初始数据中的 URL
// 2. 编辑模式:初始数据中的 URL
const initialCodexConfig = initialData?.settingsConfig as
| {
config?: string;
@@ -146,7 +134,7 @@ export function useSpeedTestEndpoints({
add(match[1]);
}
// 4. 预设中的 endpointCandidates(也允许用户删除)
// 3. 预设中的 endpointCandidates
if (selectedPresetId && selectedPresetId !== "custom") {
const entry = presetEntries.find((item) => item.id === selectedPresetId);
if (entry) {