import dayjs from 'dayjs' ; import { RootState } from './store' ; interface Calendar { [ key ] : { selected : boolean ; } ; } export const selectFilePaths = ( state ) => state . meditation . filepaths || [ ] ; export const selectActivity = ( state ) => state . meditation . activity ; export const selectFavourites = ( state ) => state . meditation . favourites || [ ] ; export const selectTotalSessions = ( state ) => Object . keys ( state . meditation . activity ) . length || 0 ; export const selectTotalDuration = ( state ) => { let total = 0 ; Object . values ( state . meditation . activity ) . forEach ( ( a ) => { if ( a . duration ) { total += a . duration ; } } ) ; return total ; } ; export const selectCalendar = ( state ) => { const { activity } = state . meditation ; const calendar = { } ; Object . keys ( activity ) . forEach ( ( k ) => { const date = dayjs ( parseInt ( k , 10 ) ) . format ( 'YYYY-MM-DD' ) ; calendar [ date ] = { selected : true , } ; } ) ; return calendar ; } ; export const selectStreak = ( state ) => { const calendar = selectCalendar ( state ) ; let streak = 0 ; let n = 0 ; while ( true ) { const date = dayjs ( ) . subtract ( n , 'day' ) . format ( 'YYYY-MM-DD' ) ; if ( ! calendar [ date ] && n !== 0 ) { break ; } if ( calendar [ date ] ) { streak += 1 ; } n += 1 ; } return streak ; } ;