Files
thrilltrack-explorer/src/components/ui/flexible-date-display.tsx
gpt-engineer-app[bot] d0c613031e Migrate date precision to exact
Batch update all date precision handling to use expanded DatePrecision, replace hardcoded day defaults, and adjust related validation, UI, and helpers. Includes wrapper migration across Phase 1-3 functions, updates to logs, displays, and formatting utilities to align frontend with new precision values ('exact', 'month', 'year', 'decade', 'century', 'approximate').
2025-11-11 22:05:29 +00:00

56 lines
1.5 KiB
TypeScript

import { format } from 'date-fns';
import { parseDateForDisplay } from '@/lib/dateUtils';
import type { DatePrecision } from './flexible-date-input';
interface FlexibleDateDisplayProps {
date?: string | Date | null;
precision?: DatePrecision;
fallback?: string;
className?: string;
}
export function FlexibleDateDisplay({
date,
precision = 'exact',
fallback = 'Unknown',
className
}: FlexibleDateDisplayProps) {
if (!date) {
return <span className={className || "text-muted-foreground"}>{fallback}</span>;
}
// ⚠️ IMPORTANT: Use parseDateForDisplay to prevent timezone shifts
// YYYY-MM-DD strings must be interpreted as local dates, not UTC
const dateObj = parseDateForDisplay(date);
// Check for invalid date
if (isNaN(dateObj.getTime())) {
return <span className={className || "text-muted-foreground"}>{fallback}</span>;
}
let formatted: string;
switch (precision) {
case 'year':
formatted = format(dateObj, 'yyyy');
break;
case 'month':
formatted = format(dateObj, 'MMMM yyyy');
break;
case 'decade':
formatted = `${Math.floor(dateObj.getFullYear() / 10) * 10}s`;
break;
case 'century':
formatted = `${Math.ceil(dateObj.getFullYear() / 100)}th century`;
break;
case 'approximate':
formatted = `circa ${format(dateObj, 'yyyy')}`;
break;
case 'exact':
default:
formatted = format(dateObj, 'PPP');
break;
}
return <span className={className}>{formatted}</span>;
}