Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

disable date navigation arrows when date is invalid for site (#483)

Mackenzie27b60894bbe1
diff --git a/assets/.eslintrc.json b/assets/.eslintrc.json
index 072c84aff5..7ee18d54ed 100644
--- a/assets/.eslintrc.json
+++ b/assets/.eslintrc.json
@@ -1,4 +1,7 @@
{
+ "env": {
+ "browser": true
+ },
"extends": ["airbnb", "prettier"],
"plugins": ["prettier"],
"rules": {
@@ -9,6 +12,7 @@
"react/prop-types": [0],
"max-classes-per-file": [0],
"react/jsx-one-expression-per-line": [0],
- "react/self-closing-comp": [0]
+ "react/self-closing-comp": [0],
+ "no-unused-expressions": [1, { "allowShortCircuit": true }]
}
}
diff --git a/assets/js/dashboard/date.js b/assets/js/dashboard/date.js
index f3f462a342..0470381c35 100644
--- a/assets/js/dashboard/date.js
+++ b/assets/js/dashboard/date.js
@@ -71,3 +71,31 @@ export function isToday(site, date) {
export function isThisMonth(site, date) {
return formatMonthYYYY(date) === formatMonthYYYY(nowForSite(site))
}
+
+export function isBefore(date1, date2, period) {
+ /* assumes 'day' and 'month' are the only valid periods */
+ if (date1.getFullYear() !== date2.getFullYear()) {
+ return date1.getFullYear() < date2.getFullYear();
+ }
+ if (date1.getMonth() !== date2.getMonth()) {
+ return date1.getMonth() < date2.getMonth();
+ }
+ if (period === "month") {
+ return false;
+ }
+ return date1.getDate() < date2.getDate()
+}
+
+export function isAfter(date1, date2, period) {
+ /* assumes 'day' and 'month' are the only valid periods */
+ if (date1.getFullYear() !== date2.getFullYear()) {
+ return date1.getFullYear() > date2.getFullYear();
+ }
+ if (date1.getMonth() !== date2.getMonth()) {
+ return date1.getMonth() > date2.getMonth();
+ }
+ if (period === "month") {
+ return false;
+ }
+ return date1.getDate() > date2.getDate()
+}
diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js
index 181e5f3b6c..151dd54367 100644
--- a/assets/js/dashboard/datepicker.js
+++ b/assets/js/dashboard/datepicker.js
@@ -1,147 +1,253 @@
-import React from 'react';
-import Transition from "../transition.js";
-import { withRouter, Link } from 'react-router-dom'
+import React from "react";
+import { withRouter } from "react-router-dom";
import Flatpickr from "react-flatpickr";
-import {shiftDays, shiftMonths, formatDay, formatDayShort, formatMonthYYYY, formatISO, isToday, lastMonth, nowForSite, isSameMonth, isThisMonth} from './date'
-import { navigateToQuery, QueryLink } from './query.js'
-
+import {
+ shiftDays,
+ shiftMonths,
+ formatDay,
+ formatDayShort,
+ formatMonthYYYY,
+ formatISO,
+ isToday,
+ lastMonth,
+ nowForSite,
+ isSameMonth,
+ isThisMonth,
+ parseUTCDate,
+ isBefore,
+ isAfter,
+} from "./date";
+import Transition from "../transition";
+import { navigateToQuery, QueryLink, QueryButton } from "./query";
class DatePicker extends React.Component {
constructor(props) {
- super(props)
- this.handleKeyup = this.handleKeyup.bind(this)
- this.handleClick = this.handleClick.bind(this)
- this.state = {mode: 'menu', open: false}
+ super(props);
+ this.handleKeyup = this.handleKeyup.bind(this);
+ this.handleClick = this.handleClick.bind(this);
+ this.state = { mode: "menu", open: false };
}
componentDidMount() {
- document.addEventListener('keyup', this.handleKeyup);
- document.addEventListener('mousedown', this.handleClick, false);
+ document.addEventListener("keyup", this.handleKeyup);
+ document.addEventListener("mousedown", this.handleClick, false);
}
componentWillUnmount() {
document.removeEventListener("keyup", this.handleKeyup);
- document.removeEventListener('mousedown', this.handleClick, false);
+ document.removeEventListener("mousedown", this.handleClick, false);
}
handleKeyup(e) {
- const {query, history} = this.props
+ const { query, history } = this.props;
- if (e.ctrlKey || e.ctrlKey || e.altKey) return
+ if (e.ctrlKey || e.ctrlKey || e.altKey) return;
const newSearch = {
period: false,
from: false,
to: false,
- date: false
- }
+ date: false,
+ };
+
+ const insertionDate = parseUTCDate(this.props.site.insertedAt);
- if (e.key === 'ArrowLeft') {
- if (query.period === 'day') {
- const prevDate = formatISO(shiftDays(query.date, -1))
- newSearch.period = 'day'
- newSearch.date = prevDate
- } else if (query.period === 'month') {
- const prevMonth = formatISO(shiftMonths(query.date, -1))
- newSearch.period = 'month'
- newSearch.date = prevMonth
+ if (e.key === "ArrowLeft") {
+ const prevDate = formatISO(shiftDays(query.date, -1));
+ const prevMonth = formatISO(shiftMonths(query.date, -1));
+
+ if (
+ query.period === "day" &&
+ !isBefore(parseUTCDate(prevDate), insertionDate, query.period)
+ ) {
+ newSearch.period = "day";
+ newSearch.date = prevDate;
+ } else if (
+ query.period === "month" &&
+ !isBefore(parseUTCDate(prevMonth), insertionDate, query.period)
+ ) {
+ newSearch.period = "month";
+ newSearch.date = prevMonth;
}
- } else if (e.key === 'ArrowRight') {
- if (query.period === 'day') {
- const nextDate = formatISO(shiftDays(query.date, 1))
- newSearch.period = 'day'
- newSearch.date = nextDate
- } else if (query.period === 'month') {
- const nextMonth = formatISO(shiftMonths(query.date, 1))
- newSearch.period = 'month'
- newSearch.date = nextMonth
+ } else if (e.key === "ArrowRight") {
+ const nextDate = formatISO(shiftDays(query.date, 1));
+ const nextMonth = formatISO(shiftMonths(query.date, 1));
+
+ if (
+ query.period === "day" &&
+ !isAfter(
+ parseUTCDate(nextDate),
+ nowForSite(this.props.site),
+ query.period
+ )
+ ) {
+ newSearch.period = "day";
+ newSearch.date = nextDate;
+ } else if (
+ query.period === "month" &&
+ !isAfter(
+ parseUTCDate(nextMonth),
+ nowForSite(this.props.site),
+ query.period
+ )
+ ) {
+ newSearch.period = "month";
+ newSearch.date = nextMonth;
}
}
if (newSearch.date) {
- navigateToQuery(
- history,
- query,
- newSearch
- )
+ navigateToQuery(history, query, newSearch);
}
}
handleClick(e) {
if (this.dropDownNode && this.dropDownNode.contains(e.target)) return;
- this.setState({open: false})
+ this.setState({ open: false });
}
timeFrameText() {
- const {query, site} = this.props
+ const { query, site } = this.props;
- if (query.period === 'day') {
+ if (query.period === "day") {
if (isToday(site, query.date)) {
- return 'Today'
- } else {
- return formatDay(query.date)
+ return "Today";
}
- } else if (query.period === '7d') {
+ return formatDay(query.date);
+ } if (query.period === '7d') {
return 'Last 7 days'
- } else if (query.period === '30d') {
+ } if (query.period === '30d') {
return 'Last 30 days'
- } else if (query.period === 'month') {
+ } if (query.period === 'month') {
if (isThisMonth(site, query.date)) {
return 'Month to Date'
- } else {
- return formatMonthYYYY(query.date)
}
- } else if (query.period === '6mo') {
+ return formatMonthYYYY(query.date)
+ } if (query.period === '6mo') {
return 'Last 6 months'
- } else if (query.period === '12mo') {
+ } if (query.period === '12mo') {
return 'Last 12 months'
- } else if (query.period === 'realtime') {
- return 'Realtime'
- } else if (query.period === 'custom') {
+ } if (query.period === 'custom') {
return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}`
}
+ return 'Realtime'
}
renderArrow(period, prevDate, nextDate) {
+ const insertionDate = parseUTCDate(this.props.site.insertedAt);
+ const disabledLeft = isBefore(
+ parseUTCDate(prevDate),
+ insertionDate,
+ period
+ );
+ const disabledRight = isAfter(
+ parseUTCDate(nextDate),
+ nowForSite(this.props.site),
+ period
+ );
+
+ const leftClasses = `flex items-center px-2 border-r border-gray-300 rounded-l
+ dark:border-gray-500 dark:text-gray-100 ${
+ disabledLeft ? "bg-gray-200 dark:bg-gray-900" : ""
+ }`;
+ const rightClasses = `flex items-center px-2 rounded-r dark:text-gray-100 ${
+ disabledRight ? "bg-gray-200 dark:bg-gray-900" : ""
+ }`;
return (
- <div className="flex rounded shadow bg-white dark:bg-gray-800 mr-2 cursor-pointer">
- <QueryLink to={{date: prevDate}} query={this.props.query} className="flex items-center px-2 border-r border-gray-300 dark:border-gray-500 dark:text-gray-100">
- <svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>
- </QueryLink>
- <QueryLink to={{date: nextDate}} query={this.props.query} className="flex items-center px-2 dark:text-gray-100">
- <svg className="feather h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>
- </QueryLink>
+ <div className="flex rounded shadow bg-white mr-4 cursor-pointer dark:bg-gray-800">
+ <QueryButton
+ to={{ date: prevDate }}
+ query={this.props.query}
+ className={leftClasses}
+ disabled={disabledLeft}
+ >
+ <svg
+ className="feather h-4 w-4"
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ >
+ <polyline points="15 18 9 12 15 6"></polyline>
+ </svg>
+ </QueryButton>
+ <QueryButton
+ to={{ date: nextDate }}
+ query={this.props.query}
+ className={rightClasses}
+ disabled={disabledRight}
+ >
+ <svg
+ className="feather h-4 w-4"
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ >
+ <polyline points="9 18 15 12 9 6"></polyline>
+ </svg>
+ </QueryButton>
</div>
- )
+ );
}
renderArrows() {
- const {query} = this.props
+ const { query } = this.props;
- if (query.period === 'month') {
- const prevDate = formatISO(shiftMonths(query.date, -1))
- const nextDate = formatISO(shiftMonths(query.date, 1))
+ if (query.period === "month") {
+ const prevDate = formatISO(shiftMonths(query.date, -1));
+ const nextDate = formatISO(shiftMonths(query.date, 1));
- return this.renderArrow('month', prevDate, nextDate)
- } else if (query.period === 'day') {
- const prevDate = formatISO(shiftDays(query.date, -1))
- const nextDate = formatISO(shiftDays(query.date, 1))
+ return this.renderArrow("month", prevDate, nextDate);
+ } if (query.period === "day") {
+ const prevDate = formatISO(shiftDays(query.date, -1));
+ const nextDate = formatISO(shiftDays(query.date, 1));
- return this.renderArrow('day', prevDate, nextDate)
+ return this.renderArrow("day", prevDate, nextDate);
}
}
open() {
- this.setState({mode: 'menu', open: true})
+ this.setState({ mode: "menu", open: true });
}
renderDropDown() {
return (
- <div className="relative" style={{height: '35.5px', width: '170px'}} ref={node => this.dropDownNode = node}>
- <div onClick={this.open.bind(this)} className="flex items-center justify-between rounded bg-white dark:bg-gray-800 shadow px-4 pr-3 py-2 leading-tight cursor-pointer text-sm font-medium text-gray-800 dark:text-gray-200 h-full">
+ <div
+ className="relative"
+ style={{ height: "35.5px", width: "190px" }}
+ ref={(node) => (this.dropDownNode = node)}
+ >
+ <div
+ onClick={this.open.bind(this)}
+ onKeyPress={this.open.bind(this)}
+ className="flex items-center justify-between rounded bg-white dark:bg-gray-800 shadow px-4
+ pr-3 py-2 leading-tight cursor-pointer text-sm font-medium text-gray-800
+ dark:text-gray-200 h-full"
+ tabIndex="0"
+ role="button"
+ aria-haspopup="true"
+ aria-expanded="false"
+ aria-controls="datemenu"
+ >
<span className="mr-2">{this.timeFrameText()}</span>
- <svg className="text-pink-500 h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
+ <svg
+ className="text-pink-500 h-4 w-4"
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ >
<polyline points="6 9 12 15 18 9"></polyline>
</svg>
</div>
@@ -158,47 +264,60 @@ class DatePicker extends React.Component {
{this.renderDropDownContent()}
</Transition>
</div>
- )
+ );
}
close() {
- this.setState({open: false})
+ this.setState({ open: false });
}
renderLink(period, text, opts = {}) {
- const {query, site} = this.props
+ const { query, site } = this.props;
let boldClass;
- if (query.period === 'day' && period === 'day') {
- boldClass = isToday(site, query.date) ? 'font-bold' : ''
- } else if (query.period === 'month' && period === 'month') {
- const linkDate = opts.date || nowForSite(site)
- boldClass = isSameMonth(linkDate, query.date) ? 'font-bold' : ''
+ if (query.period === "day" && period === "day") {
+ boldClass = isToday(site, query.date) ? "font-bold" : "";
+ } else if (query.period === "month" && period === "month") {
+ const linkDate = opts.date || nowForSite(site);
+ boldClass = isSameMonth(linkDate, query.date) ? "font-bold" : "";
} else {
- boldClass = query.period === period ? 'font-bold' : ''
+ boldClass = query.period === period ? "font-bold" : "";
}
- if (opts.date) { opts.date = formatISO(opts.date) }
+ const date = opts.date ? formatISO(opts.date) : false;
return (
- <QueryLink to={{from: false, to: false, date: false, period, ...opts}} onClick={this.close.bind(this)} query={this.props.query} className={boldClass + ' block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100'}>
+ <QueryLink
+ to={{from: false, to: false, date, period, ...opts}}
+ onClick={this.close.bind(this)}
+ query={this.props.query}
+ className={`${boldClass } block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100
+ dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100`}
+ >
{text}
</QueryLink>
- )
+ );
}
renderDropDownContent() {
- if (this.state.mode === 'menu') {
+ if (this.state.mode === "menu") {
return (
- <div className="absolute mt-2 rounded shadow-md z-10" style={{width: '235px', right: '-5px'}}>
- <div className="rounded bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 font-medium text-gray-800 dark:text-gray-200">
+ <div
+ id="datemenu"
+ className="absolute mt-2 rounded shadow-md z-10"
+ style={{width: '235px', right: '-5px'}}
+ >
+ <div
+ className="rounded bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5
+ font-medium text-gray-800 dark:text-gray-200"
+ >
<div className="py-1">
- { this.renderLink('day', 'Today') }
- { this.renderLink('realtime', 'Realtime') }
+ {this.renderLink("day", "Today")}
+ {this.renderLink("realtime", "Realtime")}
</div>
<div className="border-t border-gray-200 dark:border-gray-500"></div>
<div className="py-1">
- { this.renderLink('7d', 'Last 7 days') }
- { this.renderLink('30d', 'Last 30 days') }
+ {this.renderLink("7d", "Last 7 days")}
+ {this.renderLink("30d", "Last 30 days")}
</div>
<div className="border-t border-gray-200 dark:border-gray-500"></div>
<div className="py-1">
@@ -207,20 +326,47 @@ class DatePicker extends React.Component {
</div>
<div className="border-t border-gray-200 dark:border-gray-500"></div>
<div className="py-1">
- { this.renderLink('6mo', 'Last 6 months') }
- { this.renderLink('12mo', 'Last 12 months') }
+ {this.renderLink("6mo", "Last 6 months")}
+ {this.renderLink("12mo", "Last 12 months")}
</div>
<div className="border-t border-gray-200 dark:border-gray-500"></div>
<div className="py-1">
- <span onClick={e => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))} className="block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 cursor-pointer">Custom range</span>
+ <span
+ onClick={() => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))}
+ onKeyPress={() => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))}
+ className="block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100
+ dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100
+ cursor-pointer"
+ tabIndex="0"
+ role="button"
+ aria-haspopup="true"
+ aria-expanded="false"
+ aria-controls="calendar"
+ >
+ Custom range
+ </span>
</div>
</div>
</div>
- )
- } else if (this.state.mode === 'calendar') {
+ );
+ } if (this.state.mode === "calendar") {
const insertionDate = new Date(this.props.site.insertedAt);
const dayBeforeCreation = insertionDate - 86400000;
- return <Flatpickr options={{mode: 'range', maxDate: 'today', minDate: dayBeforeCreation, showMonths: 1, static: true, animate: true}} ref={calendar => this.calendar = calendar} className="invisible" onChange={this.setCustomDate.bind(this)} />
+ return (
+ <Flatpickr
+ id="calendar"
+ options={{
+ mode: 'range',
+ maxDate: 'today',
+ minDate: dayBeforeCreation,
+ showMonths: 1,
+ static: true,
+ animate: true}}
+ ref={calendar => this.calendar = calendar}
+ className="invisible"
+ onChange={this.setCustomDate.bind(this)}
+ />
+ )
}
}
@@ -255,7 +401,7 @@ class DatePicker extends React.Component {
}
openCalendar() {
- this.calendar && this.calendar.flatpickr.open()
+ this.calendar && this.calendar.flatpickr.open();
}
render() {
@@ -264,8 +410,8 @@ class DatePicker extends React.Component {
{ this.renderArrows() }
{ this.renderDropDown() }
</div>
- )
+ );
}
}
-export default withRouter(DatePicker)
+export default withRouter(DatePicker);
diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js
index adbf002927..42a06afc26 100644
--- a/assets/js/dashboard/query.js
+++ b/assets/js/dashboard/query.js
@@ -7,20 +7,18 @@ const PERIODS = ['realtime', 'day', 'month', '7d', '30d', '6mo', '12mo', 'custom
export function parseQuery(querystring, site) {
const q = new URLSearchParams(querystring)
let period = q.get('period')
- const periodKey = 'period__' + site.domain
+ const periodKey = `period__${ site.domain}`
if (PERIODS.includes(period)) {
if (period !== 'custom' && period !== 'realtime') window.localStorage[periodKey] = period
- } else {
- if (window.localStorage[periodKey]) {
+ } else if (window.localStorage[periodKey]) {
period = window.localStorage[periodKey]
} else {
period = '30d'
}
- }
return {
- period: period,
+ period,
date: q.get('date') ? parseUTCDate(q.get('date')) : nowForSite(site),
from: q.get('from') ? parseUTCDate(q.get('from')) : undefined,
to: q.get('to') ? parseUTCDate(q.get('to')) : undefined,
@@ -66,7 +64,8 @@ function generateQueryString(data) {
}
export function navigateToQuery(history, queryFrom, newData) {
- // if we update any data that we store in localstorage, make sure going back in history will revert them
+ // if we update any data that we store in localstorage, make sure going back in history will
+ // revert them
if (newData.period && newData.period !== queryFrom.period) {
const replaceQuery = new URLSearchParams(window.location.search)
replaceQuery.set('period', queryFrom.period)
@@ -91,28 +90,53 @@ class QueryLink extends React.Component {
render() {
const { history, query, to, ...props } = this.props
- return <Link
- {...props}
- to={{ pathname: window.location.pathname, search: generateQueryString(to) }}
- onClick={this.onClick}
- />
+ return (
+ <Link
+ {...props}
+ to={{ pathname: window.location.pathname, search: generateQueryString(to) }}
+ onClick={this.onClick}
+ />
+)
}
}
const QueryLinkWithRouter = withRouter(QueryLink)
export { QueryLinkWithRouter as QueryLink };
+class QueryButton extends React.Component {
+ render() {
+ const { history, query, to, disabled, className, children } = this.props
+ return (
+ <button
+ className={className}
+ onClick={(event) => {
+ event.preventDefault()
+ navigateToQuery(history, query, to)
+ if (this.props.onClick) this.props.onClick(event)
+ history.push({ pathname: window.location.pathname, search: generateQueryString(to) })
+ }}
+ type="button"
+ disabled={disabled}
+ >
+ {children}
+ </button>
+ )
+ }
+}
+const QueryButtonWithRouter = withRouter(QueryButton)
+export { QueryButtonWithRouter as QueryButton };
+
export function toHuman(query) {
if (query.period === 'day') {
return `on ${formatDay(query.date)}`
- } else if (query.period === 'month') {
+ } if (query.period === 'month') {
return `in ${formatMonthYYYY(query.date)}`
- } else if (query.period === '7d') {
+ } if (query.period === '7d') {
return 'in the last 7 days'
- } else if (query.period === '30d') {
+ } if (query.period === '30d') {
return 'in the last 30 days'
- } else if (query.period === '6mo') {
+ } if (query.period === '6mo') {
return 'in the last 6 months'
- } else if (query.period === '12mo') {
+ } if (query.period === '12mo') {
return 'in the last 12 months'
}
}
@@ -129,7 +153,6 @@ export function eventName(query) {
return 'pageviews'
}
return 'events'
- } else {
- return 'pageviews'
}
+ return 'pageviews'
}