@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 122 lines 4.02 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import { msg, T, useMessages } from "gt-react";
3 import { useState } from "react";
4
5 import {
6 desktopCliCommandStateAtom,
7 getDesktopCliCommandBridge,
8 refreshDesktopCliCommandState,
9 } from "~/state/desktopCliCommand";
10 import { useEnvironmentQuery } from "~/state/query";
11 import { Button } from "../ui/button";
12 import { m } from "../../i18n/messages";
13 import { stackedThreadToast, toastManager } from "../ui/toast";
14 import { SettingsRow } from "./settingsLayout";
15 import { useSearchableSetting } from "./settingsSearch";
16
17 const FAILURE_TITLES = {
18 install: msg("Could not install t3"),
19 uninstall: msg("Could not remove t3"),
20 addToPath: msg("Could not add t3 to PATH"),
21 } as const;
22
23 /** The desktop `t3` command's state, and its actions with a toast on failure. */
24 export function useDesktopCliCommand() {
25 const bridge = getDesktopCliCommandBridge();
26 const query = useEnvironmentQuery(bridge ? desktopCliCommandStateAtom : null);
27 const [pending, setPending] = useState(false);
28
29 const run = (action: keyof typeof FAILURE_TITLES) => {
30 if (!bridge || pending) return;
31 setPending(true);
32 void bridge[action]()
33 .then(refreshDesktopCliCommandState)
34 .catch((error: unknown) => {
35 toastManager.add(
36 stackedThreadToast({
37 type: "error",
38 title: m(FAILURE_TITLES[action]),
39 description:
40 error instanceof Error
41 ? (thrownErrorMessage(error, m) ?? error.message)
42 : m(msg("Something went wrong.")),
43 }),
44 );
45 })
46 .finally(() => setPending(false));
47 };
48
49 return {
50 state: bridge ? query.data : undefined,
51 busy: pending || query.isPending,
52 run,
53 };
54 }
55
56 export function CliCommandSettingsRow() {
57 const searchableSetting = useSearchableSetting();
58 const { state, busy, run } = useDesktopCliCommand();
59 const resolve = useMessages();
60
61 if (!state?.supported) return null;
62 const installed = state.installedPath !== null;
63 const description = state.shadowedBy
64 ? resolve(
65 msg("Another t3 at {path} runs first in a new terminal. Remove it to use T3 Code's.", {
66 path: state.shadowedBy,
67 }),
68 )
69 : !installed
70 ? resolve(msg("Run T3 Code's CLI as `t3` from any terminal."))
71 : state.onPath
72 ? resolve(
73 msg("Installed at {path}. Open a new terminal to use it.", {
74 path: state.installedPath,
75 }),
76 )
77 : state.pathSetup?.added
78 ? resolve(
79 msg(
80 "Installed at {path} and added to PATH in {file}. Open a new terminal to use it.",
81 { path: state.installedPath, file: state.pathSetup.file },
82 ),
83 )
84 : state.pathSetup
85 ? resolve(
86 msg("Installed at {path}, which is not on your PATH yet.", {
87 path: state.installedPath,
88 }),
89 )
90 : resolve(
91 msg(
92 "Installed at {path}, which is not on your PATH yet. Add its folder to your PATH to run `t3`.",
93 { path: state.installedPath },
94 ),
95 );
96
97 return (
98 <SettingsRow
99 {...searchableSetting("cli-command")}
100 description={description}
101 control={
102 <div className="flex gap-2">
103 {state.pathSetup && !state.pathSetup.added ? (
104 <Button size="sm" variant="outline" disabled={busy} onClick={() => run("addToPath")}>
105 <T>Add to PATH</T>
106 </Button>
107 ) : null}
108 <Button
109 size="sm"
110 variant="outline"
111 disabled={busy || (!installed && Boolean(state.shadowedBy))}
112 onClick={() => run(installed ? "uninstall" : "install")}
113 >
114 {installed
115 ? resolve(msg("Remove", { $context: "verb, uninstalls the t3 command" }))
116 : resolve(msg("Install", { $context: "verb, installs the t3 command" }))}
117 </Button>
118 </div>
119 }
120 />
121 );
122 }