refact: bookmarks
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -218,6 +218,11 @@ export default {
|
||||
'no relays found': 'لم يتم العثور على ريلايات',
|
||||
video: 'فيديو',
|
||||
'Show n new notes': 'عرض {{n}} ملاحظات جديدة',
|
||||
YouTabName: 'أنت'
|
||||
YouTabName: 'أنت',
|
||||
Bookmark: 'الإشارة المرجعية',
|
||||
'Remove bookmark': 'إزالة الإشارة',
|
||||
'no bookmarks found': 'لم يتم العثور على إشارات',
|
||||
'no more bookmarks': 'لا مزيد من الإشارات',
|
||||
Bookmarks: 'الإشارات المرجعية'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,6 +222,11 @@ export default {
|
||||
'no relays found': 'Keine Relays gefunden',
|
||||
video: 'Video',
|
||||
'Show n new notes': 'Zeige {{n}} neue Notizen',
|
||||
YouTabName: 'Du'
|
||||
YouTabName: 'Du',
|
||||
Bookmark: 'Lesezeichen',
|
||||
'Remove bookmark': 'Lesezeichen entfernen',
|
||||
'no bookmarks found': 'Keine Lesezeichen gefunden',
|
||||
'no more bookmarks': 'Keine weiteren Lesezeichen',
|
||||
Bookmarks: 'Lesezeichen'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +218,11 @@ export default {
|
||||
'no relays found': 'no relays found',
|
||||
video: 'video',
|
||||
'Show n new notes': 'Show {{n}} new notes',
|
||||
YouTabName: 'You'
|
||||
YouTabName: 'You',
|
||||
Bookmark: 'Bookmark',
|
||||
'Remove bookmark': 'Remove bookmark',
|
||||
'no bookmarks found': 'no bookmarks found',
|
||||
'no more bookmarks': 'no more bookmarks',
|
||||
Bookmarks: 'Bookmarks'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,6 +222,11 @@ export default {
|
||||
'no relays found': 'no se encontraron relés',
|
||||
video: 'video',
|
||||
'Show n new notes': 'Mostrar {{n}} nuevas notas',
|
||||
YouTabName: 'You'
|
||||
YouTabName: 'You',
|
||||
Bookmark: 'Marcador',
|
||||
'Remove bookmark': 'Quitar marcador',
|
||||
'no bookmarks found': 'No se encontraron marcadores',
|
||||
'no more bookmarks': 'No hay más marcadores',
|
||||
Bookmarks: 'Marcadores'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +221,11 @@ export default {
|
||||
'no relays found': 'aucun relais trouvé',
|
||||
video: 'vidéo',
|
||||
'Show n new notes': 'Afficher {{n}} nouvelles notes',
|
||||
YouTabName: 'Vous'
|
||||
YouTabName: 'Vous',
|
||||
Bookmark: 'Favori',
|
||||
'Remove bookmark': 'Retirer le favori',
|
||||
'no bookmarks found': 'Aucun favori trouvé',
|
||||
'no more bookmarks': 'Plus de favoris',
|
||||
Bookmarks: 'Favoris'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +221,11 @@ export default {
|
||||
'no relays found': 'Nessun relay trovato',
|
||||
video: 'video',
|
||||
'Show n new notes': 'Mostra {{n}} nuove note',
|
||||
YouTabName: 'Tu'
|
||||
YouTabName: 'Tu',
|
||||
Bookmark: 'Segnalibro',
|
||||
'Remove bookmark': 'Rimuovi segnalibro',
|
||||
'no bookmarks found': 'Nessun segnalibro trovato',
|
||||
'no more bookmarks': 'Nessun altro segnalibro',
|
||||
Bookmarks: 'Segnalibri'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +219,11 @@ export default {
|
||||
'no relays found': 'リレイが見つかりません',
|
||||
video: 'ビデオ',
|
||||
'Show n new notes': '新しいノートを{{n}}件表示',
|
||||
YouTabName: 'あなた'
|
||||
YouTabName: 'あなた',
|
||||
Bookmark: 'ブックマーク',
|
||||
'Remove bookmark': 'ブックマークを削除',
|
||||
'no bookmarks found': 'ブックマークが見つかりません',
|
||||
'no more bookmarks': 'これ以上ブックマークはありません',
|
||||
Bookmarks: 'ブックマーク一覧'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,6 +220,11 @@ export default {
|
||||
'no relays found': 'Nie znaleziono transmiterów',
|
||||
video: 'wideo',
|
||||
'Show n new notes': 'Pokaż {{n}} nowych wpisów',
|
||||
YouTabName: 'Ty'
|
||||
YouTabName: 'Ty',
|
||||
Bookmark: 'Zakładka',
|
||||
'Remove bookmark': 'Usuń zakładkę',
|
||||
'no bookmarks found': 'Nie znaleziono zakładek',
|
||||
'no more bookmarks': 'Brak więcej zakładek',
|
||||
Bookmarks: 'Zakładki'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,6 +220,11 @@ export default {
|
||||
'no relays found': 'nenhum relé encontrado',
|
||||
video: 'vídeo',
|
||||
'Show n new notes': 'Ver {{n}} novas notas',
|
||||
YouTabName: 'Você'
|
||||
YouTabName: 'Você',
|
||||
Bookmark: 'Favorito',
|
||||
'Remove bookmark': 'Remover favorito',
|
||||
'no bookmarks found': 'Nenhum favorito encontrado',
|
||||
'no more bookmarks': 'Sem mais favoritos',
|
||||
Bookmarks: 'Favoritos'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +221,11 @@ export default {
|
||||
'no relays found': 'nenhum relé encontrado',
|
||||
video: 'vídeo',
|
||||
'Show n new notes': 'Mostrar {{n}} novas notas',
|
||||
YouTabName: 'Você'
|
||||
YouTabName: 'Você',
|
||||
Bookmark: 'Favorito',
|
||||
'Remove bookmark': 'Remover favorito',
|
||||
'no bookmarks found': 'Nenhum favorito encontrado',
|
||||
'no more bookmarks': 'Sem mais favoritos',
|
||||
Bookmarks: 'Favoritos'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,6 +222,11 @@ export default {
|
||||
'no relays found': 'ретрансляторы не найдены',
|
||||
video: 'видео',
|
||||
'Show n new notes': 'Показать {{n}} новых заметок',
|
||||
YouTabName: 'Вы'
|
||||
YouTabName: 'Вы',
|
||||
Bookmark: 'Закладка',
|
||||
'Remove bookmark': 'Удалить закладку',
|
||||
'no bookmarks found': 'Закладки не найдены',
|
||||
'no more bookmarks': 'Больше нет закладок',
|
||||
Bookmarks: 'Закладки'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,6 +219,11 @@ export default {
|
||||
'no relays found': '未找到服务器',
|
||||
video: '视频',
|
||||
'Show n new notes': '显示 {{n}} 条新笔记',
|
||||
YouTabName: '与你'
|
||||
YouTabName: '与你',
|
||||
Bookmark: '收藏',
|
||||
'Remove bookmark': '取消收藏',
|
||||
'no bookmarks found': '暂无收藏',
|
||||
'no more bookmarks': '到底了',
|
||||
Bookmarks: '收藏'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,10 +283,10 @@ export function createSeenNotificationsAtDraftEvent(): TDraftEvent {
|
||||
}
|
||||
}
|
||||
|
||||
export function createBookmarkDraftEvent(tags: string[][]): TDraftEvent {
|
||||
export function createBookmarkDraftEvent(tags: string[][], content = ''): TDraftEvent {
|
||||
return {
|
||||
kind: kinds.BookmarkList,
|
||||
content: '',
|
||||
content,
|
||||
tags,
|
||||
created_at: dayjs().unix()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import BookmarksList from '@/components/BookmarksList'
|
||||
import BookmarkList from '@/components/BookmarkList'
|
||||
import NoteList from '@/components/NoteList'
|
||||
import PostEditor from '@/components/PostEditor'
|
||||
import SaveRelayDropdownMenu from '@/components/SaveRelayDropdownMenu'
|
||||
@@ -46,7 +46,7 @@ const NoteListPage = forwardRef((_, ref) => {
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
content = <BookmarksList />
|
||||
content = <BookmarkList />
|
||||
}
|
||||
} else if (isReady) {
|
||||
content = (
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { createBookmarkDraftEvent } from '@/lib/draft-event'
|
||||
import { createContext, useContext, useMemo } from 'react'
|
||||
import { useNostr } from './NostrProvider'
|
||||
import client from '@/services/client.service'
|
||||
import { createContext, useContext } from 'react'
|
||||
import { useNostr } from './NostrProvider'
|
||||
import { Event } from 'nostr-tools'
|
||||
|
||||
type TBookmarksContext = {
|
||||
bookmarks: string[][]
|
||||
addBookmark: (eventId: string, eventPubkey: string, relayHint?: string) => Promise<void>
|
||||
removeBookmark: (eventId: string) => Promise<void>
|
||||
addBookmark: (event: Event) => Promise<void>
|
||||
removeBookmark: (event: Event) => Promise<void>
|
||||
}
|
||||
|
||||
const BookmarksContext = createContext<TBookmarksContext | undefined>(undefined)
|
||||
@@ -20,40 +20,34 @@ export const useBookmarks = () => {
|
||||
}
|
||||
|
||||
export function BookmarksProvider({ children }: { children: React.ReactNode }) {
|
||||
const { pubkey: accountPubkey, bookmarkListEvent, publish, updateBookmarkListEvent } = useNostr()
|
||||
const bookmarks = useMemo(
|
||||
() => (bookmarkListEvent ? bookmarkListEvent.tags : []),
|
||||
[bookmarkListEvent]
|
||||
)
|
||||
const { pubkey: accountPubkey, publish, updateBookmarkListEvent } = useNostr()
|
||||
|
||||
const addBookmark = async (eventId: string, eventPubkey: string, relayHint?: string) => {
|
||||
const addBookmark = async (event: Event) => {
|
||||
if (!accountPubkey) return
|
||||
|
||||
const relayHintToUse = relayHint || client.getEventHint(eventId)
|
||||
|
||||
const newTag = ['e', eventId, relayHintToUse, eventPubkey]
|
||||
|
||||
const bookmarkListEvent = await client.fetchBookmarkListEvent(accountPubkey)
|
||||
const currentTags = bookmarkListEvent?.tags || []
|
||||
|
||||
const isDuplicate = currentTags.some((tag) => tag[0] === 'e' && tag[1] === eventId)
|
||||
if (currentTags.some((tag) => tag[0] === 'e' && tag[1] === event.id)) return
|
||||
|
||||
if (isDuplicate) return
|
||||
|
||||
const newTags = [...currentTags, newTag]
|
||||
|
||||
const newBookmarkDraftEvent = createBookmarkDraftEvent(newTags)
|
||||
const newBookmarkDraftEvent = createBookmarkDraftEvent(
|
||||
[...currentTags, ['e', event.id, client.getEventHint(event.id), event.pubkey]],
|
||||
bookmarkListEvent?.content
|
||||
)
|
||||
const newBookmarkEvent = await publish(newBookmarkDraftEvent)
|
||||
await updateBookmarkListEvent(newBookmarkEvent)
|
||||
}
|
||||
|
||||
const removeBookmark = async (eventId: string) => {
|
||||
if (!accountPubkey || !bookmarkListEvent) return
|
||||
const removeBookmark = async (event: Event) => {
|
||||
if (!accountPubkey) return
|
||||
|
||||
const newTags = bookmarkListEvent.tags.filter((tag) => !(tag[0] === 'e' && tag[1] === eventId))
|
||||
const bookmarkListEvent = await client.fetchBookmarkListEvent(accountPubkey)
|
||||
if (!bookmarkListEvent) return
|
||||
|
||||
const newTags = bookmarkListEvent.tags.filter((tag) => !(tag[0] === 'e' && tag[1] === event.id))
|
||||
if (newTags.length === bookmarkListEvent.tags.length) return
|
||||
|
||||
const newBookmarkDraftEvent = createBookmarkDraftEvent(newTags)
|
||||
const newBookmarkDraftEvent = createBookmarkDraftEvent(newTags, bookmarkListEvent.content)
|
||||
const newBookmarkEvent = await publish(newBookmarkDraftEvent)
|
||||
await updateBookmarkListEvent(newBookmarkEvent)
|
||||
}
|
||||
@@ -61,7 +55,6 @@ export function BookmarksProvider({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<BookmarksContext.Provider
|
||||
value={{
|
||||
bookmarks,
|
||||
addBookmark,
|
||||
removeBookmark
|
||||
}}
|
||||
|
||||
@@ -93,6 +93,10 @@ export function FeedProvider({ children }: { children: React.ReactNode }) {
|
||||
if (feedInfo.feedType === 'following' && pubkey) {
|
||||
return await switchFeed('following', { pubkey })
|
||||
}
|
||||
|
||||
if (feedInfo.feedType === 'bookmarks' && pubkey) {
|
||||
return await switchFeed('bookmarks', { pubkey })
|
||||
}
|
||||
}
|
||||
|
||||
init()
|
||||
|
||||
@@ -746,6 +746,21 @@ class ClientService extends EventTarget {
|
||||
return event
|
||||
}
|
||||
|
||||
async fetchBookmarkListEvent(pubkey: string): Promise<NEvent | undefined> {
|
||||
const storedBookmarkListEvent = await indexedDb.getReplaceableEvent(pubkey, kinds.BookmarkList)
|
||||
if (storedBookmarkListEvent) {
|
||||
return storedBookmarkListEvent
|
||||
}
|
||||
|
||||
const relayList = await this.fetchRelayList(pubkey)
|
||||
const events = await this.query(relayList.write.concat(BIG_RELAY_URLS), {
|
||||
authors: [pubkey],
|
||||
kinds: [kinds.BookmarkList]
|
||||
})
|
||||
|
||||
return events.sort((a, b) => b.created_at - a.created_at)[0]
|
||||
}
|
||||
|
||||
async fetchFollowings(pubkey: string, storeToIndexedDb = false) {
|
||||
const followListEvent = await this.fetchFollowListEvent(pubkey, storeToIndexedDb)
|
||||
return followListEvent ? extractPubkeysFromEventTags(followListEvent.tags) : []
|
||||
|
||||
Reference in New Issue
Block a user