Open History to see the last commit
1import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2import { msg, T, useMessages } from "gt-react";
3import { useState } from "react";
4
5import {
6 desktopCliCommandStateAtom,
7 getDesktopCliCommandBridge,
8 refreshDesktopCliCommandState,
9} from "~/state/desktopCliCommand";
10import { useEnvironmentQuery } from "~/state/query";
11import { Button } from "../ui/button";
12import { m } from "../../i18n/messages";
13import { stackedThreadToast, toastManager } from "../ui/toast";
14import { SettingsRow } from "./settingsLayout";
15import { useSearchableSetting } from "./settingsSearch";
16
17const 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. */
24export 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
56export 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: state.shadowedBy }} runs first in a new terminal. Remove it to use T3 Code's.`,
66 )
67 : !installed
68 ? resolve(msg("Run T3 Code's CLI as `t3` from any terminal."))
69 : state.onPath
70 ? resolve(
71 msg`Installed at ${{ path: state.installedPath }}. Open a new terminal to use it.`,
72 )
73 : state.pathSetup?.added
74 ? resolve(
75 msg`Installed at ${{ path: state.installedPath }} and added to PATH in ${{ file: state.pathSetup.file }}. Open a new terminal to use it.`,
76 )
77 : state.pathSetup
78 ? resolve(
79 msg`Installed at ${{ path: state.installedPath }}, which is not on your PATH yet.`,
80 )
81 : resolve(
82 msg`Installed at ${{ path: state.installedPath }}, which is not on your PATH yet. Add its folder to your PATH to run \`t3\`.`,
83 );
84
85 return (
86 <SettingsRow
87 {...searchableSetting("cli-command")}
88 description={description}
89 control={
90 <div className="flex gap-2">
91 {state.pathSetup && !state.pathSetup.added ? (
92 <Button size="sm" variant="outline" disabled={busy} onClick={() => run("addToPath")}>
93 <T>Add to PATH</T>
94 </Button>
95 ) : null}
96 <Button
97 size="sm"
98 variant="outline"
99 disabled={busy || (!installed && Boolean(state.shadowedBy))}
100 onClick={() => run(installed ? "uninstall" : "install")}
101 >
102 {installed
103 ? resolve(msg({ context: "verb, uninstalls the t3 command" })`Remove`)
104 : resolve(msg({ context: "verb, installs the t3 command" })`Install`)}
105 </Button>
106 </div>
107 }
108 />
109 );
110}