feat: add shared StatusBadge, SourceTag, AgeIndicator components and format utilities

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-16 14:46:29 +05:30
parent 5075fa3f04
commit d36f9f39b5
4 changed files with 162 additions and 0 deletions

39
src/lib/format.ts Normal file
View File

@@ -0,0 +1,39 @@
// Format currency from micros to display string (INR)
export const formatCurrency = (amountMicros: number, currency = 'INR'): string => {
const amount = amountMicros / 1_000_000;
return new Intl.NumberFormat('en-IN', { style: 'currency', currency, maximumFractionDigits: 0 }).format(amount);
};
// Format phone number for display
export const formatPhone = (phone: { number: string; callingCode: string }): string =>
`${phone.callingCode} ${phone.number.replace(/(\d{5})(\d{5})/, '$1 $2')}`;
// Calculate days ago from ISO date string
export const daysAgoFromNow = (dateStr: string): number => {
const diff = Date.now() - new Date(dateStr).getTime();
return Math.floor(diff / (1000 * 60 * 60 * 24));
};
// Get aging bracket from days
export const getAgeBracket = (days: number): 'fresh' | 'warm' | 'cold' =>
days < 2 ? 'fresh' : days <= 5 ? 'warm' : 'cold';
// Format relative age string
export const formatRelativeAge = (dateStr: string): string => {
const days = daysAgoFromNow(dateStr);
if (days === 0) return 'Today';
if (days === 1) return '1 day ago';
return `${days} days ago`;
};
// Format short date (Mar 15, 2:30 PM)
export const formatShortDate = (dateStr: string): string =>
new Intl.DateTimeFormat('en-IN', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true }).format(new Date(dateStr));
// Get initials from a name
export const getInitials = (firstName: string, lastName: string): string =>
`${firstName[0] || ''}${lastName[0] || ''}`.toUpperCase();
// Format large numbers (1234 -> "1.2K", 1234567 -> "1.2M")
export const formatCompact = (n: number): string =>
new Intl.NumberFormat('en-IN', { notation: 'compact', maximumFractionDigits: 1 }).format(n);