import * as React from 'react' import { useRef, useState } from 'react' import Alert from '../../Alerts/Alert' import Button from '../../Button/Button' import Icon from '../../Icons/Icon' import Popover from '../../Popover/Popover' import { SideDrawer } from '../../SideDrawer/SideDrawer' import Tooltip from '../../Tooltip/Tooltip' import { useMediaQuery } from '../../../hooks/responsiveHooks' import type { ComparisonDatePickerProps, SelectionState, SelectionStateBase, SelectionStateDispatchActions, } from './ComparisonDatePicker' import { ComparisonDatePicker } from './ComparisonDatePicker' import styles from './comparison_date_picker_popup.module.scss' import type { SideDrawerBase } from '../../SideDrawer/SideDrawer' import { type AlertPropsWithDates } from '../Timeframe' import { c } from '../../../translations/LibraryTranslationService' type ComparisonDatePickerPopupProps = ComparisonDatePickerProps & { /** method will receive two values, `selectionState` and `selectionStateDispatch`; (selectionState, selectionStateDispatch) => void */ onApply: OnApply /** indicates if the custom date range has been selected with valid dates */ selectedCustomDateRange?: boolean /** Optionally add a layer position to the drawer to allow layered drawers. The number here will * be the position in the stack of SideDrawer layers if there are multiple SideDrawers that * need to be displayed. NOTE: Omit this prop if there is only 1 SideDrawer layer. This is * important for styling. */ sideDrawerLayerPosition?: number /** Passes any optional SideDrawer props to the ComparisonDatePicker SideDrawer */ cdpSideDrawerProps?: Omit< SideDrawerBase, 'children' | 'isOpen' | 'closeCallout' > alertProps?: AlertPropsWithDates /** Label for the first range */ firstRangeLabel?: string /** Label for the second range */ secondRangeLabel?: string } const CheckIcon = () => ( ) const DateRangeDisplay = ( props: ComparisonDatePickerPopupProps, ): React.JSX.Element => { const firstRange_startDate = props?.initial_firstRange_startDate?.format('MM-DD-YY') ?? null const firstRange_endDate = props?.initial_firstRange_endDate?.format('MM-DD-YY') ?? null const secondRange_startDate = props?.initial_secondRange_startDate?.format('MM-DD-YY') ?? null const secondRange_endDate = props?.initial_secondRange_endDate?.format('MM-DD-YY') ?? null return (
Custom Timeframe
Date Range
{firstRange_startDate} - {firstRange_endDate}
{props.showComparisonRange && (
Compare Range
{secondRange_startDate} - {secondRange_endDate}
)}
) } export function ComparisonDatePickerPopup( props: ComparisonDatePickerPopupProps, ): React.JSX.Element { const isMobileView = useMediaQuery({ type: 'max', breakpoint: 'md' }) const [isDrawerOpen, setDrawerOpen] = useState(false) const [isSelectedRangeDrawerOpen, setSelectedRangeDrawerOpen] = useState(false) // State to capture selectionState and selectionStateDispatch from ComparisonDatePicker const [capturedSelectionState, setCapturedSelectionState] = useState(null) const capturedSelectionStateDispatch = useRef | null>(null) const PopoverOrDrawerContent = ({ visible, setVisible, }: { visible: boolean setVisible: React.Dispatch> }) => { return ( <> {props.alertProps && (
)} { capturedSelectionStateDispatch.current = dispatch }} /> ) } const content = (
{isMobileView ? ( <> setDrawerOpen(false)} headerContent='' {...(capturedSelectionState && capturedSelectionStateDispatch.current && { footerContent: footerButtons( { ...props, setVisible: setDrawerOpen, visible: isDrawerOpen, }, capturedSelectionState, capturedSelectionStateDispatch.current, ), })} > {PopoverOrDrawerContent({ visible: isDrawerOpen, setVisible: setDrawerOpen, })} ) : ( PopoverOrDrawerContent({ visible, setVisible, }) } position='right' className='datepicker' maxWidth='460' // datePicker width(428) + padding(32) > {({ visible, setVisible }) => ( )} )} {props.selectedCustomDateRange && (
{isMobileView ? ( <>
setSelectedRangeDrawerOpen(false)} headerContent='' contentClassName={styles.drawerStyles} > ) : ( )}
)}
) return props.selectedCustomDateRange && !isMobileView ? ( } maxWidth='auto' > {content} ) : ( content ) } function footerButtons( props: ComparisonDatePickerPopupProps & { setVisible: React.Dispatch> visible: boolean }, selectionState: SelectionState, selectionStateDispatch: React.Dispatch, ) { const { setVisible, visible } = props return (
) } function ComparisonWrapper( props: ComparisonDatePickerPopupProps & { setVisible: React.Dispatch> visible: boolean isDrawerOpen: boolean onSelectionStateChange: (state: SelectionState) => void onSelectionStateDispatchChange: ( dispatch: React.Dispatch, ) => void }, ) { const { onSelectionStateChange, onSelectionStateDispatchChange, isDrawerOpen, } = props return ( {(selectionState, selectionStateDispatch) => { // Capture the current values whenever this renders onSelectionStateChange(selectionState) onSelectionStateDispatchChange(selectionStateDispatch) return !isDrawerOpen ? ( footerButtons(props, selectionState, selectionStateDispatch) ) : ( <> ) }} ) } type OnApply = (dates: SelectionStateBase) => void