@huaqiu/dsh-hardware-ui 0.7.19 → 0.7.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -120,6 +120,28 @@ export interface InstallJob {
120
120
  readonly finishedAt?: number
121
121
  }
122
122
 
123
+ /**
124
+ * Verdict for the text currently in one card's input.
125
+ *
126
+ * The wizard's primary button must not pretend a typed path is usable, and the
127
+ * Settings verdict must not claim "not saved" about a path the Host just
128
+ * accepted, so the input's own validity is checked against the Host whenever the
129
+ * text settles ({@link ToolchainController.setDraft} debounces it).
130
+ */
131
+ export interface DraftCheck {
132
+ readonly state: 'pending' | 'valid' | 'invalid'
133
+ /** Root the Host normalised the text to; present when known. */
134
+ readonly installPath?: string
135
+ readonly version?: string
136
+ readonly error?: string
137
+ }
138
+
139
+ /** A save the Host just accepted, shown as the card's success badge until it fades. */
140
+ export interface SaveConfirmation {
141
+ readonly at: number
142
+ readonly version?: string
143
+ }
144
+
123
145
  /** Verdict of a validate / save round trip. */
124
146
  export interface ValidationOutcome {
125
147
  readonly ok: boolean
@@ -161,6 +183,10 @@ export interface ToolchainSnapshot {
161
183
  readonly jobs: Readonly<Partial<Record<ToolchainId, InstallJob>>>
162
184
  /** Path currently held in each card's input, kept here so both surfaces agree. */
163
185
  readonly drafts: Readonly<Record<ToolchainId, string>>
186
+ /** Live verdict of each card's text, from the Host's own validation. */
187
+ readonly checks: Readonly<Partial<Record<ToolchainId, DraftCheck>>>
188
+ /** Saves the Host accepted recently; the card shows a badge, then it fades. */
189
+ readonly confirmed: Readonly<Partial<Record<ToolchainId, SaveConfirmation>>>
164
190
  readonly toasts: readonly ToastItem[]
165
191
  /** Open executable picker (Browse), if any. */
166
192
  readonly picker?: ToolchainPicker
@@ -192,6 +218,10 @@ export const HOST_TRANSPORT: ToolchainTransport = {
192
218
  const TOAST_MS = 7000
193
219
  /** Poll cadence while a job or a discovery run is live. */
194
220
  const POLL_MS = 700
221
+ /** How long a typed path must settle before its validity is asked of the Host. */
222
+ const CHECK_MS = 500
223
+ /** How long the "saved" badge stays on a card after the Host accepted the write. */
224
+ const CONFIRM_MS = 6000
195
225
 
196
226
  const EMPTY_DRAFTS: Readonly<Record<ToolchainId, string>> = { kicad: '', solidcraft: '' }
197
227
 
@@ -259,7 +289,9 @@ type Listener = () => void
259
289
  * nothing is running, which keeps an idle settings panel free of requests.
260
290
  */
261
291
  export class ToolchainController {
262
- private snapshot: ToolchainSnapshot = { loading: true, jobs: {}, drafts: EMPTY_DRAFTS, toasts: [] }
292
+ private snapshot: ToolchainSnapshot = {
293
+ loading: true, jobs: {}, drafts: EMPTY_DRAFTS, checks: {}, confirmed: {}, toasts: [],
294
+ }
263
295
  private readonly listeners = new Set<Listener>()
264
296
  private toastSeq = 0
265
297
  private timer?: ReturnType<typeof setTimeout>
@@ -270,6 +302,12 @@ export class ToolchainController {
270
302
  private readonly phases = new Map<ToolchainId, InstallPhase>()
271
303
  /** The job id each tool's card is watching; the poll asks for all of them at once. */
272
304
  private readonly watched = new Map<ToolchainId, string>()
305
+ /** Debounce timers for the input's own validity check, one per tool. */
306
+ private readonly checkTimers = new Map<ToolchainId, ReturnType<typeof setTimeout>>()
307
+ /** Per-tool sequence so a late answer cannot overwrite a newer verdict. */
308
+ private readonly checkSeq = new Map<ToolchainId, number>()
309
+ /** Timers that retract the "saved" badge after {@link CONFIRM_MS}. */
310
+ private readonly confirmTimers = new Map<ToolchainId, ReturnType<typeof setTimeout>>()
273
311
 
274
312
  constructor(
275
313
  private readonly transport: ToolchainTransport = HOST_TRANSPORT,
@@ -307,6 +345,10 @@ export class ToolchainController {
307
345
  this.timer = undefined
308
346
  for (const timer of this.timers.values()) clearTimeout(timer)
309
347
  this.timers.clear()
348
+ for (const timer of this.checkTimers.values()) clearTimeout(timer)
349
+ this.checkTimers.clear()
350
+ for (const timer of this.confirmTimers.values()) clearTimeout(timer)
351
+ this.confirmTimers.clear()
310
352
  this.listeners.clear()
311
353
  }
312
354
 
@@ -325,7 +367,14 @@ export class ToolchainController {
325
367
  const solidcraft = this.syncDraft('solidcraft', status.solidcraft.installPath)
326
368
  if (solidcraft !== undefined) drafts = { ...drafts, solidcraft }
327
369
  const jobs = collectJobs(status, this.snapshot.jobs)
328
- this.set({ status, loading: false, error: undefined, drafts, jobs })
370
+ // A stored root the Host itself resolved is valid by definition; adopt its
371
+ // verdict so a configured tool needs no round trip to light up the button.
372
+ const checks = { ...this.snapshot.checks }
373
+ if ((kicad ?? solidcraft) !== undefined) {
374
+ if (kicad !== undefined) checks.kicad = { state: 'valid', installPath: kicad }
375
+ if (solidcraft !== undefined) checks.solidcraft = { state: 'valid', installPath: solidcraft }
376
+ }
377
+ this.set({ status, loading: false, error: undefined, drafts, jobs, checks })
329
378
  for (const job of Object.values(jobs)) {
330
379
  if (job !== undefined) this.noteJob(job)
331
380
  }
@@ -568,20 +617,141 @@ export class ToolchainController {
568
617
  this.set({ picker: picked === undefined ? { ...current, nativePending: false } : undefined })
569
618
  }
570
619
 
571
- /** Type a path without persisting it. */
620
+ /**
621
+ * Type a path without persisting it, and ask the Host whether it is usable.
622
+ *
623
+ * The verdict is not cosmetic: the wizard's primary button is enabled only
624
+ * when a tool is configured or one input has been verified, so this check is
625
+ * what keeps "greyed out" honest instead of guessing from the text.
626
+ */
572
627
  setDraft(tool: ToolchainId, value: string): void {
573
628
  this.set({ drafts: this.withDraft(tool, value) })
629
+ this.scheduleCheck(tool, value)
630
+ }
631
+
632
+ /** Debounce a validity check for the text that just changed. */
633
+ private scheduleCheck(tool: ToolchainId, value: string): void {
634
+ const pending = this.checkTimers.get(tool)
635
+ if (pending !== undefined) {
636
+ clearTimeout(pending)
637
+ this.checkTimers.delete(tool)
638
+ }
639
+ const text = value.trim()
640
+ if (text === '') {
641
+ this.patchCheck(tool, undefined)
642
+ return
643
+ }
644
+ // Text that still spells the stored root needs no round trip: the Host has
645
+ // already validated it, and a poll may not have landed yet.
646
+ const root = this.roots[tool]
647
+ if (root !== '' && text === root) {
648
+ this.patchCheck(tool, { state: 'valid', installPath: root })
649
+ return
650
+ }
651
+ const known = this.knownVersion(tool, text)
652
+ if (known !== undefined) {
653
+ this.patchCheck(tool, { state: 'valid', version: known })
654
+ return
655
+ }
656
+ this.patchCheck(tool, { state: 'pending' })
657
+ const timer = setTimeout(() => {
658
+ this.checkTimers.delete(tool)
659
+ void this.runCheck(tool, text)
660
+ }, CHECK_MS)
661
+ this.checkTimers.set(tool, timer)
662
+ }
663
+
664
+ /** A discovered, already-validated executable: no process needs to run again. */
665
+ private knownVersion(tool: ToolchainId, text: string): string | undefined {
666
+ const candidates = this.snapshot.status?.[tool].discovery.candidates ?? []
667
+ const match = candidates.find(candidate => candidate.valid && candidate.path === text)
668
+ return match?.version ?? (match === undefined ? undefined : '?')
669
+ }
670
+
671
+ /** Ask the Host about one path and record the verdict if it is still current. */
672
+ private async runCheck(tool: ToolchainId, text: string): Promise<void> {
673
+ if (this.stopped) return
674
+ const seq = (this.checkSeq.get(tool) ?? 0) + 1
675
+ this.checkSeq.set(tool, seq)
676
+ let outcome: ValidationOutcome
677
+ try {
678
+ outcome = await this.transport.validate(tool, text)
679
+ } catch (error) {
680
+ outcome = { ok: false, error: messageOf(error) }
681
+ }
682
+ if (this.stopped || this.checkSeq.get(tool) !== seq) return
683
+ // The input moved on while the answer was in flight: this verdict is stale.
684
+ if (this.snapshot.drafts[tool].trim() !== text) return
685
+ this.patchCheck(tool, outcome.ok
686
+ ? { state: 'valid', ...(outcome.installPath === undefined ? {} : { installPath: outcome.installPath }), ...(outcome.version === undefined ? {} : { version: outcome.version }) }
687
+ : { state: 'invalid', ...(outcome.error === undefined ? {} : { error: outcome.error }) })
688
+ }
689
+
690
+ /** Replace (or drop, with `undefined`) one tool's verdict. */
691
+ private patchCheck(tool: ToolchainId, check: DraftCheck | undefined): void {
692
+ const next = { ...this.snapshot.checks }
693
+ if (check === undefined) delete next[tool]
694
+ else next[tool] = check
695
+ this.set({ checks: next })
696
+ }
697
+
698
+ /**
699
+ * Show the success badge for a write the Host accepted, then retract it.
700
+ *
701
+ * The badge replaces the verdict line, so the card stops shouting the full
702
+ * executable path (or "not saved") about a path that is in fact saved.
703
+ */
704
+ private confirm(tool: ToolchainId, version: string | undefined): void {
705
+ this.clearConfirm(tool)
706
+ this.set({
707
+ confirmed: {
708
+ ...this.snapshot.confirmed,
709
+ [tool]: { at: Date.now(), ...(version === undefined ? {} : { version }) },
710
+ },
711
+ })
712
+ const timer = setTimeout(() => {
713
+ this.confirmTimers.delete(tool)
714
+ this.clearConfirm(tool)
715
+ }, CONFIRM_MS)
716
+ this.confirmTimers.set(tool, timer)
717
+ }
718
+
719
+ private clearConfirm(tool: ToolchainId): void {
720
+ const timer = this.confirmTimers.get(tool)
721
+ if (timer !== undefined) {
722
+ clearTimeout(timer)
723
+ this.confirmTimers.delete(tool)
724
+ }
725
+ if (this.snapshot.confirmed[tool] === undefined) return
726
+ const next = { ...this.snapshot.confirmed }
727
+ delete next[tool]
728
+ this.set({ confirmed: next })
574
729
  }
575
730
 
576
731
  /** Validate and persist one path through the Host (§6.3). */
577
732
  async save(tool: ToolchainId, path: string, source: ToolSource = 'manual'): Promise<ValidationOutcome> {
578
733
  const name = tool === 'kicad' ? 'KiCad' : 'SolidCraft'
579
734
  this.set({ saving: tool })
735
+ this.clearConfirm(tool)
580
736
  try {
581
737
  const outcome = await this.transport.save(tool, path, source)
582
738
  if (outcome.ok) {
739
+ // The Host stores the *root* it resolved, which is rarely what the user
740
+ // typed (an executable path, a `bin` directory). Adopting that root here
741
+ // is what ends the "not saved" state: the input then shows what is saved,
742
+ // and the verdict stops being a false alarm.
743
+ const accepted = outcome.installPath ?? path.trim()
744
+ this.roots[tool] = accepted
745
+ const draft = this.snapshot.drafts[tool]
746
+ if (draft.trim() !== accepted) this.set({ drafts: this.withDraft(tool, accepted) })
747
+ this.patchCheck(tool, {
748
+ state: 'valid', installPath: accepted,
749
+ ...(outcome.version === undefined ? {} : { version: outcome.version }),
750
+ })
751
+ this.confirm(tool, outcome.version)
583
752
  this.toast('ok', COPY.toolchain.toastSaved(name, outcome.version ?? '?'))
584
753
  } else {
754
+ this.patchCheck(tool, { state: 'invalid', ...(outcome.error === undefined ? {} : { error: outcome.error }) })
585
755
  this.toast('warn', COPY.toolchain.toastInvalid(name, outcome.error ?? COPY.toolchain.notDetected))
586
756
  }
587
757
  await this.refresh()
@@ -264,9 +264,10 @@ export interface PlacedOrder {
264
264
  }
265
265
 
266
266
  /**
267
- * Submit one order. `confirm` is what the host requires; the panel sets it only
268
- * after the user confirms the upload in the dialog. The host never opens the
269
- * page by itself — the client shows `orderUrl` in its own Browser tab.
267
+ * Submit one order. The upload is the user's click on the order card, so there
268
+ * is no confirmation flag to pass and no second dialog to satisfy: the panel
269
+ * shows the exact package first, then posts it. The host never opens the page
270
+ * by itself — the client shows `orderUrl` in its own Browser tab.
270
271
  */
271
272
  export async function placeOrder(
272
273
  kind: 'pcb' | 'smt',
@@ -277,7 +278,6 @@ export async function placeOrder(
277
278
  cwd?: string
278
279
  session?: string
279
280
  quantity?: number
280
- confirm: boolean
281
281
  },
282
282
  signal?: AbortSignal,
283
283
  ): Promise<PlacedOrder> {
@@ -34,7 +34,7 @@ import { COPY } from './copy.js'
34
34
  import type { ConfigFormLike, HardwareInstallationConfig } from './model.js'
35
35
  import { ToolchainCard, TOOLCHAIN_ICONS } from './toolchain-view.js'
36
36
  import {
37
- setupComplete, type ToolchainController, type ToolchainId, type ToolchainSnapshot, type ToolchainStatus,
37
+ jobRunning, setupComplete, type ToolchainController, type ToolchainId, type ToolchainSnapshot, type ToolchainStatus,
38
38
  } from './toolchain.js'
39
39
  import { buttonStyle, C, disabled, MUTED, R } from './theme.js'
40
40
 
@@ -100,18 +100,21 @@ export function onboardingPhase(input: {
100
100
  /**
101
101
  * May the footer's primary button submit?
102
102
  *
103
- * The prototype's wizard card carries no per-card validate control, so this
104
- * button is the step's *only* way to persist a typed path. Gating it on "both
105
- * tools already configured" made a clean first launch a dead end: nothing could
106
- * be configured because the button that configures was disabled. Readiness is
107
- * therefore "there is something to save".
103
+ * Two gates, both asked for after the first-run walk:
104
+ * - **No run in flight.** While a toolchain is downloading or extracting, the
105
+ * step must not hand itself back: the install owns the profile, and
106
+ * "Get Started" would dismiss the modal out from under the bar it shows.
107
+ * - **Something usable.** At least one tool is already configured, or the text
108
+ * in an input has been verified against the Host (`checks`). A merely
109
+ * non-empty input is not enough: the button used to be pressed with garbage
110
+ * paths and then reported the Host's rejection, which is a worse first run
111
+ * than a disabled button that lights up the moment a path checks out.
108
112
  */
109
113
  export function saveReady(snapshot: ToolchainSnapshot): boolean {
110
114
  if (snapshot.saving !== undefined) return false
111
- if (TOOLCHAIN_IDS.some(id => (snapshot.drafts[id] ?? '').trim() !== '')) return true
112
- return snapshot.status === undefined
113
- ? false
114
- : TOOLCHAIN_IDS.some(id => snapshot.status![id].state === 'configured')
115
+ if (TOOLCHAIN_IDS.some(id => jobRunning(snapshot.jobs[id]))) return false
116
+ if (TOOLCHAIN_IDS.some(id => snapshot.status?.[id].state === 'configured')) return true
117
+ return TOOLCHAIN_IDS.some(id => snapshot.checks[id]?.state === 'valid')
115
118
  }
116
119
 
117
120
  /** Fields of the stored configuration this step reacts to. */function sameConfig(left: HardwareInstallationConfig | undefined, right: HardwareInstallationConfig | undefined): boolean {
@@ -190,7 +193,9 @@ function Glyph(props: { readonly path: string; readonly size?: number; readonly
190
193
  }
191
194
 
192
195
  /** Stable empty snapshot for the no-controller case (identity must not churn). */
193
- const EMPTY: ToolchainSnapshot = { loading: true, jobs: {}, drafts: { kicad: '', solidcraft: '' }, toasts: [] }
196
+ const EMPTY: ToolchainSnapshot = {
197
+ loading: true, jobs: {}, drafts: { kicad: '', solidcraft: '' }, checks: {}, confirmed: {}, toasts: [],
198
+ }
194
199
  const emptySnapshot = (): ToolchainSnapshot => EMPTY
195
200
 
196
201
  /**