Skip to content

Commit 38c75fe

Browse files
committed
fix(web): resolve waveform lint errors at their source
- start timing-cursor drags from an inline handler instead of creating a ref-writing closure during render - derive the timeline viewport from the captured range during render instead of calling setState in a layout effect - drop unused imports, variables and the unused exportToCsv packets parameter; use const where values are never reassigned - move formatFrequency out of the component module so fast refresh works
1 parent cdd46fb commit 38c75fe

7 files changed

Lines changed: 31 additions & 42 deletions

File tree

‎apps/vds-web/src/features/waveform/TimingCursors.tsx‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ interface TimingCursorsProps {
1212
width: number
1313
}
1414

15-
export function formatFrequency(deltaNs: number): string {
16-
if (deltaNs <= 0) return '—'
17-
const freqHz = 1e9 / deltaNs
18-
19-
if (freqHz >= 1e9 - 1e-3) return `${(freqHz / 1e9).toFixed(2)} GHz`
20-
if (freqHz >= 1e6 - 1e-3) return `${(freqHz / 1e6).toFixed(2)} MHz`
21-
if (freqHz >= 1e3 - 1e-3) return `${(freqHz / 1e3).toFixed(2)} kHz`
22-
return `${freqHz.toFixed(1)} Hz`
23-
}
24-
2515
export function TimingCursors({
2616
cursorANs,
2717
cursorBNs,
@@ -34,7 +24,7 @@ export function TimingCursors({
3424
}: TimingCursorsProps) {
3525
const draggingRef = useRef<'A' | 'B' | null>(null)
3626

37-
const handlePointerDown = (cursor: 'A' | 'B') => (e: React.PointerEvent) => {
27+
const beginDrag = (cursor: 'A' | 'B', e: React.PointerEvent) => {
3828
e.stopPropagation()
3929
e.preventDefault()
4030
draggingRef.current = cursor
@@ -102,7 +92,7 @@ export function TimingCursors({
10292
{/* Flag handle at top */}
10393
<g
10494
className="cursor-flag cursor-flag-a"
105-
onPointerDown={handlePointerDown('A')}
95+
onPointerDown={(event) => beginDrag('A', event)}
10696
style={{ cursor: 'ew-resize' }}
10797
transform={`translate(${xA}, 4)`}
10898
>
@@ -149,7 +139,7 @@ export function TimingCursors({
149139
{/* Flag handle at top */}
150140
<g
151141
className="cursor-flag cursor-flag-b"
152-
onPointerDown={handlePointerDown('B')}
142+
onPointerDown={(event) => beginDrag('B', event)}
153143
style={{ cursor: 'ew-resize' }}
154144
transform={`translate(${xB}, 4)`}
155145
>

‎apps/vds-web/src/features/waveform/WaveformPage.tsx‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {
22
Activity,
33
Circle,
4-
Clock,
54
Download,
65
FileCode,
76
FileSpreadsheet,
8-
FileText,
97
Pause,
108
RotateCcw,
119
SlidersHorizontal,
@@ -14,16 +12,15 @@ import {
1412
import { useEffect, useMemo, useState } from 'react'
1513
import { useSearchParams } from 'react-router-dom'
1614

17-
import { useAdapters, useDevices } from '../../api/queries'
15+
import { useDevices } from '../../api/queries'
1816
import { AsyncState } from '../../components/AsyncState'
1917
import { GlassPanel } from '../../components/GlassPanel'
2018
import { StatusBadge } from '../../components/StatusBadge'
2119
import { useEventStore } from '../../stores/eventStore'
2220
import { formatVirtualTime } from '../../utils/format'
23-
import { buildLiveTransactions, type LiveTransaction } from '../transactions/transactionModel'
21+
import { buildLiveTransactions } from '../transactions/transactionModel'
2422
import { ChannelList } from './ChannelList'
25-
import { formatFrequency } from './TimingCursors'
26-
import type { WaveformChannel } from './types'
23+
import { formatFrequency } from './frequency'
2724
import { downloadFile, exportToCsv, exportToJson, exportToVcd } from './waveformExport'
2825
import { synthesizeWaveforms } from './waveformSynthesizer'
2926
import { WaveformTimeline } from './WaveformTimeline'
@@ -35,7 +32,6 @@ export function WaveformPage() {
3532
const initialDevice = searchParams.get('device') ?? 'all'
3633

3734
const devices = useDevices()
38-
const adapters = useAdapters()
3935
const connectionStatus = useEventStore((state) => state.connectionStatus)
4036
const events = useEventStore((state) => state.events)
4137
const lastEventId = useEventStore((state) => state.lastEventId)
@@ -139,7 +135,7 @@ export function WaveformPage() {
139135
}
140136

141137
const handleExportCsv = () => {
142-
const csv = exportToCsv(channels, allPackets)
138+
const csv = exportToCsv(channels)
143139
downloadFile(csv, `vds4e-waveform-${Date.now()}.csv`, 'text/csv')
144140
}
145141

‎apps/vds-web/src/features/waveform/WaveformTimeline.tsx‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ export function WaveformTimeline({
6262
return () => obs.disconnect()
6363
}, [])
6464

65-
// Sync initial view if totalDuration changes significantly
66-
useLayoutEffect(() => {
65+
// Reset the viewport when the captured time range changes. Adjusting state
66+
// during render (instead of in an effect) avoids a cascading extra render.
67+
const [syncedRange, setSyncedRange] = useState({ minTimeNs, maxTimeNs })
68+
if (syncedRange.minTimeNs !== minTimeNs || syncedRange.maxTimeNs !== maxTimeNs) {
69+
setSyncedRange({ minTimeNs, maxTimeNs })
6770
setViewStartNs(minTimeNs)
6871
setViewEndNs(Math.max(maxTimeNs, minTimeNs + 1_000))
69-
}, [minTimeNs, maxTimeNs])
72+
}
7073

7174
const traceAreaWidth = Math.max(containerWidth - CHANNEL_LABEL_WIDTH, 200)
7275
const visibleDurationNs = Math.max(viewEndNs - viewStartNs, 10)
@@ -357,11 +360,10 @@ export function WaveformTimeline({
357360
let pathStr = ''
358361
if (channel.samples.length > 0) {
359362
// Find initial value
360-
let currentVal = channel.samples[0].value
361-
let currentY = currentVal === 1 ? highY : lowY
362-
let currentX = Math.max(CHANNEL_LABEL_WIDTH, timeToX(channel.samples[0].timeNs))
363+
let currentY = channel.samples[0].value === 1 ? highY : lowY
364+
const startX = Math.max(CHANNEL_LABEL_WIDTH, timeToX(channel.samples[0].timeNs))
363365

364-
pathStr = `M ${currentX} ${currentY}`
366+
pathStr = `M ${startX} ${currentY}`
365367

366368
for (let i = 1; i < channel.samples.length; i++) {
367369
const s = channel.samples[i]
@@ -375,9 +377,7 @@ export function WaveformTimeline({
375377
pathStr += ` L ${nextX} ${nextY}`
376378
}
377379

378-
currentX = nextX
379380
currentY = nextY
380-
currentVal = s.value
381381
}
382382

383383
// Extend to right edge of trace area
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function formatFrequency(deltaNs: number): string {
2+
if (deltaNs <= 0) return '—'
3+
const freqHz = 1e9 / deltaNs
4+
5+
if (freqHz >= 1e9 - 1e-3) return `${(freqHz / 1e9).toFixed(2)} GHz`
6+
if (freqHz >= 1e6 - 1e-3) return `${(freqHz / 1e6).toFixed(2)} MHz`
7+
if (freqHz >= 1e3 - 1e-3) return `${(freqHz / 1e3).toFixed(2)} kHz`
8+
return `${freqHz.toFixed(1)} Hz`
9+
}

‎apps/vds-web/src/features/waveform/waveformExport.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ export function exportToVcd(
8989
return vcd
9090
}
9191

92-
export function exportToCsv(
93-
channels: WaveformChannel[],
94-
packets: ProtocolPacket[],
95-
): string {
92+
export function exportToCsv(channels: WaveformChannel[]): string {
9693
const lines: string[] = []
9794
lines.push('Timestamp_ns,Bus,Device,Channel,Signal_State,Decoded_Label,Decoded_Detail')
9895

‎apps/vds-web/src/features/waveform/waveformModel.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22
import type { LiveTransaction } from '../transactions/transactionModel'
3-
import { formatFrequency } from './TimingCursors'
3+
import { formatFrequency } from './frequency'
44
import { exportToCsv, exportToJson, exportToVcd } from './waveformExport'
55
import { synthesizeWaveforms } from './waveformSynthesizer'
66

@@ -129,8 +129,8 @@ describe('waveformExport', () => {
129129

130130
it('generates CSV export with timestamps and decoded labels', () => {
131131
const txn = createTxn('i2c', [0x50], [0x12])
132-
const { channels, allPackets } = synthesizeWaveforms([txn])
133-
const csv = exportToCsv(channels, allPackets)
132+
const { channels } = synthesizeWaveforms([txn])
133+
const csv = exportToCsv(channels)
134134

135135
expect(csv).toContain('Timestamp_ns,Bus,Device,Channel,Signal_State,Decoded_Label,Decoded_Detail')
136136
expect(csv).toContain('I2C')

‎apps/vds-web/src/features/waveform/waveformSynthesizer.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
DigitalLogicLevel,
44
ProtocolPacket,
55
WaveformChannel,
6-
WaveformSample,
76
} from './types'
87

98
function hex(byte: number): string {
@@ -147,7 +146,6 @@ export function synthesizeWaveforms(transactions: LiveTransaction[]): {
147146
)
148147

149148
const halfClockNs = 100 // 5 MHz clock
150-
const clockPeriodNs = halfClockNs * 2
151149
let curT = t0
152150

153151
// CS goes LOW (Assert)
@@ -453,7 +451,7 @@ export function synthesizeWaveforms(transactions: LiveTransaction[]): {
453451
)
454452

455453
const bitDurationNs = 8_680 // 115200 baud
456-
let curT = t0
454+
const curT = t0
457455

458456
const emitUartBytes = (
459457
bytes: number[],
@@ -462,7 +460,6 @@ export function synthesizeWaveforms(transactions: LiveTransaction[]): {
462460
) => {
463461
let t = curT
464462
bytes.forEach((byte, idx) => {
465-
const frameStart = t
466463
// Start bit (0)
467464
addSample(chan, t, 0)
468465
addPacket(chan, {
@@ -737,7 +734,7 @@ export function synthesizeWaveforms(transactions: LiveTransaction[]): {
737734
}
738735

739736
const pulseDurationNs = 1_000
740-
let curT = t0
737+
const curT = t0
741738

742739
Array.from(activeLines).sort((a, b) => a - b).forEach((line) => {
743740
const color = GPIO_COLORS[line % GPIO_COLORS.length]

0 commit comments

Comments
 (0)