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