From 0de89bad82ab04ef1daa506c20bc7e7c41ca01d9 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Wed, 21 Jul 2021 16:50:26 +0300 Subject: [PATCH] Add filter groups (#1167) * Add filter groups * New flow for filters * Visual consistency * Mobile improvements to dropdown * Do not let filters wrap on a new line * Fix country filter * Add mix format to pre-commit configuration * Make eslint happy * Fix formatting * Use transition from headlessUI --- .pre-commit-config.yaml | 5 + assets/.eslintrc.json | 7 +- assets/css/app.css | 13 + .../js/dashboard/components/search-select.js | 14 +- assets/js/dashboard/datepicker-arrows.js | 91 ++++ assets/js/dashboard/datepicker.js | 217 +++------ assets/js/dashboard/filters.js | 445 ++++++++++-------- assets/js/dashboard/historical.js | 24 +- assets/js/dashboard/query.js | 11 +- assets/js/dashboard/realtime.js | 20 +- assets/js/dashboard/router.js | 2 +- assets/js/dashboard/site-switcher.js | 10 +- assets/js/dashboard/stats/current-visitors.js | 8 +- assets/js/dashboard/stats/modals/filter.js | 353 ++++++++------ assets/js/dashboard/stats/modals/sources.js | 1 - assets/js/dashboard/stats/visitor-graph.js | 2 +- assets/js/dashboard/url.js | 7 + assets/js/transition.js | 107 ----- assets/package-lock.json | 54 +++ assets/package.json | 3 + .../templates/layout/_header.html.eex | 2 +- lib/workers/notify_annual_renewal.ex | 41 +- test/workers/notify_annual_renewal_test.exs | 4 +- 23 files changed, 751 insertions(+), 690 deletions(-) create mode 100644 assets/js/dashboard/datepicker-arrows.js create mode 100644 assets/js/dashboard/url.js delete mode 100644 assets/js/transition.js diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 47a5baffc..d894db377 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,3 +26,8 @@ repos: - eslint-plugin-jsx-a11y@6.4.1 - eslint-plugin-react@7.21.5 - eslint-plugin-react-hooks@4.2.0 + + - repo: https://gitlab.com/jvenom/elixir-pre-commit-hooks + rev: v1.0.0 + hooks: + - id: mix-format diff --git a/assets/.eslintrc.json b/assets/.eslintrc.json index 7ee18d54e..e25d18b46 100644 --- a/assets/.eslintrc.json +++ b/assets/.eslintrc.json @@ -5,7 +5,7 @@ "extends": ["airbnb", "prettier"], "plugins": ["prettier"], "rules": { - "max-len": [2, {"code": 100}], + "max-len": [0, {"code": 120}], "prettier/prettier": [2], "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }], "react/destructuring-assignment": [0], @@ -13,6 +13,9 @@ "max-classes-per-file": [0], "react/jsx-one-expression-per-line": [0], "react/self-closing-comp": [0], - "no-unused-expressions": [1, { "allowShortCircuit": true }] + "no-unused-expressions": [1, { "allowShortCircuit": true }], + "no-unused-vars": [2, { "varsIgnorePattern": "^_", "argsIgnorePattern": "^_" }], + "jsx-a11y/click-events-have-key-events": [0], + "jsx-a11y/no-static-element-interactions": [0] } } diff --git a/assets/css/app.css b/assets/css/app.css index e3e0d7660..e1af8c8d8 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -315,3 +315,16 @@ iframe[hidden] { .pagination-link[disabled] { @apply cursor-default bg-gray-100 dark:bg-gray-300 pointer-events-none; } + +.flatpickr-calendar.static.open { + top: 12px; +} + +@media (max-width: 768px) { + .flatpickr-wrapper { + position: absolute!important; + right: 0!important; + left: 0!important; + } +} + diff --git a/assets/js/dashboard/components/search-select.js b/assets/js/dashboard/components/search-select.js index 24ec5e2b3..766140af8 100644 --- a/assets/js/dashboard/components/search-select.js +++ b/assets/js/dashboard/components/search-select.js @@ -2,6 +2,7 @@ import React, {useState, useCallback} from 'react' import {useCombobox} from 'downshift' import classNames from 'classnames' import debounce from 'debounce-promise' +import { ChevronDownIcon } from '@heroicons/react/solid' function selectInputText(e) { e.target.select() @@ -27,7 +28,6 @@ function Spinner() { export default function SearchSelect(props) { const [items, setItems] = useState([]) const [loading, setLoading] = useState(false) - const [initialLoadComplete, setInitialLoadComplete] = useState(false) function fetchOptions({inputValue, isOpen}) { setLoading(isOpen) @@ -60,11 +60,9 @@ export default function SearchSelect(props) { props.onInput(changes.inputValue) }, initialSelectedItem: props.initialSelectedItem, - onIsOpenChange: ({inputValue}) => { - if (!initialLoadComplete) { - fetchOptions({inputValue: inputValue, isOpen: true}).then(() => { - setInitialLoadComplete(true) - }) + onIsOpenChange: ({isOpen, inputValue}) => { + if (isOpen) { + fetchOptions({inputValue, isOpen}) } } }) @@ -79,11 +77,11 @@ export default function SearchSelect(props) { } return ( -
+
- { !loading && } + { !loading && } { loading && }
diff --git a/assets/js/dashboard/datepicker-arrows.js b/assets/js/dashboard/datepicker-arrows.js new file mode 100644 index 000000000..8d6ec9e98 --- /dev/null +++ b/assets/js/dashboard/datepicker-arrows.js @@ -0,0 +1,91 @@ +import React from "react"; +import { + shiftDays, + shiftMonths, + formatISO, + nowForSite, + parseUTCDate, + isBefore, + isAfter, +} from "./date"; +import { QueryButton } from "./query"; + +function renderArrow(query, site, period, prevDate, nextDate) { + const insertionDate = parseUTCDate(site.insertedAt); + const disabledLeft = isBefore( + parseUTCDate(prevDate), + insertionDate, + period + ); + const disabledRight = isAfter( + parseUTCDate(nextDate), + nowForSite(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-300 dark:bg-gray-950" : "hover:bg-gray-100 dark:hover:bg-gray-900" + }`; + const rightClasses = `flex items-center px-2 rounded-r dark:text-gray-100 ${ + disabledRight ? "bg-gray-300 dark:bg-gray-950" : "hover:bg-gray-100 dark:hover:bg-gray-900" + }`; + return ( +
+ + + + + + + + + + +
+ ); +} + +export default function DatePickerArrows({site, query}) { + if (query.period === "month") { + const prevDate = formatISO(shiftMonths(query.date, -1)); + const nextDate = formatISO(shiftMonths(query.date, 1)); + + return renderArrow(query, site, "month", prevDate, nextDate); + } if (query.period === "day") { + const prevDate = formatISO(shiftDays(query.date, -1)); + const nextDate = formatISO(shiftDays(query.date, 1)); + + return renderArrow(query, site, "day", prevDate, nextDate); + } + + return null +} diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index c5b4a897f..f047d225a 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -1,6 +1,8 @@ -import React from "react"; +import React, { Fragment } from "react"; import { withRouter } from "react-router-dom"; import Flatpickr from "react-flatpickr"; +import classNames from 'classnames' +import { ChevronDownIcon } from '@heroicons/react/solid' import { shiftDays, shiftMonths, @@ -17,7 +19,7 @@ import { isBefore, isAfter, } from "./date"; -import Transition from "../transition"; +import { Transition } from '@headlessui/react' import { navigateToQuery, QueryLink, QueryButton } from "./query"; class DatePicker extends React.Component { @@ -26,7 +28,7 @@ class DatePicker extends React.Component { this.handleKeydown = this.handleKeydown.bind(this); this.handleClick = this.handleClick.bind(this); this.openCalendar = this.openCalendar.bind(this); - this.open = this.open.bind(this); + this.toggle = this.toggle.bind(this); this.state = { mode: "menu", open: false }; } @@ -148,137 +150,9 @@ class DatePicker extends React.Component { 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-300 dark:bg-gray-950" : "hover:bg-gray-200 dark:hover:bg-gray-900" - }`; - const rightClasses = `flex items-center px-2 rounded-r dark:text-gray-100 ${ - disabledRight ? "bg-gray-300 dark:bg-gray-950" : "hover:bg-gray-200 dark:hover:bg-gray-900" - }`; - return ( -
- - - - - - - - - - -
- ); - } - - renderArrows() { - const { query } = this.props; - - if (query.period === "month") { - const prevDate = formatISO(shiftMonths(query.date, -1)); - const nextDate = formatISO(shiftMonths(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); - } - } - - open() { - this.setState({ mode: "menu", open: true }); - } - - renderDropDown() { - return ( -
(this.dropDownNode = node)} - > - - - - {this.renderDropDownContent()} - -
- ); + toggle() { + const newMode = this.state.mode === 'calendar' && !this.state.open ? 'menu' : this.state.mode + this.setState({ mode: newMode, open: !this.state.open }); } close() { @@ -314,7 +188,7 @@ class DatePicker extends React.Component { to={{from: false, to: false, period, ...opts}} onClick={this.close.bind(this)} query={this.props.query} - className={`${boldClass } px-4 py-2 md:text-sm leading-tight hover:bg-gray-200 + className={`${boldClass } px-4 py-2 text-sm leading-tight hover:bg-gray-100 hover:text-gray-900 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 flex items-center justify-between`} > {text} @@ -328,11 +202,10 @@ class DatePicker extends React.Component { return (
@@ -359,7 +232,7 @@ class DatePicker extends React.Component { this.setState({mode: 'calendar'}, this.openCalendar)} onKeyPress={() => this.setState({mode: 'calendar'}, this.openCalendar)} - className="px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 + 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" tabIndex="0" @@ -379,19 +252,21 @@ class DatePicker extends React.Component { const insertionDate = new Date(this.props.site.insertedAt); const dayBeforeCreation = insertionDate - 86400000; return ( - this.calendar = calendar} - className="invisible" - onChange={this.setCustomDate.bind(this)} - /> +
+ this.calendar = calendar} + className="invisible" + onChange={this.setCustomDate.bind(this)} + /> +
) } } @@ -432,9 +307,41 @@ class DatePicker extends React.Component { render() { return ( -
- { this.renderArrows() } - { this.renderDropDown() } +
(this.dropDownNode = node)} + > + + + + {this.renderDropDownContent()} +
); } diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index 6189f7dd2..769040435 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -1,29 +1,173 @@ -import React from 'react'; -import { withRouter, Link } from 'react-router-dom' -import { countFilters, formattedFilters, navigateToQuery } from './query' +import React, { Fragment, useState } from 'react'; +import { Link, withRouter } from 'react-router-dom' +import { AdjustmentsIcon, PlusIcon, XIcon, PencilIcon } from '@heroicons/react/solid' +import classNames from 'classnames' import Datamap from 'datamaps' -import Transition from "../transition.js"; +import { Menu, Transition } from '@headlessui/react' + +import { appliedFilters, navigateToQuery, formattedFilters } from './query' +import { FILTER_GROUPS, formatFilterGroup, filterGroupForFilter } from './stats/modals/filter' + +function removeFilter(key, history, query) { + const newOpts = { + [key]: false + } + if (key === 'goal') { newOpts.props = false } + navigateToQuery( + history, + query, + newOpts + ) +} + +function clearAllFilters(history, query) { + const newOpts = Object.keys(query.filters).reduce((acc, red) => ({ ...acc, [red]: false }), {}); + navigateToQuery( + history, + query, + newOpts + ); +} + +function filterText(key, value, query) { + if (key === "goal") { + return Completed goal {value} + } + if (key === "props") { + const [metaKey, metaValue] = Object.entries(value)[0] + const eventName = query.filters.goal ? query.filters.goal : 'event' + return {eventName}.{metaKey} is {metaValue} + } + if (key === "source") { + return Source: {value} + } + if (key === "utm_medium") { + return UTM medium: {value} + } + if (key === "utm_source") { + return UTM source: {value} + } + if (key === "utm_campaign") { + return UTM campaign: {value} + } + if (key === "referrer") { + return Referrer: {value} + } + if (key === "screen") { + return Screen size: {value} + } + if (key === "browser") { + return Browser: {value} + } + if (key === "browser_version") { + const browserName = query.filters.browser ? query.filters.browser : 'Browser' + return {browserName}.Version: {value} + } + if (key === "os") { + return Operating System: {value} + } + if (key === "os_version") { + const osName = query.filters.os ? query.filters.os : 'OS' + return {osName}.Version: {value} + } + if (key === "country") { + const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; + const selectedCountry = allCountries.find((c) => c.id === value) || {properties: {name: value}}; + return Country: {selectedCountry.properties.name} + } + if (key === "page") { + return Page: {value} + } + if (key === "entry_page") { + return Entry Page: {value} + } + if (key === "exit_page") { + return Exit Page: {value} + } + + throw new Error(`Unknown filter: ${key}`) +} + +function renderDropdownFilter(site, history, [key, value], query) { + if (key === 'props') { + return ( + +
+ {filterText(key, value, query)} + removeFilter(key, history, query)}>✕ +
+
+ ) + } + + return ( + +
+ + {filterText(key, value, query)} + + + removeFilter(key, history, query)}> + + +
+
+ ) +} + +function filterDropdownOption(site, option) { + return ( + + {({ active }) => ( + + {formatFilterGroup(option)} + + )} + + ) +} + +function DropdownContent({history, site, query, wrapped}) { + const [addingFilter, setAddingFilter] = useState(false); + + if (wrapped === 0 || addingFilter) { + return Object.keys(FILTER_GROUPS).map((option) => filterDropdownOption(site, option)) + } + + return ( + <> +
setAddingFilter(true)}> + + Add filter +
+ {appliedFilters(query).map((filter) => renderDropdownFilter(site, history, filter, query))} +
clearAllFilters(history, query)}> + Clear All Filters +
+ + ) +} class Filters extends React.Component { constructor(props) { super(props); this.state = { - dropdownOpen: false, wrapped: 1, // 0=unwrapped, 1=waiting to check, 2=wrapped - viewport: 1080 + viewport: 1080, }; - this.appliedFilters = Object.keys(props.query.filters) - .map((key) => [key, props.query.filters[key]]) - .filter(([key, value]) => !!value); - this.renderDropDown = this.renderDropDown.bind(this); - this.renderDropDownContent = this.renderDropDownContent.bind(this); - this.handleClick = this.handleClick.bind(this); this.handleResize = this.handleResize.bind(this); - this.rewrapFilters = this.rewrapFilters.bind(this); - this.renderFilterList = this.renderFilterList.bind(this); this.handleKeyup = this.handleKeyup.bind(this) } @@ -32,19 +176,16 @@ class Filters extends React.Component { window.addEventListener('resize', this.handleResize, false); document.addEventListener('keyup', this.handleKeyup); - this.rewrapFilters(); this.handleResize(); + this.rewrapFilters(); } componentDidUpdate(prevProps, prevState) { const { query } = this.props; const { viewport, wrapped } = this.state; - this.appliedFilters = Object.keys(query.filters) - .map((key) => [key, query.filters[key]]) - .filter(([key, value]) => !!value) - if (JSON.stringify(query) !== JSON.stringify(prevProps.query) || viewport !== prevState.viewport) { + // eslint-disable-next-line react/no-did-update-set-state this.setState({ wrapped: 1 }); } @@ -60,40 +201,42 @@ class Filters extends React.Component { } handleKeyup(e) { - const { query, history } = this.props + const {query, history} = this.props if (e.ctrlKey || e.metaKey || e.altKey) return if (e.key === 'Escape') { - this.clearAllFilters(history, query) + clearAllFilters(history, query) } } handleResize() { - this.setState({ viewport: window.innerWidth || 639 }); + this.setState({ viewport: window.innerWidth || 639}); } - handleClick(e) { - if (this.dropDownNode && this.dropDownNode.contains(e.target)) return; - - this.setState({ dropdownOpen: false }); - }; - // Checks if the filter container is wrapping items rewrapFilters() { - let currItem, prevItem, items = document.getElementById('filters'); - const { wrapped } = this.state; + const items = document.getElementById('filters'); + const { wrapped, viewport } = this.state; + + // Always wrap on mobile + if (appliedFilters(this.props.query).length > 0 && viewport <= 768) { + this.setState({ wrapped: 2 }) + return; + } this.setState({ wrapped: 0 }); // Don't rewrap if we're already properly wrapped, there are no DOM children, or there is only filter - if (wrapped !== 1 || !items || this.appliedFilters.length === 1) { + if (wrapped !== 1 || !items || appliedFilters(this.props.query).length === 1) { return; }; + let prevItem = null; + // For every filter DOM Node, check if its y value is higher than the previous (this indicates a wrap) [...(items.childNodes)].forEach(item => { - currItem = item.getBoundingClientRect(); + const currItem = item.getBoundingClientRect(); if (prevItem && prevItem.top < currItem.top) { this.setState({ wrapped: 2 }); } @@ -101,206 +244,106 @@ class Filters extends React.Component { }); }; - filterText(key, value, query) { - const negated = value[0] == '!' && ['page', 'entry_page', 'exit_page'].includes(key) - value = negated ? value.slice(1) : value - - if (key === "goal") { - return Completed goal {value} - } - if (key === "props") { - const [metaKey, metaValue] = Object.entries(value)[0] - const eventName = query.filters["goal"] ? query.filters["goal"] : 'event' - return {eventName}.{metaKey} is {metaValue} - } - if (key === "source") { - return Source is {value} - } - if (key === "utm_medium") { - return UTM medium is {value} - } - if (key === "utm_source") { - return UTM source is {value} - } - if (key === "utm_campaign") { - return UTM campaign is {value} - } - if (key === "referrer") { - return Referrer URL is {value} - } - if (key === "screen") { - return Screen size is {value} - } - if (key === "browser") { - return Browser is {value} - } - if (key === "browser_version") { - const browserName = query.filters["browser"] ? query.filters["browser"] : 'Browser' - return {browserName} Version is {value} - } - if (key === "os") { - return Operating System is {value} - } - if (key === "os_version") { - const osName = query.filters["os"] ? query.filters["os"] : 'OS' - return {osName} Version is {value} - } - if (key === "country") { - const allCountries = Datamap.prototype.worldTopo.objects.world.geometries; - const selectedCountry = allCountries.find((c) => c.id === value) || { properties: { name: value } }; - return Country is {selectedCountry.properties.name} - } - if (key === "page") { - return Page is{negated ? ' not' : ''} {value} - } - if (key === "entry_page") { - return Entry Page is{negated ? ' not' : ''} {value} - } - if (key === "exit_page") { - return Exit Page is{negated ? ' not' : ''} {value} - } - } - - removeFilter(key, history, query) { - const newOpts = { - [key]: false - } - if (key === 'goal') { newOpts.props = false } - navigateToQuery( - history, - query, - newOpts - ) - } - - renderDropdownFilter(history, [key, value], query) { - if ('props' == key) { - return ( -
- {this.filterText(key, value, query)} - this.removeFilter(key, history, query)}>✕ -
- ) - } - - return ( -
- - {this.filterText(key, value, query)} - - - this.removeFilter(key, history, query)}>✕ -
- ) - } - renderListFilter(history, [key, value], query) { return ( - {'props' == key ? ( + {key === 'props' ? ( - {this.filterText(key, value, query)} + {filterText(key, value, query)} ) : ( <> - - {this.filterText(key, value, query)} + + {filterText(key, value, query)} - - - )} - this.removeFilter(key, history, query)}> - + removeFilter(key, history, query)}> + ) } - clearAllFilters(history, query) { - const newOpts = Object.keys(query.filters).reduce((acc, red) => ({ ...acc, [red]: false }), {}); - navigateToQuery( - history, - query, - newOpts - ); - } - - renderDropDownContent() { - const { viewport } = this.state; - const { history, query, site } = this.props; + renderDropdownButton() { + if (this.state.wrapped === 2) { + return ( + <> +