This commit is contained in:
alma 2026-01-11 23:18:00 +01:00
parent 92da9196c6
commit 05d97e2c50
2 changed files with 50 additions and 2 deletions

View File

@ -180,6 +180,35 @@ export default function CarnetPage() {
}
}, [selectedFolder, session?.user?.id]);
// Listen for note-saved events to refresh the list
useEffect(() => {
const handleNoteSaved = (event: CustomEvent) => {
const folder = event.detail?.folder;
if (folder && selectedFolder.toLowerCase() === folder.toLowerCase()) {
console.log('Note saved event received, refreshing notes list');
// Clear cache and fetch fresh notes
const cacheKey = `${session?.user?.id}-${folder}`;
if (notesCache.current[cacheKey]) {
delete notesCache.current[cacheKey];
}
try {
localStorage.removeItem(`notes-cache-${cacheKey}`);
} catch (error) {
console.error('Error removing cache:', error);
}
// Fetch notes will be called by the existing useEffect when selectedFolder changes
// But we can also call it directly here
fetchNotes();
}
};
window.addEventListener('note-saved', handleNoteSaved as EventListener);
return () => {
window.removeEventListener('note-saved', handleNoteSaved as EventListener);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedFolder, session?.user?.id]);
const parseVCardContent = (content: string): Contact[] => {
try {
// Split the content into individual vCards

View File

@ -55,11 +55,11 @@ export function NotesDialog({ open, onOpenChange }: NotesDialogProps) {
setError("");
// Construct API payload with lowercase folder name (always "notes" for quick notes)
// Note: The API will construct the full path from folder and title
const payload = {
id: `user-${session?.user?.id}/notes/${title}${title.endsWith('.md') ? '' : '.md'}`,
title: title,
content: content,
folder: "notes", // Always save to Notes folder
folder: "notes", // Always save to Notes folder (lowercase for consistency)
mime: "text/markdown"
};
@ -73,9 +73,28 @@ export function NotesDialog({ open, onOpenChange }: NotesDialogProps) {
});
if (!response.ok) {
const errorText = await response.text();
console.error('Failed to save note:', errorText);
throw new Error('Failed to save note');
}
// Invalidate cache and trigger refresh
try {
// Clear localStorage cache for notes
const userId = session?.user?.id;
if (userId) {
const cacheKey = `${userId}-notes`;
localStorage.removeItem(`notes-cache-${cacheKey}`);
}
// Dispatch custom event to notify pages to refresh
window.dispatchEvent(new CustomEvent('note-saved', {
detail: { folder: 'notes' }
}));
} catch (cacheError) {
console.error('Error clearing cache:', cacheError);
}
// Reset form and close dialog
setTitle("");
setContent("");