From caeff41932a47dc0e9ec124e12dd8229e8201bcf Mon Sep 17 00:00:00 2001 From: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:09:33 +0300 Subject: [PATCH] Prop filter modal (#2841) * Add PropFilterModal (only UI) * small variable refactor * allow selecting prop value filter type * allow selecting only one prop_key * allow selecting many prop_values only when prop_key selected * handle submitting filter * get applied filters from query + remove option * change prop filter label format * support member and not_member filter types for pageview props * show (none) value in filter suggestions * refactor zip_results/4 and remove unused code * fix displaying (none) values in goals section prop breakdown * remove unnecessary functionality * fix bug: returning prop names for goal :member filter * fix bug: submitting regular filter modal with Enter key * bugfix: disallow opening prop filter modal when feature flag disabled * mix format * break selected combobox values into multiple lines * fix useEffect behavior for focusing on prop_key field * support submitting prop filter with Enter key * refactor getFormState in PropFilterModal * separate fetchPropKey and fetchPropValue functions * Allow querying props for pageview goals * Make the internal props API only return a list of props (not map) * Separate function for fetching all props in Stats API goal breakdown (this returns a map as before) * ditch state for keeping search bar visible * group by event_name in db query --- assets/js/dashboard/components/combobox.js | 38 +- .../components/filter-type-selector.js | 70 ++- assets/js/dashboard/filters.js | 48 +- .../js/dashboard/stats/modals/filter-modal.js | 8 +- .../stats/modals/prop-filter-modal.js | 170 ++++++ .../stats/modals/regular-filter-modal.js | 50 +- assets/js/dashboard/util/filters.js | 4 +- lib/plausible/stats/base.ex | 28 + lib/plausible/stats/breakdown.ex | 45 +- lib/plausible/stats/custom_props.ex | 51 ++ lib/plausible/stats/filter_suggestions.ex | 32 +- lib/plausible/stats/mod.ex | 1 - lib/plausible/stats/props.ex | 34 -- .../api/external_stats_controller.ex | 4 +- .../controllers/api/stats_controller.ex | 21 +- .../breakdown_test.exs | 136 +++-- .../api/stats_controller/conversions_test.exs | 555 +++++++++++++++++- .../api/stats_controller/pages_test.exs | 88 +++ .../api/stats_controller/suggestions_test.exs | 93 ++- 19 files changed, 1180 insertions(+), 296 deletions(-) create mode 100644 assets/js/dashboard/stats/modals/prop-filter-modal.js create mode 100644 lib/plausible/stats/custom_props.ex diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 7a727d422..fc5e99bc4 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -129,17 +129,24 @@ export default function PlausibleCombobox(props) { function selectOption(option) { if (isDisabled(option)) return - props.onSelect([...props.values, option]) + if (props.singleOption) { + props.onSelect([option]) + } else { + searchRef.current.focus() + props.onSelect([...props.values, option]) + } + setOpen(false) setInput('') - searchRef.current.focus() } function removeOption(option, e) { e.stopPropagation() const newValues = props.values.filter((val) => val.value !== option.value) props.onSelect(newValues) - searchRef.current.focus() + if (!searchBoxHidden) { + searchRef.current.focus() + } setOpen(false) } @@ -155,19 +162,38 @@ export default function PlausibleCombobox(props) { return () => { document.removeEventListener("mousedown", handleClick, false); } }, []) + useEffect(() => { + if (props.singleOption && props.values.length === 0) { + 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 containerClass = classNames('relative w-full', { + [props.className]: !!props.className, + 'opacity-20 cursor-default pointer-events-none': props.isDisabled + }) return ( -