1+ import type { DesktopBridge } from "@t3tools/contracts" ;
2+
3+ const FALLBACK_TRIAGE_COMMAND = "npx t3 triage" ;
4+
5+ /** The Windows launcher is a `.cmd` run from PowerShell, which needs `&` to run a quoted path. */
6+ function 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 ( "'" , `'"'"'` ) } '` ;
15+ }
16+
17+ /** Desktop can run its bundled launcher without Node; everything else falls back to npx. */
18+ export 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 ;
29+ }
30+
31+ const 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>` ;
33+ const 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+ ) ;
36+ const CHECK_ICON = icon ( '<path d="M20 6 9 17l-5-5"/>' ) ;
37+
138export function showBootError ( error : unknown ) {
239 console . error ( "T3 Code failed to start." , error ) ;
340 const bootShell = document . getElementById ( "boot-shell" ) ;
@@ -7,20 +44,56 @@ export function showBootError(error: unknown) {
744 content . id = "boot-error" ;
845 content . setAttribute ( "role" , "alert" ) ;
946
10- const message = document . createElement ( "p " ) ;
11- message . textContent = "T3 Code could not load. " ;
12- content . append ( message ) ;
47+ const title = document . createElement ( "h1 " ) ;
48+ title . textContent = "T3 Code could not load" ;
49+ content . append ( title ) ;
1350
1451 if ( import . meta. env . DEV && error instanceof Error ) {
1552 const detail = document . createElement ( "p" ) ;
16- detail . textContent = error . message ;
53+ detail . append ( Object . assign ( document . createElement ( "code" ) , { textContent : error . message } ) ) ;
1754 content . append ( detail ) ;
1855 }
1956
2057 const reload = document . createElement ( "button" ) ;
58+ reload . id = "boot-error-reload" ;
2159 reload . type = "button" ;
2260 reload . textContent = "Reload" ;
2361 reload . addEventListener ( "click" , ( ) => window . location . reload ( ) ) ;
24- content . append ( 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 ) ;
2596 bootShell . replaceChildren ( content ) ;
97+
98+ void resolveTriageCommand ( window . desktopBridge ) . then ( ( value ) => ( command . textContent = value ) ) ;
2699}
0 commit comments