openreplay/frontend/app/components/shared/SelectDateRange/SelectDateRange.tsx
Delirium a2fce7e291
Assist stats UI (#1489)
* feat(ui): start assist stats

* feat(ui): design mock up for stats

* feat(ui): naming...

* feat(ui): types, api service, some changes for recs and loaders

* feat(ui): csv export button/request

* feat(ui): csv export button/request

* feat(ui): format dates

* feat(ui): some fixes for stats requests

* fix(tracker): fix test

* fix(tracker): fix ci build

* fix(tracker): fix assist tests

* fix(tracker): bump test coverage, fix minor bug

* fix(ui): more cypress fixes

* fix(ui): add proj id to socket connection

* fix(ui): remove console log

* fix(ui): update filters

* feat(ui):fix some api keys for stats

* feat(ui): fix type

* feat(ui): remove unused

* feat(ui): some fixes

* feat(ui): some fixes 2

* fix(ui): some search fixes

* fix(ui): change api keys

* fix(ui): change api keys

* fix(ui): pdf button fix

* fix(ui): pdf button fix

* fix(ui): some ui fixes after review

* fix(ui): fix csv export

* fix(ui): change header for pds export for stats

* fix(ui): change header width for exports
2023-10-16 13:54:37 +02:00

159 lines
4.8 KiB
TypeScript

import React from 'react';
import { Button, Dropdown, Space, Typography, Input } from 'antd';
import { FilePdfOutlined, DownOutlined, TableOutlined } from '@ant-design/icons';
import { DATE_RANGE_OPTIONS, CUSTOM_RANGE } from 'App/dateRange';
import Select from 'Shared/Select';
import Period from 'Types/app/period';
import { components } from 'react-select';
import DateRangePopup from 'Shared/DateRangeDropdown/DateRangePopup';
import OutsideClickDetectingDiv from 'Shared/OutsideClickDetectingDiv';
import cn from 'classnames';
import { observer } from 'mobx-react-lite';
interface Props {
period: any;
onChange: (data: any) => void;
disableCustom?: boolean;
right?: boolean;
timezone?: string;
isAnt?: boolean;
[x: string]: any;
}
function SelectDateRange(props: Props) {
const [isCustom, setIsCustom] = React.useState(false);
const { right = false, period, disableCustom = false, timezone, ...rest } = props;
let selectedValue = DATE_RANGE_OPTIONS.find((obj: any) => obj.value === period.rangeName);
const options = DATE_RANGE_OPTIONS.filter((obj: any) =>
disableCustom ? obj.value !== CUSTOM_RANGE : true
);
const onChange = (value: any) => {
if (value === CUSTOM_RANGE) {
setIsCustom(true);
} else {
// @ts-ignore
props.onChange(new Period({ rangeName: value }));
}
};
const onApplyDateRange = (value: any) => {
// @ts-ignore
const range = new Period({ rangeName: CUSTOM_RANGE, start: value.start, end: value.end });
props.onChange(range);
setIsCustom(false);
};
const isCustomRange = period.rangeName === CUSTOM_RANGE;
const customRange = isCustomRange ? period.rangeFormatted() : '';
if (props.isAnt) {
const onAntUpdate = ({ key }: { key: string }) => {
onChange(key);
};
return (
<div className={'relative'}>
<Dropdown
menu={{
items: options.map((o) => ({ key: o.value, label: o.label })),
onClick: onAntUpdate,
}}
>
<Button size={'small'}>
<Space>
<Typography.Text>{selectedValue?.label || 'Select Range'}</Typography.Text>
<DownOutlined rev={undefined} />
</Space>
</Button>
</Dropdown>
{isCustom && (
<OutsideClickDetectingDiv
onClickOutside={(e: any) => {
if (
e.target.parentElement.parentElement.classList.contains(
'rc-time-picker-panel-select'
) ||
e.target.parentElement.parentElement.classList[0]?.includes('-menu')
) {
return false;
}
setIsCustom(false);
}}
>
<div
className={cn('absolute top-0 mt-10 z-40', { 'right-0': right })}
style={{
width: '770px',
fontSize: '14px',
textAlign: 'left',
}}
>
<DateRangePopup
timezone={timezone}
onApply={onApplyDateRange}
onCancel={() => setIsCustom(false)}
selectedDateRange={period.range}
/>
</div>
</OutsideClickDetectingDiv>
)}
</div>
);
}
return (
<div className="relative">
<Select
plain
value={selectedValue}
options={options}
onChange={({ value }: any) => onChange(value.value)}
components={{
SingleValue: ({ children, ...props }: any) => {
return (
<components.SingleValue {...props}>
{isCustomRange ? customRange : children}
</components.SingleValue>
);
},
}}
period={period}
right={true}
style={{ width: '100%' }}
/>
{isCustom && (
<OutsideClickDetectingDiv
onClickOutside={(e: any) => {
if (
e.target.parentElement.parentElement.classList.contains(
'rc-time-picker-panel-select'
) ||
e.target.parentElement.parentElement.classList[0]?.includes('-menu')
) {
return false;
}
setIsCustom(false);
}}
>
<div
className={cn('absolute top-0 mt-10 z-40', { 'right-0': right })}
style={{
width: '770px',
fontSize: '14px',
textAlign: 'left',
}}
>
<DateRangePopup
timezone={timezone}
onApply={onApplyDateRange}
onCancel={() => setIsCustom(false)}
selectedDateRange={period.range}
/>
</div>
</OutsideClickDetectingDiv>
)}
</div>
);
}
export default observer(SelectDateRange);