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

bootError.ts

99 lines3.7 KB

1import type { DesktopBridge } from "@t3tools/contracts";
2​
3const FALLBACK_TRIAGE_COMMAND = "npx t3 triage";
4​
5/** The Windows launcher is a `.cmd` run from PowerShell, which needs `&` to run a quoted path. */
6function launcherCommand(launcherPath: string) {
7 if (launcherPath.endsWith(".cmd")) {
8 return /^[\w:\\.-]+$/.test(launcherPath)
9 ? launcherPath
10 : `& '${launcherPath.replace(/['\u2018\u2019\u201A\u201B]/g, "$&$&")}'`;
11 }
12 return /^[\w@%+=:,./-]+$/.test(launcherPath)
13 ? launcherPath
14 : `'${launcherPath.replaceAll("'", `'"'"'`)}'`;
16​
17/** Desktop can run its bundled launcher without Node; everything else falls back to npx. */
18export async function resolveTriageCommand(bridge: DesktopBridge | undefined) {
19 try {
20 const state = await bridge?.cliCommand?.getState();
21 // Windows reports onPath from its PATH entry alone; only an existing launcher proves it runs.
22 if (state?.launcherPath) {
23 return state.onPath ? "t3 triage" : `${launcherCommand(state.launcherPath)} triage`;
24 }
25 } catch (error) {
26 console.warn("Could not read the desktop CLI state.", error);
27 }
28 return FALLBACK_TRIAGE_COMMAND;
30​
31const icon = (paths: string) =>
32 `<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${paths}</svg>`;
33const COPY_ICON = icon(
34 '<rect width="14" height="14" x="8" y="8" rx="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>',
35);
36const CHECK_ICON = icon('<path d="M20 6 9 17l-5-5"/>');
37​
38export function showBootError(error: unknown) {
39 console.error("T3 Code failed to start.", error);
40 const bootShell = document.getElementById("boot-shell");
41 if (!bootShell) return;
42​
43 const content = document.createElement("div");
44 content.id = "boot-error";
45 content.setAttribute("role", "alert");
46​
47 const title = document.createElement("h1");
48 title.textContent = "T3 Code could not load";
49 content.append(title);
50​
51 if (import.meta.env.DEV && error instanceof Error) {
52 const detail = document.createElement("p");
53 detail.append(Object.assign(document.createElement("code"), { textContent: error.message }));
54 content.append(detail);
55 }
56​
57 const reload = document.createElement("button");
58 reload.id = "boot-error-reload";
59 reload.type = "button";
60 reload.textContent = "Reload";
61 reload.addEventListener("click", () => window.location.reload());
62​
63 const hint = document.createElement("p");
64 hint.textContent = "If reloading does not help, run this in a terminal to find the cause:";
65​
66 const command = document.createElement("code");
67 command.textContent = FALLBACK_TRIAGE_COMMAND;
68 const commandRow = document.createElement("div");
69 commandRow.id = "boot-error-command";
70 commandRow.append(command);
71 // The clipboard API exists only in secure contexts, such as HTTPS and the desktop app.
72 if (window.isSecureContext) {
73 const copy = document.createElement("button");
74 copy.id = "boot-error-copy";
75 copy.type = "button";
76 copy.title = "Copy command";
77 copy.setAttribute("aria-label", "Copy command");
78 copy.innerHTML = COPY_ICON;
79 copy.addEventListener("click", () => {
80 navigator.clipboard.writeText(command.textContent ?? "").then(
81 () => {
82 copy.innerHTML = CHECK_ICON;
83 copy.setAttribute("aria-label", "Copied");
84 },
85 (copyError) => console.warn("Could not copy the triage command.", copyError),
86 );
87 });
88 commandRow.append(copy);
89 }
90​
91 const triage = document.createElement("div");
92 triage.id = "boot-error-triage";
93 triage.append(hint, commandRow);
94​
95 content.append(reload, triage);
96 bootShell.replaceChildren(content);
97​
98 void resolveTriageCommand(window.desktopBridge).then((value) => (command.textContent = value));