Open History to see the last commit
1import type { EnvironmentId, SecretReference, UnifiedSettings } from "@t3tools/contracts";
2import { T, useGT, Var } from "gt-react";
3import { useId, useState } from "react";
4
5import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
6import { Button } from "../ui/button";
7import {
8 Dialog,
9 DialogDescription,
10 DialogFooter,
11 DialogHeader,
12 DialogPanel,
13 DialogPopup,
14 DialogTitle,
15} from "../ui/dialog";
16import { Input } from "../ui/input";
17import { sourceIdFromUrl } from "./AddUsageLimitSourceDialog.logic";
18import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
19import { Label } from "../ui/label";
20
21/** Send the management key once to server secret storage; settings retain only a redaction marker. */
22export function AddUsageLimitSourceDialog({
23 open,
24 onOpenChange,
25 environmentId,
26 environmentLabel,
27 sources,
28 kind = "cliproxy",
29}: {
30 readonly open: boolean;
31 readonly onOpenChange: (open: boolean) => void;
32 readonly environmentId: EnvironmentId;
33 readonly environmentLabel: string;
34 readonly kind?: "cliproxy" | "http";
35 readonly sources: UnifiedSettings["usageLimitSources"];
36}) {
37 const gt = useGT();
38 const formId = useId();
39 const updateSettings = useUpdateEnvironmentSettings(environmentId);
40 const [label, setLabel] = useState("");
41 const [url, setUrl] = useState("");
42 const secretSources = useSecretSources(environmentId);
43 const [keyDraft, setKeyDraft] = useState<string | SecretReference>("");
44 const [keyValid, setKeyValid] = useState(true);
45 const managementKey = typeof keyDraft === "string" ? keyDraft.trim() : keyDraft;
46 const trimmedUrl = url.trim();
47 const http = kind === "http";
48 const endpoint = URL.parse(trimmedUrl);
49 const canSave =
50 keyValid &&
51 (http
52 ? endpoint !== null &&
53 /^https?:$/.test(endpoint.protocol) &&
54 !endpoint.username &&
55 !endpoint.password
56 : trimmedUrl.length > 0 && managementKey !== "");
57
58 const reset = () => {
59 setLabel("");
60 setUrl("");
61 setKeyDraft("");
62 setKeyValid(true);
63 };
64
65 const save = () => {
66 if (!canSave) return;
67 const id = sourceIdFromUrl(trimmedUrl, sources, kind);
68 updateSettings({
69 usageLimitSources: {
70 [id]: {
71 kind,
72 ...(label.trim() ? { label: label.trim() } : {}),
73 url: trimmedUrl,
74 managementKey,
75 enabled: true,
76 },
77 },
78 });
79 reset();
80 onOpenChange(false);
81 };
82
83 return (
84 <Dialog
85 open={open}
86 onOpenChange={(next) => {
87 if (!next) reset();
88 onOpenChange(next);
89 }}
90 >
91 <DialogPopup className="max-w-md">
92 <DialogHeader>
93 <DialogTitle>
94 {http ? <T>Add an HTTP quota source</T> : <T>Add a CLIProxyAPI hub</T>}
95 </DialogTitle>
96 <DialogDescription>
97 {http ? (
98 <T>
99 Read quotas from an endpoint on <Var>{environmentLabel}</Var>.
100 </T>
101 ) : (
102 <T>
103 Show the quota of every account the hub pools, next to the providers on{" "}
104 <Var>{environmentLabel}</Var>. The key stays on that server.
105 </T>
106 )}
107 </DialogDescription>
108 </DialogHeader>
109 <DialogPanel>
110 <form
111 id={formId}
112 className="grid gap-4"
113 onSubmit={(event) => {
114 event.preventDefault();
115 save();
116 }}
117 >
118 <div className="grid gap-1.5">
119 <Label htmlFor="usage-source-url">
120 {http ? <T>Endpoint URL</T> : <T>Hub URL</T>}
121 </Label>
122 <Input
123 id="usage-source-url"
124 placeholder="https://hub.example.ts.net:8318"
125 value={url}
126 onChange={(event) => setUrl(event.target.value)}
127 autoFocus
128 />
129 </div>
130 <div className="grid gap-1.5">
131 <Label htmlFor="usage-source-key">
132 {http ? <T>Bearer token (optional)</T> : <T>Management key</T>}
133 </Label>
134 <SecretSourceInput
135 id="usage-source-key"
136 aria-label={http ? gt("Bearer token (optional)") : gt("Management key")}
137 value={keyDraft}
138 sources={secretSources}
139 environmentId={environmentId}
140 onChange={(event) => setKeyDraft(event.target.value)}
141 onSecretChange={(secret) => setKeyDraft(secret ?? "")}
142 onValidityChange={setKeyValid}
143 />
144 </div>
145 <div className="grid gap-1.5">
146 <Label htmlFor="usage-source-label">
147 <T>Label (optional)</T>
148 </Label>
149 <Input
150 id="usage-source-label"
151 placeholder={gt("Defaults to the hub's host name")}
152 value={label}
153 onChange={(event) => setLabel(event.target.value)}
154 />
155 </div>
156 </form>
157 </DialogPanel>
158 <DialogFooter variant="bare">
159 <Button
160 variant="outline"
161 onClick={() => {
162 reset();
163 onOpenChange(false);
164 }}
165 >
166 <T>Cancel</T>
167 </Button>
168 <Button type="submit" form={formId} disabled={!canSave}>
169 {http ? <T>Add source</T> : <T>Add hub</T>}
170 </Button>
171 </DialogFooter>
172 </DialogPopup>
173 </Dialog>
174 );
175}