From 7bda58bbf688e28fb1c2cebf6da1b01dc84a1225 Mon Sep 17 00:00:00 2001 From: lucaronin Date: Thu, 7 May 2026 22:59:58 +0200 Subject: [PATCH] fix: clear codacy focus and history highs --- src/components/SortDropdown.tsx | 8 ++++---- src/components/note-list/noteListHooks.ts | 14 +++++++------- .../note-list/useMultiSelectKeyboard.ts | 14 +++++++------- src/utils/ai-chat.ts | 15 +++++++-------- src/utils/streamAiAgent.ts | 4 ++-- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/components/SortDropdown.tsx b/src/components/SortDropdown.tsx index 195cb78d..fbf89ded 100644 --- a/src/components/SortDropdown.tsx +++ b/src/components/SortDropdown.tsx @@ -36,11 +36,11 @@ function buildSortItems(locale: AppLocale, customProperties?: string[]): SortIte } function resolveFocusedIndex(groupLabel: string, current: SortOption, sortItems: SortItem[]) { - const activeElement = document.activeElement - if (activeElement instanceof HTMLElement) { - const activeIndexText = activeElement.dataset.sortItemIndex + const activeHTMLElement = document.activeElement + if (activeHTMLElement instanceof HTMLElement) { + const activeIndexText = activeHTMLElement.dataset.sortItemIndex const activeIndex = activeIndexText === undefined ? -1 : Number(activeIndexText) - if (activeElement.dataset.sortGroupLabel === groupLabel && activeIndex >= 0) return activeIndex + if (activeHTMLElement.dataset.sortGroupLabel === groupLabel && activeIndex >= 0) return activeIndex } const currentIndex = sortItems.findIndex((item) => item.value === current) diff --git a/src/components/note-list/noteListHooks.ts b/src/components/note-list/noteListHooks.ts index 79522c89..5cf5866f 100644 --- a/src/components/note-list/noteListHooks.ts +++ b/src/components/note-list/noteListHooks.ts @@ -400,13 +400,13 @@ export function useNoteListSort({ // --- useMultiSelectKeyboard --- -function isInputFocused(): boolean { - const activeElement = document.activeElement - if (!(activeElement instanceof HTMLElement)) return false +function isInputHtmlElementFocused(): boolean { + const activeHTMLElement = document.activeElement + if (!(activeHTMLElement instanceof HTMLElement)) return false - return activeElement.tagName === 'INPUT' - || activeElement.tagName === 'TEXTAREA' - || activeElement.isContentEditable + return activeHTMLElement.tagName === 'INPUT' + || activeHTMLElement.tagName === 'TEXTAREA' + || activeHTMLElement.isContentEditable } function handleEscapeKey(e: KeyboardEvent, multiSelect: MultiSelectState) { @@ -416,7 +416,7 @@ function handleEscapeKey(e: KeyboardEvent, multiSelect: MultiSelectState) { } function handleSelectAllKey(e: KeyboardEvent, multiSelect: MultiSelectState, isEntityView: boolean) { - if (e.key !== 'a' || !(e.metaKey || e.ctrlKey) || isEntityView || isInputFocused()) return + if (e.key !== 'a' || !(e.metaKey || e.ctrlKey) || isEntityView || isInputHtmlElementFocused()) return e.preventDefault() multiSelect.selectAll() } diff --git a/src/components/note-list/useMultiSelectKeyboard.ts b/src/components/note-list/useMultiSelectKeyboard.ts index d99d431a..68482c70 100644 --- a/src/components/note-list/useMultiSelectKeyboard.ts +++ b/src/components/note-list/useMultiSelectKeyboard.ts @@ -9,13 +9,13 @@ interface UseMultiSelectKeyboardOptions { enableActionShortcuts?: boolean } -function isInputFocused(): boolean { - const activeElement = document.activeElement - if (!(activeElement instanceof HTMLElement)) return false +function isInputHtmlElementFocused(): boolean { + const activeHTMLElement = document.activeElement + if (!(activeHTMLElement instanceof HTMLElement)) return false - return activeElement.tagName === 'INPUT' - || activeElement.tagName === 'TEXTAREA' - || activeElement.isContentEditable + return activeHTMLElement.tagName === 'INPUT' + || activeHTMLElement.tagName === 'TEXTAREA' + || activeHTMLElement.isContentEditable } function usesCommandModifier(event: KeyboardEvent): boolean { @@ -29,7 +29,7 @@ function clearSelectionOnEscape(event: KeyboardEvent, multiSelect: MultiSelectSt } function selectVisibleNotes(event: KeyboardEvent, multiSelect: MultiSelectState, isEntityView: boolean) { - if (event.key !== 'a' || !(event.metaKey || event.ctrlKey) || isEntityView || isInputFocused()) return + if (event.key !== 'a' || !(event.metaKey || event.ctrlKey) || isEntityView || isInputHtmlElementFocused()) return event.preventDefault() multiSelect.selectAll() } diff --git a/src/utils/ai-chat.ts b/src/utils/ai-chat.ts index 958f8f85..db614bd6 100644 --- a/src/utils/ai-chat.ts +++ b/src/utils/ai-chat.ts @@ -71,16 +71,15 @@ const CONVERSATION_HISTORY_CLOSE_MARKER = [''].jo /** Keep the most recent messages that fit within `maxTokens`. Drops oldest first. */ export function trimHistory(history: ChatMessage[], maxTokens: number): ChatMessage[] { let tokenCount = 0 - const result: ChatMessage[] = [] - for (let i = history.length - 1; i >= 0; i--) { - const message = history.at(i) - if (!message) continue + const newestFirst = [...history].reverse() + const keptNewestFirst: ChatMessage[] = [] + for (const message of newestFirst) { const tokens = estimateTokens(message.content) if (tokenCount + tokens > maxTokens) break - result.unshift(message) + keptNewestFirst.push(message) tokenCount += tokens } - return result + return keptNewestFirst.reverse() } /** Format conversation history + new message into a single prompt for the CLI. */ @@ -158,11 +157,11 @@ function handleChatStreamEvent( * can verify that history is actually being sent. */ function mockChatResponse(message: string): string { - if (message.includes(CONVERSATION_HISTORY_OPEN_MARKER)) { + if (message.indexOf(CONVERSATION_HISTORY_OPEN_MARKER) >= 0) { const allUserLines = message.match(/\[user\]: .+/g) ?? [] const turnCount = allUserLines.length // The last [user] line is the actual new message - const lastLine = allUserLines[allUserLines.length - 1] ?? '' + const lastLine = allUserLines.at(-1) ?? '' const lastUserMsg = lastLine.replace('[user]: ', '') return `[mock-with-history turns=${turnCount}] You asked: "${lastUserMsg}"` } diff --git a/src/utils/streamAiAgent.ts b/src/utils/streamAiAgent.ts index e64411c0..4a59f2ad 100644 --- a/src/utils/streamAiAgent.ts +++ b/src/utils/streamAiAgent.ts @@ -36,10 +36,10 @@ const CONVERSATION_HISTORY_OPEN_MARKER = ['<', 'conversation_history', '>'].join function mockAgentResponse(agent: AiAgentId, message: string): string { const agentLabel = getAiAgentDefinition(agent).label - if (message.includes(CONVERSATION_HISTORY_OPEN_MARKER)) { + if (message.indexOf(CONVERSATION_HISTORY_OPEN_MARKER) >= 0) { const allUserLines = message.match(/\[user\]: .+/g) ?? [] const turnCount = allUserLines.length - const lastLine = allUserLines[allUserLines.length - 1] ?? '' + const lastLine = allUserLines.at(-1) ?? '' const lastUserMsg = lastLine.replace('[user]: ', '') return `[mock-${agentLabel.toLowerCase()} turns=${turnCount}] You asked: "${lastUserMsg}" — This note is related to [[Build Laputa App]] and [[Matteo Cellini]].` }