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

CliCommandSettingsRow.tsx

74 lines2.3 KB

1import type { DesktopCliCommandState } from "@t3tools/contracts";
2import { useCallback, 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 = useCallback(
29 (action: "install" | "uninstall") => {
30 if (!bridge || pending) return;
31 setPending(true);
32 void bridge[action]()
33 .then(setState)
34 .catch((error: unknown) => {
35 toastManager.add(
36 stackedThreadToast({
37 type: "error",
38 title: action === "install" ? "Could not install t3" : "Could not remove t3",
39 description: error instanceof Error ? error.message : "Something went wrong.",
40 }),
41 );
42 })
43 .finally(() => setPending(false));
44 },
45 [bridge, pending],
46 );
47​
48 if (!bridge || !state?.supported) return null;
49 const installed = state.installedPath !== null;
50 const description = state.shadowedBy
51 ? `Another t3 at ${state.shadowedBy} runs first in a new terminal. Remove it to use T3 Code's.`
52 : !installed
53 ? "Run T3 Code's CLI as `t3` from any terminal."
54 : state.onPath
55 ? `Installed at ${state.installedPath}. Open a new terminal to use it.`
56 : `Installed at ${state.installedPath}, which is not on your PATH yet. Add its folder to your PATH to run \`t3\`.`;
57​
58 return (
59 <SettingsRow
60 {...searchableSetting("cli-command")}
61 description={description}
62 control={
63 <Button
64 size="sm"
65 variant="outline"
66 disabled={pending}
67 onClick={() => change(installed ? "uninstall" : "install")}
68 >
69 {installed ? "Remove" : "Install"}
70 </Button>
71 }
72 />
73 );