Skip to content

refactor: consolidate timelineProgress calculation into vaultLifecycl… - #986

Merged
1nonlypiece merged 2 commits into
Disciplr-Org:mainfrom
A-one-tech:refactor/shared-timeline-progress
Jul 29, 2026
Merged

refactor: consolidate timelineProgress calculation into vaultLifecycl…#986
1nonlypiece merged 2 commits into
Disciplr-Org:mainfrom
A-one-tech:refactor/shared-timeline-progress

Conversation

@A-one-tech

@A-one-tech A-one-tech commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

#Closes
#743

PR: Extract shared timelineProgress helper and guard equal timestamps

Summary

Extracts the duplicate timelineProgress(created, deadline) utility function into src/utils/vaultLifecycle.ts and replaces the inline module-level implementations in both src/pages/Dashboard.tsx and src/pages/VaultDetail.tsx.

Also resolves an unguarded divide-by-zero issue where equal created and deadline timestamps resulted in (end - start) === 0 and returned NaN.


Motivation & Problem Statement

  • Code Duplication: Both Dashboard.tsx (line 42) and VaultDetail.tsx (line 51) maintained self-acknowledged identical implementations of timelineProgress.
  • Edge-Case Bug: If a vault was configured with created === deadline, (end - start) equaled 0, leading to ((now - start) / 0) * 100 => NaN which propagated through Math.max / Math.min into UI progress bar rendering.

Key Changes

1. src/utils/vaultLifecycle.ts

  • Added and exported timelineProgress(created: string, deadline: string, now: number = Date.now()): number.
  • Added guards for end <= start (returns 100 if now >= start, else 0) to prevent NaN divide-by-zero errors.
  • Added guards for invalid date string inputs (returns 0).

2. src/pages/Dashboard.tsx & src/pages/VaultDetail.tsx

  • Removed duplicate inline timelineProgress functions.
  • Imported timelineProgress from ../utils/vaultLifecycle.

3. src/utils/__tests__/vaultLifecycle.test.ts

  • Added comprehensive unit test suite covering:
    • Exact percentage calculation during vault lifetime (50% halfway point).
    • Clamping to 0% before creation date and 100% past deadline date.
    • Equal creation and deadline timestamps (created === deadline) returning 100 / 0 without NaN.
    • Inverted creation and deadline timestamps (deadline < created).
    • Invalid date string handling.

Verification & Testing

Automated Test Execution

# Targeted utility tests
npx vitest run src/utils/__tests__/vaultLifecycle.test.ts

# Consuming page test suites
npx vitest run src/pages/__tests__/Dashboard.test.tsx src/pages/__tests__/VaultDetail.test.tsx

Test Results

  • vaultLifecycle.test.ts: 11 passed (5 stages tests + 6 timelineProgress tests)
  • Dashboard.test.tsx: 3 passed
  • VaultDetail.test.tsx: 19 passed

Checklist

  • Extracted timelineProgress helper into src/utils/
  • Imported shared helper in Dashboard.tsx and VaultDetail.tsx
  • Added divide-by-zero / equal timestamp guard
  • Added unit tests for edge cases and normal progress
  • All tests passing locally

Closes #743

…e utility with robust error handling and tests
@drips-wave

drips-wave Bot commented Jul 27, 2026

Copy link
Copy Markdown

@A-one-tech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@1nonlypiece

Copy link
Copy Markdown
Contributor

Hi @A-one-tech, closing this for now — I don't see it linked to an issue that's assigned to you. Feel free to reopen (or open a fresh PR) once you're assigned to the issue it addresses.

@1nonlypiece 1nonlypiece reopened this Jul 29, 2026
# Conflicts:
#	src/pages/Dashboard.tsx
@1nonlypiece
1nonlypiece merged commit 31eea79 into Disciplr-Org:main Jul 29, 2026
3 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.

Consolidate the duplicate timelineProgress function in Dashboard.tsx and VaultDetail.tsx

4 participants