'use client' import { useEffect, useMemo, useState } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { Umbrella, Wind, ThermometerSun, TramFront, Calendar as CalendarIcon, Receipt, Sparkles, Moon, Sun, } from 'lucide-react' // —————————————————————————————— // Types // —————————————————————————————— export interface FocusWeather { temp: string desc: string feelsLike?: string } export interface FocusTram { route: string minutes: number direction: string } export interface FocusEvent { id: string title: string start: string allDay?: boolean ownerName?: string color?: string } export interface FocusCountdown { label: string date: string // ISO YYYY-MM-DD } export interface FocusBill { title: string amount: string daysLeft: number } interface Props { weather: FocusWeather | null tramNext?: FocusTram | null nextEvent?: FocusEvent | null countdowns?: FocusCountdown[] bills?: FocusBill[] } // —————————————————————————————— // Focus state machine // —————————————————————————————— type FocusKind = | { kind: 'morning-outfit'; tempNow: string; feels: string; advice: string; Icon: any; accent: string } | { kind: 'tram-imminent'; route: string; minutes: number; direction: string } | { kind: 'event-upcoming'; title: string; inMinutes: number; owner?: string; color?: string } | { kind: 'countdown'; label: string; days: number } | { kind: 'bill-due'; title: string; amount: string; daysLeft: number } | { kind: 'night'; hour: number } | { kind: 'quiet'; greeting: string } function pickFocus(p: Props, hour: number): FocusKind { // 1. Bill due today / tomorrow — всегда приоритет const bill = p.bills?.find(b => b.daysLeft <= 1) if (bill) return { kind: 'bill-due', ...bill } // 2. Ближайшее событие ≤30 минут if (p.nextEvent && !p.nextEvent.allDay) { const start = new Date(p.nextEvent.start).getTime() const inMin = Math.round((start - Date.now()) / 60_000) if (inMin >= -5 && inMin <= 30) { return { kind: 'event-upcoming', title: p.nextEvent.title, inMinutes: inMin, owner: p.nextEvent.ownerName, color: p.nextEvent.color, } } } // 3. Трамвай в рабочий час, ≤3 мин const rushHour = (hour >= 7 && hour <= 10) || (hour >= 17 && hour <= 20) if (rushHour && p.tramNext && p.tramNext.minutes >= 0 && p.tramNext.minutes <= 3) { return { kind: 'tram-imminent', ...p.tramNext } } // 4. Утро (7-10) → одевалка if (hour >= 7 && hour < 11 && p.weather) { const t = parseInt(p.weather.temp, 10) const descLower = p.weather.desc?.toLowerCase() || '' const rain = /дожд|ливен|грозa|морос/.test(descLower) const snow = /снег|метел/.test(descLower) const advice = rain ? 'возьми зонт' : snow ? 'шапка и зимняя обувь' : t <= -10 ? 'пуховик, шапка, перчатки' : t < 0 ? 'шапка и перчатки' : t < 7 ? 'тёплая куртка' : t < 15 ? 'лёгкая куртка' : t < 22 ? 'свитер или рубашка' : 'футболка' const Icon = rain ? Umbrella : snow ? Wind : t < 0 ? Wind : ThermometerSun const accent = rain ? 'var(--data-cool)' : snow ? 'var(--data-info)' : t < 0 ? 'var(--data-info)' : t >= 22 ? 'var(--data-warm)' : 'var(--data-warm)' return { kind: 'morning-outfit', tempNow: p.weather.temp, feels: p.weather.feelsLike || '', advice, Icon, accent, } } // 5. Ближайший countdown (≤14 дней) const cd = (p.countdowns || []) .map(c => { const target = new Date(c.date + 'T00:00:00').getTime() const days = Math.ceil((target - Date.now()) / 86_400_000) return { label: c.label, days } }) .filter(c => c.days >= 0 && c.days <= 14) .sort((a, b) => a.days - b.days)[0] if (cd) return { kind: 'countdown', ...cd } // 6. Ночь if (hour >= 22 || hour < 5) return { kind: 'night', hour } // 7. Тихо — приветствие const greeting = hour >= 5 && hour < 12 ? 'Доброе утро' : hour >= 12 && hour < 17 ? 'Добрый день' : hour >= 17 && hour < 22 ? 'Добрый вечер' : 'Доброй ночи' return { kind: 'quiet', greeting } } // —————————————————————————————— // Presentations // —————————————————————————————— function Eyebrow({ children }: { children: React.ReactNode }) { return (
{children}
) } function FocusShell({ eyebrow, accent, children, }: { eyebrow: React.ReactNode accent: string children: React.ReactNode }) { return (
{/* Top eyebrow */}
{eyebrow}
{children}
) } // —————————————————————————————— // Individual states // —————————————————————————————— function MorningOutfit(f: Extract) { return ( Собираясь на улицу } >
{f.tempNow}°
{f.feels && (
ощущается {f.feels}°
)}
{f.advice}
) } function TramImminent(f: Extract) { const accent = 'var(--data-hot)' return ( Трамвай подходит } >
{f.minutes <= 0 ? 'сейчас' : f.minutes}
{f.minutes > 0 && (
мин
)}
Маршрут {f.route} · {f.direction}
) } function EventUpcoming(f: Extract) { const accent = f.color || 'var(--accent)' const timeWord = f.inMinutes < 0 ? 'сейчас' : f.inMinutes === 0 ? 'сейчас' : `через ${f.inMinutes}м` return ( {timeWord} } >
{f.title}
{f.owner && (
{f.owner}
)}
) } function CountdownView(f: Extract) { const accent = f.days <= 3 ? 'var(--data-hot)' : f.days <= 7 ? 'var(--data-warm)' : 'var(--data-violet)' const word = f.days === 0 ? 'сегодня' : f.days === 1 ? 'завтра' : f.days < 5 ? `${f.days} дня` : `${f.days} дней` return ( До события } >
{f.days === 0 ? 'сегодня' : f.days} {f.days > 0 && {f.days < 5 ? 'дня' : 'дней'} }
{f.label}
) } function BillDue(f: Extract) { const accent = 'var(--data-danger)' const word = f.daysLeft === 0 ? 'сегодня' : f.daysLeft === 1 ? 'завтра' : `через ${f.daysLeft}д` return ( К оплате {word} } >
{f.amount}
{f.title}
) } function NightView() { const accent = 'var(--data-violet)' return ( Тихое время } >
Спокойной ночи
Дом в режиме ожидания. Касание — разблокировать.
) } function QuietView({ greeting, weather }: { greeting: string; weather: FocusWeather | null }) { const accent = 'var(--accent)' const [now, setNow] = useState(() => new Date()) useEffect(() => { const t = setInterval(() => setNow(new Date()), 30_000) return () => clearInterval(t) }, []) return ( {now.toLocaleDateString('ru-RU', { weekday: 'long', day: 'numeric', month: 'long' })} } >
{greeting}
{weather && (
{weather.temp}°
{weather.desc}
)}
) } // —————————————————————————————— // Root // —————————————————————————————— export default function FocusCard(props: Props) { const [hour, setHour] = useState(() => new Date().getHours()) useEffect(() => { const t = setInterval(() => setHour(new Date().getHours()), 60_000) return () => clearInterval(t) }, []) const focus = useMemo(() => pickFocus(props, hour), [props, hour]) const key = focus.kind + ( focus.kind === 'morning-outfit' ? focus.advice : focus.kind === 'tram-imminent' ? `${focus.route}-${focus.minutes}` : focus.kind === 'event-upcoming' ? focus.title : focus.kind === 'countdown' ? focus.label : focus.kind === 'bill-due' ? focus.title : focus.kind === 'quiet' ? focus.greeting : '' ) return ( {focus.kind === 'morning-outfit' && } {focus.kind === 'tram-imminent' && } {focus.kind === 'event-upcoming' && } {focus.kind === 'countdown' && } {focus.kind === 'bill-due' && } {focus.kind === 'night' && } {focus.kind === 'quiet' && } ) }