From 26dfe80347b8ae7904fbf92b851ae9969f6b2c21 Mon Sep 17 00:00:00 2001 From: Dominik Hryshaiev Date: Sun, 26 Jan 2025 20:34:16 +0100 Subject: [PATCH] fix(habits): get_longest_streak db fn returns correct data (#152) --- src/components/calendar-month/NoteDialog.tsx | 4 ++ .../habit/habits-page/HabitLastEntryCell.tsx | 23 ++++++-- .../habits-page/HabitLongestStreakCell.tsx | 36 ++++++++++--- .../habit/habits-page/HabitsPage.tsx | 20 +++++-- .../habits-page/HabitsTotalEntriesCell.tsx | 4 +- src/models/occurrence.model.ts | 8 ++- src/services/occurrences.service.ts | 16 +++--- supabase/database.types.ts | 15 +++--- ...36_rewrite_get_longest_streak_function.sql | 53 +++++++++++++++++++ 9 files changed, 140 insertions(+), 39 deletions(-) create mode 100644 supabase/migrations/20250126165836_rewrite_get_longest_streak_function.sql diff --git a/src/components/calendar-month/NoteDialog.tsx b/src/components/calendar-month/NoteDialog.tsx index 417ba3c..f926a55 100644 --- a/src/components/calendar-month/NoteDialog.tsx +++ b/src/components/calendar-month/NoteDialog.tsx @@ -45,6 +45,10 @@ const NoteDialog = ({ open, onClose, date }: NoteDialogProps) => { if (existingNote?.content && !content) { setContent(existingNote.content); } + + return () => { + setContent(''); + }; }, [existingNote, content]); const handleSubmit: MouseEventHandler = async (event) => { diff --git a/src/components/habit/habits-page/HabitLastEntryCell.tsx b/src/components/habit/habits-page/HabitLastEntryCell.tsx index 89c6557..72c8ee0 100644 --- a/src/components/habit/habits-page/HabitLastEntryCell.tsx +++ b/src/components/habit/habits-page/HabitLastEntryCell.tsx @@ -1,6 +1,12 @@ +import { Tooltip } from '@nextui-org/react'; import { getLatestHabitOccurrenceTimestamp } from '@services'; import { capitalizeFirstLetter } from '@utils'; -import { formatDistanceStrict, formatRelative, isThisWeek } from 'date-fns'; +import { + format, + formatDistanceStrict, + formatRelative, + isThisWeek, +} from 'date-fns'; import { enGB } from 'date-fns/locale'; import React from 'react'; @@ -39,11 +45,18 @@ const HabitLastEntryCell = ({ id }: { id: number }) => { }; return latestOccurrenceTimestamp ? ( -

- {capitalizeFirstLetter(formatRelativeDate(latestOccurrenceTimestamp))} -

+ + + {capitalizeFirstLetter(formatRelativeDate(latestOccurrenceTimestamp))} + + ) : ( -

None

+ None ); }; diff --git a/src/components/habit/habits-page/HabitLongestStreakCell.tsx b/src/components/habit/habits-page/HabitLongestStreakCell.tsx index 39aa0d7..d7ce09b 100644 --- a/src/components/habit/habits-page/HabitLongestStreakCell.tsx +++ b/src/components/habit/habits-page/HabitLongestStreakCell.tsx @@ -1,21 +1,41 @@ +import type { Streak } from '@models'; +import { Tooltip } from '@nextui-org/react'; import { getLongestHabitStreak } from '@services'; import React from 'react'; +const options: Intl.DateTimeFormatOptions = { + weekday: 'short', + year: 'numeric', + month: 'short', + day: 'numeric', +}; + +const dateTimeFormat = new Intl.DateTimeFormat('en', options); + const HabitLongestStreakCell = ({ id }: { id: number }) => { - const [longestStreakLength, setLongestStreakLength] = React.useState< - number | null - >(null); + const [longestStreak, setLongestStreak] = React.useState({ + streakLength: null, + streakStart: null, + streakEnd: null, + }); React.useEffect(() => { - getLongestHabitStreak(id).then((streakLength) => { - setLongestStreakLength(streakLength); + getLongestHabitStreak(id).then((longestStreak) => { + setLongestStreak(longestStreak); }); }, [id]); - return longestStreakLength ? ( -

{longestStreakLength} days

+ const range = dateTimeFormat.formatRange( + new Date(longestStreak.streakStart || 0), + new Date(longestStreak.streakEnd || 0) + ); + + return longestStreak.streakLength ? ( + + {longestStreak.streakLength} days + ) : ( -

None

+ None ); }; diff --git a/src/components/habit/habits-page/HabitsPage.tsx b/src/components/habit/habits-page/HabitsPage.tsx index df031c1..3d78104 100644 --- a/src/components/habit/habits-page/HabitsPage.tsx +++ b/src/components/habit/habits-page/HabitsPage.tsx @@ -28,7 +28,13 @@ import HabitLastEntryCell from './HabitLastEntryCell'; import HabitLongestStreakCell from './HabitLongestStreakCell'; import HabitsTotalEntriesCell from './HabitsTotalEntriesCell'; -const habitColumns = [ +type Column = { + key: string; + label: string; + align?: 'start' | 'center' | 'end'; +}; + +const habitColumns: Column[] = [ { key: 'icon', label: 'Icon', @@ -56,10 +62,12 @@ const habitColumns = [ { key: 'total-entries', label: 'Total entries', + align: 'center', }, { key: 'actions', label: 'Actions', + align: 'end', }, ]; @@ -101,7 +109,7 @@ const HabitsPage = () => { return ( <> -

+

Your habits

{ > {(column) => ( - {column.label} + + {column.label} + )} @@ -145,11 +155,11 @@ const HabitsPage = () => { - + -
+