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

AddUsageLimitSourceDialog.tsx

144 lines4.1 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 { SecretInput } from "./SecretInput";
17import { Label } from "../ui/label";
18​
19function sourceIdFromUrl(url: string): UsageLimitSourceId {
20 let host = url;
21 try {
22 host = new URL(url).host;
23 } catch {}
24 const slug = host
25 .toLowerCase()
26 .replace(/[^a-z0-9.-]+/g, "-")
27 .replace(/^-+|-+$/g, "");
28 return UsageLimitSourceId.make(`cliproxy-${slug || "hub"}`);
30​
31/** Send the management key once to server secret storage; settings retain only a redaction marker. */
32export function AddUsageLimitSourceDialog({
33 open,
34 onOpenChange,
35 environmentId,
36 environmentLabel,
37}: {
38 readonly open: boolean;
39 readonly onOpenChange: (open: boolean) => void;
40 readonly environmentId: EnvironmentId;
41 readonly environmentLabel: string;
42}) {
43 const updateSettings = useUpdateEnvironmentSettings(environmentId);
44 const [label, setLabel] = useState("");
45 const [url, setUrl] = useState("");
46 const [managementKey, setManagementKey] = useState("");
47 const trimmedUrl = url.trim();
48 const canSave = trimmedUrl.length > 0 && managementKey.trim().length > 0;
49​
50 const reset = () => {
51 setLabel("");
52 setUrl("");
53 setManagementKey("");
54 };
55​
56 const save = () => {
57 if (!canSave) return;
58 const id = sourceIdFromUrl(trimmedUrl);
59 updateSettings({
60 usageLimitSources: {
61 [id]: {
62 kind: "cliproxy",
63 ...(label.trim() ? { label: label.trim() } : {}),
64 url: trimmedUrl,
65 managementKey: managementKey.trim(),
66 enabled: true,
67 },
68 },
69 });
70 reset();
71 onOpenChange(false);
72 };
73​
74 return (
75 <Dialog
76 open={open}
77 onOpenChange={(next) => {
78 if (!next) reset();
79 onOpenChange(next);
80 }}
81 >
82 <DialogPopup className="max-w-md">
83 <DialogHeader>
84 <DialogTitle>Add a CLIProxyAPI hub</DialogTitle>
85 <DialogDescription>
86 Show the quota of every account the hub pools, next to the providers on{" "}
87 {environmentLabel}. The key stays on that server.
88 </DialogDescription>
89 </DialogHeader>
90 <DialogPanel>
91 <form
92 className="grid gap-4"
93 onSubmit={(event) => {
94 event.preventDefault();
95 save();
96 }}
97 >
98 <div className="grid gap-1.5">
99 <Label htmlFor="usage-source-url">Hub URL</Label>
100 <Input
101 id="usage-source-url"
102 placeholder="https://hub.example.ts.net:8318"
103 value={url}
104 onChange={(event) => setUrl(event.target.value)}
105 autoFocus
106 />
107 </div>
108 <div className="grid gap-1.5">
109 <Label htmlFor="usage-source-key">Management key</Label>
110 <SecretInput
111 id="usage-source-key"
112 value={managementKey}
113 onChange={(event) => setManagementKey(event.target.value)}
114 />
115 </div>
116 <div className="grid gap-1.5">
117 <Label htmlFor="usage-source-label">Label (optional)</Label>
118 <Input
119 id="usage-source-label"
120 placeholder="Defaults to the hub's host name"
121 value={label}
122 onChange={(event) => setLabel(event.target.value)}
123 />
124 </div>
125 </form>
126 </DialogPanel>
127 <DialogFooter variant="bare">
128 <Button
129 variant="outline"
130 onClick={() => {
131 reset();
132 onOpenChange(false);
133 }}
134 >
135 Cancel
136 </Button>
137 <Button onClick={save} disabled={!canSave}>
138 Add hub
139 </Button>
140 </DialogFooter>
141 </DialogPopup>
142 </Dialog>
143 );