import React, { Fragment } from 'react' import { withRouter } from 'react-router-dom' import { navigateToQuery } from './query' import { Menu, Transition } from '@headlessui/react' import { ChevronDownIcon } from '@heroicons/react/20/solid' import classNames from 'classnames' import * as storage from './util/storage' import Flatpickr from 'react-flatpickr' import { parseNaiveDate, formatISO, formatDateRange } from './util/date.js' const COMPARISON_MODES = { 'off': 'Disable comparison', 'previous_period': 'Previous period', 'year_over_year': 'Year over year', 'custom': 'Custom period', } const DEFAULT_COMPARISON_MODE = 'previous_period' export const COMPARISON_DISABLED_PERIODS = ['realtime', 'all'] export const getStoredMatchDayOfWeek = function(domain) { return storage.getItem(`comparison_match_day_of_week__${domain}`) || 'true' } export const getStoredComparisonMode = function(domain) { const mode = storage.getItem(`comparison_mode__${domain}`) if (Object.keys(COMPARISON_MODES).includes(mode)) { return mode } else { return null } } const storeComparisonMode = function(domain, mode) { if (mode == "custom") return storage.setItem(`comparison_mode__${domain}`, mode) } export const isComparisonEnabled = function(mode) { return mode && mode !== "off" } export const toggleComparisons = function(history, query, site) { if (COMPARISON_DISABLED_PERIODS.includes(query.period)) return if (isComparisonEnabled(query.comparison)) { storeComparisonMode(site.domain, "off") navigateToQuery(history, query, { comparison: "off" }) } else { const storedMode = getStoredComparisonMode(site.domain) const newMode = isComparisonEnabled(storedMode) ? storedMode : DEFAULT_COMPARISON_MODE storeComparisonMode(site.domain, newMode) navigateToQuery(history, query, { comparison: newMode }) } } function ComparisonModeOption({ label, value, isCurrentlySelected, updateMode, setUiMode }) { const click = () => { if (value == "custom") { setUiMode("datepicker") } else { updateMode(value) } } const render = ({ active }) => { const buttonClass = classNames("px-4 py-2 w-full text-left font-medium text-sm dark:text-white cursor-pointer", { "bg-gray-100 text-gray-900 dark:bg-gray-900 dark:text-gray-100": active, "font-bold": isCurrentlySelected, }) return } const disabled = isCurrentlySelected && value !== "custom" return ( { render } ) } function MatchDayOfWeekInput({ history, query, site }) { const click = (matchDayOfWeek) => { storage.setItem(`comparison_match_day_of_week__${site.domain}`, matchDayOfWeek.toString()) navigateToQuery(history, query, { match_day_of_week: matchDayOfWeek.toString() }) } const buttonClass = (hover, selected) => classNames("px-4 py-2 w-full text-left font-medium text-sm dark:text-white cursor-pointer", { "bg-gray-100 text-gray-900 dark:bg-gray-900 dark:text-gray-100": hover, "font-bold": selected, }) return <> click(true)}> {({ active }) => ( )} click(false)}> {({ active }) => ( )} } const ComparisonInput = function({ site, query, history }) { if (COMPARISON_DISABLED_PERIODS.includes(query.period)) return null if (!isComparisonEnabled(query.comparison)) return null const updateMode = (mode, from = null, to = null) => { storeComparisonMode(site.domain, mode) navigateToQuery(history, query, { comparison: mode, compare_from: from, compare_to: to }) } const buildLabel = (site, query) => { if (query.comparison == "custom") { return formatDateRange(site, query.compare_from, query.compare_to) } else { return COMPARISON_MODES[query.comparison] } } const calendar = React.useRef(null) const [uiMode, setUiMode] = React.useState("menu") React.useEffect(() => { if (uiMode == "datepicker") { setTimeout(() => calendar.current.flatpickr.open(), 100) } }, [uiMode]) const flatpickrOptions = { mode: 'range', showMonths: 1, maxDate: 'today', minDate: site.statsBegin, animate: true, static: true, onClose: ([from, to], _dateStr, _instance) => { setUiMode("menu") if (from && to) { [from, to] = [parseNaiveDate(from), parseNaiveDate(to)] updateMode("custom", formatISO(from), formatISO(to)) } } } return ( <> vs.
{ buildLabel(site, query) } { Object.keys(COMPARISON_MODES).map((key) => ComparisonModeOption({ label: COMPARISON_MODES[key], value: key, isCurrentlySelected: key == query.comparison, updateMode, setUiMode })) } { query.comparison !== "custom" &&
}
{ uiMode == "datepicker" &&
}
) } export default withRouter(ComparisonInput)