import * as React from 'react'; import { BookingCalendarNowSectionClasses, bookingCalendarNowSectionDefaultClasses, } from './classes'; import classNames from 'classnames'; import moment, { Moment } from 'moment'; export interface BookingCalendarNowSectionProps extends BookingCalendarNowSectionClasses { dataRowsCount: number; firstDataRowIndex: number; width: number; from: Moment; till: Moment; alwaysShowSections?: boolean; } export function BookingCalendarNowSection( props: BookingCalendarNowSectionProps ) { const { className, areaAfter, areaBefore, now, dataRowsCount, firstDataRowIndex, width, from, till, alwaysShowSections, } = props; const style = React.useMemo( () => ({ gridRowStart: firstDataRowIndex, gridRowEnd: `span ${dataRowsCount}`, ...(width ? { width: `${width}px` } : {}), }), [dataRowsCount, width, firstDataRowIndex] ); const widthMs = till.valueOf() - from.valueOf(); const startMs = from.valueOf(); const endMs = till.valueOf(); const onePxMs = Math.max(widthMs / (width || 1), 5000); const [nowMs, setNowMs] = React.useState(moment().valueOf()); const renderBefore = nowMs > startMs; const renderAfter = nowMs < endMs; React.useEffect(() => { if (renderBefore && renderAfter) { const interval = setInterval(() => { setNowMs(moment().valueOf()); }, onePxMs); return () => clearInterval(interval); } }, [onePxMs, renderBefore, renderAfter]); return (
{renderBefore && (alwaysShowSections || renderAfter) && (
)} {renderBefore && renderAfter && (
)} {renderAfter && (alwaysShowSections || renderBefore) && (
)}
); }