2025-12-08 22:08:30 +00:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2025-12-11 22:20:23 +00:00
|
|
|
import "../../main.css";
|
2025-12-08 22:08:30 +00:00
|
|
|
import { motion } from "framer-motion";
|
2025-12-11 22:20:23 +00:00
|
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
|
|
|
import { useNoteStore } from "@/stores/notesStore";
|
|
|
|
|
import { useUIStore } from "@/stores/uiStore";
|
|
|
|
|
import { Login } from "../Login";
|
|
|
|
|
import { TiptapEditor } from "../TipTap";
|
|
|
|
|
import { Sidebar } from "./components/sidebar/SideBar";
|
|
|
|
|
import { StatusIndicator } from "./components/StatusIndicator";
|
2025-11-23 09:08:01 +00:00
|
|
|
|
|
|
|
|
function Home() {
|
2025-12-11 22:20:23 +00:00
|
|
|
const [newFolder] = useState(false);
|
2025-12-08 22:08:30 +00:00
|
|
|
const [lastSavedNote, setLastSavedNote] = useState<{
|
|
|
|
|
id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
content: string;
|
|
|
|
|
} | null>(null);
|
2025-11-25 22:09:02 +00:00
|
|
|
|
2025-12-11 22:20:23 +00:00
|
|
|
const { loadFolderTree, updateNote, setContent, selectedNote, setTitle } =
|
|
|
|
|
useNoteStore();
|
2025-11-29 12:45:41 +00:00
|
|
|
|
2025-12-11 22:20:23 +00:00
|
|
|
const { encryptionKey } = useAuthStore();
|
2025-12-08 22:08:30 +00:00
|
|
|
|
2025-12-11 22:20:23 +00:00
|
|
|
const { showModal, setUpdating } = useUIStore();
|
2025-11-25 22:09:02 +00:00
|
|
|
|
|
|
|
|
const newFolderRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
2025-11-23 09:08:01 +00:00
|
|
|
useEffect(() => {
|
2025-12-11 22:20:23 +00:00
|
|
|
if (!encryptionKey) return;
|
2025-11-23 09:08:01 +00:00
|
|
|
loadFolderTree();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-11-25 22:09:02 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (newFolder && newFolderRef.current) {
|
|
|
|
|
newFolderRef.current.focus();
|
|
|
|
|
}
|
|
|
|
|
}, [newFolder]);
|
|
|
|
|
|
2025-12-08 22:08:30 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedNote) return;
|
|
|
|
|
if (!encryptionKey) return; // Don't try to save without encryption key
|
|
|
|
|
|
|
|
|
|
// Check if content or title actually changed (not just selecting a different note)
|
|
|
|
|
const hasChanges =
|
|
|
|
|
lastSavedNote &&
|
|
|
|
|
lastSavedNote.id === selectedNote.id &&
|
|
|
|
|
(lastSavedNote.title !== selectedNote.title ||
|
|
|
|
|
lastSavedNote.content !== selectedNote.content);
|
|
|
|
|
|
|
|
|
|
// If it's a new note selection, just update lastSavedNote without saving
|
|
|
|
|
if (!lastSavedNote || lastSavedNote.id !== selectedNote.id) {
|
|
|
|
|
setLastSavedNote({
|
|
|
|
|
id: selectedNote.id,
|
|
|
|
|
title: selectedNote.title,
|
|
|
|
|
content: selectedNote.content,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasChanges) return;
|
|
|
|
|
|
|
|
|
|
const timer = setTimeout(async () => {
|
|
|
|
|
setUpdating(true);
|
|
|
|
|
await handleUpdate();
|
|
|
|
|
setLastSavedNote({
|
|
|
|
|
id: selectedNote.id,
|
|
|
|
|
title: selectedNote.title,
|
|
|
|
|
content: selectedNote.content,
|
|
|
|
|
});
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, [selectedNote, encryptionKey]);
|
|
|
|
|
|
|
|
|
|
const handleUpdate = async () => {
|
|
|
|
|
if (!selectedNote) return;
|
|
|
|
|
if (!encryptionKey) {
|
|
|
|
|
setUpdating(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await updateNote(selectedNote.id);
|
|
|
|
|
console.log(selectedNote.id);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to update note:", error);
|
|
|
|
|
} finally {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setUpdating(false);
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
2025-11-23 09:08:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-30 20:32:22 +00:00
|
|
|
<div className="flex bg-ctp-base h-screen text-ctp-text overflow-hidden">
|
|
|
|
|
{/* Sidebar */}
|
2025-12-08 22:08:30 +00:00
|
|
|
{showModal && <Modal />}
|
2025-11-30 19:40:10 +00:00
|
|
|
|
2025-12-11 22:20:23 +00:00
|
|
|
<Sidebar />
|
2025-11-25 22:09:02 +00:00
|
|
|
|
2025-11-30 20:32:22 +00:00
|
|
|
{/* Main editor area */}
|
|
|
|
|
<div className="flex flex-col w-full h-screen overflow-hidden">
|
2025-12-08 22:08:30 +00:00
|
|
|
{/*<Editor />*/}
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
placeholder="Untitled note..."
|
|
|
|
|
value={selectedNote?.title || ""}
|
|
|
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
|
|
|
className="w-full px-4 py-3 text-3xl font-semibold bg-transparentfocus:outline-none focus:border-ctp-mauve transition-colors placeholder:text-ctp-overlay0 text-ctp-text"
|
|
|
|
|
/>
|
|
|
|
|
<TiptapEditor
|
|
|
|
|
key={selectedNote?.id}
|
|
|
|
|
content={selectedNote?.content || ""}
|
|
|
|
|
onChange={setContent}
|
|
|
|
|
/>
|
2025-11-23 09:08:01 +00:00
|
|
|
</div>
|
2025-11-30 20:32:22 +00:00
|
|
|
|
2025-12-11 22:20:23 +00:00
|
|
|
<StatusIndicator />
|
2025-11-30 20:32:22 +00:00
|
|
|
</div>
|
2025-11-23 09:08:01 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Home;
|
2025-12-08 22:08:30 +00:00
|
|
|
|
|
|
|
|
const Modal = () => {
|
|
|
|
|
const { setShowModal } = useUIStore();
|
|
|
|
|
return (
|
|
|
|
|
<motion.div
|
|
|
|
|
initial={{ opacity: 0 }}
|
|
|
|
|
animate={{ opacity: 1 }}
|
|
|
|
|
onClick={() => setShowModal(false)}
|
|
|
|
|
className="absolute h-screen w-screen flex items-center justify-center bg-ctp-crust/60 z-50"
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
|
className="w-2/3 h-2/3 bg-ctp-base rounded-xl border-ctp-surface2 border p-5"
|
|
|
|
|
>
|
|
|
|
|
<Login />
|
|
|
|
|
</div>
|
|
|
|
|
</motion.div>
|
|
|
|
|
);
|
|
|
|
|
};
|