@huaqiu/dsh-hardware-ui 0.7.20 → 0.7.22

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.
@@ -0,0 +1,275 @@
1
+ /**
2
+ * The Place Order view: pick a KiCad project, then pick the operation.
3
+ *
4
+ * This is the plugin-owned entry point the desktop menu and the user both reach
5
+ * without an agent message. It does exactly two things — list what the Host
6
+ * discovered in the current workspace, and hand one project to the existing
7
+ * order destination — because the manufacturing work already exists: packaging,
8
+ * validation, the upload, the token and the page are the same code the agent's
9
+ * tools and the order panel use. Nothing here prices, packages or scans on its
10
+ * own; `discover()` is the Host's deterministic scan and every row is rendered
11
+ * from its answer.
12
+ *
13
+ * Readiness is shown, not guessed: PCB needs a saved board, PCBA additionally
14
+ * needs the project schematic the positions and BOM are exported from, and both
15
+ * need a configured KiCad and a signed-in account. An unavailable operation keeps
16
+ * its button visible but disabled with the reason next to it.
17
+ */
18
+ import { createElement, useEffect, useState, type CSSProperties, type ReactNode } from 'react'
19
+ import type { HardwareArtifactInfo, HardwareProjectInfo, HardwareState } from './model.js'
20
+ import { COPY } from './copy.js'
21
+ import { messageOf, loadOrderConfig, type OrderConfigInfo } from './transport.js'
22
+ import { buttonStyle, C, cardStyle, disabled, dotStyle, MUTED, MONO, pillStyle, R, SECTION_TITLE } from './theme.js'
23
+
24
+ type UseStore = <S>(selector: (state: HardwareState) => S) => S
25
+
26
+ export interface PlaceOrderProps {
27
+ readonly useStore?: UseStore
28
+ readonly sessionId?: string
29
+ readonly useSessions?: (selector: (state: {
30
+ readonly ids: readonly string[]
31
+ readonly byId: Readonly<Record<string, { readonly cwd?: string }>>
32
+ }) => string | undefined) => string | undefined
33
+ readonly discover: (cwd?: string, project?: string, session?: string) => Promise<{
34
+ readonly cwd: string
35
+ readonly projects?: readonly HardwareProjectInfo[]
36
+ readonly issue?: { readonly code: string; readonly message: string }
37
+ }>
38
+ readonly installations: () => Promise<{ readonly kicad?: { readonly configured: boolean } }>
39
+ readonly orderProject: (request: {
40
+ readonly projectPath: string
41
+ readonly boardPath: string
42
+ readonly schematicPath?: string
43
+ readonly kind: 'pcb' | 'smt'
44
+ }) => void
45
+ }
46
+
47
+ const ROOT: CSSProperties = {
48
+ display: 'flex', flexDirection: 'column', height: '100%', minHeight: 0, minWidth: 0,
49
+ boxSizing: 'border-box', background: C.base, color: C.text,
50
+ }
51
+ const HEAD: CSSProperties = {
52
+ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap',
53
+ padding: '8px 12px', borderBottom: `1px solid ${C.borderSoft}`, flex: '0 0 auto',
54
+ }
55
+ const BODY: CSSProperties = {
56
+ flex: '1 1 0', minHeight: 0, overflow: 'auto', padding: 12,
57
+ display: 'flex', flexDirection: 'column', gap: 8,
58
+ }
59
+ const ROW: CSSProperties = {
60
+ display: 'flex', alignItems: 'center', gap: 8, minWidth: 0,
61
+ }
62
+ const NAME: CSSProperties = { fontSize: 12.5, fontWeight: 600, color: C.text, minWidth: 0 }
63
+ const SUB: CSSProperties = { ...MUTED, fontFamily: MONO, fontSize: 10.5, wordBreak: 'break-all' }
64
+ const CHIP: CSSProperties = { ...pillStyle('muted'), fontSize: 9.5, padding: '0 5px', whiteSpace: 'nowrap' }
65
+ const REASON: CSSProperties = { ...MUTED, fontSize: 10, maxWidth: 190, lineHeight: 1.45 }
66
+
67
+ /** Boardless-safe artifact summary for one row. */
68
+ function artifactsLine(project: HardwareProjectInfo): string {
69
+ const boards = project.boards?.length ?? 0
70
+ const board = boards === 0 ? undefined : (project.defaultBoard ?? project.boards?.[0]?.path)
71
+ const boardName = board === undefined ? undefined : board.slice(Math.max(board.lastIndexOf('\\'), board.lastIndexOf('/')) + 1)
72
+ const schematic = project.schematic?.path
73
+ const schematicName = schematic === undefined ? undefined : schematic.slice(Math.max(schematic.lastIndexOf('\\'), schematic.lastIndexOf('/')) + 1)
74
+ return [
75
+ COPY.order.placeOrder.boardCount(boards),
76
+ boardName ?? COPY.order.placeOrder.noBoard,
77
+ schematicName ?? COPY.order.placeOrder.noSchematic,
78
+ ].join(' · ')
79
+ }
80
+
81
+ /** Path of the project directory relative to the scanned workspace. */
82
+ function relativePath(root: string, path: string): string {
83
+ const normalizedRoot = root.replace(/[\\/]+$/u, '')
84
+ if (!path.startsWith(normalizedRoot)) return path
85
+ return path.slice(normalizedRoot.length).replace(/^[\\/]+/u, '') || '.'
86
+ }
87
+
88
+ export function PlaceOrderBody(props: PlaceOrderProps): ReactNode {
89
+ const { discover, installations, orderProject, sessionId, useSessions } = props
90
+ const cwd = useSessions?.(state => {
91
+ if (sessionId === undefined) return undefined
92
+ return state.byId[sessionId]?.cwd
93
+ })
94
+ const [root, setRoot] = useState<string>()
95
+ const [projects, setProjects] = useState<readonly HardwareProjectInfo[]>()
96
+ const [issue, setIssue] = useState<{ code: string; message: string }>()
97
+ const [error, setError] = useState<string>()
98
+ const [scanning, setScanning] = useState(false)
99
+ const [kicadReady, setKicadReady] = useState<boolean>()
100
+ const [config, setConfig] = useState<OrderConfigInfo>()
101
+ const [revision, setRevision] = useState(0)
102
+
103
+ useEffect(() => {
104
+ let live = true
105
+ setScanning(true)
106
+ setError(undefined)
107
+ discover(cwd, undefined, sessionId)
108
+ .then((result) => {
109
+ if (!live) return
110
+ setRoot(result.cwd)
111
+ setProjects(result.projects ?? [])
112
+ setIssue(result.issue)
113
+ })
114
+ .catch((failure: unknown) => {
115
+ if (!live) return
116
+ setProjects([])
117
+ setError(messageOf(failure))
118
+ })
119
+ .finally(() => { if (live) setScanning(false) })
120
+ return () => { live = false }
121
+ }, [cwd, sessionId, discover, revision])
122
+
123
+ useEffect(() => {
124
+ let live = true
125
+ installations()
126
+ .then((result) => { if (live) setKicadReady(result.kicad?.configured === true) })
127
+ .catch(() => { if (live) setKicadReady(false) })
128
+ loadOrderConfig()
129
+ .then((value) => { if (live) setConfig(value) })
130
+ .catch(() => { if (live) setConfig(undefined) })
131
+ return () => { live = false }
132
+ }, [installations])
133
+
134
+ const signedIn = config?.hasToken
135
+ const scanningNow = scanning
136
+
137
+ /** Why one operation is unavailable for this row, in priority order. */
138
+ const blockedReason = (kind: 'pcb' | 'pcba', project: HardwareProjectInfo): string | undefined => {
139
+ if (kind === 'pcb') {
140
+ if ((project.boards?.length ?? 0) === 0) return COPY.order.placeOrder.noBoard
141
+ } else {
142
+ if ((project.boards?.length ?? 0) === 0) return COPY.order.placeOrder.noBoard
143
+ if (project.schematic === undefined) return COPY.order.placeOrder.noSchematic
144
+ }
145
+ if (kicadReady === false) return COPY.order.placeOrder.needsKicad
146
+ if (signedIn === false) return COPY.order.placeOrder.needsSignIn
147
+ return undefined
148
+ }
149
+
150
+ const order = (kind: 'pcb' | 'pcba', project: HardwareProjectInfo): void => {
151
+ const boards = project.boards ?? []
152
+ const board: HardwareArtifactInfo | undefined = project.defaultBoard === undefined
153
+ ? boards[0]
154
+ : boards.find(candidate => candidate.path === project.defaultBoard) ?? boards[0]
155
+ if (board === undefined) return
156
+ orderProject({
157
+ projectPath: project.path,
158
+ boardPath: board.path,
159
+ ...(project.schematic === undefined ? {} : { schematicPath: project.schematic.path }),
160
+ kind: kind === 'pcb' ? 'pcb' : 'smt',
161
+ })
162
+ }
163
+
164
+ const row = (project: HardwareProjectInfo): ReactNode => {
165
+ const pcbReason = blockedReason('pcb', project)
166
+ const pcbaReason = blockedReason('pcba', project)
167
+ return createElement(
168
+ 'div',
169
+ {
170
+ key: project.path,
171
+ style: cardStyle({ gap: 6, padding: 10 }),
172
+ 'data-hardware-place-order-project': project.path,
173
+ 'data-hardware-place-order-pcb': pcbReason === undefined ? 'ready' : 'blocked',
174
+ 'data-hardware-place-order-pcba': pcbaReason === undefined ? 'ready' : 'blocked',
175
+ },
176
+ createElement(
177
+ 'div',
178
+ { style: { ...ROW, justifyContent: 'space-between', alignItems: 'flex-start' } },
179
+ createElement(
180
+ 'div',
181
+ { style: { display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0 } },
182
+ createElement('span', { style: NAME }, project.name),
183
+ createElement('span', { style: SUB, title: project.path }, relativePath(root ?? '', project.path)),
184
+ ),
185
+ createElement('span', { style: CHIP }, artifactsLine(project)),
186
+ ),
187
+ createElement(
188
+ 'div',
189
+ { style: { ...ROW, alignItems: 'flex-start', flexWrap: 'wrap' } },
190
+ createElement(
191
+ 'div',
192
+ { style: { display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0 } },
193
+ createElement('button', {
194
+ type: 'button',
195
+ style: { ...buttonStyle('primary', { padding: '4px 10px', fontSize: 11 }), ...disabled(pcbReason !== undefined || scanningNow) },
196
+ disabled: pcbReason !== undefined || scanningNow,
197
+ 'data-hardware-place-order-action': 'pcb',
198
+ onClick: () => { order('pcb', project) },
199
+ }, COPY.order.placeOrder.pcbAction),
200
+ ...(pcbReason === undefined ? [] : [createElement('span', { key: 'pcb-why', style: REASON }, pcbReason)]),
201
+ ),
202
+ createElement(
203
+ 'div',
204
+ { style: { display: 'flex', flexDirection: 'column', gap: 2, minWidth: 0 } },
205
+ createElement('button', {
206
+ type: 'button',
207
+ style: { ...buttonStyle('secondary', { padding: '4px 10px', fontSize: 11 }), ...disabled(pcbaReason !== undefined || scanningNow) },
208
+ disabled: pcbaReason !== undefined || scanningNow,
209
+ 'data-hardware-place-order-action': 'pcba',
210
+ onClick: () => { order('pcba', project) },
211
+ }, COPY.order.placeOrder.pcbaAction),
212
+ ...(pcbaReason === undefined ? [] : [createElement('span', { key: 'pcba-why', style: REASON }, pcbaReason)]),
213
+ ),
214
+ ),
215
+ )
216
+ }
217
+
218
+ const header = createElement(
219
+ 'div',
220
+ { style: HEAD },
221
+ createElement('span', { style: { ...SECTION_TITLE, marginRight: 2 } }, COPY.order.placeOrder.title),
222
+ createElement(
223
+ 'span',
224
+ { style: { fontFamily: MONO, fontSize: 10.5, color: C.text3, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: '1 1 120px' }, title: root ?? '' },
225
+ root ?? COPY.order.placeOrder.workspaceUnknown,
226
+ ),
227
+ createElement(
228
+ 'span',
229
+ { style: pillStyle(kicadReady === false ? 'warn' : 'muted'), 'data-hardware-place-order-kicad': kicadReady === false ? 'missing' : kicadReady === true ? 'ok' : 'checking' },
230
+ createElement('span', { style: dotStyle(kicadReady === false ? C.warn : kicadReady === true ? C.ok : C.text3, 6) }),
231
+ kicadReady === false ? COPY.order.placeOrder.needsKicad : kicadReady === true ? COPY.order.placeOrder.kicadReady : COPY.order.placeOrder.checking,
232
+ ),
233
+ createElement(
234
+ 'span',
235
+ { style: pillStyle(signedIn === false ? 'warn' : 'muted'), 'data-hardware-place-order-signin': signedIn === false ? 'missing' : signedIn === true ? 'ok' : 'checking' },
236
+ createElement('span', { style: dotStyle(signedIn === false ? C.warn : signedIn === true ? C.ok : C.text3, 6) }),
237
+ signedIn === false ? COPY.order.placeOrder.needsSignIn : signedIn === true ? COPY.order.placeOrder.signInReady : COPY.order.placeOrder.checking,
238
+ ),
239
+ createElement(
240
+ 'button',
241
+ {
242
+ type: 'button',
243
+ style: { ...buttonStyle('quiet', { padding: '3px 8px', fontSize: 10.5 }), ...disabled(scanningNow) },
244
+ disabled: scanningNow,
245
+ 'data-hardware-place-order-refresh': true,
246
+ onClick: () => { setRevision(value => value + 1) },
247
+ },
248
+ scanningNow ? COPY.order.placeOrder.refreshing : COPY.order.placeOrder.refresh,
249
+ ),
250
+ )
251
+
252
+ const body = createElement(
253
+ 'div',
254
+ { style: BODY },
255
+ createElement('p', { style: MUTED }, COPY.order.placeOrder.lead),
256
+ ...(error === undefined ? [] : [createElement('p', {
257
+ key: 'error', role: 'status',
258
+ style: { ...MUTED, color: C.warn, borderLeft: `2px solid ${C.warn}`, paddingLeft: 8 },
259
+ }, COPY.order.placeOrder.discoveryFailed(error))]),
260
+ ...(scanningNow && projects === undefined
261
+ ? [createElement('p', { key: 'loading', style: MUTED }, COPY.order.placeOrder.loading)]
262
+ : []),
263
+ ...(projects === undefined || scanningNow || error !== undefined || projects.length > 0
264
+ ? []
265
+ : [createElement(
266
+ 'div',
267
+ { key: 'empty', style: cardStyle({ alignItems: 'flex-start', gap: 4 }), 'data-hardware-place-order-empty': issue?.code ?? 'none' },
268
+ createElement('span', { style: pillStyle('warn') }, COPY.order.placeOrder.noProjects),
269
+ createElement('p', { style: MUTED }, COPY.order.placeOrder.noProjectsHint),
270
+ )]),
271
+ ...(projects === undefined ? [] : projects.map(row)),
272
+ )
273
+
274
+ return createElement('div', { style: ROOT, 'data-hardware-place-order': true }, header, body)
275
+ }
@@ -405,11 +405,13 @@ export interface HardwarePreviewProps {
405
405
  readonly revision: number
406
406
  /** Whether a validated KiCad installation exists. */
407
407
  readonly configured?: boolean
408
+ /** Operation the user picked in the Place Order view, if any. */
409
+ readonly orderKind?: 'pcb' | 'smt'
408
410
  }
