refact: bookmarks
This commit is contained in:
parent
7876f26d0c
commit
46d48a6d52
22 changed files with 223 additions and 175 deletions
|
|
@ -9,16 +9,12 @@ import { Event } from 'nostr-tools'
|
|||
export default function BookmarkButton({ event }: { event: Event }) {
|
||||
const { t } = useTranslation()
|
||||
const { toast } = useToast()
|
||||
const { pubkey: accountPubkey, checkLogin } = useNostr()
|
||||
const { bookmarks, addBookmark, removeBookmark } = useBookmarks()
|
||||
const { pubkey: accountPubkey, bookmarkListEvent, checkLogin } = useNostr()
|
||||
const { addBookmark, removeBookmark } = useBookmarks()
|
||||
const [updating, setUpdating] = useState(false)
|
||||
|
||||
const eventId = event.id
|
||||
const eventPubkey = event.pubkey
|
||||
|
||||
const isBookmarked = useMemo(
|
||||
() => bookmarks.some((tag) => tag[0] === 'e' && tag[1] === eventId),
|
||||
[bookmarks, eventId]
|
||||
() => bookmarkListEvent?.tags.some((tag) => tag[0] === 'e' && tag[1] === event.id),
|
||||
[bookmarkListEvent, event]
|
||||
)
|
||||
|
||||
if (!accountPubkey) return null
|
||||
|
|
@ -30,11 +26,7 @@ export default function BookmarkButton({ event }: { event: Event }) {
|
|||
|
||||
setUpdating(true)
|
||||
try {
|
||||
await addBookmark(eventId, eventPubkey)
|
||||
toast({
|
||||
title: t('Note bookmarked'),
|
||||
description: t('This note has been added to your bookmarks')
|
||||
})
|
||||
await addBookmark(event)
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: t('Bookmark failed'),
|
||||
|
|
@ -54,11 +46,7 @@ export default function BookmarkButton({ event }: { event: Event }) {
|
|||
|
||||
setUpdating(true)
|
||||
try {
|
||||
await removeBookmark(eventId)
|
||||
toast({
|
||||
title: t('Bookmark removed'),
|
||||
description: t('This note has been removed from your bookmarks')
|
||||
})
|
||||
await removeBookmark(event)
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: t('Remove bookmark failed'),
|
||||
|
|
@ -74,8 +62,8 @@ export default function BookmarkButton({ event }: { event: Event }) {
|
|||
return (
|
||||
<button
|
||||
className={`flex items-center gap-1 ${
|
||||
isBookmarked ? 'text-primary' : 'text-muted-foreground'
|
||||
} enabled:hover:text-primary px-3 h-full`}
|
||||
isBookmarked ? 'text-rose-400' : 'text-muted-foreground'
|
||||
} enabled:hover:text-rose-400 px-3 h-full`}
|
||||
onClick={isBookmarked ? handleRemoveBookmark : handleBookmark}
|
||||
disabled={updating}
|
||||
title={isBookmarked ? t('Remove bookmark') : t('Bookmark')}
|
||||
|
|
@ -83,7 +71,7 @@ export default function BookmarkButton({ event }: { event: Event }) {
|
|||
{updating ? (
|
||||
<Loader className="animate-spin" />
|
||||
) : (
|
||||
<BookmarkIcon className={isBookmarked ? 'fill-primary' : ''} />
|
||||
<BookmarkIcon className={isBookmarked ? 'fill-rose-400' : ''} />
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
|
|
|
|||
97
src/components/BookmarkList/index.tsx
Normal file
97
src/components/BookmarkList/index.tsx
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import { useFetchEvent } from '@/hooks'
|
||||
import { generateEventIdFromETag } from '@/lib/tag'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import { kinds } from 'nostr-tools'
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import NoteCard, { NoteCardLoadingSkeleton } from '../NoteCard'
|
||||
|
||||
const SHOW_COUNT = 10
|
||||
|
||||
export default function BookmarkList() {
|
||||
const { t } = useTranslation()
|
||||
const { bookmarkListEvent } = useNostr()
|
||||
const eventIds = useMemo(() => {
|
||||
if (!bookmarkListEvent) return []
|
||||
|
||||
return (
|
||||
bookmarkListEvent.tags
|
||||
.map((tag) => (tag[0] === 'e' ? generateEventIdFromETag(tag) : undefined))
|
||||
.filter(Boolean) as `nevent1${string}`[]
|
||||
).reverse()
|
||||
}, [bookmarkListEvent])
|
||||
const [showCount, setShowCount] = useState(SHOW_COUNT)
|
||||
const bottomRef = useRef<HTMLDivElement | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const options = {
|
||||
root: null,
|
||||
rootMargin: '10px',
|
||||
threshold: 0.1
|
||||
}
|
||||
|
||||
const loadMore = () => {
|
||||
if (showCount < eventIds.length) {
|
||||
setShowCount((prev) => prev + SHOW_COUNT)
|
||||
}
|
||||
}
|
||||
|
||||
const observerInstance = new IntersectionObserver((entries) => {
|
||||
if (entries[0].isIntersecting) {
|
||||
loadMore()
|
||||
}
|
||||
}, options)
|
||||
|
||||
const currentBottomRef = bottomRef.current
|
||||
|
||||
if (currentBottomRef) {
|
||||
observerInstance.observe(currentBottomRef)
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (observerInstance && currentBottomRef) {
|
||||
observerInstance.unobserve(currentBottomRef)
|
||||
}
|
||||
}
|
||||
}, [showCount, eventIds])
|
||||
|
||||
if (eventIds.length === 0) {
|
||||
return (
|
||||
<div className="mt-2 text-sm text-center text-muted-foreground">
|
||||
{t('no bookmarks found')}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{eventIds.slice(0, showCount).map((eventId) => (
|
||||
<BookmarkedNote key={eventId} eventId={eventId} />
|
||||
))}
|
||||
|
||||
{showCount < eventIds.length ? (
|
||||
<div ref={bottomRef}>
|
||||
<NoteCardLoadingSkeleton isPictures={false} />
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center text-sm text-muted-foreground mt-2">
|
||||
{t('no more bookmarks')}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function BookmarkedNote({ eventId }: { eventId: string }) {
|
||||
const { event, isFetching } = useFetchEvent(eventId)
|
||||
|
||||
if (isFetching) {
|
||||
return <NoteCardLoadingSkeleton isPictures={false} />
|
||||
}
|
||||
|
||||
if (!event || event.kind !== kinds.ShortTextNote) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <NoteCard event={event} className="w-full" />
|
||||
}
|
||||
|
|
@ -1,107 +0,0 @@
|
|||
import { useFetchEvent } from '@/hooks'
|
||||
import { useBookmarks } from '@/providers/BookmarksProvider'
|
||||
import { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { generateEventIdFromETag } from '@/lib/tag'
|
||||
import NoteCard, { NoteCardLoadingSkeleton } from '../NoteCard'
|
||||
|
||||
export default function BookmarksList() {
|
||||
const { t } = useTranslation()
|
||||
const { bookmarks } = useBookmarks()
|
||||
const [visibleBookmarks, setVisibleBookmarks] = useState<
|
||||
{ eventId: string; neventId?: string }[]
|
||||
>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const bottomRef = useRef<HTMLDivElement | null>(null)
|
||||
const SHOW_COUNT = 10
|
||||
|
||||
const bookmarkItems = useMemo(() => {
|
||||
return bookmarks
|
||||
.filter((tag) => tag[0] === 'e')
|
||||
.map((tag) => ({
|
||||
eventId: tag[1],
|
||||
neventId: generateEventIdFromETag(tag)
|
||||
}))
|
||||
.reverse()
|
||||
}, [bookmarks])
|
||||
|
||||
useEffect(() => {
|
||||
setVisibleBookmarks(bookmarkItems.slice(0, SHOW_COUNT))
|
||||
setLoading(false)
|
||||
}, [bookmarkItems])
|
||||
|
||||
useEffect(() => {
|
||||
const options = {
|
||||
root: null,
|
||||
rootMargin: '10px',
|
||||
threshold: 0.1
|
||||
}
|
||||
|
||||
const loadMore = () => {
|
||||
if (visibleBookmarks.length < bookmarkItems.length) {
|
||||
setVisibleBookmarks((prev) => [
|
||||
...prev,
|
||||
...bookmarkItems.slice(prev.length, prev.length + SHOW_COUNT)
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
const observerInstance = new IntersectionObserver((entries) => {
|
||||
if (entries[0].isIntersecting) {
|
||||
loadMore()
|
||||
}
|
||||
}, options)
|
||||
|
||||
const currentBottomRef = bottomRef.current
|
||||
|
||||
if (currentBottomRef) {
|
||||
observerInstance.observe(currentBottomRef)
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (observerInstance && currentBottomRef) {
|
||||
observerInstance.unobserve(currentBottomRef)
|
||||
}
|
||||
}
|
||||
}, [visibleBookmarks, bookmarkItems])
|
||||
|
||||
if (loading) {
|
||||
return <NoteCardLoadingSkeleton isPictures={false} />
|
||||
}
|
||||
|
||||
if (bookmarkItems.length === 0) {
|
||||
return (
|
||||
<div className="mt-2 text-sm text-center text-muted-foreground">
|
||||
{t('No bookmarks found. Add some by clicking the bookmark icon on notes.')}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{visibleBookmarks.map((item) => (
|
||||
<BookmarkedNote key={item.eventId} eventId={item.eventId} neventId={item.neventId} />
|
||||
))}
|
||||
|
||||
{visibleBookmarks.length < bookmarkItems.length && (
|
||||
<div ref={bottomRef}>
|
||||
<NoteCardLoadingSkeleton isPictures={false} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function BookmarkedNote({ eventId, neventId }: { eventId: string; neventId?: string }) {
|
||||
const { event, isFetching } = useFetchEvent(neventId || eventId)
|
||||
|
||||
if (isFetching) {
|
||||
return <NoteCardLoadingSkeleton isPictures={false} />
|
||||
}
|
||||
|
||||
if (!event) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <NoteCard event={event} className="w-full" />
|
||||
}
|
||||
|
|
@ -1,20 +1,18 @@
|
|||
import { toRelaySettings } from '@/lib/link'
|
||||
import { simplifyUrl } from '@/lib/url'
|
||||
import { SecondaryPageLink } from '@/PageManager'
|
||||
import { useBookmarks } from '@/providers/BookmarksProvider'
|
||||
import { useFavoriteRelays } from '@/providers/FavoriteRelaysProvider'
|
||||
import { useFeed } from '@/providers/FeedProvider'
|
||||
import { useNostr } from '@/providers/NostrProvider'
|
||||
import { BookmarkIcon, UsersRound } from 'lucide-react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import RelayIcon from '../RelayIcon'
|
||||
import RelaySetCard from '../RelaySetCard'
|
||||
import SaveRelayDropdownMenu from '../SaveRelayDropdownMenu'
|
||||
import { BookmarkIcon, UsersRound } from 'lucide-react'
|
||||
|
||||
export default function FeedSwitcher({ close }: { close?: () => void }) {
|
||||
const { t } = useTranslation()
|
||||
const { pubkey } = useNostr()
|
||||
const { bookmarks } = useBookmarks()
|
||||
const { relaySets, favoriteRelays } = useFavoriteRelays()
|
||||
const { feedInfo, switchFeed, temporaryRelayUrls } = useFeed()
|
||||
|
||||
|
|
@ -38,7 +36,7 @@ export default function FeedSwitcher({ close }: { close?: () => void }) {
|
|||
</FeedSwitcherItem>
|
||||
)}
|
||||
|
||||
{pubkey && bookmarks.length > 0 && (
|
||||
{pubkey && (
|
||||
<FeedSwitcherItem
|
||||
isActive={feedInfo.feedType === 'bookmarks'}
|
||||
onClick={() => {
|
||||
|
|
|
|||
|
|
@ -65,9 +65,9 @@ export default function NoteStats({
|
|||
<RepostButton event={event} />
|
||||
<LikeButton event={event} />
|
||||
<ZapButton event={event} />
|
||||
<BookmarkButton event={event} />
|
||||
</div>
|
||||
<div className="flex items-center" onClick={(e) => e.stopPropagation()}>
|
||||
<BookmarkButton event={event} />
|
||||
<SeenOnButton event={event} />
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue