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

UsageProviderSettings.tsx

170 lines5.5 KB

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 const result = await updateCursorSettings({
53 environmentId,
54 input: { patch: { cursorKeychainUsageEnabled: enabled } },
55 });
56 if (result._tag === "Success") {
57 await refreshProviders({ environmentId, input: {} });
58 }
59 setUpdatingCursor(false);
60 };
61​
62 return (
63 <>
64 <SettingsSection
65 {...searchableSetting("usage-providers")}
66 headerAction={
67 !readOnly ? (
68 <Button size="xs" variant="outline" onClick={() => setAdding(true)}>
69 <PlusIcon className="size-3" aria-hidden />
70 Add hub
71 </Button>
72 ) : null
73 }
74 >
75 {platform?.os === "darwin" ? (
76 <SettingsRow
77 id="cursor-keychain-usage"
78 title="Cursor account usage"
79 description="Read your existing Cursor CLI login from macOS Keychain to show account history and monthly limits. macOS may ask you to allow access."
80 control={
81 <Switch
82 aria-label="Cursor account usage"
83 checked={cursorKeychainUsageEnabled}
84 disabled={readOnly || updatingCursor}
85 onCheckedChange={(enabled) => void setCursorUsageEnabled(enabled)}
86 />
87 }
88 />
89 ) : null}
90 {entries.length === 0 ? (
91 <SettingsRow title="No hubs configured." />
92 ) : (
93 entries.map(([id, source]) => {
94 const label = source.label?.trim() || source.url;
95 return (
96 <SettingsRow
97 key={id}
98 title={label}
99 description={
100 <span className="break-all">
101 CLI Proxy{source.enabled ? "" : " · Disabled"}
102 {label !== source.url ? ` · ${source.url}` : ""}
103 </span>
104 }
105 control={
106 !readOnly ? (
107 <RemoveUsageProviderButton
108 label={label}
109 onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })}
110 />
111 ) : null
112 }
113 />
114 );
115 })
116 )}
117 </SettingsSection>
118 {adding && !readOnly ? (
119 <AddUsageLimitSourceDialog
120 open
121 onOpenChange={setAdding}
122 environmentId={environmentId}
123 environmentLabel={environmentLabel}
124 sources={sources}
125 />
126 ) : null}
127 </>
128 );
130​
131function RemoveUsageProviderButton({
132 label,
133 onConfirm,
134}: {
135 readonly label: string;
136 readonly onConfirm: () => void;
137}) {
138 const [open, setOpen] = useState(false);
139 return (
140 <>
141 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
142 Remove
143 </Button>
144 <AlertDialog open={open} onOpenChange={setOpen}>
145 <AlertDialogPopup>
146 <AlertDialogHeader>
147 <AlertDialogTitle>Remove {label}?</AlertDialogTitle>
148 <AlertDialogDescription>
149 The hub's management key is deleted from this server. Its accounts leave the Limits
150 view; the hub itself is untouched. Add it again with the URL and key to bring them
151 back.
152 </AlertDialogDescription>
153 </AlertDialogHeader>
154 <AlertDialogFooter>
155 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
156 <Button
157 variant="destructive"
158 onClick={() => {
159 setOpen(false);
160 onConfirm();
161 }}
162 >
163 Remove hub
164 </Button>
165 </AlertDialogFooter>
166 </AlertDialogPopup>
167 </AlertDialog>
168 </>
169 );