Pages corrections pages missions
This commit is contained in:
parent
f57cb42c79
commit
5395be8a3a
@ -547,36 +547,82 @@ export default function CarnetPage() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For other folders (Bloc-notes), check if a note with the same title already exists
|
||||
// This prevents duplicates when creating new notes
|
||||
if (!note.id || note.id.startsWith('temp-')) {
|
||||
// This is a new note (no id or temporary id), check for duplicates
|
||||
const sanitizedTitle = noteTitle ? noteTitle.replace(/[^a-zA-Z0-9._-]/g, '_') : 'Untitled';
|
||||
const expectedFileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}${sanitizedTitle.endsWith('.md') ? '' : '.md'}`;
|
||||
|
||||
// Check if a note with this title already exists
|
||||
const existingNote = notes.find(n => {
|
||||
const nTitle = n.title || '';
|
||||
const nId = n.id || '';
|
||||
// Check if title matches or if the id matches the expected fileKey
|
||||
return (nTitle === noteTitle && noteTitle !== '') ||
|
||||
(noteTitle === '' && (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '')) ||
|
||||
nId === expectedFileKey;
|
||||
});
|
||||
|
||||
if (existingNote) {
|
||||
// Update the existing note instead of creating a new one
|
||||
console.log(`[handleSaveNote] Found existing note with same title "${noteTitle}", updating: ${existingNote.id}`);
|
||||
note.id = existingNote.id;
|
||||
noteTitle = existingNote.title || noteTitle || 'Untitled';
|
||||
fileKey = existingNote.id;
|
||||
} else {
|
||||
// No existing note found, use the expected fileKey
|
||||
fileKey = expectedFileKey;
|
||||
}
|
||||
} else {
|
||||
// Note has an id, use it (this is an update to an existing note)
|
||||
// For other folders (Bloc-notes)
|
||||
// Refresh notes list to ensure we have the latest data
|
||||
await fetchNotes(true);
|
||||
|
||||
// If note has a valid id (not temp), use it directly
|
||||
if (note.id && !note.id.startsWith('temp-') && note.id.includes(`user-${session?.user?.id}/`)) {
|
||||
fileKey = note.id;
|
||||
console.log(`[handleSaveNote] Using existing note id: ${fileKey}`);
|
||||
} else {
|
||||
// This is a new note or note with temp id
|
||||
// Strategy: If we have a title, check for existing note with that title
|
||||
// If no title or title is "Untitled", check for existing "Untitled" note
|
||||
// If we're updating from "Untitled" to a real title, find the "Untitled" note and update it
|
||||
|
||||
if (noteTitle && noteTitle.trim() !== '' && noteTitle !== 'Untitled') {
|
||||
// Note has a real title, check if a note with this title already exists
|
||||
const sanitizedTitle = noteTitle.replace(/[^a-zA-Z0-9._-]/g, '_');
|
||||
const expectedFileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}.md`;
|
||||
|
||||
const existingNoteWithTitle = notes.find(n => {
|
||||
const nTitle = n.title || '';
|
||||
const nId = n.id || '';
|
||||
// Check if title matches exactly or if the id matches the expected fileKey
|
||||
return (nTitle === noteTitle && nTitle !== 'Untitled') || nId === expectedFileKey;
|
||||
});
|
||||
|
||||
if (existingNoteWithTitle) {
|
||||
// Update the existing note with this title
|
||||
console.log(`[handleSaveNote] Found existing note with title "${noteTitle}", updating: ${existingNoteWithTitle.id}`);
|
||||
note.id = existingNoteWithTitle.id;
|
||||
fileKey = existingNoteWithTitle.id;
|
||||
} else {
|
||||
// Check if there's an "Untitled" note we should update (user added title to Untitled note)
|
||||
const untitledNote = notes.find(n => {
|
||||
const nTitle = n.title || '';
|
||||
return (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') &&
|
||||
n.id && n.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`);
|
||||
});
|
||||
|
||||
if (untitledNote) {
|
||||
// Rename the Untitled note with the new title
|
||||
// Use the new fileKey based on the new title
|
||||
console.log(`[handleSaveNote] Renaming Untitled note to "${noteTitle}": ${untitledNote.id} -> ${expectedFileKey}`);
|
||||
// Store the old id to delete it later
|
||||
const oldUntitledId = untitledNote.id;
|
||||
fileKey = expectedFileKey;
|
||||
note.id = fileKey; // Update note.id to the new fileKey
|
||||
// We'll delete the old Untitled file after saving
|
||||
// Store it in a variable accessible after the save
|
||||
(note as any)._oldUntitledId = oldUntitledId;
|
||||
} else {
|
||||
// No existing note found, create new one
|
||||
fileKey = expectedFileKey;
|
||||
console.log(`[handleSaveNote] Creating new note with fileKey: ${fileKey}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No title or title is "Untitled", check for existing Untitled note
|
||||
const untitledNote = notes.find(n => {
|
||||
const nTitle = n.title || '';
|
||||
return (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') &&
|
||||
n.id && n.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`);
|
||||
});
|
||||
|
||||
if (untitledNote) {
|
||||
// Use existing Untitled note
|
||||
console.log(`[handleSaveNote] Using existing Untitled note: ${untitledNote.id}`);
|
||||
note.id = untitledNote.id;
|
||||
noteTitle = 'Untitled';
|
||||
fileKey = untitledNote.id;
|
||||
} else {
|
||||
// Create new Untitled note
|
||||
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/Untitled.md`;
|
||||
console.log(`[handleSaveNote] Creating new Untitled note: ${fileKey}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -614,6 +660,31 @@ export default function CarnetPage() {
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
// If we renamed a note from Untitled to a title, delete the old Untitled file
|
||||
const oldUntitledId = (note as any)._oldUntitledId;
|
||||
if (oldUntitledId && oldUntitledId !== fileKey) {
|
||||
try {
|
||||
const deleteResponse = await fetch(`/api/storage/files`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: oldUntitledId,
|
||||
folder: selectedFolder.toLowerCase()
|
||||
})
|
||||
});
|
||||
if (deleteResponse.ok) {
|
||||
console.log(`[handleSaveNote] Deleted old Untitled file: ${oldUntitledId}`);
|
||||
} else {
|
||||
console.warn(`[handleSaveNote] Failed to delete old Untitled file: ${oldUntitledId}`);
|
||||
}
|
||||
} catch (deleteError) {
|
||||
console.error('Error deleting old Untitled file:', deleteError);
|
||||
// Continue anyway, the new file was created successfully
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate the cache for this folder to ensure fresh data on next fetch
|
||||
if (session?.user?.id) {
|
||||
invalidateFolderCache(session.user.id, selectedFolder);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user