analytics/assets/js/dashboard/stats/modals/modal.js
Uku Taht 46048e50f7
Support multiple filters - frontend (#2773)
* Wrap Plausible.Stats.Filters with unit tests

* Parse `member` filter type

* Support for `member` filter in `aggregate_time_on_page`

* Support `not_member` filter type

* Support `matches_member` and `not_matches_member` filters

* Extract util module for React filters

* Implement Combobox from scratch with no libs

* Support multple filter clauses in combobox

* Don't use browser / os in version label

* Show highlighted option in combobox

* WIP

* Fix location filters outside filter modal

* Align open/close behaviour with react-select

* Styling updates for combobox

* Add support for wildcards in Combobox

* Implement keybindings for combobox

* Allow free choice inputs in combobox

* Rename 'Save filter' -> Apply filter

* Remove TODO comment

* Clean up some rebase mistakes

* Rename `allowWildcard` -> `freeChoice`

* Dark mode fixes

* Remove hint from filter modal

* Escape pipe character in filter modal

* Do not allow selecting duplicate options in combobox

* Escape brackets in `page_regex/1`

* Fix disabled style in dark mode

* Add regex fallback for safari

* Show no matches found when visibleOptions is empty

* Disable enter key when no visible options

* Do not submit empty form fields

* Remove unnecessary setOpen(true)
2023-03-27 16:51:31 +03:00

104 lines
3.0 KiB
JavaScript

import React from "react";
import { createPortal } from "react-dom";
import { withRouter } from 'react-router-dom';
import { shouldIgnoreKeypress } from '../../keybinding'
// This corresponds to the 'md' breakpoint on TailwindCSS.
const MD_WIDTH = 768;
// We assume that the dashboard is by default opened on a desktop. This is also a fall-back for when, for any reason, the width is not ascertained.
const DEFAULT_WIDTH = 1080;
class Modal extends React.Component {
constructor(props) {
super(props)
this.state = {
viewport: DEFAULT_WIDTH,
}
this.node = React.createRef()
this.handleClickOutside = this.handleClickOutside.bind(this)
this.handleKeyup = this.handleKeyup.bind(this)
this.handleResize = this.handleResize.bind(this)
}
componentDidMount() {
document.body.style.overflow = 'hidden';
document.body.style.height = '100vh';
document.addEventListener("mousedown", this.handleClickOutside);
document.addEventListener("keyup", this.handleKeyup);
window.addEventListener('resize', this.handleResize, false);
this.handleResize();
}
componentWillUnmount() {
document.body.style.overflow = null;
document.body.style.height = null;
document.removeEventListener("mousedown", this.handleClickOutside);
document.removeEventListener("keyup", this.handleKeyup);
window.removeEventListener('resize', this.handleResize, false);
}
handleClickOutside(e) {
if (this.node.current.contains(e.target)) {
return;
}
this.close()
}
handleKeyup(e) {
if (!shouldIgnoreKeypress(e) && e.code === 'Escape') {
this.close()
}
}
handleResize() {
this.setState({ viewport: window.innerWidth });
}
close() {
this.props.history.push(`/${encodeURIComponent(this.props.site.domain)}${this.props.location.search}`)
}
/**
* @description
* Decide whether to set max-width, and if so, to what.
* If no max-width is available, set width instead to min-content such that we can rely on widths set on th.
* On >md, we use the same behaviour as before: set width to 800 pixels.
* Note that When a max-width comes from the parent component, we rely on that *always*.
*/
getStyle() {
const { maxWidth } = this.props;
const { viewport } = this.state;
const styleObject = {};
if (maxWidth) {
styleObject.maxWidth = maxWidth;
} else {
styleObject.width = viewport <= MD_WIDTH ? "min-content" : "860px";
}
return styleObject;
}
render() {
return createPortal(
<div className="modal is-open" onClick={this.props.onClick}>
<div className="modal__overlay">
<button className="modal__close"></button>
<div
ref={this.node}
className="modal__container dark:bg-gray-800"
style={this.getStyle()}
>
{this.props.children}
</div>
</div>
</div>,
document.getElementById("modal_root"),
);
}
}
export default withRouter(Modal)