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

LocalEnvironmentSetting.tsx

102 lines3.4 KB

1import { useState } from "react";
2​
3import { isLocalEnvironmentDisabled } from "../../localEnvironment";
4import {
5 AlertDialog,
6 AlertDialogClose,
7 AlertDialogDescription,
8 AlertDialogFooter,
9 AlertDialogHeader,
10 AlertDialogPopup,
11 AlertDialogTitle,
12} from "../ui/alert-dialog";
13import { Button } from "../ui/button";
14import { Spinner } from "../ui/spinner";
15import { Switch } from "../ui/switch";
16import { SettingsRow } from "./settingsLayout";
17import { searchableSetting } from "./settingsSearch";
18​
19// Toggling relaunches desktop; the switch reflects startup state.
20export function LocalEnvironmentSetting() {
21 const setEnabled = window.desktopBridge?.setLocalEnvironmentEnabled;
22 const [enabled] = useState(() => !isLocalEnvironmentDisabled());
23 const [confirmOpen, setConfirmOpen] = useState(false);
24 const [isUpdating, setIsUpdating] = useState(false);
25 const [error, setError] = useState<string | null>(null);
26 if (!setEnabled) return null;
27​
28 const applyChange = async () => {
29 setIsUpdating(true);
30 setError(null);
31 try {
32 await setEnabled(!enabled);
33 } catch (cause) {
34 setError(cause instanceof Error ? cause.message : "Couldn't change this setting.");
35 setIsUpdating(false);
36 }
37 };
38​
39 return (
40 <>
41 <SettingsRow
42 {...searchableSetting("local-environment")}
43 description={
44 enabled
45 ? "Run agents on this computer. Turn off to use T3 Code only with remote environments."
46 : "Turned off. Agents only run in remote environments."
47 }
48 control={
49 <Switch
50 checked={enabled}
51 disabled={isUpdating}
52 onCheckedChange={() => setConfirmOpen(true)}
53 aria-label="Local environment"
54 />
55 }
56 />
57 <AlertDialog
58 open={confirmOpen}
59 onOpenChange={(open) => {
60 if (isUpdating) return;
61 setConfirmOpen(open);
62 if (!open) setError(null);
63 }}
64 >
65 <AlertDialogPopup>
66 <AlertDialogHeader>
67 <AlertDialogTitle>
68 {enabled ? "Turn off local environment?" : "Turn on local environment?"}
69 </AlertDialogTitle>
70 <AlertDialogDescription>
71 {enabled
72 ? "T3 Code will restart without running a server on this computer. Any agents and terminals running here will stop, and other devices will no longer be able to connect to this computer. Your projects, history, and remote environments are unaffected."
73 : "T3 Code will restart and start running a server on this computer again."}
74 </AlertDialogDescription>
75 </AlertDialogHeader>
76 {error ? <p className="px-6 pb-4 text-sm text-destructive">{error}</p> : null}
77 <AlertDialogFooter>
78 <AlertDialogClose disabled={isUpdating} render={<Button variant="outline" />}>
79 Cancel
80 </AlertDialogClose>
81 <Button
82 variant={enabled ? "destructive" : "default"}
83 disabled={isUpdating}
84 onClick={() => void applyChange()}
85 >
86 {isUpdating ? (
87 <>
88 <Spinner size="sm" />
89 Restarting…
90 </>
91 ) : enabled ? (
92 "Restart and turn off"
93 ) : (
94 "Restart and turn on"
95 )}
96 </Button>
97 </AlertDialogFooter>
98 </AlertDialogPopup>
99 </AlertDialog>
100 </>
101 );