admin_ruspet/src/pages/telephony/components/ReportPopover.tsx

88 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Общая кнопка «Отчёт»: выбор периода и выгрузка в 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<BlobPart>
}
export function ReportPopover({ title, fileName, hint, onDownload }: Props) {
const [opened, setOpened] = useState(false)
const [range, setRange] = useState<DateRange>([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 (
<Popover opened={opened} onChange={setOpened} position="bottom-end" shadow="md" width={300}>
<Popover.Target>
<Button variant="default" leftSection={<IconDownload size={16} />} onClick={() => setOpened((o) => !o)}>
Отчёт
</Button>
</Popover.Target>
<Popover.Dropdown>
<Stack gap="sm">
<Stack gap={2}>
<Text size="sm" fw={600}>{title}</Text>
{hint && <Text size="xs" c="dimmed">{hint}</Text>}
</Stack>
<Group gap={6}>
{PRESETS.map((p) => (
<Button key={p.label} size="compact-xs" variant="default" onClick={() => setRange(p.range())}>
{p.label}
</Button>
))}
</Group>
<DatePicker type="range" value={range} onChange={(v) => setRange(v as DateRange)} />
<Button fullWidth leftSection={<IconDownload size={16} />} onClick={download} loading={loading}>
Скачать XLSX
</Button>
</Stack>
</Popover.Dropdown>
</Popover>
)
}