Skip to content

[Refactoring] Utilities: Consolidate duplicate formatCurrency implementations across components #271

@syed-reza98

Description

@syed-reza98

Problem

Multiple components across the codebase define their own local formatCurrency function with identical or nearly identical implementations. This violates the DRY (Don't Repeat Yourself) principle and creates maintenance issues:

  • 9+ duplicate implementations found across subscription, order, and pricing components
  • Inconsistent formatting options (some use minimumFractionDigits: 0, others use 2)
  • Central utility in src/lib/utils.ts exists but is not being used
  • Any changes to currency formatting require updating multiple files

Current Code Location

Files affected:

  • src/components/subscription/admin/revenue-overview.tsx
  • src/components/subscription/admin/subscriptions-table.tsx
  • src/components/subscription/admin/plan-management.tsx
  • src/components/subscription/billing-history.tsx
  • src/components/subscription/plan-selector.tsx
  • src/components/subscription/subscription-banner.tsx
  • src/components/orders-table.tsx
  • src/components/order-detail-client.tsx
  • Several other components

Central utility location: src/lib/utils.ts (lines 15-45)

Complexity: Low (simple refactor)

Proposed Refactoring

Replace all local formatCurrency implementations with imports from the central utility module.

Benefits

  • Single source of truth for currency formatting logic
  • Consistency across the entire application
  • Easier maintenance - changes only need to be made in one place
  • Reduced code size - eliminate ~90+ lines of duplicate code
  • Better testability - centralized function can be thoroughly tested once

Suggested Approach

  1. Audit all usages: Identify the exact formatting requirements for each component
  2. Standardize the central utility: Ensure formatCurrency and formatCurrencyFromTaka in src/lib/utils.ts meet all use cases
  3. Replace local implementations:
    • Remove local formatCurrency functions
    • Add import: import { formatCurrency, formatCurrencyFromTaka } from "@/lib/utils"
    • Update function calls to use correct variant (paisa vs taka)
  4. Test each component: Verify currency displays correctly after refactor

Code Example

Before (in multiple files):

// src/components/subscription/admin/revenue-overview.tsx
function formatCurrency(amount: number): string {
  return new Intl.NumberFormat('en-BD', {
    style: 'currency',
    currency: 'BDT',
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  }).format(amount);
}

// Used like this:
(div){formatCurrency(analytics.mrr)}(/div)

After:

// src/components/subscription/admin/revenue-overview.tsx
import { formatCurrencyFromTaka } from "@/lib/utils";

// Used like this:
(div){formatCurrencyFromTaka(analytics.mrr)}(/div)

Central utility (already exists):

// src/lib/utils.ts (lines 15-45)
export function formatCurrency(
  amount: number,
  currency: string = "BDT",
  locale: string = "en-US"
): string {
  return new Intl.NumberFormat(locale, {
    style: "currency",
    currency,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount / 100); // Paisa to taka
}

export function formatCurrencyFromTaka(
  amount: number,
  currency: string = "BDT",
  locale: string = "en-US"
): string {
  return new Intl.NumberFormat(locale, {
    style: "currency",
    currency,
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(amount);
}

Impact Assessment

  • Effort: Low - Simple search & replace with import updates (~2-3 hours)
  • Risk: Low - Straightforward refactor with clear before/after behavior
  • Benefit: High - Eliminates code duplication, improves maintainability
  • Priority: High - Quick win with immediate positive impact

Related Files

All files listed in "Current Code Location" section above, plus:

  • src/lib/utils.ts (central utility)
  • src/components/price-range-filter.tsx (already uses central utility - good example)

Testing Strategy

  1. Visual regression testing: Verify currency displays correctly in:
    • Revenue overview dashboard
    • Subscription tables
    • Order tables and detail pages
    • Billing history
    • Plan selector cards
  2. Unit tests: Add tests for edge cases in central utility:
    • Zero amounts
    • Very large amounts
    • Negative amounts (refunds)
    • Different currencies (if supported)
  3. Manual testing: Check all affected pages in both dev and staging environments

AI generated by Daily Codebase Analyzer - Semantic Function Extraction & Refactoring

  • expires on Mar 6, 2026, 1:56 PM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions