-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
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 use2) - Central utility in
src/lib/utils.tsexists 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.tsxsrc/components/subscription/admin/subscriptions-table.tsxsrc/components/subscription/admin/plan-management.tsxsrc/components/subscription/billing-history.tsxsrc/components/subscription/plan-selector.tsxsrc/components/subscription/subscription-banner.tsxsrc/components/orders-table.tsxsrc/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
- Audit all usages: Identify the exact formatting requirements for each component
- Standardize the central utility: Ensure
formatCurrencyandformatCurrencyFromTakainsrc/lib/utils.tsmeet all use cases - Replace local implementations:
- Remove local
formatCurrencyfunctions - Add import:
import { formatCurrency, formatCurrencyFromTaka } from "@/lib/utils" - Update function calls to use correct variant (paisa vs taka)
- Remove local
- 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
- Visual regression testing: Verify currency displays correctly in:
- Revenue overview dashboard
- Subscription tables
- Order tables and detail pages
- Billing history
- Plan selector cards
- Unit tests: Add tests for edge cases in central utility:
- Zero amounts
- Very large amounts
- Negative amounts (refunds)
- Different currencies (if supported)
- 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Type
Projects
Status
Backlog