import React from 'react'; import * as CFI from 'foliate-js/epubcfi.js'; import { useBookDataStore } from '@/store/bookDataStore'; import { useSidebarStore } from '@/store/sidebarStore'; import { findTocItemBS } from '@/utils/toc'; import { TOCItem } from '@/libs/document'; import { BooknoteGroup, BookNoteType } from '@/types/book'; import BooknoteItem from './BooknoteItem'; const BooknoteView: React.FC<{ type: BookNoteType; bookKey: string; toc: TOCItem[]; }> = ({ type, bookKey, toc }) => { const { getConfig } = useBookDataStore(); const { setActiveBooknoteType, setBooknoteResults } = useSidebarStore(); const config = getConfig(bookKey)!; const { booknotes: allNotes = [] } = config; const booknotes = allNotes.filter((note) => note.type === type && !note.deletedAt); const booknoteGroups: { [href: string]: BooknoteGroup } = {}; for (const booknote of booknotes) { const tocItem = findTocItemBS(toc ?? [], booknote.cfi); const href = tocItem?.href || ''; const label = tocItem?.label || ''; const id = tocItem?.id || 0; if (!booknoteGroups[href]) { booknoteGroups[href] = { id, href, label, booknotes: [] }; } booknoteGroups[href].booknotes.push(booknote); } Object.values(booknoteGroups).forEach((group) => { group.booknotes.sort((a, b) => { return CFI.compare(a.cfi, b.cfi); }); }); const sortedGroups = Object.values(booknoteGroups).sort((a, b) => { return a.id - b.id; }); const handleBrowseBookNotes = () => { if (booknotes.length === 0) return; const sorted = [...booknotes].sort((a, b) => CFI.compare(a.cfi, b.cfi)); setActiveBooknoteType(bookKey, type); setBooknoteResults(bookKey, sorted); }; return (