umami/components/common/DateFilter.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

import { endOfYear, isSameDay } from 'date-fns';
import { useState } from 'react';
import { Icon, Modal, Dropdown, Item } from 'react-basics';
import { useIntl, defineMessages } from 'react-intl';
import DatePickerForm from 'components/metrics/DatePickerForm';
2020-09-13 10:26:54 +02:00
import useLocale from 'hooks/useLocale';
2022-01-14 09:04:07 +01:00
import { dateFormat } from 'lib/date';
import Calendar from 'assets/calendar-alt.svg';
const messages = defineMessages({
today: { id: 'label.today', defaultMessage: 'Today' },
lastHours: { id: 'label.last-hours', defaultMessage: 'Last {x} hours' },
yesterday: { id: 'label.yesterday', defaultMessage: 'Yesterday' },
thisWeek: { id: 'label.this-week', defaultMessage: 'This week' },
lastDays: { id: 'label.last-days', defaultMessage: 'Last {x} days' },
thisMonth: { id: 'label.this-month', defaultMessage: 'This month' },
thisYear: { id: 'label.this-year', defaultMessage: 'This year' },
allTime: { id: 'label.all-time', defaultMessage: 'All time' },
customRange: { id: 'label.custom-range', defaultMessage: 'Custom-range' },
});
function DateFilter({ value, startDate, endDate, onChange, className }) {
const { formatMessage } = useIntl();
2020-09-13 10:26:54 +02:00
const [showPicker, setShowPicker] = useState(false);
const options = [
{ label: formatMessage(messages.today), value: '1day' },
{
label: formatMessage(messages.lastHours, { x: 24 }),
value: '24hour',
},
{
label: formatMessage(messages.yesterday),
value: '-1day',
},
{
label: formatMessage(messages.thisWeek),
value: '1week',
divider: true,
},
{
label: formatMessage(messages.lastDays, { x: 7 }),
value: '7day',
},
{
label: formatMessage(messages.thisMonth),
value: '1month',
divider: true,
},
{
label: formatMessage(messages.lastDays, { x: 30 }),
value: '30day',
},
{
label: formatMessage(messages.lastDays, { x: 90 }),
value: '90day',
},
{ label: formatMessage(messages.thisYear), value: '1year' },
{
label: formatMessage(messages.allTime),
value: 'all',
divider: true,
},
{
label: formatMessage(messages.customRange),
value: 'custom',
divider: true,
},
];
const renderValue = value => {
return value === 'custom' ? (
<CustomRange startDate={startDate} endDate={endDate} onClick={() => handleChange('custom')} />
) : (
options.find(e => e.value === value).label
);
};
2020-09-13 10:26:54 +02:00
const handleChange = async value => {
2020-09-13 10:26:54 +02:00
if (value === 'custom') {
setShowPicker(true);
return;
}
2022-01-14 09:04:07 +01:00
onChange(value);
};
const handlePickerChange = value => {
2020-09-13 10:26:54 +02:00
setShowPicker(false);
onChange(value);
};
const handleClose = () => setShowPicker(false);
2020-09-13 10:26:54 +02:00
2020-08-02 06:20:52 +02:00
return (
2020-09-13 10:26:54 +02:00
<>
<Dropdown
2020-09-13 10:26:54 +02:00
className={className}
items={options}
renderValue={renderValue}
value={value}
2020-09-13 10:26:54 +02:00
onChange={handleChange}
>
{({ label, value }) => <Item key={value}>{label}</Item>}
</Dropdown>
2020-09-13 10:26:54 +02:00
{showPicker && (
<Modal onClose={handleClose}>
2020-09-13 10:26:54 +02:00
<DatePickerForm
startDate={startDate}
endDate={endDate}
minDate={new Date(2000, 0, 1)}
maxDate={endOfYear(new Date())}
onChange={handlePickerChange}
onClose={() => setShowPicker(false)}
/>
</Modal>
)}
</>
2020-08-02 06:20:52 +02:00
);
}
const CustomRange = ({ startDate, endDate, onClick }) => {
const { locale } = useLocale();
function handleClick(e) {
e.stopPropagation();
onClick();
}
return (
<>
<Icon className="mr-2" onClick={handleClick}>
<Calendar />
</Icon>
2020-09-17 01:28:54 +02:00
{dateFormat(startDate, 'd LLL y', locale)}
{!isSameDay(startDate, endDate) && `${dateFormat(endDate, 'd LLL y', locale)}`}
</>
);
};
export default DateFilter;