mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-30 21:51:59 +02:00
Refactored date navigation.
This commit is contained in:
parent
87bb9b1e73
commit
c5046d0043
|
@ -7,7 +7,7 @@ import { DateRange } from 'lib/types';
|
||||||
import { useLocale } from './useLocale';
|
import { useLocale } from './useLocale';
|
||||||
import { useApi } from './queries/useApi';
|
import { useApi } from './queries/useApi';
|
||||||
|
|
||||||
export function useDateRange(websiteId?: string) {
|
export function useDateRange(websiteId?: string): [DateRange, (value: string | DateRange) => void] {
|
||||||
const { get } = useApi();
|
const { get } = useApi();
|
||||||
const { locale } = useLocale();
|
const { locale } = useLocale();
|
||||||
const websiteConfig = websiteStore(state => state[websiteId]?.dateRange);
|
const websiteConfig = websiteStore(state => state[websiteId]?.dateRange);
|
||||||
|
@ -45,10 +45,7 @@ export function useDateRange(websiteId?: string) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return [dateRange, saveDateRange] as [
|
return [dateRange, saveDateRange];
|
||||||
{ startDate: Date; endDate: Date; modified?: number; value?: string; unit?: string },
|
|
||||||
(value: string | DateRange) => void,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useDateRange;
|
export default useDateRange;
|
||||||
|
|
|
@ -5,12 +5,13 @@ import DatePickerForm from 'components/metrics/DatePickerForm';
|
||||||
import { useLocale } from 'components/hooks';
|
import { useLocale } from 'components/hooks';
|
||||||
import { useMessages } from 'components/hooks';
|
import { useMessages } from 'components/hooks';
|
||||||
import Icons from 'components/icons';
|
import Icons from 'components/icons';
|
||||||
import { formatDate } from 'lib/date';
|
import { formatDate, parseDateValue } from 'lib/date';
|
||||||
|
|
||||||
export interface DateFilterProps {
|
export interface DateFilterProps {
|
||||||
value: string;
|
value: string;
|
||||||
startDate: Date;
|
startDate: Date;
|
||||||
endDate: Date;
|
endDate: Date;
|
||||||
|
offset: number;
|
||||||
className?: string;
|
className?: string;
|
||||||
onChange?: (value: string) => void;
|
onChange?: (value: string) => void;
|
||||||
selectedUnit?: string;
|
selectedUnit?: string;
|
||||||
|
@ -19,17 +20,18 @@ export interface DateFilterProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function DateFilter({
|
export function DateFilter({
|
||||||
value,
|
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
value,
|
||||||
|
offset,
|
||||||
className,
|
className,
|
||||||
onChange,
|
onChange,
|
||||||
selectedUnit,
|
|
||||||
showAllTime = false,
|
showAllTime = false,
|
||||||
alignment = 'end',
|
alignment = 'end',
|
||||||
}: DateFilterProps) {
|
}: DateFilterProps) {
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const [showPicker, setShowPicker] = useState(false);
|
const [showPicker, setShowPicker] = useState(false);
|
||||||
|
const { locale } = useLocale();
|
||||||
|
|
||||||
const options = [
|
const options = [
|
||||||
{ label: formatMessage(labels.today), value: '1day' },
|
{ label: formatMessage(labels.today), value: '1day' },
|
||||||
|
@ -76,19 +78,6 @@ export function DateFilter({
|
||||||
},
|
},
|
||||||
].filter(n => n);
|
].filter(n => n);
|
||||||
|
|
||||||
const renderValue = (value: string) => {
|
|
||||||
return value.startsWith('range') ? (
|
|
||||||
<CustomRange
|
|
||||||
startDate={startDate}
|
|
||||||
endDate={endDate}
|
|
||||||
selectedUnit={selectedUnit}
|
|
||||||
onClick={() => handleChange('custom')}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
options.find(e => e.value === value).label
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleChange = (value: string) => {
|
const handleChange = (value: string) => {
|
||||||
if (value === 'custom') {
|
if (value === 'custom') {
|
||||||
setShowPicker(true);
|
setShowPicker(true);
|
||||||
|
@ -104,6 +93,31 @@ export function DateFilter({
|
||||||
|
|
||||||
const handleClose = () => setShowPicker(false);
|
const handleClose = () => setShowPicker(false);
|
||||||
|
|
||||||
|
const renderValue = (value: string) => {
|
||||||
|
const { unit } = parseDateValue(value);
|
||||||
|
|
||||||
|
if (offset && unit === 'year') {
|
||||||
|
return formatDate(startDate, 'yyyy', locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset && unit === 'month') {
|
||||||
|
return formatDate(startDate, 'MMMM yyyy', locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.startsWith('range') || offset) {
|
||||||
|
return (
|
||||||
|
<CustomRange
|
||||||
|
startDate={startDate}
|
||||||
|
endDate={endDate}
|
||||||
|
unit={unit}
|
||||||
|
onClick={() => handleChange('custom')}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return options.find(e => e.value === value).label;
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Dropdown
|
<Dropdown
|
||||||
|
@ -137,10 +151,10 @@ export function DateFilter({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomRange = ({ startDate, endDate, selectedUnit, onClick }) => {
|
const CustomRange = ({ startDate, endDate, unit, onClick }) => {
|
||||||
const { locale } = useLocale();
|
const { locale } = useLocale();
|
||||||
|
|
||||||
const monthFormat = +selectedUnit?.num === 1 && selectedUnit?.unit === 'month';
|
const monthFormat = unit === 'month';
|
||||||
|
|
||||||
function handleClick(e) {
|
function handleClick(e) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
|
@ -1,36 +1,35 @@
|
||||||
import { useDateRange } from 'components/hooks';
|
import { useDateRange } from 'components/hooks';
|
||||||
import { isAfter } from 'date-fns';
|
import { isAfter } from 'date-fns';
|
||||||
import { incrementDateRange } from 'lib/date';
|
import { getOffsetDateRange } from 'lib/date';
|
||||||
import { Button, Icon, Icons } from 'react-basics';
|
import { Button, Icon, Icons } from 'react-basics';
|
||||||
import DateFilter from './DateFilter';
|
import DateFilter from './DateFilter';
|
||||||
import styles from './WebsiteDateFilter.module.css';
|
import styles from './WebsiteDateFilter.module.css';
|
||||||
|
import { DateRange } from 'lib/types';
|
||||||
|
|
||||||
export function WebsiteDateFilter({ websiteId }: { websiteId: string }) {
|
export function WebsiteDateFilter({ websiteId }: { websiteId: string }) {
|
||||||
const [dateRange, setDateRange] = useDateRange(websiteId);
|
const [dateRange, setDateRange] = useDateRange(websiteId);
|
||||||
const { value, startDate, endDate, selectedUnit } = dateRange;
|
const { value, startDate, endDate, offset } = dateRange;
|
||||||
const isFutureDate =
|
const disableForward =
|
||||||
value !== 'all' &&
|
value === 'all' || isAfter(getOffsetDateRange(dateRange, 1).startDate, new Date());
|
||||||
selectedUnit &&
|
|
||||||
isAfter(incrementDateRange(dateRange, -1).startDate, new Date());
|
|
||||||
|
|
||||||
const handleChange = value => {
|
const handleChange = (value: string | DateRange) => {
|
||||||
setDateRange(value);
|
setDateRange(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleIncrement = value => {
|
const handleIncrement = (increment: number) => {
|
||||||
setDateRange(incrementDateRange(dateRange, value));
|
setDateRange(getOffsetDateRange(dateRange, increment));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
{value !== 'all' && selectedUnit && (
|
{value !== 'all' && (
|
||||||
<div className={styles.buttons}>
|
<div className={styles.buttons}>
|
||||||
<Button onClick={() => handleIncrement(1)}>
|
<Button onClick={() => handleIncrement(-1)}>
|
||||||
<Icon rotate={90}>
|
<Icon rotate={90}>
|
||||||
<Icons.ChevronDown />
|
<Icons.ChevronDown />
|
||||||
</Icon>
|
</Icon>
|
||||||
</Button>
|
</Button>
|
||||||
<Button onClick={() => handleIncrement(-1)} disabled={isFutureDate}>
|
<Button onClick={() => handleIncrement(1)} disabled={disableForward}>
|
||||||
<Icon rotate={270}>
|
<Icon rotate={270}>
|
||||||
<Icons.ChevronDown />
|
<Icons.ChevronDown />
|
||||||
</Icon>
|
</Icon>
|
||||||
|
@ -42,7 +41,7 @@ export function WebsiteDateFilter({ websiteId }: { websiteId: string }) {
|
||||||
value={value}
|
value={value}
|
||||||
startDate={startDate}
|
startDate={startDate}
|
||||||
endDate={endDate}
|
endDate={endDate}
|
||||||
selectedUnit={selectedUnit}
|
offset={offset}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
showAllTime={true}
|
showAllTime={true}
|
||||||
/>
|
/>
|
||||||
|
|
215
src/lib/date.ts
215
src/lib/date.ts
|
@ -5,6 +5,7 @@ import {
|
||||||
addDays,
|
addDays,
|
||||||
addMonths,
|
addMonths,
|
||||||
addYears,
|
addYears,
|
||||||
|
subMinutes,
|
||||||
subHours,
|
subHours,
|
||||||
subDays,
|
subDays,
|
||||||
subMonths,
|
subMonths,
|
||||||
|
@ -23,13 +24,16 @@ import {
|
||||||
differenceInMinutes,
|
differenceInMinutes,
|
||||||
differenceInHours,
|
differenceInHours,
|
||||||
differenceInCalendarDays,
|
differenceInCalendarDays,
|
||||||
|
differenceInCalendarWeeks,
|
||||||
differenceInCalendarMonths,
|
differenceInCalendarMonths,
|
||||||
differenceInCalendarYears,
|
differenceInCalendarYears,
|
||||||
format,
|
format,
|
||||||
max,
|
max,
|
||||||
min,
|
min,
|
||||||
isDate,
|
isDate,
|
||||||
|
addWeeks,
|
||||||
subWeeks,
|
subWeeks,
|
||||||
|
endOfMinute,
|
||||||
} from 'date-fns';
|
} from 'date-fns';
|
||||||
import { getDateLocale } from 'lib/lang';
|
import { getDateLocale } from 'lib/lang';
|
||||||
import { DateRange } from 'lib/types';
|
import { DateRange } from 'lib/types';
|
||||||
|
@ -43,20 +47,75 @@ export const TIME_UNIT = {
|
||||||
year: 'year',
|
year: 'year',
|
||||||
};
|
};
|
||||||
|
|
||||||
const dateFuncs = {
|
export const CUSTOM_FORMATS = {
|
||||||
minute: [differenceInMinutes, addMinutes, startOfMinute],
|
'en-US': {
|
||||||
hour: [differenceInHours, addHours, startOfHour],
|
p: 'ha',
|
||||||
day: [differenceInCalendarDays, addDays, startOfDay],
|
pp: 'h:mm:ss',
|
||||||
month: [differenceInCalendarMonths, addMonths, startOfMonth],
|
},
|
||||||
year: [differenceInCalendarYears, addYears, startOfYear],
|
'fr-FR': {
|
||||||
|
'M/d': 'd/M',
|
||||||
|
'MMM d': 'd MMM',
|
||||||
|
'EEE M/d': 'EEE d/M',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const DATE_FUNCTIONS = {
|
||||||
|
minute: {
|
||||||
|
diff: differenceInMinutes,
|
||||||
|
add: addMinutes,
|
||||||
|
sub: subMinutes,
|
||||||
|
start: startOfMinute,
|
||||||
|
end: endOfMinute,
|
||||||
|
},
|
||||||
|
hour: {
|
||||||
|
diff: differenceInHours,
|
||||||
|
add: addHours,
|
||||||
|
sub: subHours,
|
||||||
|
start: startOfHour,
|
||||||
|
end: endOfHour,
|
||||||
|
},
|
||||||
|
day: {
|
||||||
|
diff: differenceInCalendarDays,
|
||||||
|
add: addDays,
|
||||||
|
sub: subDays,
|
||||||
|
start: startOfDay,
|
||||||
|
end: endOfDay,
|
||||||
|
},
|
||||||
|
week: {
|
||||||
|
diff: differenceInCalendarWeeks,
|
||||||
|
add: addWeeks,
|
||||||
|
sub: subWeeks,
|
||||||
|
start: startOfWeek,
|
||||||
|
end: endOfWeek,
|
||||||
|
},
|
||||||
|
month: {
|
||||||
|
diff: differenceInCalendarMonths,
|
||||||
|
add: addMonths,
|
||||||
|
sub: subMonths,
|
||||||
|
start: startOfMonth,
|
||||||
|
end: endOfMonth,
|
||||||
|
},
|
||||||
|
year: {
|
||||||
|
diff: differenceInCalendarYears,
|
||||||
|
add: addYears,
|
||||||
|
sub: subYears,
|
||||||
|
start: startOfYear,
|
||||||
|
end: endOfYear,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getTimezone() {
|
export function getTimezone() {
|
||||||
return moment.tz.guess();
|
return moment.tz.guess();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLocalTime(t: string | number | Date) {
|
export function parseDateValue(value: string) {
|
||||||
return addMinutes(new Date(t), new Date().getTimezoneOffset());
|
const match = value.match?.(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
|
||||||
|
|
||||||
|
if (!match) return null;
|
||||||
|
|
||||||
|
const { num, unit } = match.groups;
|
||||||
|
|
||||||
|
return { num: +num, unit };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseDateRange(value: string | object, locale = 'en-US'): DateRange {
|
export function parseDateRange(value: string | object, locale = 'en-US'): DateRange {
|
||||||
|
@ -81,91 +140,95 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
|
||||||
return {
|
return {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
unit: getMinimumUnit(startDate, endDate),
|
|
||||||
value,
|
value,
|
||||||
|
...parseDateValue(value),
|
||||||
|
offset: 0,
|
||||||
|
unit: getMinimumUnit(startDate, endDate),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const dateLocale = getDateLocale(locale);
|
const dateLocale = getDateLocale(locale);
|
||||||
|
const { num, unit } = parseDateValue(value);
|
||||||
|
|
||||||
const match = value?.match?.(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
|
if (num === 1) {
|
||||||
|
|
||||||
if (!match) return null;
|
|
||||||
|
|
||||||
const { num, unit } = match.groups;
|
|
||||||
const selectedUnit = { num: +num, unit };
|
|
||||||
|
|
||||||
if (+num === 1) {
|
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case 'day':
|
case 'day':
|
||||||
return {
|
return {
|
||||||
startDate: startOfDay(now),
|
startDate: startOfDay(now),
|
||||||
endDate: endOfDay(now),
|
endDate: endOfDay(now),
|
||||||
unit: 'hour',
|
unit: 'hour',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'week':
|
case 'week':
|
||||||
return {
|
return {
|
||||||
startDate: startOfWeek(now, { locale: dateLocale }),
|
startDate: startOfWeek(now, { locale: dateLocale }),
|
||||||
endDate: endOfWeek(now, { locale: dateLocale }),
|
endDate: endOfWeek(now, { locale: dateLocale }),
|
||||||
unit: 'day',
|
unit: 'day',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'month':
|
case 'month':
|
||||||
return {
|
return {
|
||||||
startDate: startOfMonth(now),
|
startDate: startOfMonth(now),
|
||||||
endDate: endOfMonth(now),
|
endDate: endOfMonth(now),
|
||||||
unit: 'day',
|
unit: 'day',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'year':
|
case 'year':
|
||||||
return {
|
return {
|
||||||
startDate: startOfYear(now),
|
startDate: startOfYear(now),
|
||||||
endDate: endOfYear(now),
|
endDate: endOfYear(now),
|
||||||
unit: 'month',
|
unit: 'month',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (+num === -1) {
|
if (num === -1) {
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case 'day':
|
case 'day':
|
||||||
return {
|
return {
|
||||||
startDate: subDays(startOfDay(now), 1),
|
startDate: subDays(startOfDay(now), 1),
|
||||||
endDate: subDays(endOfDay(now), 1),
|
endDate: subDays(endOfDay(now), 1),
|
||||||
unit: 'hour',
|
unit: 'hour',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'week':
|
case 'week':
|
||||||
return {
|
return {
|
||||||
startDate: subDays(startOfWeek(now, { locale: dateLocale }), 7),
|
startDate: subDays(startOfWeek(now, { locale: dateLocale }), 7),
|
||||||
endDate: subDays(endOfWeek(now, { locale: dateLocale }), 1),
|
endDate: subDays(endOfWeek(now, { locale: dateLocale }), 1),
|
||||||
unit: 'day',
|
unit: 'day',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'month':
|
case 'month':
|
||||||
return {
|
return {
|
||||||
startDate: subMonths(startOfMonth(now), 1),
|
startDate: subMonths(startOfMonth(now), 1),
|
||||||
endDate: subMonths(endOfMonth(now), 1),
|
endDate: subMonths(endOfMonth(now), 1),
|
||||||
unit: 'day',
|
unit: 'day',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'year':
|
case 'year':
|
||||||
return {
|
return {
|
||||||
startDate: subYears(startOfYear(now), 1),
|
startDate: subYears(startOfYear(now), 1),
|
||||||
endDate: subYears(endOfYear(now), 1),
|
endDate: subYears(endOfYear(now), 1),
|
||||||
unit: 'month',
|
unit: 'month',
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -175,66 +238,60 @@ export function parseDateRange(value: string | object, locale = 'en-US'): DateRa
|
||||||
return {
|
return {
|
||||||
startDate: subDays(startOfDay(now), +num - 1),
|
startDate: subDays(startOfDay(now), +num - 1),
|
||||||
endDate: endOfDay(now),
|
endDate: endOfDay(now),
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
unit,
|
unit,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
case 'hour':
|
case 'hour':
|
||||||
return {
|
return {
|
||||||
startDate: subHours(startOfHour(now), +num - 1),
|
startDate: subHours(startOfHour(now), +num - 1),
|
||||||
endDate: endOfHour(now),
|
endDate: endOfHour(now),
|
||||||
|
num: +num,
|
||||||
|
offset: 0,
|
||||||
unit,
|
unit,
|
||||||
value,
|
value,
|
||||||
selectedUnit,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function incrementDateRange(
|
export function getOffsetDateRange(dateRange: DateRange, increment: number) {
|
||||||
value: { startDate: any; endDate: any; selectedUnit: any },
|
const { startDate, endDate, unit, num, offset, value } = dateRange;
|
||||||
increment: number,
|
|
||||||
) {
|
|
||||||
const { startDate, endDate, selectedUnit } = value;
|
|
||||||
|
|
||||||
const { num, unit } = selectedUnit;
|
const change = num * increment;
|
||||||
|
const { add } = DATE_FUNCTIONS[unit];
|
||||||
|
const { unit: originalUnit } = parseDateValue(value);
|
||||||
|
|
||||||
const sub = Math.abs(num) * increment;
|
switch (originalUnit) {
|
||||||
|
|
||||||
switch (unit) {
|
|
||||||
case 'hour':
|
|
||||||
return {
|
|
||||||
...value,
|
|
||||||
startDate: subHours(startDate, sub),
|
|
||||||
endDate: subHours(endDate, sub),
|
|
||||||
value: 'range',
|
|
||||||
};
|
|
||||||
case 'day':
|
|
||||||
return {
|
|
||||||
...value,
|
|
||||||
startDate: subDays(startDate, sub),
|
|
||||||
endDate: subDays(endDate, sub),
|
|
||||||
value: 'range',
|
|
||||||
};
|
|
||||||
case 'week':
|
case 'week':
|
||||||
return {
|
return {
|
||||||
...value,
|
...dateRange,
|
||||||
startDate: subWeeks(startDate, sub),
|
startDate: addWeeks(startDate, increment),
|
||||||
endDate: subWeeks(endDate, sub),
|
endDate: addWeeks(endDate, increment),
|
||||||
value: 'range',
|
offset: offset + increment,
|
||||||
};
|
};
|
||||||
case 'month':
|
case 'month':
|
||||||
return {
|
return {
|
||||||
...value,
|
...dateRange,
|
||||||
startDate: subMonths(startDate, sub),
|
startDate: addMonths(startDate, increment),
|
||||||
endDate: subMonths(endDate, sub),
|
endDate: addMonths(endDate, increment),
|
||||||
value: 'range',
|
offset: offset + increment,
|
||||||
};
|
};
|
||||||
case 'year':
|
case 'year':
|
||||||
return {
|
return {
|
||||||
...value,
|
...dateRange,
|
||||||
startDate: subYears(startDate, sub),
|
startDate: addYears(startDate, increment),
|
||||||
endDate: subYears(endDate, sub),
|
endDate: addYears(endDate, increment),
|
||||||
value: 'range',
|
offset: offset + increment,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return {
|
||||||
|
startDate: add(startDate, change),
|
||||||
|
endDate: add(endDate, change),
|
||||||
|
value,
|
||||||
|
unit,
|
||||||
|
num,
|
||||||
|
offset: offset + increment,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -276,19 +333,19 @@ export function getDateFromString(str: string) {
|
||||||
|
|
||||||
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
|
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
|
||||||
const arr = [];
|
const arr = [];
|
||||||
const [diff, add, normalize] = dateFuncs[unit];
|
const { diff, add, start } = DATE_FUNCTIONS[unit];
|
||||||
const n = diff(endDate, startDate) + 1;
|
const n = diff(endDate, startDate) + 1;
|
||||||
|
|
||||||
function findData(date: Date) {
|
function findData(date: Date) {
|
||||||
const d = data.find(({ x }) => {
|
const d = data.find(({ x }) => {
|
||||||
return normalize(getDateFromString(x)).getTime() === date.getTime();
|
return start(getDateFromString(x)).getTime() === date.getTime();
|
||||||
});
|
});
|
||||||
|
|
||||||
return d?.y || 0;
|
return d?.y || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < n; i++) {
|
for (let i = 0; i < n; i++) {
|
||||||
const t = normalize(add(startDate, i));
|
const t = start(add(startDate, i));
|
||||||
const y = findData(t);
|
const y = findData(t);
|
||||||
|
|
||||||
arr.push({ x: t, y });
|
arr.push({ x: t, y });
|
||||||
|
@ -297,23 +354,6 @@ export function getDateArray(data: any[], startDate: Date, endDate: Date, unit:
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDateLength(startDate: Date, endDate: Date, unit: string | number) {
|
|
||||||
const [diff] = dateFuncs[unit];
|
|
||||||
return diff(endDate, startDate) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const CUSTOM_FORMATS = {
|
|
||||||
'en-US': {
|
|
||||||
p: 'ha',
|
|
||||||
pp: 'h:mm:ss',
|
|
||||||
},
|
|
||||||
'fr-FR': {
|
|
||||||
'M/d': 'd/M',
|
|
||||||
'MMM d': 'd MMM',
|
|
||||||
'EEE M/d': 'EEE d/M',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export function formatDate(date: string | number | Date, str: string, locale = 'en-US') {
|
export function formatDate(date: string | number | Date, str: string, locale = 'en-US') {
|
||||||
return format(
|
return format(
|
||||||
typeof date === 'string' ? new Date(date) : date,
|
typeof date === 'string' ? new Date(date) : date,
|
||||||
|
@ -331,3 +371,12 @@ export function maxDate(...args: Date[]) {
|
||||||
export function minDate(...args: any[]) {
|
export function minDate(...args: any[]) {
|
||||||
return min(args.filter(n => isDate(n)));
|
return min(args.filter(n => isDate(n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getLocalTime(t: string | number | Date) {
|
||||||
|
return addMinutes(new Date(t), new Date().getTimezoneOffset());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDateLength(startDate: Date, endDate: Date, unit: string | number) {
|
||||||
|
const { diff } = DATE_FUNCTIONS[unit];
|
||||||
|
return diff(endDate, startDate) + 1;
|
||||||
|
}
|
||||||
|
|
|
@ -179,11 +179,12 @@ export interface RealtimeUpdate {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DateRange {
|
export interface DateRange {
|
||||||
|
value: string;
|
||||||
startDate: Date;
|
startDate: Date;
|
||||||
endDate: Date;
|
endDate: Date;
|
||||||
value: string;
|
|
||||||
unit?: TimeUnit;
|
unit?: TimeUnit;
|
||||||
selectedUnit?: { num: number; unit: TimeUnit };
|
num?: number;
|
||||||
|
offset?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueryFilters {
|
export interface QueryFilters {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user