diff --git a/src/components/Bookmarks.tsx b/src/components/Bookmarks.tsx index 46a4db62..1b5d20fb 100644 --- a/src/components/Bookmarks.tsx +++ b/src/components/Bookmarks.tsx @@ -11,6 +11,7 @@ import { useContentSelection } from '../hooks/useContentSelection' import { useHighlightCreation } from '../hooks/useHighlightCreation' import { useBookmarksUI } from '../hooks/useBookmarksUI' import ThreePaneLayout from './ThreePaneLayout' +import { classifyHighlights } from '../utils/highlightClassification' export type ViewMode = 'compact' | 'cards' | 'large' @@ -142,15 +143,7 @@ const Bookmarks: React.FC = ({ relayPool, onLogout }) => { // Classify highlights with levels based on user context const classifiedHighlights = useMemo(() => { - return highlights.map(h => { - let level: 'mine' | 'friends' | 'nostrverse' = 'nostrverse' - if (h.pubkey === activeAccount?.pubkey) { - level = 'mine' - } else if (followedPubkeys.has(h.pubkey)) { - level = 'friends' - } - return { ...h, level } - }) + return classifyHighlights(highlights, activeAccount?.pubkey, followedPubkeys) }, [highlights, activeAccount?.pubkey, followedPubkeys]) return ( diff --git a/src/hooks/useFilteredHighlights.ts b/src/hooks/useFilteredHighlights.ts index b224f939..c069a691 100644 --- a/src/hooks/useFilteredHighlights.ts +++ b/src/hooks/useFilteredHighlights.ts @@ -1,18 +1,8 @@ import { useMemo } from 'react' import { Highlight } from '../types/highlights' import { HighlightVisibility } from '../components/HighlightsPanel' - -/** - * Normalize URL for comparison - */ -function normalizeUrl(url: string): string { - try { - const urlObj = new URL(url.startsWith('http') ? url : `https://${url}`) - return `${urlObj.hostname.replace(/^www\./, '')}${urlObj.pathname}`.replace(/\/$/, '').toLowerCase() - } catch { - return url.replace(/^https?:\/\//, '').replace(/^www\./, '').replace(/\/$/, '').toLowerCase() - } -} +import { normalizeUrl } from '../utils/urlHelpers' +import { classifyHighlights } from '../utils/highlightClassification' interface UseFilteredHighlightsParams { highlights: Highlight[] @@ -48,21 +38,12 @@ export const useFilteredHighlights = ({ } // Classify and filter by visibility levels - return urlFiltered - .map(h => { - let level: 'mine' | 'friends' | 'nostrverse' = 'nostrverse' - if (h.pubkey === currentUserPubkey) { - level = 'mine' - } else if (followedPubkeys.has(h.pubkey)) { - level = 'friends' - } - return { ...h, level } - }) - .filter(h => { - if (h.level === 'mine') return highlightVisibility.mine - if (h.level === 'friends') return highlightVisibility.friends - return highlightVisibility.nostrverse - }) + const classified = classifyHighlights(urlFiltered, currentUserPubkey, followedPubkeys) + return classified.filter(h => { + if (h.level === 'mine') return highlightVisibility.mine + if (h.level === 'friends') return highlightVisibility.friends + return highlightVisibility.nostrverse + }) }, [highlights, selectedUrl, highlightVisibility, currentUserPubkey, followedPubkeys]) } diff --git a/src/hooks/useHighlightedContent.ts b/src/hooks/useHighlightedContent.ts index ec375bd7..c24a85c6 100644 --- a/src/hooks/useHighlightedContent.ts +++ b/src/hooks/useHighlightedContent.ts @@ -3,6 +3,7 @@ import { Highlight } from '../types/highlights' import { applyHighlightsToHTML } from '../utils/highlightMatching' import { filterHighlightsByUrl } from '../utils/urlHelpers' import { HighlightVisibility } from '../components/HighlightsPanel' +import { classifyHighlights } from '../utils/highlightClassification' interface UseHighlightedContentParams { html?: string @@ -41,21 +42,12 @@ export const useHighlightedContent = ({ console.log('📌 URL filtered highlights:', urlFiltered.length) // Apply visibility filtering - const filtered = urlFiltered - .map(h => { - let level: 'mine' | 'friends' | 'nostrverse' = 'nostrverse' - if (h.pubkey === currentUserPubkey) { - level = 'mine' - } else if (followedPubkeys.has(h.pubkey)) { - level = 'friends' - } - return { ...h, level } - }) - .filter(h => { - if (h.level === 'mine') return highlightVisibility.mine - if (h.level === 'friends') return highlightVisibility.friends - return highlightVisibility.nostrverse - }) + const classified = classifyHighlights(urlFiltered, currentUserPubkey, followedPubkeys) + const filtered = classified.filter(h => { + if (h.level === 'mine') return highlightVisibility.mine + if (h.level === 'friends') return highlightVisibility.friends + return highlightVisibility.nostrverse + }) console.log('✅ Relevant highlights after filtering:', filtered.length, filtered.map(h => h.content.substring(0, 30))) return filtered diff --git a/src/utils/highlightClassification.ts b/src/utils/highlightClassification.ts new file mode 100644 index 00000000..3f318e3a --- /dev/null +++ b/src/utils/highlightClassification.ts @@ -0,0 +1,34 @@ +import { Highlight } from '../types/highlights' + +export type HighlightLevel = 'mine' | 'friends' | 'nostrverse' + +/** + * Classify a highlight based on the current user and their followed pubkeys + */ +export function classifyHighlight( + highlight: Highlight, + currentUserPubkey?: string, + followedPubkeys: Set = new Set() +): Highlight & { level: HighlightLevel } { + let level: HighlightLevel = 'nostrverse' + + if (highlight.pubkey === currentUserPubkey) { + level = 'mine' + } else if (followedPubkeys.has(highlight.pubkey)) { + level = 'friends' + } + + return { ...highlight, level } +} + +/** + * Classify an array of highlights + */ +export function classifyHighlights( + highlights: Highlight[], + currentUserPubkey?: string, + followedPubkeys: Set = new Set() +): Array { + return highlights.map(h => classifyHighlight(h, currentUserPubkey, followedPubkeys)) +} +