// Общая кнопка «Отчёт»: выбор периода и выгрузка в Excel. // Тот же паттерн, что в ApprovalsPage, — ReportButton там можно заменить на этот компонент. import { useState } from 'react' import { Button, Group, Popover, Stack, Text } from '@mantine/core' import { DatePicker } from '@mantine/dates' import { notifications } from '@mantine/notifications' import { IconDownload } from '@tabler/icons-react' import dayjs from 'dayjs' export type DateRange = [Date | null, Date | null] const PRESETS: { label: string; range: () => DateRange }[] = [ { label: 'Сегодня', range: () => [dayjs().startOf('day').toDate(), dayjs().endOf('day').toDate()] }, { label: 'Этот месяц', range: () => [dayjs().startOf('month').toDate(), dayjs().endOf('month').toDate()] }, { label: 'Прошлый месяц', range: () => [ dayjs().subtract(1, 'month').startOf('month').toDate(), dayjs().subtract(1, 'month').endOf('month').toDate(), ], }, { label: 'Всё время', range: () => [null, null] }, ] interface Props { title: string fileName: string // без расширения hint?: string onDownload: (range: DateRange) => Promise } export function ReportPopover({ title, fileName, hint, onDownload }: Props) { const [opened, setOpened] = useState(false) const [range, setRange] = useState([null, null]) const [loading, setLoading] = useState(false) const download = async () => { setLoading(true) try { const blob = new Blob([await onDownload(range)], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }) const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${fileName}_${dayjs().format('DD.MM.YYYY_HH-mm')}.xlsx` document.body.appendChild(a) a.click() a.remove() window.URL.revokeObjectURL(url) notifications.show({ title: 'Готово', message: 'Отчёт скачан', color: 'green' }) setOpened(false) } catch { notifications.show({ title: 'Ошибка', message: 'Не удалось сформировать отчёт', color: 'red' }) } finally { setLoading(false) } } return ( {title} {hint && {hint}} {PRESETS.map((p) => ( ))} setRange(v as DateRange)} /> ) }