Images adding in notes and cleanup of orphaned images from notes
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { type Component, createSignal, createMemo, createEffect, For, Show } from "solid-js";
|
||||
import { store, renameTagDefinition, updateNote, removeNote, restoreNote } from "@/store";
|
||||
import { type Component, createSignal, createMemo, createEffect, onCleanup, For, Show } from "solid-js";
|
||||
import { store, renameTagDefinition, updateNote, removeNote, restoreNote, setActiveNoteId, cleanupNoteAttachments } from "@/store";
|
||||
import { pb } from "@/lib/pocketbase";
|
||||
import { type Note } from "@/store";
|
||||
import { Trash2, Lock, Unlock, Search, Link, X, ChevronRight } from "lucide-solid";
|
||||
@@ -21,6 +21,38 @@ export const NotepadView: Component<{
|
||||
|
||||
const currentUserId = pb.authStore.model?.id;
|
||||
|
||||
// Sync activeNoteId for slash commands and run cleanup on unmount/change
|
||||
createEffect(() => {
|
||||
// By reading the prop inside the effect block, SolidJS tracks it reactively.
|
||||
const currentId = props.selectedNoteId;
|
||||
setActiveNoteId(currentId);
|
||||
|
||||
// This will run when the effect re-runs (currentId changes) or the component unmounts
|
||||
onCleanup(() => {
|
||||
setActiveNoteId(null);
|
||||
|
||||
// 1. Immediately flush any pending debounce updates for the outgoing note
|
||||
if (contentUpdateTimeout) {
|
||||
clearTimeout(contentUpdateTimeout);
|
||||
if (pendingContentUpdate) {
|
||||
// Fire update manually to PocketBase & Store synchronously (fire-and-forget)
|
||||
updateNote(pendingContentUpdate.id, { content: pendingContentUpdate.html }).catch(console.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Clear pending state
|
||||
contentUpdateTimeout = undefined;
|
||||
pendingContentUpdate = null;
|
||||
|
||||
// 3. Trigger cleanup after a tiny delay so the updateNote above processes first
|
||||
if (currentId) {
|
||||
setTimeout(() => {
|
||||
cleanupNoteAttachments(currentId).catch(console.error);
|
||||
}, 100);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Derived states
|
||||
const activeNote = createMemo(() => {
|
||||
const id = props.selectedNoteId?.trim();
|
||||
@@ -32,10 +64,15 @@ export const NotepadView: Component<{
|
||||
};
|
||||
|
||||
let contentUpdateTimeout: number | undefined;
|
||||
let pendingContentUpdate: { id: string, html: string } | null = null;
|
||||
|
||||
const handleUpdateContent = (id: string, html: string) => {
|
||||
clearTimeout(contentUpdateTimeout);
|
||||
pendingContentUpdate = { id, html };
|
||||
|
||||
contentUpdateTimeout = window.setTimeout(async () => {
|
||||
await updateNote(id, { content: html });
|
||||
pendingContentUpdate = null;
|
||||
}, 500);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user