134 lines
4.2 KiB
TypeScript
134 lines
4.2 KiB
TypeScript
"use client";
|
||
|
||
import { useMemo, useState } from "react";
|
||
import { ArrowRight, Loader2, NotebookPen, Sparkles } from "lucide-react";
|
||
import { toast } from "sonner";
|
||
|
||
import { useTheme } from "@/contexts/ThemeContext";
|
||
import { createSearchSpace } from "@/core/note/api";
|
||
import { useNoteAuth } from "@/hooks/useNoteAuth";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
export default function LightAppNewNotePage() {
|
||
const { theme } = useTheme();
|
||
const isDark = theme === "dark";
|
||
const { loading: authLoading, error: authError } = useNoteAuth();
|
||
const [titleInput, setTitleInput] = useState("");
|
||
const [creating, setCreating] = useState(false);
|
||
|
||
const hasTitle = useMemo(() => titleInput.trim().length > 0, [titleInput]);
|
||
const isBusy = authLoading || creating;
|
||
|
||
const handleCreate = async () => {
|
||
const name = titleInput.trim();
|
||
if (!name || isBusy || authError) {
|
||
return;
|
||
}
|
||
|
||
setCreating(true);
|
||
try {
|
||
const searchSpace = await createSearchSpace({ name });
|
||
toast.success(`笔记「${searchSpace.name}」创建成功`);
|
||
setTitleInput("");
|
||
} catch (error) {
|
||
const message =
|
||
error instanceof Error ? error.message : "创建笔记失败,请稍后重试";
|
||
toast.error(message);
|
||
} finally {
|
||
setCreating(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div
|
||
className={cn(
|
||
"flex h-full w-full items-center justify-center px-8",
|
||
isDark ? "bg-slate-900/20" : "bg-[#fdfcfc]",
|
||
)}
|
||
>
|
||
<div className="w-full max-w-[720px] -translate-y-12 space-y-8">
|
||
<div className="flex items-center gap-3">
|
||
<NotebookPen
|
||
className={cn("h-5 w-5 shrink-0", isDark ? "text-slate-400" : "text-black/70")}
|
||
/>
|
||
<h1
|
||
className={cn(
|
||
"text-base font-medium tracking-tight",
|
||
isDark ? "text-slate-200" : "text-black/85",
|
||
)}
|
||
>
|
||
新建笔记
|
||
</h1>
|
||
</div>
|
||
|
||
<div className="flex items-center gap-3">
|
||
<input
|
||
value={titleInput}
|
||
onChange={(e) => setTitleInput(e.target.value)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter") {
|
||
e.preventDefault();
|
||
void handleCreate();
|
||
}
|
||
}}
|
||
placeholder="新建笔记"
|
||
disabled={isBusy}
|
||
className={cn(
|
||
"min-w-0 flex-1 bg-transparent text-[42px] leading-tight font-normal tracking-tight outline-none",
|
||
hasTitle
|
||
? isDark
|
||
? "text-white"
|
||
: "text-black/85"
|
||
: isDark
|
||
? "text-slate-500 placeholder:text-slate-600"
|
||
: "text-black/25 placeholder:text-black/25",
|
||
)}
|
||
/>
|
||
{hasTitle && (
|
||
<button
|
||
type="button"
|
||
onClick={() => void handleCreate()}
|
||
disabled={isBusy || !!authError}
|
||
className={cn(
|
||
"inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full transition-colors disabled:cursor-not-allowed disabled:opacity-70",
|
||
isDark
|
||
? "bg-blue-500 text-white hover:bg-blue-400"
|
||
: "bg-sky-200 text-slate-700 hover:bg-sky-300",
|
||
)}
|
||
aria-label="创建笔记"
|
||
>
|
||
{creating || authLoading ? (
|
||
<Loader2 className="h-4 w-4 animate-spin" />
|
||
) : (
|
||
<ArrowRight className="h-4 w-4" />
|
||
)}
|
||
</button>
|
||
)}
|
||
</div>
|
||
|
||
<div
|
||
className={cn(
|
||
"flex h-11 items-center gap-2.5 rounded-xl px-4",
|
||
isDark
|
||
? "border border-slate-700 bg-slate-800/70"
|
||
: "border border-black/5 bg-[#ececef]",
|
||
)}
|
||
>
|
||
<Sparkles
|
||
className={cn("h-3.5 w-3.5 shrink-0", isDark ? "text-slate-400" : "text-black/55")}
|
||
/>
|
||
<p
|
||
className={cn(
|
||
"text-sm",
|
||
isDark ? "text-slate-400" : "text-black/55",
|
||
)}
|
||
>
|
||
新建笔记开始使用
|
||
{authError ? `(${authError})` : ""}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|