Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ src/paraglide/
target/

.reference
.magi
9 changes: 8 additions & 1 deletion src/features/config/form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe("config/form", () => {
expect(validate({ ...EMPTY_FORM, upstreams: [upstream] }).valid).toBe(true);
});

it("creates new upstream as disabled draft", () => {
const upstream = createEmptyUpstream();
upstream.id = "openai-1";

expect(upstream.enabled).toBe(false);
expect(validate({ ...EMPTY_FORM, upstreams: [upstream] }).valid).toBe(true);
});

it("extracts and merges unknown config keys as extras", () => {
const payload = toPayload(EMPTY_FORM);
const configWithExtras = { ...payload, foo: 1, bar: { nested: true } };
Expand Down Expand Up @@ -82,4 +90,3 @@ describe("config/form", () => {
expect(payload.upstreams[0]?.convert_from_map).toBeUndefined();
});
});

3 changes: 2 additions & 1 deletion src/features/config/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export function createEmptyUpstream(): UpstreamForm {
preferredEndpoint: "",
proxyUrl: "",
priority: "",
enabled: true,
// 新增上游默认作为草稿,避免用户尚未填完必填项时被“无法保存”阻塞。
enabled: false,
modelMappings: [],
convertFromMap: {},
overrides: { header: [] },
Expand Down
21 changes: 21 additions & 0 deletions src/features/config/sections.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";

import {
DEFAULT_CONFIG_SECTION,
getSectionIdFromPathname,
} from "@/features/config/sections";

describe("config/sections", () => {
it("parses section id from config pathname", () => {
expect(getSectionIdFromPathname("/config/upstreams")).toBe("upstreams");
expect(getSectionIdFromPathname("/config/providers")).toBe("providers");
expect(getSectionIdFromPathname("/config/settings/")).toBe("settings");
});

it("falls back to default section for invalid pathname", () => {
expect(getSectionIdFromPathname("/config")).toBe(DEFAULT_CONFIG_SECTION);
expect(getSectionIdFromPathname("/config/unknown")).toBe(DEFAULT_CONFIG_SECTION);
expect(getSectionIdFromPathname("/other")).toBe(DEFAULT_CONFIG_SECTION);
});
});

12 changes: 12 additions & 0 deletions src/features/config/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,15 @@ export function findSection(sectionId: ConfigSectionId) {
export function getSectionRoute(sectionId: ConfigSectionId) {
return getSection(sectionId).route;
}

export function getSectionIdFromPathname(pathname: string) {
const normalizedPathname = pathname.replace(/\/+$/, "") || "/";
if (normalizedPathname === "/config") {
return DEFAULT_CONFIG_SECTION;
}
if (!normalizedPathname.startsWith("/config/")) {
return DEFAULT_CONFIG_SECTION;
}
const section = normalizedPathname.slice("/config/".length);
return toConfigSectionId(section) ?? DEFAULT_CONFIG_SECTION;
}
13 changes: 11 additions & 2 deletions src/routes/config/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Outlet, createFileRoute } from "@tanstack/react-router";
import { createFileRoute, useLocation } from "@tanstack/react-router";

import { ConfigScreen } from "@/features/config/ConfigScreen";
import { getSectionIdFromPathname } from "@/features/config/sections";

function ConfigLayoutRoute() {
const location = useLocation();
const sectionId = getSectionIdFromPathname(location.pathname);
return <ConfigScreen activeSectionId={sectionId} />;
}

export const Route = createFileRoute("/config")({
component: Outlet,
component: ConfigLayoutRoute,
});