Open History to see the last commit
1import type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts";
2import { useAtomValue } from "@effect/atom-react";
3import { PlusIcon } from "lucide-react";
4import { useState } from "react";
5
6import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
7import { serverEnvironment } from "../../state/server";
8import { useAtomCommand } from "../../state/use-atom-command";
9import {
10 AlertDialog,
11 AlertDialogClose,
12 AlertDialogDescription,
13 AlertDialogFooter,
14 AlertDialogHeader,
15 AlertDialogPopup,
16 AlertDialogTitle,
17} from "../ui/alert-dialog";
18import { Button } from "../ui/button";
19import { Switch } from "../ui/switch";
20import { AddUsageLimitSourceDialog } from "./AddUsageLimitSourceDialog";
21import { searchableSetting } from "./settingsSearch";
22import { SettingsRow, SettingsSection } from "./settingsLayout";
23
24export function UsageProviderSettings({
25 environmentId,
26 environmentLabel,
27 sources,
28 cursorKeychainUsageEnabled,
29 readOnly,
30}: {
31 readonly environmentId: EnvironmentId;
32 readonly environmentLabel: string;
33 readonly sources: UnifiedSettings["usageLimitSources"];
34 readonly cursorKeychainUsageEnabled: boolean;
35 readonly readOnly: boolean;
36}) {
37 const updateSettings = useUpdateEnvironmentSettings(environmentId);
38 const updateCursorSettings = useAtomCommand(serverEnvironment.updateSettings, {
39 label: "update Cursor account usage",
40 });
41 const refreshProviders = useAtomCommand(serverEnvironment.refreshProviders, {
42 reportFailure: false,
43 });
44 const platform = useAtomValue(serverEnvironment.configValueAtom(environmentId))?.environment
45 .platform;
46 const [adding, setAdding] = useState(false);
47 const [updatingCursor, setUpdatingCursor] = useState(false);
48 const entries = Object.entries(sources);
49
50 const setCursorUsageEnabled = async (enabled: boolean) => {
51 setUpdatingCursor(true);
52 try {
53 const result = await updateCursorSettings({
54 environmentId,
55 input: { patch: { cursorKeychainUsageEnabled: enabled } },
56 });
57 if (result._tag === "Success") {
58 await refreshProviders({ environmentId, input: {} });
59 }
60 } finally {
61 setUpdatingCursor(false);
62 }
63 };
64
65 return (
66 <>
67 <SettingsSection
68 {...searchableSetting("usage-providers")}
69 headerAction={
70 !readOnly ? (
71 <Button size="xs" variant="outline" onClick={() => setAdding(true)}>
72 <PlusIcon className="size-3" aria-hidden />
73 Add hub
74 </Button>
75 ) : null
76 }
77 >
78 {platform?.os === "darwin" ? (
79 <SettingsRow
80 id="cursor-keychain-usage"
81 title="Cursor account usage"
82 description="Read your existing Cursor CLI login from macOS Keychain to show account history and monthly limits. macOS may ask you to allow access."
83 control={
84 <Switch
85 aria-label="Cursor account usage"
86 checked={cursorKeychainUsageEnabled}
87 disabled={readOnly || updatingCursor}
88 onCheckedChange={(enabled) => void setCursorUsageEnabled(enabled)}
89 />
90 }
91 />
92 ) : null}
93 {entries.length === 0 ? (
94 <SettingsRow title="No hubs configured." />
95 ) : (
96 entries.map(([id, source]) => {
97 const label = source.label?.trim() || source.url;
98 return (
99 <SettingsRow
100 key={id}
101 title={label}
102 description={
103 <span className="break-all">
104 CLI Proxy{source.enabled ? "" : " · Disabled"}
105 {label !== source.url ? ` · ${source.url}` : ""}
106 </span>
107 }
108 control={
109 !readOnly ? (
110 <RemoveUsageProviderButton
111 label={label}
112 onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}
113 />
114 ) : null
115 }
116 />
117 );
118 })
119 )}
120 </SettingsSection>
121 {adding && !readOnly ? (
122 <AddUsageLimitSourceDialog
123 open
124 onOpenChange={setAdding}
125 environmentId={environmentId}
126 environmentLabel={environmentLabel}
127 />
128 ) : null}
129 </>
130 );
131}
132
133function RemoveUsageProviderButton({
134 label,
135 onConfirm,
136}: {
137 readonly label: string;
138 readonly onConfirm: () => void;
139}) {
140 const [open, setOpen] = useState(false);
141 return (
142 <>
143 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
144 Remove
145 </Button>
146 <AlertDialog open={open} onOpenChange={setOpen}>
147 <AlertDialogPopup>
148 <AlertDialogHeader>
149 <AlertDialogTitle>Remove {label}?</AlertDialogTitle>
150 <AlertDialogDescription>
151 The hub's management key is deleted from this server. Its accounts leave the Limits
152 view; the hub itself is untouched. Add it again with the URL and key to bring them
153 back.
154 </AlertDialogDescription>
155 </AlertDialogHeader>
156 <AlertDialogFooter>
157 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
158 <Button
159 variant="destructive"
160 onClick={() => {
161 setOpen(false);
162 onConfirm();
163 }}
164 >
165 Remove hub
166 </Button>
167 </AlertDialogFooter>
168 </AlertDialogPopup>
169 </AlertDialog>
170 </>
171 );
172}