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 (