mirror of
https://github.com/dergigi/boris.git
synced 2026-01-09 09:54:34 +01:00
refactor: keep Bookmarks.tsx under 210 lines by extracting logic
Extracted large functions into separate modules to follow DRY principles
and keep files manageable:
- Created useArticleLoader.ts hook (92 lines)
- Handles article loading from naddr
- Fetches article content and highlights
- Sets up article coordinate for refresh
- Created contentLoader.ts utility (44 lines)
- Handles both Nostr articles and web URLs
- Unified content loading logic
- Reusable across components
Result: Bookmarks.tsx reduced from 282 to 208 lines ✅
All files now under 210 line limit while maintaining functionality.
This commit is contained in:
44
src/utils/contentLoader.ts
Normal file
44
src/utils/contentLoader.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { nip19 } from 'nostr-tools'
|
||||
import { RelayPool } from 'applesauce-relay'
|
||||
import { fetchArticleByNaddr } from '../services/articleService'
|
||||
import { fetchReadableContent, ReadableContent } from '../services/readerService'
|
||||
|
||||
export interface BookmarkReference {
|
||||
id: string
|
||||
kind: number
|
||||
tags: string[][]
|
||||
pubkey: string
|
||||
}
|
||||
|
||||
export async function loadContent(
|
||||
url: string,
|
||||
relayPool: RelayPool,
|
||||
bookmark?: BookmarkReference
|
||||
): Promise<ReadableContent> {
|
||||
// Check if this is a kind:30023 article
|
||||
if (bookmark && bookmark.kind === 30023) {
|
||||
const dTag = bookmark.tags.find(t => t[0] === 'd')?.[1] || ''
|
||||
|
||||
if (dTag !== undefined && bookmark.pubkey) {
|
||||
const pointer = {
|
||||
identifier: dTag,
|
||||
kind: 30023,
|
||||
pubkey: bookmark.pubkey,
|
||||
}
|
||||
const naddr = nip19.naddrEncode(pointer)
|
||||
const article = await fetchArticleByNaddr(relayPool, naddr)
|
||||
|
||||
return {
|
||||
title: article.title,
|
||||
markdown: article.markdown,
|
||||
image: article.image,
|
||||
url: `nostr:${naddr}`
|
||||
}
|
||||
} else {
|
||||
throw new Error('Invalid article reference - missing d tag or pubkey')
|
||||
}
|
||||
} else {
|
||||
// For regular URLs, fetch readable content
|
||||
return await fetchReadableContent(url)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user