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