import React, { useState, useEffect } from 'react'; import { History, Zap, Building2, Calendar, X, Plus } from 'lucide-react'; import { Outage, Building } from '../../types'; import { backendApi } from '../../services/apiClient'; import { CreateOutageCard } from './CreateOutageCard'; import { OutageCardDetail } from './OutageCardDetail'; interface Props { /** В режиме "по дому" передать buildingId — показываем только по этому дому и кнопка История по этому дому */ buildingId?: string; /** Компактный вид для блока в сводке дома */ compact?: boolean; } export const OutagesJournal: React.FC = ({ buildingId, compact }) => { const [activeOutages, setActiveOutages] = useState([]); const [historyOpen, setHistoryOpen] = useState(false); const [historyOutages, setHistoryOutages] = useState([]); const [historyBuildingFilter, setHistoryBuildingFilter] = useState(buildingId || ''); const [buildings, setBuildings] = useState([]); const [loading, setLoading] = useState(true); const [historyLoading, setHistoryLoading] = useState(false); const [createOpen, setCreateOpen] = useState(false); const [openOutageId, setOpenOutageId] = useState(null); const fetchActive = () => { setLoading(true); backendApi .getOutages(buildingId ? { buildingId, active: true } : { active: true }) .then(setActiveOutages) .catch(() => setActiveOutages([])) .finally(() => setLoading(false)); }; useEffect(() => { fetchActive(); }, [buildingId]); useEffect(() => { if (historyOpen) { backendApi.getBuildings().then(setBuildings).catch(() => setBuildings([])); if (!buildingId) setHistoryBuildingFilter(''); } }, [historyOpen, buildingId]); const loadHistory = () => { setHistoryLoading(true); const filter = buildingId || historyBuildingFilter; const params = filter ? { buildingId: filter } : undefined; backendApi .getOutages(params) .then(setHistoryOutages) .catch(() => setHistoryOutages([])) .finally(() => setHistoryLoading(false)); }; useEffect(() => { if (historyOpen) loadHistory(); }, [historyOpen, historyBuildingFilter, buildingId]); const formatDate = (s: string) => { const d = new Date(s); return d.toLocaleDateString('ru-RU', { day: 'numeric', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit' }); }; const title = buildingId ? 'Журнал отключений' : 'Журнал отключений'; const subtitle = buildingId ? 'По этому дому' : 'Активные по всем домам'; return (

{title}

{subtitle}

{loading ? (

Загрузка…

) : activeOutages.length === 0 ? (

Нет активных отключений

) : (
    {activeOutages.map((o) => (
  • setOpenOutageId(o.id)} className="bg-white border border-slate-200 rounded-xl p-4 shadow-sm flex flex-col sm:flex-row sm:items-center gap-3 cursor-pointer hover:border-primary-300 hover:shadow-md transition-all" >
    {!buildingId && o.buildingAddress && (

    {o.buildingAddress}

    )}

    {o.category || o.type || 'Отключение'}

    {o.description &&

    {o.description}

    }

    с {formatDate(o.startAt)} {o.endAt && ` до ${formatDate(o.endAt)}`}

    Активно
  • ))}
)} {createOpen && ( setCreateOpen(false)} onSuccess={() => { fetchActive(); setCreateOpen(false); }} buildingId={buildingId} /> )} {openOutageId !== null && ( setOpenOutageId(null)} onUpdated={fetchActive} /> )} {/* Модалка История */} {historyOpen && (
setHistoryOpen(false)}>
e.stopPropagation()}>

История отключений

{!buildingId && (
)}
{historyLoading ? (

Загрузка…

) : historyOutages.length === 0 ? (

Нет записей

) : (
    {historyOutages.map((o) => (
  • { setHistoryOpen(false); setOpenOutageId(o.id); }} className="bg-slate-50 rounded-xl p-4 border border-slate-100 cursor-pointer hover:border-primary-200 hover:bg-slate-50/80 transition-colors" > {!buildingId && o.buildingAddress && (

    {o.buildingAddress}

    )}

    {o.category || o.type || 'Отключение'}

    {o.description &&

    {o.description}

    }

    {formatDate(o.startAt)} {o.endAt ? ` — ${formatDate(o.endAt)}` : ''} {o.active && Активно}

  • ))}
)}
)}
); };