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