@huaqiu/dsh-hardware-ui 0.7.13 → 0.7.14

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.
@@ -1,45 +1,106 @@
1
1
  /**
2
- * One preview surface per destination. Every asynchronous mount is keyed by the
3
- * navigation revision and owns an AbortController, so a superseded navigation
4
- * disposes its predecessor instead of painting over it.
2
+ * One preview surface per destination.
3
+ *
4
+ * Every asynchronous mount is keyed by the navigation revision and owns an
5
+ * AbortController, so a superseded navigation disposes its predecessor instead
6
+ * of painting over it. A load that finishes after the viewer moved on is
7
+ * disposed, never attached.
5
8
  */
6
9
  import { createElement, useEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react'
7
10
  import { isAbortError, mountEcad } from './ecad.js'
8
11
  import { mountGlb } from './glb.js'
9
12
  import { loadBom, prepareBoardGlb, type BomProjection } from './transport.js'
13
+ import { OrderPanel } from './order.js'
14
+ import {
15
+ C, cardStyle, dotStyle, MONO, MONO_TEXT, pillStyle, R, SECTION_TITLE,
16
+ } from './theme.js'
10
17
  import type { HardwareArtifactInfo, HardwareDestination } from './model.js'
11
18
 
12
19
  const STAGE: CSSProperties = {
13
- position: 'relative', flex: '1 1 auto', minHeight: 240, overflow: 'hidden',
14
- border: '1px solid var(--dsw-alias-border-default, rgba(127,127,127,0.3))', borderRadius: 8,
15
- background: 'var(--dsw-alias-bg-base, #111318)',
20
+ position: 'relative', flex: '1 1 0', minHeight: 200, overflow: 'hidden',
21
+ border: `1px solid ${C.border}`, borderRadius: R.lg, background: C.base,
22
+ }
23
+ const OVERLAY: CSSProperties = {
24
+ position: 'absolute', bottom: 8, left: 8, display: 'flex', alignItems: 'center', gap: 6,
25
+ padding: '2px 8px', borderRadius: R.pill, background: 'rgba(0,0,0,0.55)', color: '#e4e4e7',
26
+ fontSize: 10, fontFamily: MONO, backdropFilter: 'blur(6px)', pointerEvents: 'none',
27
+ maxWidth: 'calc(100% - 16px)', overflow: 'hidden',
28
+ }
29
+ const TOP_RIGHT: CSSProperties = {
30
+ position: 'absolute', top: 8, right: 8, display: 'flex', alignItems: 'center', gap: 4,
31
+ padding: '1px 3px', pointerEvents: 'none',
16
32
  }
17
- const MESSAGE: CSSProperties = {
18
- display: 'flex', alignItems: 'center', justifyContent: 'center', height: '100%',
19
- margin: 0, padding: 16, textAlign: 'center', fontSize: 12,
20
- color: 'var(--dsw-alias-label-tertiary)',
33
+ const CENTER: CSSProperties = {
34
+ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
35
+ height: '100%', gap: 6, padding: 20, textAlign: 'center',
36
+ }
37
+ const CENTER_TEXT: CSSProperties = { margin: 0, fontSize: 11, color: C.text3, maxWidth: 340, lineHeight: 1.6 }
38
+ const TABLE: CSSProperties = { width: '100%', borderCollapse: 'collapse', fontSize: 11 }
39
+ const TH: CSSProperties = {
40
+ textAlign: 'left', padding: '5px 8px', position: 'sticky', top: 0, zIndex: 1,
41
+ background: C.raised, color: C.text2, fontWeight: 600, fontSize: 10,
42
+ borderBottom: `1px solid ${C.border}`, whiteSpace: 'nowrap',
43
+ }
44
+ const TD: CSSProperties = {
45
+ padding: '4px 8px', borderBottom: `1px solid ${C.borderSoft}`, verticalAlign: 'top', color: C.text,
21
46
  }
22
- const TABLE: CSSProperties = { width: '100%', borderCollapse: 'collapse', fontSize: 12 }
23
- const TH: CSSProperties = { textAlign: 'left', padding: '4px 6px', borderBottom: '1px solid rgba(127,127,127,0.3)' }
24
- const TD: CSSProperties = { padding: '4px 6px', borderBottom: '1px solid rgba(127,127,127,0.15)', verticalAlign: 'top' }
25
47
  const SCRIPT: CSSProperties = {
26
- margin: 0, padding: '8px 10px', borderRadius: 6, fontSize: 11, whiteSpace: 'pre-wrap', wordBreak: 'break-all',
27
- background: 'color-mix(in srgb, currentColor 6%, transparent)',
48
+ margin: 0, padding: '6px 8px', borderRadius: R.md, fontSize: 10, whiteSpace: 'pre-wrap',
49
+ wordBreak: 'break-all', background: C.base, fontFamily: MONO, color: C.text2,
28
50
  }
29
51
 
30
52
  type Status = 'loading' | 'ready' | 'error'
31
53
 
32
54
  function describe(error: unknown): string {
33
- if (error instanceof Error) return error.message
34
- return String(error)
55
+ return error instanceof Error ? error.message : String(error)
56
+ }
57
+
58
+ function shortRevision(revision: string | undefined): string {
59
+ return revision === undefined ? '' : revision.slice(0, 10)
60
+ }
61
+
62
+ function statusLine(status: Status, message?: string, hint?: string): ReactNode {
63
+ const text = message ?? (status === 'loading' ? 'Loading saved artifact…' : '')
64
+ return createElement(
65
+ 'div',
66
+ { style: CENTER, role: 'status', 'data-hardware-stage': status },
67
+ status === 'loading'
68
+ ? createElement('span', {
69
+ style: {
70
+ width: 18, height: 18, borderRadius: R.pill, border: `2px solid ${C.borderSoft}`,
71
+ borderTopColor: C.info, animation: 'dsh-hw-spin 900ms linear infinite',
72
+ },
73
+ })
74
+ : createElement('span', { style: { ...pillStyle('warn'), fontFamily: MONO } }, 'unavailable'),
75
+ createElement('p', { style: CENTER_TEXT }, text),
76
+ hint === undefined ? null : createElement('p', { style: { ...CENTER_TEXT, fontSize: 10, color: C.text3 } }, hint),
77
+ )
35
78
  }
36
79
 
37
- function statusLine(status: Status, message?: string): ReactNode {
38
- return createElement('p', { style: MESSAGE, role: 'status', 'data-hardware-stage': status }, message
39
- ?? (status === 'loading' ? 'Loading saved artifact…' : ''))
80
+ function Stage(props: {
81
+ /** Passed as the third `createElement` argument; React folds it into props. */
82
+ readonly children?: ReactNode
83
+ readonly overlay?: ReactNode
84
+ readonly topRight?: ReactNode
85
+ readonly label: string
86
+ readonly attrs?: Record<string, unknown>
87
+ }): ReactNode {
88
+ return createElement(
89
+ 'div',
90
+ { style: { ...STAGE, display: 'flex', flexDirection: 'column' }, ...props.attrs },
91
+ props.topRight === undefined ? null : createElement('div', { style: TOP_RIGHT }, props.topRight),
92
+ props.children,
93
+ props.overlay === undefined ? null : createElement('div', { style: OVERLAY }, props.overlay),
94
+ )
40
95
  }
41
96
 
42
- function EcadStage({ kind, path, revision }: { kind: 'schematic' | 'pcb'; path: string; revision?: string }): ReactNode {
97
+ function EcadStage({ kind, path, revision, session, cwd }: {
98
+ kind: 'schematic' | 'pcb'
99
+ path: string
100
+ revision?: string
101
+ session?: string
102
+ cwd?: string
103
+ }): ReactNode {
43
104
  const container = useRef<HTMLDivElement>(null)
44
105
  const [status, setStatus] = useState<Status>('loading')
45
106
  const [message, setMessage] = useState<string>()
@@ -57,6 +118,8 @@ function EcadStage({ kind, path, revision }: { kind: 'schematic' | 'pcb'; path:
57
118
  mountEcad(kind, path, element, {
58
119
  signal: controller.signal,
59
120
  ...(revision === undefined ? {} : { revision }),
121
+ ...(session === undefined ? {} : { session }),
122
+ ...(cwd === undefined ? {} : { cwd }),
60
123
  })
61
124
  .then((result) => {
62
125
  if (!live) {
@@ -78,46 +141,77 @@ function EcadStage({ kind, path, revision }: { kind: 'schematic' | 'pcb'; path:
78
141
  mounted?.dispose()
79
142
  element.replaceChildren()
80
143
  }
81
- }, [kind, path, revision])
144
+ }, [kind, path, revision, session, cwd])
82
145
 
83
146
  return createElement(
84
- 'div',
85
- { style: STAGE, 'data-hardware-ecad': kind },
86
- ...(status === 'ready' ? [] : [statusLine(status, message)]),
87
- createElement('div', { key: 'canvas', ref: container, style: { width: '100%', height: '100%' } }),
88
- ...(status === 'ready' && sourceRevision !== undefined
89
- ? [createElement('p', {
90
- key: 'rev', style: { ...MESSAGE, height: 'auto', justifyContent: 'flex-start', padding: '4px 8px' },
91
- }, `Saved source revision ${sourceRevision.slice(0, 12)}…`)]
92
- : []),
147
+ Stage,
148
+ {
149
+ label: kind,
150
+ attrs: { 'data-hardware-ecad': kind },
151
+ topRight: status === 'ready'
152
+ ? [
153
+ createElement('span', { key: 'kind', style: { ...pillStyle('muted'), fontSize: 9.5 } }, kind === 'schematic' ? 'sheet' : 'board'),
154
+ sourceRevision === undefined
155
+ ? null
156
+ : createElement('span', { key: 'rev', style: { ...pillStyle('muted'), fontSize: 9.5 } }, `rev ${shortRevision(sourceRevision)}`),
157
+ ]
158
+ : undefined,
159
+ overlay: status === 'ready'
160
+ ? [createElement('span', { key: 'dot', style: dotStyle(kind === 'schematic' ? C.info : C.ok, 6) }),
161
+ 'saved file · scroll to zoom, drag to pan']
162
+ : undefined,
163
+ },
164
+ createElement(
165
+ 'div',
166
+ { style: { position: 'relative', flex: '1 1 0', minHeight: 160 } },
167
+ ...(status === 'ready'
168
+ ? []
169
+ : [statusLine(status, message, status === 'error' ? 'Saved-file preview only; the file must exist and parse.' : undefined)]),
170
+ createElement('div', { key: 'canvas', ref: container, style: { position: 'absolute', inset: 0 } }),
171
+ ),
93
172
  )
94
173
  }
95
174
 
96
- function GlbStage({ boardPath, cwd }: { boardPath: string; cwd?: string }): ReactNode {
175
+ function GlbStage({ boardPath, cwd, session, configured }: {
176
+ boardPath: string
177
+ /** Workspace root the host authorizes against; omitted when discovery did not report one. */
178
+ cwd?: string
179
+ session?: string
180
+ configured: boolean
181
+ }): ReactNode {
97
182
  const container = useRef<HTMLDivElement>(null)
98
183
  const [status, setStatus] = useState<Status>('loading')
99
184
  const [message, setMessage] = useState<string>()
100
185
  const [revision, setRevision] = useState<string>()
186
+ const [cached, setCached] = useState(false)
101
187
 
102
188
  useEffect(() => {
103
189
  const element = container.current
104
190
  if (element === null) return
191
+ if (!configured) {
192
+ setStatus('error')
193
+ setMessage('KiCad is not configured.')
194
+ return
195
+ }
105
196
  const controller = new AbortController()
106
197
  let live = true
107
198
  let mounted: { dispose(): void } | undefined
108
199
  setStatus('loading')
109
- setMessage(undefined)
200
+ setMessage('Exporting the PCB to GLB with kicad-cli…')
110
201
  element.replaceChildren()
111
202
  const run = async (): Promise<void> => {
112
- const glb = await prepareBoardGlb(boardPath, cwd, controller.signal)
203
+ const glb = await prepareBoardGlb(boardPath, cwd, controller.signal, session)
113
204
  if (!live) return
114
205
  setRevision(glb.revision)
206
+ setCached(glb.cached)
207
+ setMessage('Loading the 3D model…')
115
208
  mounted = await mountGlb(glb.url, element, { signal: controller.signal })
116
209
  if (!live) {
117
210
  mounted.dispose()
118
211
  return
119
212
  }
120
213
  setStatus('ready')
214
+ setMessage(undefined)
121
215
  }
122
216
  run().catch((error: unknown) => {
123
217
  if (!live || isAbortError(error)) return
@@ -130,31 +224,61 @@ function GlbStage({ boardPath, cwd }: { boardPath: string; cwd?: string }): Reac
130
224
  mounted?.dispose()
131
225
  element.replaceChildren()
132
226
  }
133
- }, [boardPath, cwd])
227
+ }, [boardPath, cwd, session, configured])
134
228
 
135
229
  return createElement(
136
- 'div',
137
- { style: STAGE, 'data-hardware-glb': true },
138
- ...(status === 'ready' ? [] : [statusLine(status, message)]),
139
- createElement('div', { key: 'canvas', ref: container, style: { width: '100%', height: '100%' } }),
140
- ...(status === 'ready' && revision !== undefined
141
- ? [createElement('p', {
142
- key: 'rev', style: { ...MESSAGE, height: 'auto', justifyContent: 'flex-start', padding: '4px 8px' },
143
- }, `Generated from saved board revision ${revision.slice(0, 12)}…`)]
144
- : []),
230
+ Stage,
231
+ {
232
+ label: '3d',
233
+ attrs: { 'data-hardware-glb': true },
234
+ topRight: status === 'ready'
235
+ ? [
236
+ createElement('span', { key: 'mode', style: pillStyle('muted', { fontSize: 9.5 }) }, cached ? 'cached' : 'generated'),
237
+ createElement('span', { key: 'rev', style: pillStyle('muted', { fontSize: 9.5 }) }, `rev ${shortRevision(revision)}`),
238
+ ]
239
+ : undefined,
240
+ overlay: status === 'ready' ? 'drag to orbit · wheel to zoom' : 'kicad-cli pcb export glb',
241
+ },
242
+ createElement(
243
+ 'div',
244
+ { style: { position: 'relative', flex: '1 1 0', minHeight: 160 } },
245
+ ...(status === 'ready' ? [] : [statusLine(status, message)]),
246
+ createElement('div', { key: 'canvas', ref: container, style: { position: 'absolute', inset: 0 } }),
247
+ ),
145
248
  )
146
249
  }
147
250
 
148
- function BomStage({ schematicPath }: { schematicPath: string }): ReactNode {
251
+ /**
252
+ * BOM table.
253
+ *
254
+ * Rows come from `kicad-cli sch export bom`, so a missing installation is a
255
+ * configuration state, not a failure: the stage reports it without calling the
256
+ * host, the same way the 3D stage does.
257
+ */
258
+ function BomStage({ schematicPath, session, cwd, configured }: {
259
+ schematicPath: string
260
+ session?: string
261
+ cwd?: string
262
+ configured: boolean
263
+ }): ReactNode {
149
264
  const [status, setStatus] = useState<Status>('loading')
150
265
  const [message, setMessage] = useState<string>()
151
266
  const [bom, setBom] = useState<BomProjection>()
152
267
 
153
268
  useEffect(() => {
269
+ if (!configured) {
270
+ setStatus('error')
271
+ setMessage('KiCad is not configured.')
272
+ return
273
+ }
154
274
  const controller = new AbortController()
155
275
  let live = true
156
276
  setStatus('loading')
157
- loadBom(schematicPath, controller.signal)
277
+ loadBom(schematicPath, {
278
+ signal: controller.signal,
279
+ ...(session === undefined ? {} : { session }),
280
+ ...(cwd === undefined ? {} : { cwd }),
281
+ })
158
282
  .then((projection) => {
159
283
  if (!live) return
160
284
  setBom(projection)
@@ -166,95 +290,118 @@ function BomStage({ schematicPath }: { schematicPath: string }): ReactNode {
166
290
  setMessage(describe(error))
167
291
  })
168
292
  return () => { live = false; controller.abort() }
169
- }, [schematicPath])
293
+ }, [schematicPath, session, cwd, configured])
170
294
 
171
295
  if (status !== 'ready' || bom === undefined) {
172
- return createElement('div', { style: { ...STAGE, overflow: 'auto' } }, statusLine(status, message))
296
+ return createElement(
297
+ Stage,
298
+ { label: 'bom', attrs: { 'data-hardware-bom': true }, overlay: 'kicad-cli sch export bom' },
299
+ createElement('div', { style: { flex: '1 1 0', display: 'flex' } }, statusLine(
300
+ status,
301
+ message,
302
+ status === 'error' && !configured
303
+ ? 'BOM rows come from kicad-cli sch export bom. Configure KiCad in Settings → Hardware Engineering.'
304
+ : undefined,
305
+ )),
306
+ )
173
307
  }
308
+
309
+ const total = bom.rows.reduce((sum, row) => sum + row.quantity, 0)
174
310
  return createElement(
175
311
  'div',
176
- { style: { ...STAGE, overflow: 'auto', padding: 8 }, 'data-hardware-bom': true },
177
- createElement('p', { style: { ...MESSAGE, height: 'auto', padding: '0 0 8px' } },
178
- `${bom.rows.length} grouped lines from ${bom.path} (revision ${bom.revision.slice(0, 12)}…)`),
179
- createElement('table', { style: TABLE },
180
- createElement('thead', {}, createElement('tr', {},
181
- ...['Refs', 'Qty', 'Value', 'Footprint', 'Notes'].map(label => createElement('th', { key: label, style: TH }, label)))),
182
- createElement('tbody', {},
183
- ...bom.rows.map((row, index) => createElement('tr', { key: `${row.references.join(',')}:${String(index)}` },
184
- createElement('td', { style: TD }, row.references.join(' ')),
185
- createElement('td', { style: TD }, String(row.quantity)),
312
+ {
313
+ style: cardStyle({ padding: 0, overflow: 'hidden', flex: '1 1 0', minHeight: 0 }),
314
+ 'data-hardware-bom': true,
315
+ },
316
+ createElement(
317
+ 'div',
318
+ { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, padding: '8px 10px', borderBottom: `1px solid ${C.border}` } },
319
+ createElement('span', { style: SECTION_TITLE }, `Bill of materials · ${String(bom.rows.length)} lines`),
320
+ createElement('div', { style: { display: 'flex', gap: 4, alignItems: 'center' } },
321
+ createElement('span', { style: pillStyle('muted') }, `${String(total)} placements`),
322
+ createElement('span', { style: pillStyle('muted') }, `rev ${shortRevision(bom.revision)}`),
323
+ ),
324
+ ),
325
+ createElement(
326
+ 'div',
327
+ { style: { flex: '1 1 0', minHeight: 0, overflow: 'auto' } },
328
+ createElement(
329
+ 'table',
330
+ { style: TABLE },
331
+ createElement('thead', {}, createElement('tr', {},
332
+ ...['Refs', 'Qty', 'Value', 'Footprint', 'Notes'].map(label =>
333
+ createElement('th', { key: label, style: TH }, label)))),
334
+ createElement('tbody', {},
335
+ ...bom.rows.map((row, index) => createElement('tr', {
336
+ key: `${row.references.join(',')}:${String(index)}`,
337
+ style: index % 2 === 1 ? { background: 'color-mix(in srgb, currentColor 3%, transparent)' } : undefined,
338
+ },
339
+ createElement('td', { style: { ...TD, ...MONO_TEXT, whiteSpace: 'nowrap' } }, row.references.join(' ')),
340
+ createElement('td', { style: { ...TD, ...MONO_TEXT } }, String(row.quantity)),
186
341
  createElement('td', { style: TD }, row.value),
187
- createElement('td', { style: TD }, row.footprint),
188
- createElement('td', { style: TD },
342
+ createElement('td', { style: { ...TD, color: C.text2, fontFamily: MONO, fontSize: 10 } }, row.footprint),
343
+ createElement('td', { style: { ...TD, color: C.text3, fontSize: 10 } },
189
344
  [row.dnp ? 'DNP' : '', ...Object.entries(row.fields).map(([key, value]) => `${key}=${value}`)]
190
345
  .filter(Boolean).join(' · ')),
191
- ))),
346
+ ))),
347
+ ),
192
348
  ),
193
349
  )
194
350
  }
195
351
 
196
- /**
197
- * Order Placing entry points.
198
- *
199
- * The UI never constructs a quote/order URL or a second backend: it shows the
200
- * exact existing agent tool call for the selected board so the purchase path
201
- * still runs through the tool's own confirmation flow.
202
- */
203
- function OrderStage({ boardPath, schematicPath }: { boardPath: string; schematicPath?: string }): ReactNode {
204
- const [lines, setLines] = useState<number>()
205
- useEffect(() => {
206
- if (schematicPath === undefined) return
207
- const controller = new AbortController()
208
- let live = true
209
- loadBom(schematicPath, controller.signal)
210
- .then((bom) => { if (live) setLines(bom.rows.length) })
211
- .catch(() => { if (live) setLines(undefined) })
212
- return () => { live = false; controller.abort() }
213
- }, [schematicPath])
214
-
215
- const calls = [
216
- { tool: 'quote_pcb', args: { pcbPath: boardPath } },
217
- { tool: 'place_pcb_order', args: { pcbPath: boardPath } },
218
- { tool: 'quote_smt', args: { pcbPath: boardPath } },
219
- { tool: 'place_smt_order', args: { pcbPath: boardPath } },
220
- ]
221
- return createElement(
222
- 'div',
223
- { style: { ...STAGE, overflow: 'auto', padding: 10, display: 'flex', flexDirection: 'column', gap: 8 }, 'data-hardware-order': true },
224
- createElement('p', { style: { ...MESSAGE, height: 'auto', padding: 0, justifyContent: 'flex-start' } },
225
- `Selected board: ${boardPath}`),
226
- createElement('p', { style: { ...MESSAGE, height: 'auto', padding: 0, justifyContent: 'flex-start' } },
227
- lines === undefined ? 'BOM context unavailable.' : `BOM context: ${String(lines)} grouped lines from the selected project.`),
228
- createElement('p', { style: { ...MESSAGE, height: 'auto', padding: 0, justifyContent: 'flex-start' } },
229
- 'Ask the agent to run one of these existing tool calls. Quoting and ordering keep their own confirmation step; this panel never places an order.'),
230
- ...calls.map(call => createElement('div', { key: call.tool },
231
- createElement('p', { style: { margin: '0 0 4px', fontSize: 12, fontWeight: 600 } }, call.tool),
232
- createElement('pre', { style: SCRIPT }, JSON.stringify(call, null, 2)),
233
- )),
234
- )
235
- }
236
-
237
352
  export interface HardwarePreviewProps {
238
353
  readonly destination: HardwareDestination
239
354
  readonly artifact?: HardwareArtifactInfo
240
- readonly projectPath?: string
355
+ /** Selected schematic, used for the BOM/order context. */
356
+ readonly schematicPath?: string
357
+ /** Workspace root of the session on screen; every host read is authorized against it. */
358
+ readonly workspaceRoot?: string
359
+ /** DSH session the panel belongs to; sent with every host read. */
360
+ readonly session?: string
241
361
  /** Monotonic navigation revision; a change disposes the previous mount. */
242
362
  readonly revision: number
243
- readonly notice?: string
363
+ /** Whether a validated KiCad installation exists. */
364
+ readonly configured?: boolean
244
365
  }
245
366
 
246
367
  /** Destination → preview dispatcher. */
247
- export function HardwarePreview({ destination, artifact, projectPath, revision }: HardwarePreviewProps): ReactNode {
368
+ export function HardwarePreview({
369
+ destination, artifact, schematicPath, workspaceRoot, session, revision, configured = false,
370
+ }: HardwarePreviewProps): ReactNode {
248
371
  if (artifact === undefined) return null
249
372
  const key = `${destination}:${artifact.path}:${String(revision)}`
250
373
  if (destination === 'schematic' || destination === 'pcb') {
251
374
  return createElement(EcadStage, {
252
- key, kind: destination === 'schematic' ? 'schematic' : 'pcb', path: artifact.path, revision: artifact.revision,
375
+ key,
376
+ kind: destination === 'schematic' ? 'schematic' : 'pcb',
377
+ path: artifact.path,
378
+ ...(artifact.revision === undefined ? {} : { revision: artifact.revision }),
379
+ ...(session === undefined ? {} : { session }),
380
+ ...(workspaceRoot === undefined ? {} : { cwd: workspaceRoot }),
381
+ })
382
+ }
383
+ if (destination === '3d') {
384
+ return createElement(GlbStage, {
385
+ key, boardPath: artifact.path, ...(workspaceRoot === undefined ? {} : { cwd: workspaceRoot }),
386
+ ...(session === undefined ? {} : { session }), configured,
387
+ })
388
+ }
389
+ if (destination === 'bom') {
390
+ return createElement(BomStage, {
391
+ key,
392
+ schematicPath: artifact.path,
393
+ ...(session === undefined ? {} : { session }),
394
+ ...(workspaceRoot === undefined ? {} : { cwd: workspaceRoot }),
395
+ configured,
253
396
  })
254
397
  }
255
- if (destination === '3d') return createElement(GlbStage, { key, boardPath: artifact.path, cwd: projectPath })
256
- if (destination === 'bom') return createElement(BomStage, { key, schematicPath: artifact.path })
257
- return createElement(OrderStage, {
258
- key, boardPath: artifact.path, ...(projectPath === undefined ? {} : { schematicPath: artifact.sourcePath ?? undefined }),
398
+ return createElement(OrderPanel, {
399
+ key,
400
+ boardPath: artifact.path,
401
+ boardTitle: artifact.title || artifact.path,
402
+ ...(schematicPath === undefined ? {} : { schematicPath }),
403
+ ...(workspaceRoot === undefined ? {} : { workspaceRoot }),
404
+ ...(session === undefined ? {} : { session }),
405
+ configured,
259
406
  })
260
407
  }
@@ -1,12 +1,17 @@
1
1
  /**
2
- * One store handle shared by the navigation, viewer and settings
3
- * registrations. It follows DSH's declared-store contract: a spec (init +
4
- * complete write set) plus a per-scope instance factory. The handle is created
5
- * inside `apply()`, never at module scope, so a plugin reload cannot resurrect
6
- * an old snapshot.
2
+ * Product state handle: a spec (init + complete write set) plus a per-scope
3
+ * instance factory, following DSH's declared-store contract.
4
+ *
5
+ * DSH pins one handle to one slot scope ("one handle, one scope"): the
6
+ * navigation and settings seats are `root`, the right-panel viewer seat is
7
+ * `session`. That is why `apply()` creates two handles — `root` state for the
8
+ * product chrome, `session` state for one viewer — instead of sharing one.
9
+ * Handles are created inside `apply()`, never at module scope, so a plugin
10
+ * reload cannot resurrect an old snapshot.
7
11
  */
8
12
  import type {
9
13
  HardwareActions, HardwareDestination, HardwareState, DiscoveryInfo, InstallationsInfo,
14
+ ViewerNavigationParams,
10
15
  } from './model.js'
11
16
 
12
17
  type Mutable<T> = { -readonly [K in keyof T]: T[K] }
@@ -54,10 +59,24 @@ const mutators = {
54
59
  setWorkspaceRoot(draft: Mutable<HardwareState>, root: string): void {
55
60
  draft.workspaceRoot = root
56
61
  },
57
- setDiscovery(draft: Mutable<HardwareState>, discovery: DiscoveryInfo): void {
62
+ setDiscovery(draft: Mutable<HardwareState>, discovery: DiscoveryInfo, preserveSelection?: boolean): void {
63
+ // A different workspace invalidates every artifact path held from the old
64
+ // one: the host authorizes against the session's workspace, so keeping a
65
+ // selection across that move turns the next read into "artifact is outside
66
+ // the authorized workspace". The session's workspace moving is also
67
+ // user-triggered, so it takes the same reset path as a new project.
68
+ const moved = discovery.cwd !== undefined
69
+ && draft.workspaceRoot !== undefined
70
+ && discovery.cwd !== draft.workspaceRoot
58
71
  draft.discovery = discovery
59
72
  draft.workspaceRoot = discovery.cwd ?? draft.workspaceRoot
60
- adoptProject(draft, discovery.selected)
73
+ // A viewer refresh must not clobber the artifact a navigation just selected:
74
+ // it only learns which project the workspace resolves to.
75
+ if (preserveSelection === true && !moved) {
76
+ if (discovery.selected !== undefined) draft.projectPath = discovery.selected.path
77
+ } else {
78
+ adoptProject(draft, discovery.selected)
79
+ }
61
80
  draft.revision += 1
62
81
  },
63
82
  selectProject(draft: Mutable<HardwareState>, path: string): void {
@@ -74,6 +93,19 @@ const mutators = {
74
93
  }
75
94
  draft.revision += 1
76
95
  },
96
+ applyNavigation(draft: Mutable<HardwareState>, params: ViewerNavigationParams): void {
97
+ draft.activeDestination = params.destination
98
+ if (params.projectPath !== undefined) draft.projectPath = params.projectPath
99
+ if (params.path !== undefined) {
100
+ if (params.destination === 'schematic' || params.destination === 'bom') {
101
+ draft.schematicPath = params.path
102
+ draft.sheetPath = params.path
103
+ } else if (params.destination === 'pcb' || params.destination === '3d' || params.destination === 'order') {
104
+ draft.pcbPath = params.path
105
+ }
106
+ }
107
+ draft.revision += 1
108
+ },
77
109
  setInstallations(draft: Mutable<HardwareState>, installations: InstallationsInfo): void {
78
110
  draft.installations = installations
79
111
  draft.revision += 1
@@ -104,9 +136,10 @@ export function createHardwareStore(): HardwareStoreHandle {
104
136
  actions: {
105
137
  setActive: destination => commit(draft => mutators.setActive(draft, destination)),
106
138
  setWorkspaceRoot: root => commit(draft => { draft.workspaceRoot = root }),
107
- setDiscovery: discovery => commit(draft => mutators.setDiscovery(draft, discovery)),
139
+ setDiscovery: (discovery, preserveSelection) => commit(draft => mutators.setDiscovery(draft, discovery, preserveSelection)),
108
140
  selectProject: path => commit(draft => mutators.selectProject(draft, path)),
109
141
  selectArtifact: (path, kind) => commit(draft => mutators.selectArtifact(draft, path, kind)),
142
+ applyNavigation: params => commit(draft => mutators.applyNavigation(draft, params)),
110
143
  setInstallations: installations => commit(draft => mutators.setInstallations(draft, installations)),
111
144
  setNotice: notice => commit(draft => mutators.setNotice(draft, notice)),
112
145
  refresh: () => commit(draft => mutators.refresh(draft)),