409
411
 
410
412
  /** Destination → preview dispatcher. */
411
413
  export function HardwarePreview({
412
- destination, artifact, schematicPath, workspaceRoot, session, revision, configured = false,
414
+ destination, artifact, schematicPath, workspaceRoot, session, revision, configured = false, orderKind,
413
415
  }: HardwarePreviewProps): ReactNode {
414
416
  if (artifact === undefined) return null
415
417
  const key = `${destination}:${artifact.path}:${String(revision)}`
@@ -446,5 +448,6 @@ export function HardwarePreview({
446
448
  ...(workspaceRoot === undefined ? {} : { workspaceRoot }),
447
449
  ...(session === undefined ? {} : { session }),
448
450
  configured,
451
+ ...(orderKind === undefined ? {} : { focus: orderKind }),
449
452
  })
450
453
  }
@@ -96,6 +96,9 @@ const mutators = {
96
96
  applyNavigation(draft: Mutable<HardwareState>, params: ViewerNavigationParams): void {
97
97
  draft.activeDestination = params.destination
98
98
  if (params.projectPath !== undefined) draft.projectPath = params.projectPath
99
+ // An explicit schematic wins over whatever the tab was showing: an order
100
+ // carries the project it belongs to, not the previous artifact.
101
+ if (params.schematicPath !== undefined) draft.schematicPath = params.schematicPath
99
102
  if (params.path !== undefined) {
100
103
  if (params.destination === 'schematic' || params.destination === 'bom') {
101
104
  draft.schematicPath = params.path
@@ -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> {