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