2021-01-07 16:24:59 +03:00
|
|
|
import React from "react";
|
|
|
|
import { withRouter } from "react-router-dom";
|
2020-03-04 18:24:18 +03:00
|
|
|
import Flatpickr from "react-flatpickr";
|
2021-01-07 16:24:59 +03:00
|
|
|
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";
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
class DatePicker extends React.Component {
|
|
|
|
constructor(props) {
|
2021-01-07 16:24:59 +03:00
|
|
|
super(props);
|
|
|
|
this.handleKeyup = this.handleKeyup.bind(this);
|
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.state = { mode: "menu", open: false };
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-01-07 16:24:59 +03:00
|
|
|
document.addEventListener("keyup", this.handleKeyup);
|
|
|
|
document.addEventListener("mousedown", this.handleClick, false);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener("keyup", this.handleKeyup);
|
2021-01-07 16:24:59 +03:00
|
|
|
document.removeEventListener("mousedown", this.handleClick, false);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyup(e) {
|
2021-01-07 16:24:59 +03:00
|
|
|
const { query, history } = this.props;
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
if (e.ctrlKey || e.ctrlKey || e.altKey) return;
|
2020-10-19 10:56:03 +03:00
|
|
|
|
2020-11-11 13:30:29 +03:00
|
|
|
const newSearch = {
|
|
|
|
period: false,
|
|
|
|
from: false,
|
|
|
|
to: false,
|
2021-01-07 16:24:59 +03:00
|
|
|
date: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertionDate = parseUTCDate(this.props.site.insertedAt);
|
2020-11-11 13:30:29 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
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;
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
} 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;
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 13:30:29 +03:00
|
|
|
|
|
|
|
if (newSearch.date) {
|
2021-01-07 16:24:59 +03:00
|
|
|
navigateToQuery(history, query, newSearch);
|
2020-11-11 13:30:29 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
handleClick(e) {
|
|
|
|
if (this.dropDownNode && this.dropDownNode.contains(e.target)) return;
|
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
this.setState({ open: false });
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
timeFrameText() {
|
2021-01-07 16:24:59 +03:00
|
|
|
const { query, site } = this.props;
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
if (query.period === "day") {
|
2019-11-19 07:30:42 +03:00
|
|
|
if (isToday(site, query.date)) {
|
2021-01-07 16:24:59 +03:00
|
|
|
return "Today";
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
return formatDay(query.date);
|
|
|
|
} if (query.period === '7d') {
|
2019-11-19 07:30:42 +03:00
|
|
|
return 'Last 7 days'
|
2021-01-07 16:24:59 +03:00
|
|
|
} if (query.period === '30d') {
|
2019-11-19 07:30:42 +03:00
|
|
|
return 'Last 30 days'
|
2021-01-07 16:24:59 +03:00
|
|
|
} if (query.period === 'month') {
|
2020-12-23 11:42:22 +03:00
|
|
|
if (isThisMonth(site, query.date)) {
|
|
|
|
return 'Month to Date'
|
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
return formatMonthYYYY(query.date)
|
|
|
|
} if (query.period === '6mo') {
|
2020-01-08 12:25:46 +03:00
|
|
|
return 'Last 6 months'
|
2021-01-07 16:24:59 +03:00
|
|
|
} if (query.period === '12mo') {
|
2020-01-08 12:31:58 +03:00
|
|
|
return 'Last 12 months'
|
2021-01-07 16:24:59 +03:00
|
|
|
} if (query.period === 'custom') {
|
2020-03-04 18:24:18 +03:00
|
|
|
return `${formatDayShort(query.from)} - ${formatDayShort(query.to)}`
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2021-01-07 16:24:59 +03:00
|
|
|
return 'Realtime'
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderArrow(period, prevDate, nextDate) {
|
2021-01-07 16:24:59 +03:00
|
|
|
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" : ""
|
|
|
|
}`;
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2021-01-07 16:24:59 +03:00
|
|
|
<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>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
2021-01-07 16:24:59 +03:00
|
|
|
);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderArrows() {
|
2021-01-07 16:24:59 +03:00
|
|
|
const { query } = this.props;
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
if (query.period === "month") {
|
|
|
|
const prevDate = formatISO(shiftMonths(query.date, -1));
|
|
|
|
const nextDate = formatISO(shiftMonths(query.date, 1));
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
return this.renderArrow("month", prevDate, nextDate);
|
|
|
|
} if (query.period === "day") {
|
|
|
|
const prevDate = formatISO(shiftDays(query.date, -1));
|
|
|
|
const nextDate = formatISO(shiftDays(query.date, 1));
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
return this.renderArrow("day", prevDate, nextDate);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
open() {
|
2021-01-07 16:24:59 +03:00
|
|
|
this.setState({ mode: "menu", open: true });
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
renderDropDown() {
|
|
|
|
return (
|
2021-01-07 16:24:59 +03:00
|
|
|
<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"
|
|
|
|
>
|
2019-11-19 07:30:42 +03:00
|
|
|
<span className="mr-2">{this.timeFrameText()}</span>
|
2021-01-07 16:24:59 +03:00
|
|
|
<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"
|
|
|
|
>
|
2020-09-09 11:13:55 +03:00
|
|
|
<polyline points="6 9 12 15 18 9"></polyline>
|
|
|
|
</svg>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
<Transition
|
|
|
|
show={this.state.open}
|
|
|
|
enter="transition ease-out duration-100 transform"
|
|
|
|
enterFrom="opacity-0 scale-95"
|
|
|
|
enterTo="opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75 transform"
|
|
|
|
leaveFrom="opacity-100 scale-100"
|
|
|
|
leaveTo="opacity-0 scale-95"
|
|
|
|
>
|
|
|
|
{this.renderDropDownContent()}
|
|
|
|
</Transition>
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
2021-01-07 16:24:59 +03:00
|
|
|
);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
close() {
|
2021-01-07 16:24:59 +03:00
|
|
|
this.setState({ open: false });
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2020-09-09 11:13:55 +03:00
|
|
|
renderLink(period, text, opts = {}) {
|
2021-01-07 16:24:59 +03:00
|
|
|
const { query, site } = this.props;
|
2020-03-04 18:24:18 +03:00
|
|
|
let boldClass;
|
2021-01-07 16:24:59 +03:00
|
|
|
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" : "";
|
2020-03-04 18:24:18 +03:00
|
|
|
} else {
|
2021-01-07 16:24:59 +03:00
|
|
|
boldClass = query.period === period ? "font-bold" : "";
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
const date = opts.date ? formatISO(opts.date) : false;
|
2020-09-09 11:13:55 +03:00
|
|
|
|
2020-03-04 18:24:18 +03:00
|
|
|
return (
|
2021-01-07 16:24:59 +03:00
|
|
|
<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`}
|
|
|
|
>
|
2020-03-04 18:24:18 +03:00
|
|
|
{text}
|
2020-11-13 03:26:53 +03:00
|
|
|
</QueryLink>
|
2021-01-07 16:24:59 +03:00
|
|
|
);
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderDropDownContent() {
|
2021-01-07 16:24:59 +03:00
|
|
|
if (this.state.mode === "menu") {
|
2020-03-04 18:24:18 +03:00
|
|
|
return (
|
2021-01-07 16:24:59 +03:00
|
|
|
<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"
|
|
|
|
>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
2021-01-07 16:24:59 +03:00
|
|
|
{this.renderLink("day", "Today")}
|
|
|
|
{this.renderLink("realtime", "Realtime")}
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
2021-01-07 16:24:59 +03:00
|
|
|
{this.renderLink("7d", "Last 7 days")}
|
|
|
|
{this.renderLink("30d", "Last 30 days")}
|
2020-09-09 11:13:55 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-09-09 11:13:55 +03:00
|
|
|
<div className="py-1">
|
2020-12-23 11:42:22 +03:00
|
|
|
{ this.renderLink('month', 'Month to Date') }
|
2020-09-09 11:13:55 +03:00
|
|
|
{ this.renderLink('month', 'Last month', {date: lastMonth(this.props.site)}) }
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
2021-01-07 16:24:59 +03:00
|
|
|
{this.renderLink("6mo", "Last 6 months")}
|
|
|
|
{this.renderLink("12mo", "Last 12 months")}
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="border-t border-gray-200 dark:border-gray-500"></div>
|
2020-03-04 18:24:18 +03:00
|
|
|
<div className="py-1">
|
2021-01-07 16:24:59 +03:00
|
|
|
<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>
|
2020-03-04 18:24:18 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-07 16:24:59 +03:00
|
|
|
);
|
|
|
|
} if (this.state.mode === "calendar") {
|
2020-12-16 12:52:47 +03:00
|
|
|
const insertionDate = new Date(this.props.site.insertedAt);
|
|
|
|
const dayBeforeCreation = insertionDate - 86400000;
|
2021-01-07 16:24:59 +03:00
|
|
|
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)}
|
|
|
|
/>
|
|
|
|
)
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setCustomDate(dates) {
|
|
|
|
if (dates.length === 2) {
|
|
|
|
const [from, to] = dates
|
2020-12-21 16:26:37 +03:00
|
|
|
if (formatISO(from) === formatISO(to)) {
|
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'day',
|
|
|
|
date: formatISO(from),
|
|
|
|
from: false,
|
|
|
|
to: false,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'custom',
|
|
|
|
date: false,
|
|
|
|
from: formatISO(from),
|
|
|
|
to: formatISO(to),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-09-09 11:13:55 +03:00
|
|
|
this.close()
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openCalendar() {
|
2021-01-07 16:24:59 +03:00
|
|
|
this.calendar && this.calendar.flatpickr.open();
|
2020-03-04 18:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
render() {
|
|
|
|
return (
|
2020-12-29 12:00:41 +03:00
|
|
|
<div className="flex justify-end ml-auto pl-2">
|
2019-11-19 07:30:42 +03:00
|
|
|
{ this.renderArrows() }
|
|
|
|
{ this.renderDropDown() }
|
|
|
|
</div>
|
2021-01-07 16:24:59 +03:00
|
|
|
);
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:24:59 +03:00
|
|
|
export default withRouter(DatePicker);
|