使用openai对UI进行了重构

This commit is contained in:
2026-02-24 11:50:51 +08:00
parent 0c97f02e51
commit 317b9c7c26
15 changed files with 695 additions and 583 deletions
+173 -195
View File
@@ -1,117 +1,118 @@
"use client";
"use client";
import { SidebarContent, ResizableSidebar } from "@/components/sidebar";
import { Editor } from "@/components/editor";
import { useEditorStore } from "@/lib/store";
import { Hash, X, Plus, ChevronLeft, ChevronRight, Sparkles, Lock, Unlock } from "lucide-react";
import { Hash, X, Plus, ChevronLeft, ChevronRight, Sparkles, Lock, Unlock, FolderOpen } from "lucide-react";
import { useSettingsStore } from "@/lib/settings-store";
import { exportPageAsMarkdown } from "@/lib/export";
import { useState } from "react";
import { useMemo, useState } from "react";
import { cn, getTagColor } from "@/lib/utils";
import { useSearchStore } from "@/lib/search-store";
import { type Editor as TiptapEditor } from "@tiptap/react";
import { ImportProvider } from "@/components/import-context";
import { AIChatPanel } from "@/components/chat/ai-chat-panel";
const ICONS = [
"📄", "📝", "📒", "📘", "📙", "📕", "📗", "📚",
"💡", "🔥", "✅", "❗", "⭐", "📌", "📎", "🧠",
"🎯", "🚀", "⚙️", "🧪", "💼", "📊", "📈", "🧩",
"🌱", "🌟", "🎨", "🎵", "🎬", "📷", "💬", "🔖",
"⌛", "🗂️", "📦", "🏷️", "🗓️", "🕒", "🔍", "🧾",
];
export default function Home() {
const { activePageId, pages, updatePage } = useEditorStore();
const { openSearchWithTag } = useSearchStore();
const activePage = pages.find(p => p.id === activePageId);
const { timezone } = useSettingsStore();
const activePage = pages.find((p) => p.id === activePageId);
const [isChatOpen, setIsChatOpen] = useState(false);
const [editor, setEditor] = useState<TiptapEditor | null>(null);
const [isAddingTag, setIsAddingTag] = useState(false);
const [tagInput, setTagInput] = useState("");
const [isIconPickerOpen, setIsIconPickerOpen] = useState(false);
const breadcrumbs = useMemo(() => {
if (!activePage) return [];
const result = [] as typeof pages;
let current = activePage;
while (current) {
result.unshift(current);
if (!current.parentId) break;
const parent = pages.find((p) => p.id === current.parentId);
if (!parent) break;
current = parent;
}
return result;
}, [activePage, pages]);
const addTag = () => {
if (!activePage) return;
const next = tagInput.trim();
if (!next) return;
const merged = Array.from(new Set([...(activePage.tags || []), next]));
updatePage(activePage.id, { tags: merged });
setTagInput("");
setIsAddingTag(false);
};
return (
<ImportProvider>
<div className="flex h-[100dvh] w-full bg-background overflow-hidden relative">
{/* Mobile: Sidebar List View (Only visible when no page is active) */}
<div className={cn(
activePageId ? "hidden" : "flex-1 h-full md:hidden block"
)}>
<div className="relative flex h-[100dvh] w-full overflow-hidden bg-background">
<div className={cn(activePageId ? "hidden" : "block h-full flex-1 md:hidden")}>
<SidebarContent />
</div>
{/* Desktop: Sidebar (Resizable) */}
<ResizableSidebar />
{/* Main Content Area */}
<main className={cn(
"flex-1 h-full overflow-hidden flex flex-col relative z-0",
// Mobile: Hidden when no page active (showing list instead)
!activePageId && "hidden md:flex"
)}>
<main className={cn("relative z-0 flex h-full flex-1 flex-col overflow-hidden", !activePageId && "hidden md:flex")}>
{activePage ? (
<div className="flex-1 overflow-y-auto scroll-smooth">
<div className="max-w-7xl mx-auto px-4 md:px-16 py-6 min-h-screen content-start">
<div className="mx-auto min-h-screen max-w-7xl px-4 py-6 md:px-16">
<div className="ui-enter group relative mb-8">
<button
className="absolute -top-12 left-0 flex items-center gap-1 py-2 text-sm text-muted-foreground transition-colors hover:text-foreground md:hidden"
onClick={() => useEditorStore.getState().setActivePageId(null)}
>
<ChevronLeft size={18} />
</button>
<div className="group mb-8 relative">
{/* Mobile Back Button Integration in Header */}
<div className="md:hidden absolute -top-12 left-0 flex items-center gap-1 py-2 text-muted-foreground hover:text-foreground cursor-pointer"
onClick={() => useEditorStore.getState().setActivePageId(null)}>
<ChevronLeft size={20} />
<span></span>
</div>
{/* Breadcrumb Navigation */}
<div className="flex items-center flex-wrap gap-1 text-sm text-muted-foreground mb-4">
{(() => {
const breadcrumbs = [];
let current: typeof activePage | undefined = activePage;
while (current) {
breadcrumbs.unshift(current);
if (current.parentId) {
current = pages.find(p => p.id === current?.parentId);
} else {
current = undefined;
}
}
return breadcrumbs.map((crumb, index) => (
<div key={crumb.id} className="flex items-center gap-1">
{index > 0 && <ChevronRight size={14} className="opacity-50" />}
<button
onClick={() => useEditorStore.getState().setActivePageId(crumb.id)}
className={cn(
"hover:underline hover:text-foreground transition-colors flex items-center gap-1",
crumb.id === activePage.id && "font-medium text-foreground pointer-events-none"
)}
>
{crumb.icon && <span>{crumb.icon}</span>}
<span>{crumb.title || "无标题"}</span>
</button>
</div>
));
})()}
<div className="mb-4 flex flex-wrap items-center gap-1 text-sm text-muted-foreground">
{breadcrumbs.map((crumb, index) => (
<div key={crumb.id} className="flex items-center gap-1">
{index > 0 && <ChevronRight size={14} className="opacity-50" />}
<button
onClick={() => useEditorStore.getState().setActivePageId(crumb.id)}
className={cn(
"flex items-center gap-1 transition-colors hover:text-foreground hover:underline",
crumb.id === activePage.id && "pointer-events-none font-medium text-foreground"
)}
>
{crumb.icon && <span>{crumb.icon}</span>}
<span>{crumb.title || "无标题"}</span>
</button>
</div>
))}
</div>
<div className="flex items-center gap-2">
{activePage.icon && (
<span className="text-3xl select-none animate-in fade-in zoom-in-75 duration-300">
{activePage.icon}
</span>
)}
{activePage.icon && <span className="select-none text-3xl">{activePage.icon}</span>}
<input
value={activePage.title}
onChange={(e) => updatePage(activePage.id, { title: e.target.value })}
placeholder="无标题"
disabled={activePage.isLocked}
className={cn(
"w-full text-3xl font-bold bg-transparent border-none outline-none placeholder:text-muted-foreground/20 text-foreground transition-colors",
activePage.isLocked && "opacity-80 cursor-not-allowed select-none"
"w-full border-none bg-transparent text-3xl font-bold text-foreground outline-none placeholder:text-muted-foreground/30",
activePage.isLocked && "cursor-not-allowed select-none opacity-80"
)}
/>
</div>
{/* Last Updated Info moved */}
<div className="absolute right-0 top-1/2 -translate-y-1/2 flex items-center gap-1 transition-opacity">
{/* Buttons moved to Editors Toolbar */}
</div>
</div>
{/* Tags Section */}
<div className="flex flex-wrap items-center gap-2 mb-6 animate-in fade-in slide-in-from-top-2 duration-300">
<div className="ui-enter-delayed mb-6 flex flex-wrap items-center gap-2">
{activePage.tags?.map((tag) => {
const colors = getTagColor(tag);
return (
@@ -119,151 +120,127 @@ export default function Home() {
key={tag}
onClick={() => openSearchWithTag(tag)}
className={cn(
"inline-flex items-center gap-1 px-2.5 py-1 rounded-[3px] text-[11px] font-medium transition-colors border shadow-sm",
colors.bg, colors.text, colors.border
)}>
"group/tag inline-flex items-center gap-1 rounded-[4px] border px-2.5 py-1 text-[11px] font-medium shadow-sm",
colors.bg,
colors.text,
colors.border
)}
>
<Hash size={10} className="opacity-70" />
{tag}
<button
onClick={(e) => {
e.stopPropagation();
const newTags = activePage.tags?.filter(t => t !== tag) || [];
const newTags = activePage.tags?.filter((t) => t !== tag) || [];
updatePage(activePage.id, { tags: newTags });
}}
className="ml-1 rounded-full p-0.5 hover:bg-black/10 dark:hover:bg-white/10 opacity-0 group-hover:opacity-100 transition-all"
className="ml-1 rounded-full p-0.5 opacity-0 transition-all group-hover/tag:opacity-100 hover:bg-black/10 dark:hover:bg-white/10"
title="移除标签"
>
<X size={10} />
</button>
</span>
);
})}
<div className="relative">
{isAddingTag ? (
<input
autoFocus
type="text"
value={tagInput}
onChange={(e) => setTagInput(e.target.value)}
onBlur={() => {
if (tagInput.trim()) {
const newTags = [...(activePage.tags || []), tagInput.trim()];
updatePage(activePage.id, { tags: Array.from(new Set(newTags)) });
}
{isAddingTag ? (
<input
autoFocus
type="text"
value={tagInput}
onChange={(e) => setTagInput(e.target.value)}
onBlur={addTag}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
addTag();
}
if (e.key === "Escape") {
setTagInput("");
setIsAddingTag(false);
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
if (tagInput.trim()) {
const newTags = [...(activePage.tags || []), tagInput.trim()];
updatePage(activePage.id, { tags: Array.from(new Set(newTags)) });
}
setTagInput("");
setIsAddingTag(false);
}
if (e.key === 'Escape') {
setTagInput("");
setIsAddingTag(false);
}
}}
className="w-24 px-2 py-0.5 text-xs bg-transparent border border-primary rounded-sm outline-none animate-in fade-in zoom-in-95 duration-200"
placeholder="输入标签..."
/>
) : (
<div className="flex items-center gap-2">
<button
onClick={() => setIsAddingTag(true)}
className="inline-flex items-center gap-1 px-2 py-0.5 rounded-sm text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground transition-all border border-transparent hover:border-border"
>
<Plus size={12} />
</button>
}
}}
className="w-28 rounded-md border border-primary/40 bg-background px-2 py-1 text-xs outline-none focus:ring-2 focus:ring-primary/20"
placeholder="输入标签"
/>
) : (
<button
onClick={() => setIsAddingTag(true)}
className="ui-btn-secondary px-2 py-1 text-xs text-muted-foreground"
>
<Plus size={12} />
</button>
)}
{/* Icon Picker */}
<div className="relative group/icon-picker">
<div className="relative">
<button
onClick={() => setIsIconPickerOpen((v) => !v)}
className="ui-btn-secondary px-2 py-1 text-xs text-muted-foreground"
>
<Sparkles size={12} /> {activePage.icon ? "更换图标" : "添加图标"}
</button>
{isIconPickerOpen && (
<div className="ui-card absolute left-0 top-full z-50 mt-1 w-80 p-2 shadow-xl animate-in fade-in zoom-in-95">
<div className="grid h-48 grid-cols-8 gap-1 overflow-y-auto pr-1">
{ICONS.map((icon) => (
<button
key={icon}
onClick={() => {
updatePage(activePage.id, { icon });
setIsIconPickerOpen(false);
}}
className={cn(
"flex h-8 w-8 items-center justify-center rounded-md text-lg transition-colors hover:bg-accent",
activePage.icon === icon && "bg-accent"
)}
>
{icon}
</button>
))}
<button
className="inline-flex items-center gap-1 px-2 py-0.5 rounded-sm text-xs font-medium text-muted-foreground hover:bg-muted hover:text-foreground transition-all border border-transparent hover:border-border"
onClick={() => {
updatePage(activePage.id, { icon: null });
setIsIconPickerOpen(false);
}}
className="flex h-8 w-8 items-center justify-center rounded-md text-destructive transition-colors hover:bg-destructive/10"
title="清除图标"
>
<Sparkles size={12} />
{activePage.icon ? '更改图标' : '添加图标'}
<X size={14} />
</button>
<div className="absolute left-0 top-full mt-1 z-50 hidden group-hover/icon-picker:block w-80 p-2 bg-popover border shadow-md rounded-md animate-in fade-in zoom-in-95">
<div className="grid grid-cols-8 gap-1 h-64 overflow-y-auto p-1">
{[
"📄", "📝", "📁", "📂", "📊", "📈", "📉", "📅", "✅", "❌", "📌", "📍", "📎", "🗑️", "⚙️", "🔒",
"✨", "💡", "🔥", "🚀", "🎨", "🎯", "🏆", "💎", "❤️", "👍", "👋", "🎉", "🌟", "⭐", "🌈", "⚡",
"🤖", "🧠", "💻", "⌨️", "📱", "⌚", "📷", "🎥", "🎧", "🎮", "🕹️", "🎲", "🧩", "🎳", "🥋", "🥊",
"🚗", "✈️", "🛸", "🌍", "🪐", "☀️", "🌙", "☁️", "🌧️", "❄️", "🌊", "💧", "🌀",
"🏠", "🏢", "🏥", "🏫", "🏰", "🏯", "⛺", "🏕️", "🌲", "🌳", "🌴", "🌵", "🌷", "🌸", "🌹", "🌻",
"🐶", "🐱", "🐭", "🐹", "🐰", "🦊", "🐻", "🐼", "🐨", "🐯", "🦁", "🐮", "🐷", "🐸", "🐵", "🐔",
"🍎", "🍌", "🍇", "🍉", "🍊", "🍋", "🍍", "🥭", "🍓", "🍒", "🍑", "🥝", "🍅", "🥑", "🍆", "🥔",
"🍔", "🍟", "🍕", "🌭", "🥪", "🌮", "🌯", "🥗", "🥘", "🍝", "🍜", "🍲", "🍛", "🍣", "🍱", "🥟",
"🍺", "🍻", "🥂", "🍷", "🥃", "🍸", "🍹", "🍾", "☕", "🍵", "🥤", "🧃", "🧊", "🥄", "🍴", "🍽️"
].map(icon => (
<button
key={icon}
onClick={(e) => {
e.stopPropagation();
updatePage(activePage.id, { icon });
}}
className={cn(
"w-8 h-8 flex items-center justify-center rounded-sm hover:bg-accent text-lg transition-colors",
activePage.icon === icon && "bg-accent/50 ring-1 ring-primary/20"
)}
>
{icon}
</button>
))}
<button
onClick={(e) => {
e.stopPropagation();
updatePage(activePage.id, { icon: null });
}}
className="w-8 h-8 flex items-center justify-center rounded-sm hover:bg-red-50 text-red-500 hover:text-red-600 transition-colors col-span-1"
title="清除图标"
>
<X size={14} />
</button>
</div>
</div>
</div>
{/* Lock Button */}
<button
onClick={() => updatePage(activePage.id, { isLocked: !activePage.isLocked })}
className={cn(
"inline-flex items-center gap-1 px-2 py-0.5 rounded-sm text-xs font-medium transition-all border border-transparent hover:border-border",
activePage.isLocked
? "text-orange-600 bg-orange-50 hover:bg-orange-100 border-orange-200"
: "text-muted-foreground hover:bg-muted hover:text-foreground"
)}
title={activePage.isLocked ? "解锁编辑" : "锁定编辑 (防止误触)"}
>
{activePage.isLocked ? <Lock size={12} /> : <Unlock size={12} />}
{activePage.isLocked ? "已锁定" : "锁定"}
</button>
{/* Last Updated Info */}
{activePage.updatedAt && (
<div className="text-xs text-muted-foreground/40 select-none flex items-center gap-1 border-l pl-2 ml-1 h-4">
<span>
{new Date(activePage.updatedAt).toLocaleString('zh-CN', {
timeZone: useSettingsStore.getState().timezone || 'Asia/Shanghai',
hour12: false
})}
</span>
</div>
)}
</div>
)}
</div>
<button
onClick={() => updatePage(activePage.id, { isLocked: !activePage.isLocked })}
className={cn(
"ui-btn-secondary px-2 py-1 text-xs",
activePage.isLocked ? "border-orange-200 bg-orange-50 text-orange-600 dark:bg-orange-900/20 dark:text-orange-300" : "text-muted-foreground"
)}
title={activePage.isLocked ? "解锁编辑" : "锁定编辑,防止误触"}
>
{activePage.isLocked ? <Lock size={12} /> : <Unlock size={12} />}
{activePage.isLocked ? "已锁定" : "锁定"}
</button>
{activePage.updatedAt && (
<div className="ml-1 h-4 border-l pl-2 text-xs text-muted-foreground/50">
{new Date(activePage.updatedAt).toLocaleString("zh-CN", {
timeZone: timezone || "Asia/Shanghai",
hour12: false,
})}
</div>
)}
</div>
{/* Editor Area or Folder Placeholder */}
{activePage.type === 'folder' ? (
<div className="flex flex-col items-center justify-center h-[50vh] text-muted-foreground animate-in fade-in duration-500">
{/* ... folder icon ... */}
{activePage.type === "folder" ? (
<div className="ui-card ui-enter flex h-[50vh] flex-col items-center justify-center gap-3 text-muted-foreground">
<div className="ui-float rounded-2xl border border-border/70 bg-muted/40 p-4">
<FolderOpen size={34} className="text-primary/80" />
</div>
<p className="text-base font-medium text-foreground"></p>
<p className="text-sm text-muted-foreground"></p>
</div>
) : (
<div className="min-h-[60vh] pb-24">
@@ -280,17 +257,18 @@ export default function Home() {
</div>
</div>
) : (
<div className="h-full flex flex-col items-center justify-center text-muted-foreground gap-4 animate-in fade-in zoom-in-95 duration-500">
<div className="w-16 h-16 bg-muted/50 rounded-2xl flex items-center justify-center">
<span className="text-4xl">👋</span>
<div className="ui-enter flex h-full flex-col items-center justify-center gap-4 text-muted-foreground">
<div className="ui-float rounded-2xl border border-border/70 bg-muted/40 p-4">
<span className="text-4xl">🧠</span>
</div>
<div className="text-center space-y-1">
<div className="space-y-1 text-center">
<h3 className="text-lg font-semibold text-foreground">使 NoteAI</h3>
<p className="text-sm text-muted-foreground/80"></p>
<p className="text-sm text-muted-foreground/90"></p>
</div>
</div>
)}
</main>
<AIChatPanel editor={editor} isOpen={isChatOpen} onClose={() => setIsChatOpen(false)} />
</div>
</ImportProvider>