Open History to see the last commit
1import { useState } from "react";
2
3import {
4 desktopCliCommandStateAtom,
5 getDesktopCliCommandBridge,
6 refreshDesktopCliCommandState,
7} from "~/state/desktopCliCommand";
8import { useEnvironmentQuery } from "~/state/query";
9import { Button } from "../ui/button";
10import { stackedThreadToast, toastManager } from "../ui/toast";
11import { SettingsRow } from "./settingsLayout";
12import { searchableSetting } from "./settingsSearch";
13
14export function CliCommandSettingsRow() {
15 const bridge = getDesktopCliCommandBridge();
16 const query = useEnvironmentQuery(bridge ? desktopCliCommandStateAtom : null);
17 const state = query.data;
18 const [pending, setPending] = useState(false);
19
20 const change = (action: "install" | "uninstall") => {
21 if (!bridge || pending) return;
22 setPending(true);
23 void bridge[action]()
24 .then(refreshDesktopCliCommandState)
25 .catch((error: unknown) => {
26 toastManager.add(
27 stackedThreadToast({
28 type: "error",
29 title: action === "install" ? "Could not install t3" : "Could not remove t3",
30 description: error instanceof Error ? error.message : "Something went wrong.",
31 }),
32 );
33 })
34 .finally(() => setPending(false));
35 };
36
37 if (!bridge || !state?.supported) return null;
38 const installed = state.installedPath !== null;
39 const description = state.shadowedBy
40 ? `Another t3 at ${state.shadowedBy} runs first in a new terminal. Remove it to use T3 Code's.`
41 : !installed
42 ? "Run T3 Code's CLI as `t3` from any terminal."
43 : state.onPath
44 ? `Installed at ${state.installedPath}. Open a new terminal to use it.`
45 : `Installed at ${state.installedPath}, which is not on your PATH yet. Add its folder to your PATH to run \`t3\`.`;
46
47 return (
48 <SettingsRow
49 {...searchableSetting("cli-command")}
50 description={description}
51 control={
52 <Button
53 size="sm"
54 variant="outline"
55 disabled={pending || query.isPending || (!installed && Boolean(state.shadowedBy))}
56 onClick={() => change(installed ? "uninstall" : "install")}
57 >
58 {installed ? "Remove" : "Install"}
59 </Button>
60 }
61 />
62 );
63}