import { ChevronDown, ChevronRight, Clock, Edit2, FileQuestion, FileText, Folder, FolderOpen, FolderPlus, Hash, Pin, Plus, Tag, Trash2, Upload, } from "lucide-react"; import React, { useMemo, useState } from "react"; import { useI18n } from "../../application/i18n/I18nProvider"; import { ancestorNoteGroupPaths, cleanNoteGroupPath, extractAllNoteTags, getNoteGroupLeafName, getNoteGroupParentPath, isNoteGroupInside, joinNoteGroupPath, type NoteFilterMode, type VaultNote, } from "../../domain/notes"; export interface NotesSidebarProps { notes: VaultNote[]; noteGroups: string[]; selectedGroup: string | null; selectedTag: string | null; filterMode: NoteFilterMode; onSelectFilterMode: (mode: NoteFilterMode) => void; onSelectGroup: (group: string | null) => void; onSelectTag: (tag: string | null) => void; onCreateGroup: (parentGroup?: string | null) => void; onRenameGroup: (group: string) => void; onDeleteGroup: (group: string) => void; onImportMarkdown?: () => void; className?: string; } interface GroupTreeItem { path: string; name: string; depth: number; hasChildren: boolean; directCount: number; totalCount: number; } export const NotesSidebar: React.FC = ({ notes, noteGroups, selectedGroup, selectedTag, filterMode, onSelectFilterMode, onSelectGroup, onSelectTag, onCreateGroup, onRenameGroup, onDeleteGroup, onImportMarkdown, className = "", }) => { const { t } = useI18n(); const [expandedFolders, setExpandedFolders] = useState>(new Set()); const toggleFolder = (path: string, e: React.MouseEvent) => { e.stopPropagation(); setExpandedFolders((prev) => { const next = new Set(prev); if (next.has(path)) { next.delete(path); } else { next.add(path); } return next; }); }; // Compute counts for quick filter views const counts = useMemo(() => { const pinned = notes.filter((n) => n.isPinned).length; const sevenDaysAgo = Date.now() - 7 * 24 * 60 * 60 * 1000; const recent = notes.filter((n) => n.updatedAt >= sevenDaysAgo).length; const uncategorized = notes.filter((n) => !n.group).length; return { all: notes.length, pinned, recent, uncategorized, }; }, [notes]); // Compute tags const allTags = useMemo(() => extractAllNoteTags(notes), [notes]); // Build sorted folder hierarchy const folderTree = useMemo(() => { const allGroupPaths = new Set(); for (const g of noteGroups) { if (g.trim()) { const clean = cleanNoteGroupPath(g); ancestorNoteGroupPaths(clean).forEach((p) => allGroupPaths.add(p)); } } for (const note of notes) { if (note.group) { ancestorNoteGroupPaths(cleanNoteGroupPath(note.group)).forEach((p) => allGroupPaths.add(p), ); } } const sortedPaths = Array.from(allGroupPaths).sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" }), ); const items: GroupTreeItem[] = []; for (const path of sortedPaths) { const parts = path.split("/"); const depth = parts.length - 1; const name = getNoteGroupLeafName(path); const hasChildren = sortedPaths.some( (other) => other !== path && other.startsWith(`${path}/`), ); const directCount = notes.filter((n) => n.group === path).length; const totalCount = notes.filter((n) => isNoteGroupInside(n.group, path)).length; // Check if all parent folders are expanded let visible = true; let cur = getNoteGroupParentPath(path); while (cur) { if (!expandedFolders.has(cur)) { visible = false; break; } cur = getNoteGroupParentPath(cur); } if (visible || depth === 0) { items.push({ path, name, depth, hasChildren, directCount, totalCount, }); } } return items; }, [noteGroups, notes, expandedFolders]); return (
{/* Sidebar Header */}
{t("notes.title") || "笔记管理"}
{onImportMarkdown && ( )}
{/* Main Navigation Scroll Area */}
{/* Quick Filter Section */}
{/* Folders / Notebooks Section */}
{t("notes.folders") || "笔记本 / 文件夹"}
{folderTree.length === 0 ? (
暂无文件夹,点击 + 创建
) : ( folderTree.map((item) => { const isSelected = selectedGroup === item.path; const isExpanded = expandedFolders.has(item.path); return (
{ onSelectGroup(item.path); onSelectTag(null); onSelectFilterMode("all"); }} >
{item.hasChildren ? ( ) : ( )} {isExpanded ? ( ) : ( )} {item.name}
{item.totalCount}
); }) )}
{/* Tags Section */} {allTags.length > 0 && (
{t("notes.tags") || "标签"}
{allTags.map(({ tag, count }) => { const isSelected = selectedTag === tag; return ( ); })}
)}
{/* Sidebar Footer */}
共 {notes.length} 篇笔记
); };