Folo icon indicating copy to clipboard operation
Folo copied to clipboard

fix(mobile): handle durationInSeconds format

Open kovsu opened this issue 10 months ago • 3 comments

Same as #3148

kovsu avatar Mar 24 '25 02:03 kovsu

Thank you for your contribution. We will review it promptly.

@kovsu is attempting to deploy a commit to the RSS3 Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Mar 24 '25 02:03 vercel[bot]

Suggested PR Title:

fix(entry-list): improve duration calculation handling

Change Summary: Enhanced duration calculation by handling string format and added a utility function.

Code Review:

Code Review

File: apps/mobile/src/modules/entry-list/templates/EntryNormalItem.tsx

  1. Line 8:

    // @ts-expect-error durationInSeconds is string
    
    • Issue: Using @ts-expect-error suppresses TypeScript's type checking without properly addressing the underlying issue or providing a clear explanation. This could lead to unpredictable bugs if durationInSeconds is not handled properly as a string or number.
    • Recommendation: Instead of suppressing the error, explicitly handle the type conversion with a type guard or a more robust type-checking mechanism to ensure the variable is correctly validated and processed. For example:
      if (typeof durationInSeconds === 'string') {
        durationInSeconds = formatTimeToSeconds(durationInSeconds);
      } else if (typeof durationInSeconds !== 'number') {
        durationInSeconds = 0; // Handle unexpected cases gracefully
      }
      
      Avoid relying on @ts-expect-error unless absolutely necessary and there is no safer alternative.
  2. Line 6:

    durationInSeconds = attachments ? attachments[0]?.duration_in_seconds : 0
    
    • Issue: The existing code assumes that attachments[0]?.duration_in_seconds is either a string, a number, or undefined, but it lacks a comprehensive validation for unexpected data. If duration_in_seconds contains an invalid or unexpected value (e.g., an object or an array), it could lead to runtime errors.
    • Recommendation: Add stricter input validation for attachments[0]?.duration_in_seconds to ensure it can only be a string, a number, or undefined. For instance:
      const duration = attachments ? attachments[0]?.duration_in_seconds : 0;
      if (typeof duration !== 'string' && typeof duration !== 'number') {
        durationInSeconds = 0; // Fallback for invalid data
      } else {
        durationInSeconds = typeof duration === 'string' ? formatTimeToSeconds(duration) : duration;
      }
      

Summary of Proposed Changes

  • Replace @ts-expect-error with explicit type checking and type guards.
  • Add robust validation for the attachments[0]?.duration_in_seconds data to account for unexpected formats.

These changes will ensure type safety, improve code readability, and prevent runtime errors caused by malformed or unexpected data.

Let me know if further assistance is needed!

Thank you @kovsu for your contribution! 🎉

Your pull request has been merged and we really appreciate your help in making this project better. We hope to see more contributions from you in the future! 💪