diff --git a/assets/js/dashboard/comparison-input.js b/assets/js/dashboard/comparison-input.js index be332ae52..bcc12d223 100644 --- a/assets/js/dashboard/comparison-input.js +++ b/assets/js/dashboard/comparison-input.js @@ -4,59 +4,102 @@ 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' const COMPARISON_MODES = { + 'off': 'Disable comparison', 'previous_period': 'Previous period', 'year_over_year': 'Year over year', } +const DEFAULT_COMPARISON_MODE = 'previous_period' + export const COMPARISON_DISABLED_PERIODS = ['realtime', 'all'] +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) { + storage.setItem(`comparison_mode__${domain}`, mode) +} + +export const isComparisonEnabled = function(mode) { + return mode && mode !== "off" +} + +export const toggleComparisons = function(history, query, site) { + if (!site.flags.comparisons) return + 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 DropdownItem({ label, value, isCurrentlySelected, updateMode }) { + return ( + updateMode(value)} + disabled={isCurrentlySelected}> + {({ active }) => ( + + )} + + ) +} + const ComparisonInput = function({ site, query, history }) { if (!site.flags.comparisons) return null if (COMPARISON_DISABLED_PERIODS.includes(query.period)) return null + if (!isComparisonEnabled(query.comparison)) return null - function update(key) { + const updateMode = (key) => { + storeComparisonMode(site.domain, key) navigateToQuery(history, query, { comparison: key }) } - function renderItem({ label, value, isCurrentlySelected }) { - const labelClass = classNames("font-medium text-sm", { "font-bold disabled": isCurrentlySelected }) - - return ( - update(value)} - className="px-4 py-2 leading-tight hover:bg-gray-100 dark:text-white hover:text-gray-900 dark:hover:bg-gray-900 dark:hover:text-gray-100 flex hover:cursor-pointer"> - { label } - - ) - } - return ( -
-
- - - { COMPARISON_MODES[query.comparison] || 'Compare to' } - - - - - { renderItem({ label: "Disabled", value: false, isCurrentlySelected: !query.comparison }) } - { Object.keys(COMPARISON_MODES).map((key) => renderItem({ label: COMPARISON_MODES[key], value: key, isCurrentlySelected: key == query.comparison})) } - - - -
-
+ <> + vs. +
+
+ + + { COMPARISON_MODES[query.comparison] || 'Compare to' } + + + + { Object.keys(COMPARISON_MODES).map((key) => DropdownItem({ label: COMPARISON_MODES[key], value: key, isCurrentlySelected: key == query.comparison, updateMode })) } + + + +
+
+ ) } diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index e869fef47..707b6bfbe 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -23,6 +23,7 @@ import { } from "./util/date"; import { navigateToQuery, QueryLink, QueryButton } from "./query"; import { shouldIgnoreKeypress } from "./keybinding.js" +import { COMPARISON_DISABLED_PERIODS, toggleComparisons, isComparisonEnabled } from "../dashboard/comparison-input.js" function renderArrow(query, site, period, prevDate, nextDate) { const insertionDate = parseUTCDate(site.statsBegin); @@ -194,11 +195,24 @@ function DatePicker({query, site, history}) { setOpen(false); - const keys = ['d', 'e', 'r', 'w', 'm', 'y', 't', 's', 'l', 'a']; - const redirects = [{date: false, period: 'day'}, {date: formatISO(shiftDays(nowForSite(site), -1)), period: 'day'}, {period: 'realtime'}, {date: false, period: '7d'}, {date: false, period: 'month'}, {date: false, period: 'year'}, {date: false, period: '30d'}, {date: false, period: '6mo'}, {date: false, period: '12mo'}, {date: false, period: 'all'}]; + const keybindings = { + d: {date: false, period: 'day'}, + e: {date: formatISO(shiftDays(nowForSite(site), -1)), period: 'day'}, + r: {period: 'realtime'}, + w: {date: false, period: '7d'}, + m: {date: false, period: 'month'}, + y: {date: false, period: 'year'}, + t: {date: false, period: '30d'}, + s: {date: false, period: '6mo'}, + l: {date: false, period: '12mo'}, + a: {date: false, period: 'all'}, + } - if (keys.includes(e.key.toLowerCase())) { - navigateToQuery(history, query, {...newSearch, ...(redirects[keys.indexOf(e.key.toLowerCase())])}); + const redirect = keybindings[e.key.toLowerCase()] + if (redirect) { + navigateToQuery(history, query, {...newSearch, ...redirect}) + } else if (e.key.toLowerCase() === 'x') { + toggleComparisons(history, query, site) } else if (e.key.toLowerCase() === 'c') { setOpen(true) setMode('calendar') @@ -352,6 +366,18 @@ function DatePicker({query, site, history}) { C + { !COMPARISON_DISABLED_PERIODS.includes(query.period) && site.flags.comparisons && +
+ { + toggleComparisons(history, query, site) + setOpen(false) + }} + className="px-4 py-2 text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 cursor-pointer flex items-center justify-between"> + { isComparisonEnabled(query.comparison) ? 'Disable comparison' : 'Compare' } + X + +
} ); @@ -381,7 +407,7 @@ function DatePicker({query, site, history}) { function renderPicker() { return (