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

UsageProviderSettings.tsx

128 lines3.8 KB

1import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
2import { PlusIcon } from "lucide-react";
3import { useState } from "react";
4​
5import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
6import {
7 AlertDialog,
8 AlertDialogClose,
9 AlertDialogDescription,
10 AlertDialogFooter,
11 AlertDialogHeader,
12 AlertDialogPopup,
13 AlertDialogTitle,
14} from "../ui/alert-dialog";
15import { Button } from "../ui/button";
16import { AddUsageLimitSourceDialog } from "./AddUsageLimitSourceDialog";
17import { searchableSetting } from "./settingsSearch";
18import { SettingsRow, SettingsSection } from "./settingsLayout";
19​
20export function UsageProviderSettings({
21 environmentId,
22 environmentLabel,
23 sources,
24 readOnly,
25}: {
26 readonly environmentId: EnvironmentId;
27 readonly environmentLabel: string;
28 readonly sources: UnifiedSettings["usageLimitSources"];
29 readonly readOnly: boolean;
30}) {
31 const updateSettings = useUpdateEnvironmentSettings(environmentId);
32 const [adding, setAdding] = useState(false);
33 const entries = Object.entries(sources);
34​
35 return (
36 <>
37 <SettingsSection
38 {...searchableSetting("usage-providers")}
39 headerAction={
40 !readOnly ? (
41 <Button size="xs" variant="outline" onClick={() => setAdding(true)}>
42 <PlusIcon className="size-3" aria-hidden />
43 Add hub
44 </Button>
45 ) : null
46 }
47 >
48 {entries.length === 0 ? (
49 <SettingsRow title="No hubs configured." />
50 ) : (
51 entries.map(([id, source]) => {
52 const label = source.label?.trim() || source.url;
53 return (
54 <SettingsRow
55 key={id}
56 title={label}
57 description={
58 <span className="break-all">
59 CLI Proxy{source.enabled ? "" : " · Disabled"}
60 {label !== source.url ? ` · ${source.url}` : ""}
61 </span>
62 }
63 control={
64 !readOnly ? (
65 <RemoveUsageProviderButton
66 label={label}
67 onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}
68 />
69 ) : null
70 }
71 />
72 );
73 })
74 )}
75 </SettingsSection>
76 {adding && !readOnly ? (
77 <AddUsageLimitSourceDialog
78 open
79 onOpenChange={setAdding}
80 environmentId={environmentId}
81 environmentLabel={environmentLabel}
82 sources={sources}
83 />
84 ) : null}
85 </>
86 );
88​
89function RemoveUsageProviderButton({
90 label,
91 onConfirm,
92}: {
93 readonly label: string;
94 readonly onConfirm: () => void;
95}) {
96 const [open, setOpen] = useState(false);
97 return (
98 <>
99 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
100 Remove
101 </Button>
102 <AlertDialog open={open} onOpenChange={setOpen}>
103 <AlertDialogPopup>
104 <AlertDialogHeader>
105 <AlertDialogTitle>Remove {label}?</AlertDialogTitle>
106 <AlertDialogDescription>
107 The hub's management key is deleted from this server. Its accounts leave the Limits
108 view; the hub itself is untouched. Add it again with the URL and key to bring them
109 back.
110 </AlertDialogDescription>
111 </AlertDialogHeader>
112 <AlertDialogFooter>
113 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
114 <Button
115 variant="destructive"
116 onClick={() => {
117 setOpen(false);
118 onConfirm();
119 }}
120 >
121 Remove hub
122 </Button>
123 </AlertDialogFooter>
124 </AlertDialogPopup>
125 </AlertDialog>
126 </>
127 );