mirror of
https://github.com/dergigi/boris.git
synced 2026-01-27 18:54:20 +01:00
- Extract CompactView, LargeView, and CardView into separate files - Keep all files under 210 lines (BookmarkItem: 307→105 lines) - Improve code organization and maintainability - Add shared type definitions for view components - Keep DRY with shared props object
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
import React, { useState } from 'react'
|
|
import { faBookOpen, faPlay, faEye } from '@fortawesome/free-solid-svg-icons'
|
|
import { useEventModel } from 'applesauce-react/hooks'
|
|
import { Models } from 'applesauce-core'
|
|
import { npubEncode, neventEncode } from 'nostr-tools/nip19'
|
|
import { IndividualBookmark } from '../types/bookmarks'
|
|
import { extractUrlsFromContent } from '../services/bookmarkHelpers'
|
|
import { classifyUrl } from '../utils/helpers'
|
|
import { ViewMode } from './Bookmarks'
|
|
import { getPreviewImage, fetchOgImage } from '../utils/imagePreview'
|
|
import { CompactView } from './BookmarkViews/CompactView'
|
|
import { LargeView } from './BookmarkViews/LargeView'
|
|
import { CardView } from './BookmarkViews/CardView'
|
|
|
|
interface BookmarkItemProps {
|
|
bookmark: IndividualBookmark
|
|
index: number
|
|
onSelectUrl?: (url: string) => void
|
|
viewMode?: ViewMode
|
|
}
|
|
|
|
export const BookmarkItem: React.FC<BookmarkItemProps> = ({ bookmark, index, onSelectUrl, viewMode = 'cards' }) => {
|
|
const [ogImage, setOgImage] = useState<string | null>(null)
|
|
|
|
const short = (v: string) => `${v.slice(0, 8)}...${v.slice(-8)}`
|
|
|
|
// Extract URLs from bookmark content
|
|
const extractedUrls = extractUrlsFromContent(bookmark.content)
|
|
const hasUrls = extractedUrls.length > 0
|
|
const firstUrl = hasUrls ? extractedUrls[0] : null
|
|
const firstUrlClassification = firstUrl ? classifyUrl(firstUrl) : null
|
|
|
|
// Fetch OG image for large view (hook must be at top level)
|
|
const instantPreview = firstUrl ? getPreviewImage(firstUrl, firstUrlClassification?.type || '') : null
|
|
React.useEffect(() => {
|
|
if (viewMode === 'large' && firstUrl && !instantPreview && !ogImage) {
|
|
fetchOgImage(firstUrl).then(setOgImage)
|
|
}
|
|
}, [viewMode, firstUrl, instantPreview, ogImage])
|
|
|
|
// Resolve author profile using applesauce
|
|
const authorProfile = useEventModel(Models.ProfileModel, [bookmark.pubkey])
|
|
const authorNpub = npubEncode(bookmark.pubkey)
|
|
const isHexId = /^[0-9a-f]{64}$/i.test(bookmark.id)
|
|
const eventNevent = isHexId ? neventEncode({ id: bookmark.id }) : undefined
|
|
|
|
// Get display name for author
|
|
const getAuthorDisplayName = () => {
|
|
if (authorProfile?.name) return authorProfile.name
|
|
if (authorProfile?.display_name) return authorProfile.display_name
|
|
if (authorProfile?.nip05) return authorProfile.nip05
|
|
return short(bookmark.pubkey) // fallback to short pubkey
|
|
}
|
|
|
|
// use helper from kindIcon.ts
|
|
|
|
const getIconForUrlType = (url: string) => {
|
|
const classification = classifyUrl(url)
|
|
switch (classification.type) {
|
|
case 'youtube':
|
|
case 'video':
|
|
return faPlay
|
|
case 'image':
|
|
return faEye
|
|
default:
|
|
return faBookOpen
|
|
}
|
|
}
|
|
|
|
const handleReadNow = (event: React.MouseEvent<HTMLButtonElement>) => {
|
|
if (!hasUrls) return
|
|
const firstUrl = extractedUrls[0]
|
|
if (onSelectUrl) {
|
|
event.preventDefault()
|
|
onSelectUrl(firstUrl)
|
|
} else {
|
|
window.open(firstUrl, '_blank')
|
|
}
|
|
}
|
|
|
|
const sharedProps = {
|
|
bookmark,
|
|
index,
|
|
hasUrls,
|
|
extractedUrls,
|
|
onSelectUrl,
|
|
getIconForUrlType,
|
|
firstUrlClassification,
|
|
authorNpub,
|
|
eventNevent,
|
|
getAuthorDisplayName,
|
|
handleReadNow
|
|
}
|
|
|
|
if (viewMode === 'compact') {
|
|
return <CompactView {...sharedProps} />
|
|
}
|
|
|
|
if (viewMode === 'large') {
|
|
const previewImage = instantPreview || ogImage
|
|
return <LargeView {...sharedProps} previewImage={previewImage} />
|
|
}
|
|
|
|
return <CardView {...sharedProps} />
|
|
}
|