PCB_agentic_ai / frontend /src /components /ComponentPanel.tsx
mindchainai's picture
AI guide chatbot, simplified toolbar, KiCad 10 import with pours, exact outlines and hole rules
3fa1554 verified
Raw History Blame Contribute Delete
7.37 kB
import { Lock, LockOpen, RotateCcw, RotateCw, Search } from 'lucide-react'
import { useMemo, useState } from 'react'
import type { Component, Design } from '../types/pcb'
import { Badge, EmptyNote, NumberField } from './ui'
interface Props {
design: Design
selectedRef: string | null
disabled: boolean
onSelect: (ref: string) => void
onHighlightNet: (net: string) => void
onPatch: (ref: string, patch: { x?: number; y?: number; rotation?: number; locked?: boolean }) => void
}
const CAT_TONE: Record<string, 'accent' | 'ok' | 'warn' | 'neutral'> = {
mcu: 'accent', sensor: 'ok', power: 'warn', connector: 'neutral', ui: 'neutral', passive: 'neutral', antenna: 'warn',
}
export function ComponentPanel({ design, selectedRef, disabled, onSelect, onHighlightNet, onPatch }: Props) {
const [q, setQ] = useState('')
const list = useMemo(() => {
const t = q.trim().toLowerCase()
const items = design.components.filter((c) => !t || `${c.ref} ${c.value} ${c.name} ${c.package} ${c.role}`.toLowerCase().includes(t))
return items.sort((a, b) => refKey(a.ref).localeCompare(refKey(b.ref)))
}, [design.components, q])
const sel = design.components.find((c) => c.ref === selectedRef) ?? null
return (
<div className="flex h-full min-h-0 flex-col">
<div className="p-2">
<div className="relative">
<Search size={13} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-ink-400" />
<input className="field pl-8" placeholder={`Search ${design.components.length} components`} value={q}
onChange={(e) => setQ(e.target.value)} aria-label="Search components" />
</div>
</div>
{sel && <Inspector comp={sel} disabled={disabled} onPatch={onPatch} onHighlightNet={onHighlightNet} />}
<ul className="min-h-0 flex-1 overflow-y-auto px-1 pb-2" aria-label="Components">
{list.map((c) => (
<li key={c.ref}>
<button type="button" onClick={() => onSelect(c.ref)}
className={`flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-[12.5px] hover:bg-ink-800 ${c.ref === selectedRef ? 'bg-accent/10 ring-1 ring-accent/40' : ''}`}>
<span className="w-10 shrink-0 font-mono text-ink-100">{c.ref}</span>
<span className="min-w-0 flex-1 truncate text-ink-200" title={c.role || c.name}>{c.value || c.name}</span>
<span className="hidden shrink-0 truncate text-[11px] text-ink-500 xl:inline">{shortPkg(c.package)}</span>
{c.locked && <Lock size={11} className="shrink-0 text-warn" />}
</button>
</li>
))}
{!list.length && <EmptyNote title="No matching components" />}
</ul>
</div>
)
}
function Inspector({ comp: c, disabled, onPatch, onHighlightNet }: {
comp: Component
disabled: boolean
onPatch: Props['onPatch']
onHighlightNet: (n: string) => void
}) {
const pins = c.pads.filter((p) => !p.number.startsWith('~cu')) // footprint copper shapes are not pins
const connected = pins.filter((p) => p.net)
return (
<div className="mx-2 mb-2 rounded-lg border border-ink-600 bg-ink-800/70 p-2.5">
<div className="flex items-start gap-2">
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span className="font-mono text-[15px] font-semibold text-white">{c.ref}</span>
<Badge tone={CAT_TONE[c.category] ?? 'neutral'}>{c.category}</Badge>
{c.edge && <Badge>edge: {c.edge}</Badge>}
{c.side === 'bottom' && <Badge tone="warn">bottom</Badge>}
</div>
<div className="truncate text-[12.5px] text-ink-200" title={c.description}>{c.name}</div>
<div className="truncate text-[11.5px] text-ink-400">{c.value}{c.mpn ? ` · ${c.mpn}` : ''}</div>
<div className="truncate text-[11.5px] text-ink-400" title={c.fp_lib_id ?? c.package}>{c.fp_lib_id ?? c.package}</div>
{c.role && <div className="mt-0.5 text-[11.5px] text-accent/90">{c.role}</div>}
</div>
</div>
<div className="mt-2 grid grid-cols-2 gap-2">
<NumberField label="X" unit="mm" value={round(c.x)} step={design_step} disabled={disabled || c.locked}
onChange={(v) => v !== null && Math.abs(v - c.x) > 1e-6 && onPatch(c.ref, { x: v })} />
<NumberField label="Y" unit="mm" value={round(c.y)} step={design_step} disabled={disabled || c.locked}
onChange={(v) => v !== null && Math.abs(v - c.y) > 1e-6 && onPatch(c.ref, { y: v })} />
</div>
<div className="mt-2 flex items-center gap-1.5">
<span className="text-[11px] uppercase tracking-wide text-ink-400">Rotation</span>
<span className="w-12 font-mono text-[12.5px] text-ink-100">{Number.isInteger(c.rotation) ? c.rotation : c.rotation.toFixed(1)}°</span>
<button type="button" className="btn-ghost px-2 py-1" title="Rotate 90° counter-clockwise (R)" disabled={disabled || c.locked}
onClick={() => onPatch(c.ref, { rotation: (c.rotation + 90) % 360 })}><RotateCcw size={13} /></button>
<button type="button" className="btn-ghost px-2 py-1" title="Rotate 90° clockwise" disabled={disabled || c.locked}
onClick={() => onPatch(c.ref, { rotation: (c.rotation + 270) % 360 })}><RotateCw size={13} /></button>
<span className="flex-1" />
<button type="button" className={`btn-ghost px-2 py-1 ${c.locked ? 'border-warn/50 text-warn' : ''}`} disabled={disabled}
title={c.locked ? 'Unlock (L)' : 'Lock position (L) — placement and optimisation will not move it'}
onClick={() => onPatch(c.ref, { locked: !c.locked })}>
{c.locked ? <Lock size={13} /> : <LockOpen size={13} />} {c.locked ? 'Locked' : 'Lock'}
</button>
</div>
<details className="mt-2" open={pins.length <= 12}>
<summary className="cursor-pointer select-none text-[11px] uppercase tracking-wide text-ink-400 hover:text-ink-200">
Pins ({connected.length}/{pins.length} connected)
</summary>
<table className="mt-1 w-full text-[11.5px]">
<tbody>
{pins.map((p) => (
<tr key={p.number} className="border-t border-ink-700/60">
<td className="py-0.5 pr-2 font-mono text-ink-300">{p.number}</td>
<td className="py-0.5 pr-2 text-ink-200">{p.name || '—'}</td>
<td className="py-0.5 text-right">
{p.net ? (
<button type="button" className="font-mono text-accent hover:underline" onClick={() => onHighlightNet(p.net!)}>{p.net}</button>
) : (
<span className="text-ink-500">{p.pin_type === 'nc' ? 'NC' : 'unused'}</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</details>
</div>
)
}
const design_step = 0.25
const round = (v: number) => Math.round(v * 1000) / 1000
function refKey(ref: string) {
const m = /^([A-Za-z]+)(\d+)$/.exec(ref)
return m ? `${m[1]}${m[2].padStart(4, '0')}` : ref
}
function shortPkg(p: string) {
return p.replace(/_\d+(\.\d+)?x\d+(\.\d+)?mm/, '').replace(/_P\d.*$/, '').slice(0, 18)
}