mirror of
https://github.com/dergigi/boris.git
synced 2026-01-06 08:24:27 +01:00
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:
34
src/utils/highlightClassification.ts
Normal file
34
src/utils/highlightClassification.ts
Normal 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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user