Open History to see the last commit
1import { UsageLimitSourceId } from "@t3tools/contracts";
2import { useState } from "react";
3
4import { useUpdatePrimarySettings } 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"}`);
35}
36
37/**
38 * Adds a CLIProxyAPI hub as a usage-limit source on the primary environment.
39 * The management key is sent once and kept in the server's secret store;
40 * settings only ever carry a redaction marker for it afterwards.
41 */
42export function AddUsageLimitSourceDialog({
43 open,
44 onOpenChange,
45}: {
46 readonly open: boolean;
47 readonly onOpenChange: (open: boolean) => void;
48}) {
49 const updateSettings = useUpdatePrimarySettings();
50 const [label, setLabel] = useState("");
51 const [url, setUrl] = useState("");
52 const [managementKey, setManagementKey] = useState("");
53 const trimmedUrl = url.trim();
54 const canSave = trimmedUrl.length > 0 && managementKey.trim().length > 0;
55
56 const reset = () => {
57 setLabel("");
58 setUrl("");
59 setManagementKey("");
60 };
61
62 const save = () => {
63 if (!canSave) return;
64 const id = sourceIdFromUrl(trimmedUrl);
65 // The patch names only this entry; the server merges it into its map.
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 this machine.
94 The key stays on the 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 <Input
118 id="usage-source-key"
119 type="password"
120 autoComplete="off"
121 value={managementKey}
122 onChange={(event) => setManagementKey(event.target.value)}
123 />
124 </div>
125 <div className="grid gap-1.5">
126 <Label htmlFor="usage-source-label">Label (optional)</Label>
127 <Input
128 id="usage-source-label"
129 placeholder="Defaults to the hub's host name"
130 value={label}
131 onChange={(event) => setLabel(event.target.value)}
132 />
133 </div>
134 </form>
135 </DialogPanel>
136 <DialogFooter variant="bare">
137 <Button
138 variant="outline"
139 onClick={() => {
140 reset();
141 onOpenChange(false);
142 }}
143 >
144 Cancel
145 </Button>
146 <Button onClick={save} disabled={!canSave}>
147 Add hub
148 </Button>
149 </DialogFooter>
150 </DialogPopup>
151 </Dialog>
152 );
153}