Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AddUsageLimitSourceDialog.tsx

157 lines4.6 KB

1import { type EnvironmentId, UsageLimitSourceId } from "@t3tools/contracts";
2import { useState } from "react";
3​
4import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
5import { Button } from "../ui/button";
6import {
7 Dialog,
8 DialogDescription,
9 DialogFooter,
10 DialogHeader,
11 DialogPanel,
12 DialogPopup,
13 DialogTitle,
14} from "../ui/dialog";
15import { Input } from "../ui/input";
16import { Label } from "../ui/label";
17​
18/**
19 * Stable per hub and readable in settings.json. Dots and dashes in the host
20 * are kept so `foo-bar.com` and `foo.bar.com` do not collide; anything else
21 * (a port's colon, a path) is folded to a dash.
22 */
23function sourceIdFromUrl(url: string): UsageLimitSourceId {
24 let host = url;
25 try {
26 host = new URL(url).host;
27 } catch {
28 // Keep the raw text; the server reports the bad URL on its row.
29 }
30 const slug = host
31 .toLowerCase()
32 .replace(/[^a-z0-9.-]+/g, "-")
33 .replace(/^-+|-+$/g, "");
34 return UsageLimitSourceId.make(`cliproxy-${slug || "hub"}`);
36​
37/**
38 * Adds a CLIProxyAPI hub from provider settings on one environment. The
39 * management key is sent once and kept in that server's secret store;
40 * settings only ever carry a redaction marker for it afterwards.
41 */
42export function AddUsageLimitSourceDialog({
43 open,
44 onOpenChange,
45 environmentId,
46 environmentLabel,
47}: {
48 readonly open: boolean;
49 readonly onOpenChange: (open: boolean) => void;
50 readonly environmentId: EnvironmentId;
51 readonly environmentLabel: string;
52}) {
53 const updateSettings = useUpdateEnvironmentSettings(environmentId);
54 const [label, setLabel] = useState("");
55 const [url, setUrl] = useState("");
56 const [managementKey, setManagementKey] = useState("");
57 const trimmedUrl = url.trim();
58 const canSave = trimmedUrl.length > 0 && managementKey.trim().length > 0;
59​
60 const reset = () => {
61 setLabel("");
62 setUrl("");
63 setManagementKey("");
64 };
65​
66 const save = () => {
67 if (!canSave) return;
68 const id = sourceIdFromUrl(trimmedUrl);
69 // The patch names only this entry; the server merges it into its map.
70 updateSettings({
71 usageLimitSources: {
72 [id]: {
73 kind: "cliproxy",
74 ...(label.trim() ? { label: label.trim() } : {}),
75 url: trimmedUrl,
76 managementKey: managementKey.trim(),
77 enabled: true,
78 },
79 },
80 });
81 reset();
82 onOpenChange(false);
83 };
84​
85 return (
86 <Dialog
87 open={open}
88 onOpenChange={(next) => {
89 if (!next) reset();
90 onOpenChange(next);
91 }}
92 >
93 <DialogPopup className="max-w-md">
94 <DialogHeader>
95 <DialogTitle>Add a CLIProxyAPI hub</DialogTitle>
96 <DialogDescription>
97 Show the quota of every account the hub pools, next to the providers on{" "}
98 {environmentLabel}. The key stays on that server.
99 </DialogDescription>
100 </DialogHeader>
101 <DialogPanel>
102 <form
103 className="grid gap-4"
104 onSubmit={(event) => {
105 event.preventDefault();
106 save();
107 }}
108 >
109 <div className="grid gap-1.5">
110 <Label htmlFor="usage-source-url">Hub URL</Label>
111 <Input
112 id="usage-source-url"
113 placeholder="https://hub.example.ts.net:8318"
114 value={url}
115 onChange={(event) => setUrl(event.target.value)}
116 autoFocus
117 />
118 </div>
119 <div className="grid gap-1.5">
120 <Label htmlFor="usage-source-key">Management key</Label>
121 <Input
122 id="usage-source-key"
123 type="password"
124 autoComplete="off"
125 value={managementKey}
126 onChange={(event) => setManagementKey(event.target.value)}
127 />
128 </div>
129 <div className="grid gap-1.5">
130 <Label htmlFor="usage-source-label">Label (optional)</Label>
131 <Input
132 id="usage-source-label"
133 placeholder="Defaults to the hub's host name"
134 value={label}
135 onChange={(event) => setLabel(event.target.value)}
136 />
137 </div>
138 </form>
139 </DialogPanel>
140 <DialogFooter variant="bare">
141 <Button
142 variant="outline"
143 onClick={() => {
144 reset();
145 onOpenChange(false);
146 }}
147 >
148 Cancel
149 </Button>
150 <Button onClick={save} disabled={!canSave}>
151 Add hub
152 </Button>
153 </DialogFooter>
154 </DialogPopup>
155 </Dialog>
156 );