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

UsageProviderSettings.tsx

147 lines4.5 KB

1import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
2import { T, useGT, Var } from "gt-react";
3import { PlusIcon } from "lucide-react";
4import { useState } from "react";
5​
6import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
7import {
8 AlertDialog,
9 AlertDialogClose,
10 AlertDialogDescription,
11 AlertDialogFooter,
12 AlertDialogHeader,
13 AlertDialogPopup,
14 AlertDialogTitle,
15} from "../ui/alert-dialog";
16import { Button } from "../ui/button";
17import { AddUsageLimitSourceDialog } from "./AddUsageLimitSourceDialog";
18import { useSearchableSetting } from "./settingsSearch";
19import { SettingsRow, SettingsSection } from "./settingsLayout";
20​
21const CLI_PROXY_NAME = "CLI Proxy";
22​
23export function UsageProviderSettings({
24 environmentId,
25 environmentLabel,
26 sources,
27 readOnly,
28}: {
29 readonly environmentId: EnvironmentId;
30 readonly environmentLabel: string;
31 readonly sources: UnifiedSettings["usageLimitSources"];
32 readonly readOnly: boolean;
33}) {
34 const gt = useGT();
35 const searchableSetting = useSearchableSetting();
36 const updateSettings = useUpdateEnvironmentSettings(environmentId);
37 const [adding, setAdding] = useState<"cliproxy" | "http" | null>(null);
38 const entries = Object.entries(sources);
39​
40 return (
41 <>
42 <SettingsSection
43 {...searchableSetting("usage-providers")}
44 headerAction={
45 !readOnly ? (
46 <div className="flex gap-2">
47 <Button size="xs" variant="outline" onClick={() => setAdding("cliproxy")}>
48 <PlusIcon className="size-3" aria-hidden />
49 <T>Add hub</T>
50 </Button>
51 <Button size="xs" variant="outline" onClick={() => setAdding("http")}>
52 <PlusIcon className="size-3" aria-hidden />
53 <T>Add HTTP source</T>
54 </Button>
55 </div>
56 ) : null
57 }
58 >
59 {entries.length === 0 ? (
60 <SettingsRow title={gt("No usage sources configured.")} />
61 ) : (
62 entries.map(([id, source]) => {
63 const kindLabel = source.kind === "http" ? gt("HTTP quota source") : CLI_PROXY_NAME;
64 const label = source.label?.trim() || source.url;
65 return (
66 <SettingsRow
67 key={id}
68 title={label}
69 description={
70 <span className="break-all">
71 {source.enabled ? kindLabel : gt("{name} · Disabled", { name: kindLabel })}
72 {label !== source.url ? ` · ${source.url}` : ""}
73 </span>
74 }
75 control={
76 !readOnly ? (
77 <RemoveUsageProviderButton
78 label={label}
79 onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}
80 />
81 ) : null
82 }
83 />
84 );
85 })
86 )}
87 </SettingsSection>
88 {adding && !readOnly ? (
89 <AddUsageLimitSourceDialog
90 open
91 kind={adding}
92 onOpenChange={(open) => {
93 if (!open) setAdding(null);
94 }}
95 environmentId={environmentId}
96 environmentLabel={environmentLabel}
97 sources={sources}
98 />
99 ) : null}
100 </>
101 );
103​
104function RemoveUsageProviderButton({
105 label,
106 onConfirm,
107}: {
108 readonly label: string;
109 readonly onConfirm: () => void;
110}) {
111 const [open, setOpen] = useState(false);
112 return (
113 <>
114 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
115 <T>Remove</T>
116 </Button>
117 <AlertDialog open={open} onOpenChange={setOpen}>
118 <AlertDialogPopup>
119 <AlertDialogHeader>
120 <AlertDialogTitle>
121 <T>
122 Remove <Var>{label}</Var>?
123 </T>
124 </AlertDialogTitle>
125 <AlertDialogDescription>
126 <T>The saved connection and its credentials are removed from this environment.</T>
127 </AlertDialogDescription>
128 </AlertDialogHeader>
129 <AlertDialogFooter>
130 <AlertDialogClose render={<Button variant="outline" />}>
131 <T>Cancel</T>
132 </AlertDialogClose>
133 <Button
134 variant="destructive"
135 onClick={() => {
136 setOpen(false);
137 onConfirm();
138 }}
139 >
140 <T>Remove</T>
141 </Button>
142 </AlertDialogFooter>
143 </AlertDialogPopup>
144 </AlertDialog>
145 </>
146 );