perf(ui): fix O(n²) in liveSegmentChars and selectedTokenTotal, add i18n + SSR guard

Addresses bot review feedback on commit 224cab6.

## Performance: liveSegmentChars O(n²) → O(n)

The memo had three inner loops scanning all props.segments per unique
messageId. Added a single O(n) pre-pass building a
segmentsByMessageId Map, then replaced all three inner loops with
map lookups. Total complexity: O(n) instead of O(m×n).

File: packages/ui/src/components/message-timeline.tsx

## Performance: selectedTokenTotal O(n²) → O(n)

For each selected messageId, the memo scanned all segments to sum
chars. On "Select all" this was O(selected × segments). Now builds a
charsByMessageId map in one O(n) pass and does O(1) lookups per
selected message. Same pattern as aggregateTokensByMessageId.

File: packages/ui/src/components/message-section.tsx

## SSR guard: resize listener

window.addEventListener("resize", computeBadgeLayout) lacked a
typeof window !== "undefined" guard. Other window usage in the file
was guarded. Wrapped the addEventListener, requestAnimationFrame, and
onCleanup block in the guard.

File: packages/ui/src/components/message-timeline.tsx

## i18n: mirror selectionHint key in 5 locale files

messageSection.bulkDelete.selectionHint was only defined in
en/messaging.ts. Added the key (English string, since Ctrl/Shift/Esc
are universal keyboard labels) to es, fr, ja, ru, and zh-Hans.

Files: packages/ui/src/lib/i18n/messages/{es,fr,ja,ru,zh-Hans}/messaging.ts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
VooDisss
2026-03-02 17:46:51 +02:00
parent 224cab6a42
commit 2c27fc53ad
7 changed files with 40 additions and 30 deletions

View File

@@ -325,14 +325,15 @@ export default function MessageSection(props: MessageSectionProps) {
const selectedTokenTotal = createMemo(() => {
const selected = selectedForDeletion()
if (selected.size === 0) return 0
const segments = timelineSegments()
// O(n) pre-pass: aggregate chars by messageId once.
const charsByMessageId: Record<string, number> = {}
for (const seg of timelineSegments()) {
charsByMessageId[seg.messageId] = (charsByMessageId[seg.messageId] ?? 0) + seg.totalChars
}
// O(selected.size) lookup pass.
let total = 0
for (const messageId of selected) {
let charTotal = 0
for (const seg of segments) {
if (seg.messageId === messageId) charTotal += seg.totalChars
}
total += Math.max(Math.round(charTotal / 4), 1)
total += Math.max(Math.round((charsByMessageId[messageId] ?? 0) / 4), 1)
}
return total
})