refactor: DRY up highlight classification and URL normalization

- Extract classifyHighlight and classifyHighlights utilities
- Remove duplicate highlight classification logic from 3 locations
- Use existing normalizeUrl from urlHelpers instead of duplicating
- Reduce code duplication while maintaining functionality
This commit is contained in:
Gigi
2025-10-07 21:58:17 +01:00
parent e1f704a690
commit 5ef7d2c41c
4 changed files with 51 additions and 51 deletions

View File

@@ -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<BookmarksProps> = ({ 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 (

View File

@@ -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])
}

View File

@@ -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

View File

@@ -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<string> = 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<string> = new Set()
): Array<Highlight & { level: HighlightLevel }> {
return highlights.map(h => classifyHighlight(h, currentUserPubkey, followedPubkeys))
}