From b823e82911e516d10ac0185d24b85d1076578ab7 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Tue, 10 Dec 2024 14:26:35 +0100 Subject: [PATCH] fix case-sensitivity issues --- docs/2-development/1-known-issues.md | 45 ++++++++++++++++++++-------- js/handlers/context/contextState.js | 22 +++++++++----- js/handlers/context/contextUtils.js | 17 ++++++++--- js/handlers/keywordHandlers.js | 2 +- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/docs/2-development/1-known-issues.md b/docs/2-development/1-known-issues.md index f117d72..d1d7995 100644 --- a/docs/2-development/1-known-issues.md +++ b/docs/2-development/1-known-issues.md @@ -69,30 +69,51 @@ export async function updateSimpleModeState() { ### 1. Duplicate Keywords -**Problem**: Keywords like "Paris Agreement" appearing multiple times with different cases. +**Problem**: Keywords like "Paris Agreement" appearing multiple times with different cases, particularly when switching between modes. -**Root Cause**: Case-sensitive keyword storage causing duplicates. +**Root Cause**: Inconsistent case handling across different parts of the codebase, especially during mode transitions and context handling. -**Solution**: Implemented case-insensitive storage with original case preservation: +**Solution**: Implemented comprehensive case-insensitive handling across all keyword operations: ```javascript -// Store lowercase for comparison -const lowerKeyword = keyword.toLowerCase(); -state.originalMutedKeywords.add(lowerKeyword); - -// Preserve original case for display -const originalCase = ourKeywordsMap.get(lowerKeyword); -if (originalCase) { - state.activeKeywords.add(originalCase); +// Helper function for case-insensitive removal +export function removeKeyword(keyword) { + const lowerKeyword = keyword.toLowerCase(); + for (const activeKeyword of state.activeKeywords) { + if (activeKeyword.toLowerCase() === lowerKeyword) { + state.activeKeywords.delete(activeKeyword); + break; + } + } +} + +// Helper function for case-sensitive addition with deduplication +function addKeywordWithCase(keyword) { + // First remove any existing case variations + removeKeyword(keyword); + // Then add with original case + state.activeKeywords.add(keyword); } ``` +**Implementation Details**: +- Case-insensitive checks using isKeywordActive() +- Case-insensitive removal using removeKeyword() +- Case-preserving addition using addKeywordWithCase() +- Consistent handling across mode switches and context changes + +**Result**: +- No more duplicate keywords with different cases +- Maintains proper keyword counts during mode switches +- Preserves original case for display purposes +- Consistent behavior across all operations + ### 2. Payload Size Issues **Problem**: "413 Payload Too Large" error when sending to Bluesky. **Root Cause**: Duplicate keywords with different cases inflating payload size. -**Solution**: Case-insensitive deduplication before API calls. +**Solution**: Case-insensitive deduplication is now handled consistently across all operations, preventing duplicates from being added in the first place. ## Authentication Issues diff --git a/js/handlers/context/contextState.js b/js/handlers/context/contextState.js index 23d8f86..8c02459 100644 --- a/js/handlers/context/contextState.js +++ b/js/handlers/context/contextState.js @@ -1,7 +1,7 @@ import { state, saveState, getStorageKey } from '../../state.js'; import { renderInterface } from '../../renderer.js'; import { cache } from './contextCache.js'; -import { isKeywordActive } from '../keywordHandlers.js'; +import { isKeywordActive, removeKeyword } from '../keywordHandlers.js'; import { rebuildActiveKeywords, createDebouncedUpdate, @@ -9,6 +9,14 @@ import { notifyKeywordChanges } from './contextUtils.js'; +// Helper function to add keyword with case handling +function addKeywordWithCase(keyword) { + // First remove any existing case variations + removeKeyword(keyword); + // Then add with original case + state.activeKeywords.add(keyword); +} + export async function updateSimpleModeState() { if (!state.authenticated) return; @@ -70,14 +78,14 @@ export async function updateSimpleModeState() { // Add only original muted keywords that aren't already active and weren't manually unchecked for (const keyword of state.originalMutedKeywords) { - if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) { - state.activeKeywords.add(keyword); + if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) { + addKeywordWithCase(keyword); } } // Re-apply unchecked status for (const keyword of uncheckedKeywords) { - state.activeKeywords.delete(keyword); + removeKeyword(keyword); state.manuallyUnchecked.add(keyword); } } @@ -171,14 +179,14 @@ export async function initializeState() { // Add only original muted keywords that aren't already active and weren't manually unchecked for (const keyword of state.originalMutedKeywords) { - if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) { - state.activeKeywords.add(keyword); + if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) { + addKeywordWithCase(keyword); } } // Re-apply unchecked status for (const keyword of Array.from(state.manuallyUnchecked)) { - state.activeKeywords.delete(keyword); + removeKeyword(keyword); } } diff --git a/js/handlers/context/contextUtils.js b/js/handlers/context/contextUtils.js index 0bf43a9..4486eab 100644 --- a/js/handlers/context/contextUtils.js +++ b/js/handlers/context/contextUtils.js @@ -1,4 +1,5 @@ import { state } from '../../state.js'; +import { isKeywordActive, removeKeyword } from '../keywordHandlers.js'; // Helper function to notify keyword changes export function notifyKeywordChanges() { @@ -45,6 +46,14 @@ export function processBatchKeywords(keywords, operation) { processChunk(); } +// Helper function to add keyword with case handling +function addKeywordWithCase(keyword) { + // First remove any existing case variations + removeKeyword(keyword); + // Then add with original case + state.activeKeywords.add(keyword); +} + // Helper function to activate context keywords export function activateContextKeywords(contextId, cache) { const context = state.contextGroups[contextId]; @@ -57,7 +66,7 @@ export function activateContextKeywords(contextId, cache) { processBatchKeywords(keywords, keyword => { // Only activate if not manually unchecked if (!state.manuallyUnchecked.has(keyword)) { - state.activeKeywords.add(keyword); + addKeywordWithCase(keyword); } }); } @@ -78,14 +87,14 @@ export function rebuildActiveKeywords(cache) { // Add only original muted keywords that aren't already active and weren't manually unchecked for (const keyword of state.originalMutedKeywords) { - if (!state.activeKeywords.has(keyword) && !state.manuallyUnchecked.has(keyword)) { - state.activeKeywords.add(keyword); + if (!isKeywordActive(keyword) && !state.manuallyUnchecked.has(keyword)) { + addKeywordWithCase(keyword); } } // Re-apply unchecked status for (const keyword of uncheckedKeywords) { - state.activeKeywords.delete(keyword); + removeKeyword(keyword); state.manuallyUnchecked.add(keyword); } } diff --git a/js/handlers/keywordHandlers.js b/js/handlers/keywordHandlers.js index ec40bca..8b50f75 100644 --- a/js/handlers/keywordHandlers.js +++ b/js/handlers/keywordHandlers.js @@ -116,7 +116,7 @@ export function isKeywordActive(keyword) { } // Helper to remove keyword (case-insensitive) -function removeKeyword(keyword) { +export function removeKeyword(keyword) { const lowerKeyword = keyword.toLowerCase(); for (const activeKeyword of state.activeKeywords) { if (activeKeyword.toLowerCase() === lowerKeyword) {