Skip to content

feat: unify sleep reports and implement industry-standard "Wake-up Rule"#784

Merged
CodeWithCJ merged 3 commits intomainfrom
dev
Feb 28, 2026
Merged

feat: unify sleep reports and implement industry-standard "Wake-up Rule"#784
CodeWithCJ merged 3 commits intomainfrom
dev

Conversation

@CodeWithCJ
Copy link
Owner

@CodeWithCJ CodeWithCJ commented Feb 28, 2026

Description

Provide a brief summary of your changes.

  • Attribution: Updated sleep entry logic to attribute data to the wake-up date, aligning with Apple Health and Fitbit standards.
  • Formatting: Unified all sleep durations across the app to use the consistent 7h 15m (or 15m) format.
  • Accuracy: Aligned legacy charts and tables to use your Personalized Sleep Need baseline instead of a hardcoded 8 hours.
  • Fixes: Resolved a date-shifting bug caused by UTC parsing and synchronized the manual entry editor with the saving logic to prevent 24-hour mismatches.
  • UI Consolidation: Cleaned up the Check-in page by organizing sleep stages and health metrics into a structured grid and fixing misleading title dates.

Related Issue

PR type [x] Issue [ ] New Feature [ ] Documentation
Linked Issue: # #664

Checklist

Please check all that apply:

  • Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers (required for new features).
  • Tests: I have included automated tests for my changes.
  • Screenshots: I have attached "Before" vs "After" screenshots below (required for UI changes).
  • Quality: I have run npm run lint and npm run format (especially for Frontend).
  • Translations: I have only updated the English (en) translation file (if applicable).
  • Architecture: My code follows the existing architecture standards.
  • Database Security: I have updated rls_policies.sql for any new user-specific tables.
  • Integrity: I certify this is my own work, free of malicious code(phishing, malware, etc.)
  • License: I agree to the License terms.

Screenshots (if applicable)

Before

[Insert screenshot/GIF here]

After

[Insert screenshot/GIF here]

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily enhances the user experience in the sleep analytics section by standardizing and improving the readability of sleep duration displays. It achieves this by refactoring existing time formatting utilities to present durations in a more intuitive "Hh Mm" format, ensuring consistency across the application. Additionally, a minor robustness improvement was made to a related component.

Highlights

  • Sleep Duration Display Format: The display format for sleep durations in analytics tables has been updated from HH:MM to Hh Mm (e.g., 7h 15m), improving readability.
  • Time Formatting Utility Refinement: The formatMinutesToHHMM and formatSecondsToHHMM utility functions were refactored to support the new Hh Mm format, handle negative durations, and simplify output for durations less than an hour.
  • Data Null Check: A null check was added to the ChronotypeCard component to prevent potential errors if the data prop is undefined.
Changelog
  • SparkyFitnessFrontend/src/pages/Reports/SleepAnalyticsTable.tsx
    • Removed a redundant formatTime helper function.
    • Updated calls to the removed formatTime function to use the centralized formatSecondsToHHMM utility.
  • SparkyFitnessFrontend/src/pages/Reports/SleepScience/ChronotypeCard.tsx
    • Added a null check for the data prop to ensure the component handles cases where data might be undefined before accessing its properties.
  • SparkyFitnessFrontend/src/utils/timeFormatters.ts
    • Modified formatMinutesToHHMM to output durations in Hh Mm format, handle negative input, and display only minutes if hours are zero.
    • Modified formatSecondsToHHMM to output durations in Hh Mm format, handle negative input, and display only minutes if hours are zero, replacing the previous HH:MM format.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the time format in sleep charts to a more readable 'h m' format, which is a good user experience improvement. The changes involve replacing a local formatting function with a centralized utility function.

I've found a bug in the new formatMinutesToHHMM utility function that can lead to incorrect time displays like '1h 60m'. I've also noticed code duplication between formatMinutesToHHMM and formatSecondsToHHMM and suggested a refactoring to make the code more maintainable. Please see my comments for details.

Comment on lines +3 to +5
const absMinutes = Math.abs(totalMinutes);
const hours = Math.floor(absMinutes / 60);
const minutes = Math.round(absMinutes % 60);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a potential bug in this calculation. If totalMinutes is a value like 59.6, Math.round(absMinutes % 60) will result in 60, leading to an output like 0h 60m. The total minutes should be rounded before calculating hours and minutes to prevent this. The logic in formatSecondsToHHMM correctly handles this by rounding first.

Suggested change
const absMinutes = Math.abs(totalMinutes);
const hours = Math.floor(absMinutes / 60);
const minutes = Math.round(absMinutes % 60);
const roundedTotalMinutes = Math.round(Math.abs(totalMinutes));
const hours = Math.floor(roundedTotalMinutes / 60);
const minutes = roundedTotalMinutes % 60;

…ule"

   - Attribution: Updated sleep entry logic to attribute data to the wake-up date, aligning with Apple Health and Fitbit standards.
   - Formatting: Unified all sleep durations across the app to use the consistent 7h 15m (or 15m) format.
   - Accuracy: Aligned legacy charts and tables to use your Personalized Sleep Need baseline instead of a hardcoded 8 hours.
   - Fixes: Resolved a date-shifting bug caused by UTC parsing and synchronized the manual entry editor with the saving logic to prevent 24-hour mismatches.
   - UI Consolidation: Cleaned up the Check-in page by organizing sleep stages and health metrics into a structured grid and fixing misleading title dates.
@CodeWithCJ CodeWithCJ changed the title updated sleep charts to show as h&m e.g. 7h 15m instead of 07:15 feat: unify sleep reports and implement industry-standard "Wake-up Rule" Feb 28, 2026
@CodeWithCJ
Copy link
Owner Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant and welcome change by aligning sleep entry attribution with the "wake-up rule", which is an industry standard. The changes are comprehensive, touching date parsing, frontend logic, backend services, and even a database migration to correct historical data. The unification of duration formatting and the use of personalized sleep need instead of hardcoded values are great improvements for consistency and accuracy.

My review has identified one critical issue in the database migration script that could lead to data corruption for certain sleep entries. I've also provided a minor suggestion to improve code conciseness in one of the frontend components. Overall, this is a solid pull request that makes important enhancements to the sleep tracking feature.

Comment on lines +5 to +6
UPDATE sleep_entries
SET entry_date = entry_date + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This migration logic is too simplistic and may lead to incorrect data. The entry_date = entry_date + 1 statement assumes every sleep entry crosses midnight, which isn't always true (e.g., daytime naps or sleep sessions that start and end on the same calendar day). This will incorrectly shift the entry_date for such entries.

To correctly implement the "wake-up rule", the entry_date should be derived from the wake_time. A more robust approach would be to set the entry_date to the date part of the wake_time.

Suggested change
UPDATE sleep_entries
SET entry_date = entry_date + 1;
UPDATE sleep_entries
SET entry_date = DATE(wake_time);

Comment on lines +384 to +385
{entryDetails.deepSleepSeconds !== undefined &&
entryDetails.deepSleepSeconds !== null && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check for undefined and null can be simplified. Using the != null coercion checks for both null and undefined, which makes the code more concise. This can be applied to all similar checks for health metrics in this component.

          {entryDetails.deepSleepSeconds != null && (

@CodeWithCJ CodeWithCJ merged commit 609d0ae into main Feb 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant