import React from 'react'; import * as CFI from 'foliate-js/epubcfi.js'; import { useReaderStore } from '@/store/readerStore'; import { findParentPath } from '@/utils/toc'; import { TOCItem } from '@/libs/document'; import { BookNote, BookNoteType } from '@/types/book'; import BooknoteItem from './BooknoteItem'; interface BooknoteGroup { id: number; href: string; label: string; booknotes: BookNote[]; } const BooknoteView: React.FC<{ type: BookNoteType; bookKey: string; toc: TOCItem[]; }> = ({ type, bookKey, toc }) => { const { getConfig } = useReaderStore(); const config = getConfig(bookKey)!; const { booknotes: allNotes = [] } = config; const booknotes = allNotes.filter((note) => note.type === type); const booknoteGroups: { [href: string]: BooknoteGroup } = {}; for (const booknote of booknotes) { const parentPath = findParentPath(toc, booknote.href); if (parentPath.length > 0) { const href = parentPath[0]!.href || ''; const label = parentPath[0]!.label || ''; const id = toc.findIndex((item) => item.href === href) || Infinity; booknote.href = href; 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; }); return (