import { DownOutlined, SyncOutlined } from '@ant-design/icons';
import Period from 'Types/app/period';
import { Dropdown, Button, Tooltip } from 'antd';
import cn from 'classnames';
import { observer } from 'mobx-react-lite';
import React from 'react';
import { components } from 'react-select';
import {
CUSTOM_RANGE,
DATE_RANGE_OPTIONS,
DATE_RANGE_COMPARISON_OPTIONS,
} from 'App/dateRange';
import { Calendar } from 'lucide-react';
import DateRangePopup from 'Shared/DateRangeDropdown/DateRangePopup';
import OutsideClickDetectingDiv from 'Shared/OutsideClickDetectingDiv';
import Select from 'Shared/Select';
import { useTranslation } from 'react-i18next';
interface Props {
period: any | null;
onChange: (data: any) => void;
disableCustom?: boolean;
right?: boolean;
timezone?: string;
isAnt?: boolean;
small?: boolean;
useButtonStyle?: boolean;
compPeriod?: any | null;
onChangeComparison?: (data: any) => void;
comparison?: boolean;
updateInstComparison?: (range: [start: string, end?: string] | null) => void;
[x: string]: any;
className?: string;
}
function SelectDateRange(props: Props) {
const { t } = useTranslation();
const [isCustom, setIsCustom] = React.useState(false);
const {
right = false,
period,
disableCustom = false,
timezone,
useButtonStyle = false,
} = props;
const usedPeriod = props.comparison ? props.compPeriod : period;
const dateRangeOptions = props.comparison
? DATE_RANGE_COMPARISON_OPTIONS
: DATE_RANGE_OPTIONS;
const dateRangeOptionsLocalized = dateRangeOptions.map((obj: any) => ({
...obj,
label: t(obj.label),
}));
const selectedValue = usedPeriod?.rangeName
? dateRangeOptionsLocalized.find(
(obj: any) => obj.value === usedPeriod?.rangeName,
)
: null;
const options = dateRangeOptionsLocalized.filter((obj: any) =>
disableCustom ? obj.value !== CUSTOM_RANGE : true,
).map((obj) => ({ ...obj, label: t(obj.label) }));
const onChange = (value: any) => {
if (value === CUSTOM_RANGE) {
setTimeout(() => {
setIsCustom(true);
}, 1);
return;
}
if (props.comparison && props.onChangeComparison) {
if (!value) {
props.updateInstComparison?.(null);
return props.onChangeComparison(null);
}
const newPeriod = new Period({
start: props.period.start,
end: props.period.end,
substract: value,
});
props.updateInstComparison?.([value]);
props.onChangeComparison(newPeriod);
} else {
props.onChange(new Period({ rangeName: value }));
}
};
const onApplyDateRange = (value: { start: any; end: any }) => {
if (props.comparison) {
const day = 86400000;
const originalPeriodLength = Math.ceil(
(props.period.end - props.period.start) / day,
);
const start = value.start.ts;
const end = value.start.ts + originalPeriodLength * day;
const compRange = new Period({
start,
end,
rangeName: CUSTOM_RANGE,
});
props.updateInstComparison?.([start.toString(), end.toString()]);
props.onChangeComparison(compRange);
} else {
const range = new Period({
rangeName: CUSTOM_RANGE,
start: value.start,
end: value.end,
});
props.onChange(range);
}
setIsCustom(false);
};
const isCustomRange = usedPeriod
? usedPeriod.rangeName === CUSTOM_RANGE
: false;
const isUSLocale =
navigator.language === 'en-US' || navigator.language.startsWith('en-US');
const customRange = isCustomRange
? usedPeriod.rangeFormatted(
isUSLocale ? 'MMM dd yyyy, hh:mm a' : 'MMM dd yyyy, HH:mm',
)
: '';
const isTileDisabled = ({ date, view }) => {
if (view !== 'month' || !props.comparison) return false;
return (
date.getTime() >= props.period.start && date.getTime() <= props.period.end
);
};
if (props.isAnt) {
return (