Open History to see the last commit
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
20/** Hub management follows the selected device and access rules of provider settings. */
21export function UsageProviderSettings({
22 environmentId,
23 environmentLabel,
24 sources,
25 readOnly,
26}: {
27 readonly environmentId: EnvironmentId;
28 readonly environmentLabel: string;
29 readonly sources: UnifiedSettings["usageLimitSources"];
30 readonly readOnly: boolean;
31}) {
32 const updateSettings = useUpdateEnvironmentSettings(environmentId);
33 const [adding, setAdding] = useState(false);
34 const entries = Object.entries(sources);
35
36 return (
37 <>
38 <SettingsSection
39 {...searchableSetting("usage-providers")}
40 headerAction={
41 !readOnly ? (
42 <Button size="xs" variant="outline" onClick={() => setAdding(true)}>
43 <PlusIcon className="size-3" aria-hidden />
44 Add hub
45 </Button>
46 ) : null
47 }
48 >
49 {entries.length === 0 ? (
50 <SettingsRow title="No usage providers configured." />
51 ) : (
52 entries.map(([id, source]) => {
53 const label = source.label?.trim() || source.url;
54 return (
55 <SettingsRow
56 key={id}
57 title={label}
58 description={
59 <span className="break-all">
60 CLI Proxy{source.enabled ? "" : " · Disabled"}
61 {label !== source.url ? ` · ${source.url}` : ""}
62 </span>
63 }
64 control={
65 !readOnly ? (
66 <RemoveUsageProviderButton
67 label={label}
68 onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}
69 />
70 ) : null
71 }
72 />
73 );
74 })
75 )}
76 </SettingsSection>
77 {adding && !readOnly ? (
78 <AddUsageLimitSourceDialog
79 open
80 onOpenChange={setAdding}
81 environmentId={environmentId}
82 environmentLabel={environmentLabel}
83 />
84 ) : null}
85 </>
86 );
87}
88
89/** Removing a hub deletes its stored management key, so it requires confirmation. */
90function RemoveUsageProviderButton({
91 label,
92 onConfirm,
93}: {
94 readonly label: string;
95 readonly onConfirm: () => void;
96}) {
97 const [open, setOpen] = useState(false);
98 return (
99 <>
100 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
101 Remove
102 </Button>
103 <AlertDialog open={open} onOpenChange={setOpen}>
104 <AlertDialogPopup>
105 <AlertDialogHeader>
106 <AlertDialogTitle>Remove {label}?</AlertDialogTitle>
107 <AlertDialogDescription>
108 The hub's management key is deleted from this server. Its accounts leave the Limits
109 view; the hub itself is untouched. Add it again with the URL and key to bring them
110 back.
111 </AlertDialogDescription>
112 </AlertDialogHeader>
113 <AlertDialogFooter>
114 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
115 <Button
116 variant="destructive"
117 onClick={() => {
118 setOpen(false);
119 onConfirm();
120 }}
121 >
122 Remove hub
123 </Button>
124 </AlertDialogFooter>
125 </AlertDialogPopup>
126 </AlertDialog>
127 </>
128 );
129}