Repository navigation
Expand file tree
/
Copy pathAddUsageLimitSourceDialog.tsx
More file actions
143 lines (138 loc) · 4.39 KB
/
Copy pathAddUsageLimitSourceDialog.tsx
File metadata and controls
143 lines (138 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { EnvironmentId, SecretReference, UnifiedSettings } from "@t3tools/contracts";
import { useState } from "react";
import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
import { Button } from "../ui/button";
import {
Dialog,
DialogDescription,
DialogFooter,
DialogHeader,
DialogPanel,
DialogPopup,
DialogTitle,
} from "../ui/dialog";
import { Input } from "../ui/input";
import { sourceIdFromUrl } from "./AddUsageLimitSourceDialog.logic";
import { SecretSourceInput, useSecretSources } from "./SecretSourceInput";
import { Label } from "../ui/label";
/** Send the management key once to server secret storage; settings retain only a redaction marker. */
export function AddUsageLimitSourceDialog({
open,
onOpenChange,
environmentId,
environmentLabel,
sources,
}: {
readonly open: boolean;
readonly onOpenChange: (open: boolean) => void;
readonly environmentId: EnvironmentId;
readonly environmentLabel: string;
readonly sources: UnifiedSettings["usageLimitSources"];
}) {
const updateSettings = useUpdateEnvironmentSettings(environmentId);
const [label, setLabel] = useState("");
const [url, setUrl] = useState("");
const secretSources = useSecretSources(environmentId);
const [keyDraft, setKeyDraft] = useState<string | SecretReference>("");
const [keyValid, setKeyValid] = useState(true);
const managementKey = typeof keyDraft === "string" ? keyDraft.trim() : keyDraft;
const trimmedUrl = url.trim();
const canSave = keyValid && trimmedUrl.length > 0 && managementKey !== "";
const reset = () => {
setLabel("");
setUrl("");
setKeyDraft("");
setKeyValid(true);
};
const save = () => {
if (!canSave) return;
const id = sourceIdFromUrl(trimmedUrl, sources);
updateSettings({
usageLimitSources: {
[id]: {
kind: "cliproxy",
...(label.trim() ? { label: label.trim() } : {}),
url: trimmedUrl,
managementKey,
enabled: true,
},
},
});
reset();
onOpenChange(false);
};
return (
<Dialog
open={open}
onOpenChange={(next) => {
if (!next) reset();
onOpenChange(next);
}}
>
<DialogPopup className="max-w-md">
<DialogHeader>
<DialogTitle>Add a CLIProxyAPI hub</DialogTitle>
<DialogDescription>
Show the quota of every account the hub pools, next to the providers on{" "}
{environmentLabel}. The key stays on that server.
</DialogDescription>
</DialogHeader>
<DialogPanel>
<form
className="grid gap-4"
onSubmit={(event) => {
event.preventDefault();
save();
}}
>
<div className="grid gap-1.5">
<Label htmlFor="usage-source-url">Hub URL</Label>
<Input
id="usage-source-url"
placeholder="https://hub.example.ts.net:8318"
value={url}
onChange={(event) => setUrl(event.target.value)}
autoFocus
/>
</div>
<div className="grid gap-1.5">
<Label htmlFor="usage-source-key">Management key</Label>
<SecretSourceInput
id="usage-source-key"
aria-label="Management key"
value={keyDraft}
sources={secretSources}
onChange={(event) => setKeyDraft(event.target.value)}
onSecretChange={(secret) => setKeyDraft(secret ?? "")}
onValidityChange={setKeyValid}
/>
</div>
<div className="grid gap-1.5">
<Label htmlFor="usage-source-label">Label (optional)</Label>
<Input
id="usage-source-label"
placeholder="Defaults to the hub's host name"
value={label}
onChange={(event) => setLabel(event.target.value)}
/>
</div>
</form>
</DialogPanel>
<DialogFooter variant="bare">
<Button
variant="outline"
onClick={() => {
reset();
onOpenChange(false);
}}
>
Cancel
</Button>
<Button onClick={save} disabled={!canSave}>
Add hub
</Button>
</DialogFooter>
</DialogPopup>
</Dialog>
);
}