diff --git a/CHANGELOG.md b/CHANGELOG.md index ada8679cd..dbd513820 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Unique Visitors (last 30 min) as a top stat in realtime view plausible/analytics#500 - Pinned filter and date selector rows while scrolling plausible/analytics#472 - Escape keyboard shortcut to clear all filters plausible/analytics#625 +- Keybindings for selecting dates/ranges plausible/analytics#630 ### Changed - Use alpine as base image to decrease Docker image size plausible/analytics#353 diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 42882e5b7..1b9493c39 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -25,6 +25,8 @@ class DatePicker extends React.Component { super(props); this.handleKeyup = this.handleKeyup.bind(this); this.handleClick = this.handleClick.bind(this); + this.openCalendar = this.openCalendar.bind(this); + this.open = this.open.bind(this); this.state = { mode: "menu", open: false }; } @@ -96,7 +98,16 @@ class DatePicker extends React.Component { } } - if (newSearch.date) { + this.setState({open: false}); + + const keys = ['d', 'r', 'w', 'm', 'y']; + const redirects = [{date: false, period: 'day'}, {period: 'realtime'}, {date: false, period: '7d'}, {date: false, period: 'month'}, {date: false, period: '12mo'}]; + + if (keys.includes(e.key.toLowerCase())) { + navigateToQuery(history, query, {...newSearch, ...(redirects[keys.indexOf(e.key) % 5])}); + } else if (e.key.toLowerCase() === 'c') { + this.setState({mode: 'calendar', open: true}, this.openCalendar); + } else if (newSearch.date) { navigateToQuery(history, query, newSearch); } } @@ -226,8 +237,8 @@ class DatePicker extends React.Component { ref={(node) => (this.dropDownNode = node)} >