@@ -2,7 +2,7 @@ import { useState, useEffect, useRef } from 'react';
22import { apiClient } from '@/api/client' ;
33import { useAuth } from '@/lib/AuthContext' ;
44import { useQuery , useMutation , useQueryClient } from '@tanstack/react-query' ;
5- import { format , parseISO , subDays , isToday } from 'date-fns' ;
5+ import { format , parseISO , subDays , isToday , eachDayOfInterval , startOfDay } from 'date-fns' ;
66import { motion , AnimatePresence } from 'framer-motion' ;
77import { toast } from 'sonner' ;
88import { Button } from '@/components/ui/button' ;
@@ -24,21 +24,24 @@ const MOODS = [
2424const getMood = ( value ) => MOODS . find ( m => m . value === value ) || MOODS [ 2 ] ;
2525
2626// ─── Mini Mood Bar Chart ─────────────────────────────────────────────────────
27- function MoodChart ( { entries } ) {
27+ function MoodChart ( { entries, onDateSelect } ) {
2828 const last7 = Array . from ( { length : 7 } , ( _ , i ) => {
2929 const date = subDays ( new Date ( ) , 6 - i ) ;
3030 const dateStr = format ( date , 'yyyy-MM-dd' ) ;
3131 const entry = entries . find ( e => e . date === dateStr ) ;
32- return { date, mood : entry ?. mood_score ?? null } ;
32+ return { date, dateStr , mood : entry ?. mood_score ?? null } ;
3333 } ) ;
3434
3535 return (
3636 < div className = "flex items-end gap-1.5 h-12" >
37- { last7 . map ( ( { date, mood } , i ) => {
37+ { last7 . map ( ( { date, dateStr , mood } , i ) => {
3838 const m = mood ? getMood ( mood ) : null ;
3939 return (
4040 < div key = { i } className = "flex flex-col items-center gap-1 flex-1" >
41- < motion . div
41+ < motion . button
42+ whileHover = { { scale : 1.1 , y : - 2 } }
43+ whileTap = { { scale : 0.9 } }
44+ onClick = { ( ) => onDateSelect ?. ( dateStr ) }
4245 initial = { { scaleY : 0 } }
4346 animate = { { scaleY : 1 } }
4447 transition = { { delay : i * 0.05 , duration : 0.4 , ease : 'easeOut' } }
@@ -47,7 +50,8 @@ function MoodChart({ entries }) {
4750 backgroundColor : m ? m . color : '#e2e8f0' ,
4851 originY : 1 ,
4952 } }
50- className = "w-full rounded-t-sm"
53+ className = "w-full rounded-t-sm cursor-pointer"
54+ title = { mood ? `Mood: ${ m . label } on ${ format ( date , 'MMM d' ) } ` : `No entry for ${ format ( date , 'MMM d' ) } ` }
5155 />
5256 < span className = "text-[9px] text-muted-foreground font-medium" >
5357 { format ( date , 'EEE' ) [ 0 ] }
@@ -165,10 +169,10 @@ function EntryCard({ entry, onEdit }) {
165169 < Button
166170 variant = "ghost"
167171 size = "icon"
168- className = "h-7 w-7 opacity-0 group- hover:opacity-100 transition-opacity shrink-0 "
172+ className = "h-8 w-8 transition-all shrink-0 hover:bg-black/5 rounded-full "
169173 onClick = { ( ) => onEdit ( entry ) }
170174 >
171- < Pencil className = "w-3.5 h-3.5 " />
175+ < Pencil className = "w-4 h-4 text-muted-foreground " />
172176 </ Button >
173177 ) }
174178 </ div >
@@ -277,20 +281,7 @@ export default function Diary() {
277281 }
278282 } , [ isDoctor , todayEntry , mode ] ) ;
279283
280- if ( isDoctor ) {
281- return (
282- < div className = "max-w-xl mx-auto py-16 px-4 text-center space-y-4 text-muted-foreground" >
283- < BookOpen className = "w-10 h-10 mx-auto opacity-30" />
284- < p className = "text-sm" > Diary entries are available in each patient's profile.</ p >
285- < Button asChild variant = "outline" className = "rounded-full gap-2" >
286- < Link to = "/patient-logs" >
287- < Users className = "w-4 h-4" />
288- Go to Patient Logs
289- </ Link >
290- </ Button >
291- </ div >
292- ) ;
293- }
284+
294285
295286 // ── Mutation ─────────────────────────────────────────────────────────────
296287 const saveMutation = useMutation ( {
@@ -354,10 +345,37 @@ export default function Diary() {
354345 saveMutation . mutate ( { entryId : editingEntry . id , mood : selectedMood , text : notes , date : editingEntry . date } ) ;
355346 } ;
356347
357- const avgMood = entries . length > 0
358- ? ( entries . slice ( 0 , 7 ) . reduce ( ( s , e ) => s + e . mood_score , 0 ) / Math . min ( entries . length , 7 ) ) . toFixed ( 1 )
348+ const avgMood = entries . length > 0
349+ ? ( entries . reduce ( ( s , e ) => s + e . mood_score , 0 ) / entries . length ) . toFixed ( 1 )
359350 : null ;
360351
352+ if ( isDoctor ) {
353+ return (
354+ < motion . div
355+ initial = { { opacity : 0 , y : 15 } }
356+ animate = { { opacity : 1 , y : 0 } }
357+ transition = { { duration : 0.6 , ease : [ 0.22 , 1 , 0.36 , 1 ] } }
358+ className = "max-w-xl mx-auto py-16 px-4 text-center space-y-6"
359+ >
360+ < div className = "w-20 h-20 bg-violet-50 rounded-3xl flex items-center justify-center mx-auto mb-6" >
361+ < BookOpen className = "w-10 h-10 text-violet-300" />
362+ </ div >
363+ < div className = "space-y-2" >
364+ < h2 className = "text-xl font-bold font-heading" > Patient Diary View</ h2 >
365+ < p className = "text-sm text-muted-foreground max-w-xs mx-auto" >
366+ Diary entries reach you directly via each patient's profile clinical dashboard.
367+ </ p >
368+ </ div >
369+ < Button asChild variant = "outline" className = "rounded-full gap-2 h-11 px-6 font-bold shadow-sm hover:shadow-md transition-all" >
370+ < Link to = "/patient-logs" >
371+ < Users className = "w-4 h-4" />
372+ View Patient Logs
373+ </ Link >
374+ </ Button >
375+ </ motion . div >
376+ ) ;
377+ }
378+
361379 const showSuccessCard = mode === 'view' && todayEntry ;
362380 const showInlineForm = mode === 'editing' ;
363381
@@ -374,7 +392,16 @@ export default function Diary() {
374392 < BookOpen className = "w-6 h-6 text-violet-500" />
375393 My Diary
376394 </ h1 >
377- < p className = "text-sm text-muted-foreground mt-0.5" > How are you feeling today?</ p >
395+ < div className = "flex items-center justify-between mt-0.5" >
396+ < p className = "text-sm text-muted-foreground" > How are you feeling today?</ p >
397+ < Button
398+ variant = "link"
399+ className = "h-auto p-0 text-violet-600 text-xs font-bold"
400+ onClick = { ( ) => document . getElementById ( 'previous-entries' ) ?. scrollIntoView ( { behavior : 'smooth' } ) }
401+ >
402+ View History ↓
403+ </ Button >
404+ </ div >
378405 </ div >
379406
380407 { /* 7-day Mood Trend */ }
@@ -391,12 +418,27 @@ export default function Diary() {
391418 </ Badge >
392419 ) }
393420 </ div >
394- < MoodChart entries = { entries } />
421+ < MoodChart
422+ entries = { entries }
423+ onDateSelect = { ( dateStr ) => {
424+ const el = document . getElementById ( `entry-${ dateStr } ` ) ;
425+ if ( el ) {
426+ el . scrollIntoView ( { behavior : 'smooth' , block : 'center' } ) ;
427+ el . classList . add ( 'ring-2' , 'ring-violet-400' , 'ring-offset-2' ) ;
428+ setTimeout ( ( ) => el . classList . remove ( 'ring-2' , 'ring-violet-400' , 'ring-offset-2' ) , 2000 ) ;
429+ } else if ( dateStr === todayStr ) {
430+ document . getElementById ( 'today-section' ) ?. scrollIntoView ( { behavior : 'smooth' } ) ;
431+ } else {
432+ toast . info ( `No entry found for ${ format ( parseISO ( dateStr ) , 'MMM d' ) } ` ) ;
433+ }
434+ } }
435+ />
395436 </ div >
396437 ) }
397438
398439 { /* Today: success card OR inline form */ }
399- < AnimatePresence mode = "wait" >
440+ < div id = "today-section" >
441+ < AnimatePresence mode = "wait" >
400442 { showSuccessCard ? (
401443 < motion . div
402444 key = "success"
@@ -538,19 +580,22 @@ export default function Diary() {
538580 </ motion . div >
539581 ) : null }
540582 </ AnimatePresence >
583+ </ div >
541584
542585 { /* Past Entries */ }
543586 { entries . filter ( e => e . date !== todayStr ) . length > 0 && (
544587 < div className = "space-y-3" >
545- < p className = "text-xs font-bold text-muted-foreground uppercase tracking-wider flex items-center gap-1.5" >
588+ < p id = "previous-entries" className = "text-xs font-bold text-muted-foreground uppercase tracking-wider flex items-center gap-1.5" >
546589 < Calendar className = "w-3.5 h-3.5" />
547590 Previous entries
548591 </ p >
549592 < div className = "space-y-2" >
550593 { entries
551594 . filter ( e => e . date !== todayStr )
552595 . map ( entry => (
553- < EntryCard key = { entry . id } entry = { entry } onEdit = { openPastEdit } />
596+ < div key = { entry . id } id = { `entry-${ entry . date } ` } >
597+ < EntryCard entry = { entry } onEdit = { openPastEdit } />
598+ </ div >
554599 ) ) }
555600 </ div >
556601 </ div >
0 commit comments