@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 115 lines 3.85 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import { m } from "~/i18n/messages";
3 import { T, useGT } from "gt-react";
4 import { useState } from "react";
5
6 import { isLocalEnvironmentDisabled } from "../../localEnvironment";
7 import {
8 AlertDialog,
9 AlertDialogClose,
10 AlertDialogDescription,
11 AlertDialogFooter,
12 AlertDialogHeader,
13 AlertDialogPopup,
14 AlertDialogTitle,
15 } from "../ui/alert-dialog";
16 import { Button } from "../ui/button";
17 import { Spinner } from "../ui/spinner";
18 import { Switch } from "../ui/switch";
19 import { SettingsRow } from "./settingsLayout";
20 import { useSearchableSetting } from "./settingsSearch";
21
22 // Toggling relaunches desktop; the switch reflects startup state.
23 export 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(
55 "Run agents on this computer. Turn off to use T3 Code only with remote environments.",
56 )
57 : gt("Turned off. Agents only run in remote environments.")
58 }
59 control={
60 <Switch
61 checked={enabled}
62 disabled={isUpdating}
63 onCheckedChange={() => setConfirmOpen(true)}
64 aria-label={gt("Local environment")}
65 />
66 }
67 />
68 <AlertDialog
69 open={confirmOpen}
70 onOpenChange={(open) => {
71 if (isUpdating) return;
72 setConfirmOpen(open);
73 if (!open) setError(null);
74 }}
75 >
76 <AlertDialogPopup>
77 <AlertDialogHeader>
78 <AlertDialogTitle>
79 {enabled ? gt("Turn off local environment?") : gt("Turn on local environment?")}
80 </AlertDialogTitle>
81 <AlertDialogDescription>
82 {enabled
83 ? gt(
84 "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.",
85 )
86 : gt("T3 Code will restart and start running a server on this computer again.")}
87 </AlertDialogDescription>
88 </AlertDialogHeader>
89 {error ? <p className="px-6 pb-4 text-sm text-destructive">{error}</p> : null}
90 <AlertDialogFooter>
91 <AlertDialogClose disabled={isUpdating} render={<Button variant="outline" />}>
92 <T>Cancel</T>
93 </AlertDialogClose>
94 <Button
95 variant={enabled ? "destructive" : "default"}
96 disabled={isUpdating}
97 onClick={() => void applyChange()}
98 >
99 {isUpdating ? (
100 <>
101 <Spinner size="sm" />
102 <T>Restarting…</T>
103 </>
104 ) : enabled ? (
105 gt("Restart and turn off")
106 ) : (
107 gt("Restart and turn on")
108 )}
109 </Button>
110 </AlertDialogFooter>
111 </AlertDialogPopup>
112 </AlertDialog>
113 </>
114 );
115 }