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

AddUsageLimitSourceDialog.tsx

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