Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CliCommandSettingsRow.tsx

71 lines2.3 KB

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