2023-10-08 10:43:31 +00:00
|
|
|
import { CalendarOutlined } from '@ant-design/icons';
|
|
|
|
import dayjs, { Dayjs } from 'dayjs';
|
2023-10-28 17:24:46 +00:00
|
|
|
import { useMemo, useReducer } from 'react';
|
2023-10-08 10:51:47 +00:00
|
|
|
import { getMinimumUnit } from '../../shared';
|
2023-10-08 10:43:31 +00:00
|
|
|
import { DateRange, useGlobalStateStore } from '../store/global';
|
2023-10-08 10:51:47 +00:00
|
|
|
import { DateUnit } from '../utils/date';
|
2023-10-08 10:43:31 +00:00
|
|
|
|
|
|
|
export function useGlobalRangeDate(): {
|
|
|
|
label: React.ReactNode;
|
|
|
|
startDate: Dayjs;
|
|
|
|
endDate: Dayjs;
|
2023-10-08 10:51:47 +00:00
|
|
|
unit: DateUnit;
|
2023-10-28 17:24:46 +00:00
|
|
|
refresh: () => void;
|
2023-10-08 10:43:31 +00:00
|
|
|
} {
|
2023-10-28 17:24:46 +00:00
|
|
|
const {
|
|
|
|
dateRange,
|
|
|
|
startDate: globalStartDate,
|
|
|
|
endDate: globalEndDate,
|
|
|
|
} = useGlobalStateStore();
|
|
|
|
const [updateInc, refresh] = useReducer((state: number) => state + 1, 0);
|
2023-10-08 10:43:31 +00:00
|
|
|
|
2023-10-28 17:24:46 +00:00
|
|
|
const { label, startDate, endDate, unit } = useMemo(() => {
|
2023-10-08 10:43:31 +00:00
|
|
|
if (dateRange === DateRange.Custom) {
|
2023-10-28 17:24:46 +00:00
|
|
|
const _startDate = globalStartDate ?? dayjs().subtract(1, 'day');
|
|
|
|
const _endDate = globalEndDate ?? dayjs();
|
2023-10-08 10:43:31 +00:00
|
|
|
|
|
|
|
const isSameDate = dayjs(_startDate).isSame(_endDate, 'day');
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: (
|
|
|
|
<div className="flex gap-2 items-center flex-nowrap">
|
|
|
|
<CalendarOutlined />
|
|
|
|
<span>
|
|
|
|
{`${dayjs(_startDate).format('YYYY-MM-DD HH:mm')} - ${dayjs(
|
|
|
|
_endDate
|
|
|
|
).format(isSameDate ? 'HH:mm' : 'YYYY-MM-DD HH:mm')}`}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
startDate: _startDate,
|
|
|
|
endDate: _endDate,
|
2023-10-08 10:51:47 +00:00
|
|
|
unit: getMinimumUnit(_startDate, _endDate),
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.Today) {
|
|
|
|
return {
|
|
|
|
label: 'Today',
|
|
|
|
startDate: dayjs().startOf('day'),
|
|
|
|
endDate: dayjs().endOf('day'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'hour' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.Yesterday) {
|
|
|
|
return {
|
|
|
|
label: 'Yesterday',
|
|
|
|
startDate: dayjs().subtract(1, 'day').startOf('day'),
|
|
|
|
endDate: dayjs().subtract(1, 'day').endOf('day'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'hour' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.ThisWeek) {
|
|
|
|
return {
|
|
|
|
label: 'This week',
|
|
|
|
startDate: dayjs().startOf('week'),
|
|
|
|
endDate: dayjs().endOf('week'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'day' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.Last7Days) {
|
|
|
|
return {
|
|
|
|
label: 'Last 7 days',
|
|
|
|
startDate: dayjs().subtract(7, 'day').startOf('day'),
|
|
|
|
endDate: dayjs().endOf('day'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'day' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.ThisMonth) {
|
|
|
|
return {
|
|
|
|
label: 'This month',
|
|
|
|
startDate: dayjs().startOf('month'),
|
|
|
|
endDate: dayjs().endOf('month'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'day' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.Last30Days) {
|
|
|
|
return {
|
|
|
|
label: 'Last 30 days',
|
|
|
|
startDate: dayjs().subtract(30, 'day').startOf('day'),
|
|
|
|
endDate: dayjs().endOf('day'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'day' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.Last90Days) {
|
|
|
|
return {
|
|
|
|
label: 'Last 90 days',
|
|
|
|
startDate: dayjs().subtract(90, 'day').startOf('day'),
|
|
|
|
endDate: dayjs().endOf('day'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'day' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateRange === DateRange.ThisYear) {
|
|
|
|
return {
|
2023-10-08 10:51:47 +00:00
|
|
|
label: 'This year',
|
2023-10-08 10:43:31 +00:00
|
|
|
startDate: dayjs().startOf('year'),
|
|
|
|
endDate: dayjs().endOf('year'),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'month' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// default last 24 hour
|
|
|
|
return {
|
|
|
|
label: 'Last 24 hours',
|
|
|
|
startDate: dayjs().subtract(1, 'day'),
|
|
|
|
endDate: dayjs(),
|
2023-10-28 17:24:46 +00:00
|
|
|
unit: 'hour' as const,
|
2023-10-08 10:43:31 +00:00
|
|
|
};
|
2023-10-28 17:24:46 +00:00
|
|
|
}, [dateRange, globalStartDate, globalEndDate, updateInc]);
|
|
|
|
|
|
|
|
return { label, startDate, endDate, unit, refresh };
|
2023-10-08 10:43:31 +00:00
|
|
|
}
|