Skip to content
TypeScript

fix(web): keep usage hubs that share a host (#23767)

Victor Vogel06ded0b60049
diff --git a/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.test.ts b/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.test.ts
new file mode 100644
index 00000000000..33192bbaab7
--- /dev/null
+++ b/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.test.ts
@@ -0,0 +1,61 @@
+import { DEFAULT_SERVER_SETTINGS, type ServerSettings } from "@t3tools/contracts";
+import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
+import { describe, expect, it } from "vite-plus/test";
+
+import { sourceIdFromUrl } from "./AddUsageLimitSourceDialog.logic";
+
+describe("adding usage limit sources", () => {
+ it.each([
+ ["http://proxy.test:8318/hub-a", "http://proxy.test:8318/hub-b"],
+ ["localhost:8318", "localhost:9318"],
+ ])("retains saved configurations for %s and %s", (firstUrl, secondUrl) => {
+ let settings: ServerSettings = DEFAULT_SERVER_SETTINGS;
+ const sources = [
+ [firstUrl, "First hub", "first-key"],
+ [secondUrl, "Second hub", "second-key"],
+ [`${secondUrl}/extra`, "Third hub", "third-key"],
+ ] as const;
+ for (const [url, label, managementKey] of sources) {
+ const id = sourceIdFromUrl(url, settings.usageLimitSources);
+ settings = applyServerSettingsPatch(settings, {
+ usageLimitSources: {
+ [id]: { kind: "cliproxy", url, label, managementKey, enabled: true },
+ },
+ });
+ }
+ expect(Object.values(settings.usageLimitSources)).toEqual(
+ sources.map(([url, label, managementKey]) => ({
+ kind: "cliproxy",
+ url,
+ label,
+ managementKey,
+ enabled: true,
+ })),
+ );
+ });
+
+ it("updates a saved hub when its URL is added again", () => {
+ let settings: ServerSettings = DEFAULT_SERVER_SETTINGS;
+ for (const managementKey of ["old-key", "new-key"]) {
+ const url = "http://proxy.test:8318/hub-a";
+ settings = applyServerSettingsPatch(settings, {
+ usageLimitSources: {
+ [sourceIdFromUrl(url, settings.usageLimitSources)]: {
+ kind: "cliproxy",
+ url,
+ managementKey,
+ enabled: true,
+ },
+ },
+ });
+ }
+ expect(Object.values(settings.usageLimitSources)).toEqual([
+ {
+ kind: "cliproxy",
+ url: "http://proxy.test:8318/hub-a",
+ managementKey: "new-key",
+ enabled: true,
+ },
+ ]);
+ });
+});
diff --git a/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.ts b/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.ts
new file mode 100644
index 00000000000..67ccabdefef
--- /dev/null
+++ b/apps/web/src/components/settings/AddUsageLimitSourceDialog.logic.ts
@@ -0,0 +1,24 @@
+import { type UnifiedSettings, UsageLimitSourceId } from "@t3tools/contracts";
+
+export function sourceIdFromUrl(
+ url: string,
+ sources: UnifiedSettings["usageLimitSources"],
+): UsageLimitSourceId {
+ const saved = Object.entries(sources).find(([, source]) => source.url === url);
+ if (saved) return UsageLimitSourceId.make(saved[0]);
+ let host = url;
+ try {
+ host = new URL(url).host;
+ } catch {}
+ const slug = host
+ .toLowerCase()
+ .replace(/[^a-z0-9.-]+/g, "-")
+ .replace(/^-+|-+$/g, "");
+ // Settings merge by ID, so adding a different hub must not replace an existing entry.
+ const base = `cliproxy-${slug || "hub"}`;
+ let id = base;
+ for (let suffix = 2; id in sources; suffix += 1) {
+ id = `${base}-${suffix}`;
+ }
+ return UsageLimitSourceId.make(id);
+}
diff --git a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
index cc150016d73..e3abd8b9510 100644
--- a/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
+++ b/apps/web/src/components/settings/AddUsageLimitSourceDialog.tsx
@@ -1,4 +1,4 @@
-import { type EnvironmentId, UsageLimitSourceId } from "@t3tools/contracts";
+import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
import { useState } from "react";
import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
@@ -13,32 +13,23 @@ import {
DialogTitle,
} from "../ui/dialog";
import { Input } from "../ui/input";
+import { sourceIdFromUrl } from "./AddUsageLimitSourceDialog.logic";
import { SecretInput } from "./SecretInput";
import { Label } from "../ui/label";
-function sourceIdFromUrl(url: string): UsageLimitSourceId {
- let host = url;
- try {
- host = new URL(url).host;
- } catch {}
- const slug = host
- .toLowerCase()
- .replace(/[^a-z0-9.-]+/g, "-")
- .replace(/^-+|-+$/g, "");
- return UsageLimitSourceId.make(`cliproxy-${slug || "hub"}`);
-}
-
/** Send the management key once to server secret storage; settings retain only a redaction marker. */
export function AddUsageLimitSourceDialog({
open,
onOpenChange,
environmentId,
environmentLabel,
+ sources,
}: {
readonly open: boolean;
readonly onOpenChange: (open: boolean) => void;
readonly environmentId: EnvironmentId;
readonly environmentLabel: string;
+ readonly sources: UnifiedSettings["usageLimitSources"];
}) {
const updateSettings = useUpdateEnvironmentSettings(environmentId);
const [label, setLabel] = useState("");
@@ -55,7 +46,7 @@ export function AddUsageLimitSourceDialog({
const save = () => {
if (!canSave) return;
- const id = sourceIdFromUrl(trimmedUrl);
+ const id = sourceIdFromUrl(trimmedUrl, sources);
updateSettings({
usageLimitSources: {
[id]: {
diff --git a/apps/web/src/components/settings/UsageProviderSettings.tsx b/apps/web/src/components/settings/UsageProviderSettings.tsx
index bab7ddd9c52..f238850ae04 100644
--- a/apps/web/src/components/settings/UsageProviderSettings.tsx
+++ b/apps/web/src/components/settings/UsageProviderSettings.tsx
@@ -121,6 +121,7 @@ export function UsageProviderSettings({
onOpenChange={setAdding}
environmentId={environmentId}
environmentLabel={environmentLabel}
+ sources={sources}
/>
) : null}
</>