diff --git a/app/pages/page.tsx b/app/pages/page.tsx index a886e6e..05caebe 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -12,6 +12,7 @@ import { ContactsView } from '@/components/carnet/contacts-view'; import { X, Menu } from "lucide-react"; import { ContactDetails } from '@/components/carnet/contact-details'; import { parse as parseVCard, format as formatVCard } from 'vcard-parser'; +import { format } from 'date-fns'; import { PaneLayout } from './pane-layout'; import { notesCache, noteContentCache, foldersCache, invalidateFolderCache, invalidateNoteCache } from '@/lib/cache-utils'; @@ -469,8 +470,29 @@ export default function CarnetPage() { const handleSaveNote = async (note: Note) => { try { setIsSaving(true); + + // For Diary and Health, ensure title is formatted with date + let noteTitle = note.title || 'Untitled'; + if ((selectedFolder === 'Diary' || selectedFolder === 'Health') && !note.id) { + // For new notes in Diary/Health, use today's date as title + const today = new Date(); + const dateStr = format(today, 'yyyy-MM-dd'); + noteTitle = dateStr; + + // Check if a note with this date already exists + const existingNote = notes.find(n => { + return n.title?.startsWith(dateStr) || n.id?.includes(dateStr); + }); + + if (existingNote) { + // Update the existing note instead of creating a new one + console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}`); + note.id = existingNote.id; + noteTitle = existingNote.title; // Keep the existing title format + } + } + // Construct API payload with lowercase folder name - const noteTitle = note.title || 'Untitled'; const payload = { id: note.id || `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}`, title: noteTitle, @@ -483,7 +505,7 @@ export default function CarnetPage() { const endpoint = '/api/storage/files'; const method = note.id ? 'PUT' : 'POST'; - console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}`); + console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}, title: ${noteTitle}`); const response = await fetch(endpoint, { method, @@ -617,18 +639,55 @@ export default function CarnetPage() { } }; - const handleNewNote = () => { - setSelectedNote({ - id: '', - title: '', - content: '', - lastModified: new Date().toISOString(), - type: 'file', - mime: 'text/markdown', - etag: '' - }); - if (isMobile) { - setShowNotes(false); + const handleNewNote = async () => { + // For Diary and Health folders, check if a note exists for today + if (selectedFolder === 'Diary' || selectedFolder === 'Health') { + const today = new Date(); + const dateStr = format(today, 'yyyy-MM-dd'); + const dateTitle = `${dateStr}`; + + // Check if a note with this date already exists + const existingNote = notes.find(note => { + // Check if note title starts with today's date + return note.title?.startsWith(dateStr); + }); + + if (existingNote) { + // Open the existing note for today + console.log(`[handleNewNote] Found existing note for today: ${existingNote.title}`); + handleNoteSelect(existingNote); + return; + } + + // Create a new note with today's date as title + const newNote: Note = { + id: '', + title: dateStr, // Use date as title for Diary/Health + content: '', + lastModified: today.toISOString(), + type: 'file', + mime: 'text/markdown', + etag: '' + }; + + setSelectedNote(newNote); + if (isMobile) { + setShowNotes(false); + } + } else { + // For other folders, create a blank note + setSelectedNote({ + id: '', + title: '', + content: '', + lastModified: new Date().toISOString(), + type: 'file', + mime: 'text/markdown', + etag: '' + }); + if (isMobile) { + setShowNotes(false); + } } }; diff --git a/components/carnet/editor.tsx b/components/carnet/editor.tsx index ded4801..956ccfc 100644 --- a/components/carnet/editor.tsx +++ b/components/carnet/editor.tsx @@ -84,7 +84,14 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N }; if (note) { - setTitle(note.title || ''); + // For Diary and Health, if it's a new note (no id), set title to today's date + if ((currentFolder === 'Diary' || currentFolder === 'Health') && !note.id) { + const today = new Date(); + const dateStr = today.toISOString().split('T')[0]; // YYYY-MM-DD format + setTitle(dateStr); + } else { + setTitle(note.title || ''); + } if (note.id) { fetchNoteContent(); } else { @@ -94,7 +101,7 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N setTitle(''); setContent(''); } - }, [note, router]); + }, [note, router, currentFolder]); const handleTitleChange = (e: React.ChangeEvent) => { setTitle(e.target.value); @@ -236,7 +243,11 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N value={title ?? ''} onChange={handleTitleChange} placeholder="Titre" - className="w-full text-xl font-semibold text-carnet-text-primary placeholder-carnet-text-muted focus:outline-none bg-transparent" + disabled={currentFolder === 'Diary' || currentFolder === 'Health'} + className={`w-full text-xl font-semibold text-carnet-text-primary placeholder-carnet-text-muted focus:outline-none bg-transparent ${ + (currentFolder === 'Diary' || currentFolder === 'Health') ? 'opacity-60 cursor-not-allowed' : '' + }`} + title={currentFolder === 'Diary' || currentFolder === 'Health' ? 'Le titre est automatiquement la date du jour pour les journaux' : ''} />