From 9ed79542f2a71e333633b4ed8e651a646f587587 Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:19:07 +0300 Subject: [PATCH] List props frontend (#3126) * add the props section in behaviors * update listReport when keyLabel (=propKey) changes * make column min-width configurable and increase for props * add rendering condition to limit container height * fix filter link * fix tests * disable clear for single-option combobox * improve single-option combobox styling * fix fetchPropKeyOptions fn update on query change * BUGFIX: searching for prop_values in property filter modal * change the order of funnels and props section pickers * change props section Bar color from gray to light-red * remove disabled options from combobox dropdown (multi & single) * display percentage metric values without a % sign * change metric labels in goal filter view to Visitors and Events * fix realtime update timer --- assets/js/dashboard/components/combobox.js | 174 +++++++++++------- assets/js/dashboard/mount.js | 1 + assets/js/dashboard/stats/behaviours/index.js | 27 +-- assets/js/dashboard/stats/behaviours/props.js | 69 +++++++ .../stats/modals/prop-filter-modal.js | 6 +- assets/js/dashboard/stats/reports/list.js | 13 +- assets/js/dashboard/stats/reports/metrics.js | 4 +- .../controllers/stats_controller.ex | 2 + .../templates/stats/stats.html.eex | 1 + 9 files changed, 207 insertions(+), 90 deletions(-) create mode 100644 assets/js/dashboard/stats/behaviours/props.js diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index fc5e99bc4..95699220e 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -4,12 +4,10 @@ import { ChevronDownIcon } from '@heroicons/react/20/solid' import debounce from 'debounce-promise' import classNames from 'classnames' -function Option({isHighlighted, isDisabled, onClick, onMouseEnter, text, id}) { - const className = classNames('relative select-none py-2 px-3', { - 'cursor-pointer': !isDisabled, - 'text-gray-300 dark:text-gray-600': isDisabled, - 'text-gray-900 dark:text-gray-300': !isDisabled && !isHighlighted, - 'bg-indigo-600 text-white': !isDisabled && isHighlighted, +function Option({isHighlighted, onClick, onMouseEnter, text, id}) { + const className = classNames('relative cursor-pointer select-none py-2 px-3', { + 'text-gray-900 dark:text-gray-300': !isHighlighted, + 'bg-indigo-600 text-white': isHighlighted, }) return ( @@ -25,10 +23,10 @@ function Option({isHighlighted, isDisabled, onClick, onMouseEnter, text, id}) { } function scrollTo(wrapper, id) { if (wrapper) { - const el = wrapper.querySelector('#' + id); + const el = wrapper.querySelector('#' + id) if (el) { - el.scrollIntoView({block: 'center'}); + el.scrollIntoView({block: 'center'}) } } } @@ -40,12 +38,12 @@ function optionId(index) { export default function PlausibleCombobox(props) { const [options, setOptions] = useState([]) const [loading, setLoading] = useState(false) - const [isOpen, setOpen] = useState(false); - const [input, setInput] = useState(''); - const [highlightedIndex, setHighlightedIndex] = useState(0); - const searchRef = useRef(null); - const containerRef = useRef(null); - const listRef = useRef(null); + const [isOpen, setOpen] = useState(false) + const [input, setInput] = useState('') + const [highlightedIndex, setHighlightedIndex] = useState(0) + const searchRef = useRef(null) + const containerRef = useRef(null) + const listRef = useRef(null) const visibleOptions = [...options] if (props.freeChoice && input.length > 0 && options.every(option => option.value !== input)) { @@ -108,7 +106,7 @@ export default function PlausibleCombobox(props) { }) } - const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), []) + const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), [fetchOptions]) function onInput(e) { const newInput = e.target.value @@ -127,8 +125,6 @@ export default function PlausibleCombobox(props) { } function selectOption(option) { - if (isDisabled(option)) return - if (props.singleOption) { props.onSelect([option]) } else { @@ -144,22 +140,20 @@ export default function PlausibleCombobox(props) { e.stopPropagation() const newValues = props.values.filter((val) => val.value !== option.value) props.onSelect(newValues) - if (!searchBoxHidden) { - searchRef.current.focus() - } + searchRef.current.focus() setOpen(false) } const handleClick = useCallback((e) => { - if (containerRef.current && containerRef.current.contains(e.target)) return; + if (containerRef.current && containerRef.current.contains(e.target)) { return } setInput('') setOpen(false) }) useEffect(() => { - document.addEventListener("mousedown", handleClick, false); - return () => { document.removeEventListener("mousedown", handleClick, false); } + document.addEventListener("mousedown", handleClick, false) + return () => { document.removeEventListener("mousedown", handleClick, false) } }, []) useEffect(() => { @@ -167,23 +161,47 @@ export default function PlausibleCombobox(props) { searchRef.current.focus() } }, [props.values.length === 0]) - - const matchesFound = !loading && visibleOptions.length > 0 - const noMatchesFound = !loading && visibleOptions.length === 0 - const searchBoxHidden = !!props.singleOption && props.values.length === 1 - const searchBoxClass = classNames('border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm', { - 'hidden': searchBoxHidden - }) + const searchBoxClass = 'border-none py-1 px-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm' const containerClass = classNames('relative w-full', { [props.className]: !!props.className, 'opacity-20 cursor-default pointer-events-none': props.isDisabled }) - return ( -
-
+ function renderSingleOptionContent() { + const itemSelected = props.values.length === 1 + const placeholder = itemSelected ? '' : props.placeholder + + return ( +
+ { itemSelected && renderSingleSelectedItem() } + + +
+ ) + } + + function renderSingleSelectedItem() { + if (input === '') { + return ( + + {props.values[0].label} + + ) + } + } + + function renderMultiOptionContent() { + return ( + <> { props.values.map((value) => { return (
@@ -194,12 +212,63 @@ export default function PlausibleCombobox(props) { }) } + + ) + } + + function renderDropDownContent() { + const matchesFound = visibleOptions.length > 0 && visibleOptions.some(option => !isDisabled(option)) + + if (loading) { + return
Loading options...
+ } + + if (matchesFound) { + return visibleOptions + .filter(option => !isDisabled(option)) + .map((option, i) => { + const text = option.freeChoice ? `Filter by '${option.label}'` : option.label + + return ( +