import React, { useCallback, useRef, useState } from 'react'; import { WeatherWarning } from '../types'; import { IcAlert } from '../res/icons'; import { colors } from '../res/colors'; import { strings } from '../res/strings'; import { stringsEn } from '../res/strings.en'; import { useAppStrings } from '@/os/useAppStrings'; import * as TimeService from '../../../os/TimeService'; import { getLocalizedWarningText, getLocalizedWarningType } from '../utils/localizedText'; interface WarningCardProps { warnings: WeatherWarning[]; } const formatRelativeTime = (pubTime: string, s: typeof strings): string => { try { const pubDate = TimeService.fromTimestamp(TimeService.parseToTimestamp(pubTime)); const now = TimeService.getDate(); const diffMs = now.getTime() - pubDate.getTime(); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffMinutes = Math.floor(diffMs / (1000 * 60)); if (diffMinutes < 1) { return s.warning_just_updated; } if (diffMinutes < 60) { return `${diffMinutes}${s.warning_minutes_ago}`; } if (diffHours < 24) { return diffHours === 1 ? s.warning_1hour_ago : `${diffHours}${s.warning_hours_ago}`; } if (diffDays === 0) { return s.warning_today_updated; } if (diffDays === 1) { return s.warning_1day_ago; } if (diffDays < 7) { return `${diffDays}${s.warning_days_ago}`; } return `${Math.floor(diffDays / 7)}${s.warning_weeks_ago}`; } catch { return s.warning_updated; } }; export const WarningCard: React.FC = ({ warnings }) => { const s = useAppStrings(strings, stringsEn); const [currentIndex, setCurrentIndex] = useState(0); const scrollRef = useRef(null); const getIconStyle = (color: string) => { switch (color) { case 'Blue': return 'text-blue-300'; case 'Yellow': return 'text-yellow-300'; case 'Orange': return 'text-orange-300'; case 'Red': return 'text-red-300'; default: return 'text-white'; } }; const handleScroll = useCallback(() => { if (!scrollRef.current) return; const container = scrollRef.current; const newIndex = Math.round(container.scrollLeft / container.clientWidth); if (newIndex !== currentIndex && newIndex >= 0 && newIndex < warnings.length) { setCurrentIndex(newIndex); } }, [currentIndex, warnings.length]); if (!warnings || warnings.length === 0) { return (
Placeholder Placeholder
Placeholder placeholder placeholder placeholder
); } return (
{warnings.map((warning, index) => { const iconStyle = getIconStyle(warning.severityColor); const warningType = getLocalizedWarningType(warning, s); const relativeTime = formatRelativeTime(warning.pubTime, s); return (
{warningType} {relativeTime}
{getLocalizedWarningText(warning, s)}
); })}
{warnings.length > 1 && warnings.map((_, index) => (
))}
); };