import { Menu, Transition } from '@headlessui/react'; import { ChevronDownIcon } from '@heroicons/react/20/solid' import React, { Fragment, useCallback, useEffect } from 'react'; import classNames from 'classnames' import * as storage from '../../util/storage' import { isKeyPressed } from '../../keybinding.js' export const INTERVAL_LABELS = { 'minute': 'Minutes', 'hour': 'Hours', 'date': 'Days', 'week': 'Weeks', 'month': 'Months' } export const getStoredInterval = function(period, domain) { return storage.getItem(`interval__${period}__${domain}`) } export const storeInterval = function(period, domain, interval) { storage.setItem(`interval__${period}__${domain}`, interval) } function subscribeKeybinding(element) { const handleKeyPress = useCallback((event) => { if (isKeyPressed(event, "i")) element.current?.click() }, []) useEffect(() => { document.addEventListener('keydown', handleKeyPress) return () => document.removeEventListener('keydown', handleKeyPress) }, [handleKeyPress]) } function DropdownItem({ option, currentInterval, updateInterval }) { return ( updateInterval(option)} key={option} disabled={option == currentInterval}> {({ active }) => ( { INTERVAL_LABELS[option] } )} ) } export function IntervalPicker({ graphData, query, site, updateInterval }) { if (query.period == 'realtime') return null const menuElement = React.useRef(null) subscribeKeybinding(menuElement) const currentInterval = graphData?.interval const options = site.validIntervalsByPeriod[query.period] return ( {({ open }) => ( <> { INTERVAL_LABELS[currentInterval] } { options.map((option) => DropdownItem({ option, currentInterval, updateInterval })) } )} ) }