import { useState } from 'react'; import { format, startOfMonth, endOfMonth, addMonths, subMonths } from 'date-fns'; import { ru } from 'date-fns/locale'; import { useLogs, type TimeLog } from '../hooks/useLogs'; import { LogList } from './LogList'; import { AddLogModal } from './AddLogModal'; import { ReportModal } from './ReportModal'; interface Props { username: string; onLogout: () => void; } export function Dashboard({ username, onLogout }: Props) { const [currentDate, setCurrentDate] = useState(new Date()); const [isAddModalOpen, setIsAddModalOpen] = useState(false); const [isReportModalOpen, setIsReportModalOpen] = useState(false); const [editingLog, setEditingLog] = useState(null); const startDate = format(startOfMonth(currentDate), 'yyyy-MM-dd'); const endDate = format(endOfMonth(currentDate), 'yyyy-MM-dd'); const { logs, isLoading, createLog, updateLog, deleteLog } = useLogs(startDate, endDate); const totalMinutes = logs.reduce((sum, log) => sum + log.minutes, 0); const totalHours = Math.floor(totalMinutes / 60); const totalMins = totalMinutes % 60; const handlePrevMonth = () => setCurrentDate(subMonths(currentDate, 1)); const handleNextMonth = () => setCurrentDate(addMonths(currentDate, 1)); const handleDelete = async (id: string) => { if (window.confirm('Удалить эту запись?')) { await deleteLog(id); } }; const handleEdit = (log: TimeLog) => { setEditingLog(log); setIsAddModalOpen(true); }; const handleUpdateTime = async (id: string, time: string) => { await updateLog({ id, data: { time } }); }; const handleSave = async (data: { date: string; time: string; description: string }) => { if (editingLog) { await updateLog({ id: editingLog.id, data }); } else { await createLog(data); } }; const handleCloseModal = () => { setIsAddModalOpen(false); setEditingLog(null); }; return (
{/* Header */}

TimeTracker

{username}
{/* Month navigation */}

{format(currentDate, 'LLLL yyyy', { locale: ru })}

{/* Stats */}

Всего за месяц

{totalHours}:{String(totalMins).padStart(2, '0')}

Записей

{logs.length}

{/* Logs */}
{/* Floating buttons */}
{/* Modals */} setIsReportModalOpen(false)} />
); }