Compare commits
33 Commits
master
...
support-mu
| Author | SHA1 | Date |
|---|---|---|
|
|
45fe06082d | 4 years ago |
|
|
2f9108b77e | 4 years ago |
|
|
0e9b1b14ae | 4 years ago |
|
|
819a76dc35 | 4 years ago |
|
|
ff511787e6 | 4 years ago |
|
|
fde4ce8ce8 | 4 years ago |
|
|
93ac9d4a3f | 4 years ago |
|
|
3beca900a6 | 4 years ago |
|
|
f4d8ef9081 | 4 years ago |
|
|
fda03e3822 | 4 years ago |
|
|
c50793b4bd | 4 years ago |
|
|
2bad1ea32b | 4 years ago |
|
|
b688e29211 | 4 years ago |
|
|
6642a4c14e | 4 years ago |
|
|
a725be0371 | 4 years ago |
|
|
9095a468c0 | 4 years ago |
|
|
6d5fc3998c | 4 years ago |
|
|
4ed62cb0e3 | 4 years ago |
|
|
2e0852cd03 | 4 years ago |
|
|
669a3559f8 | 4 years ago |
|
|
4cac26f634 | 4 years ago |
|
|
338fcdece1 | 4 years ago |
|
|
12e7ac869e | 4 years ago |
|
|
fdd1ab65d3 | 4 years ago |
|
|
219f662fe2 | 4 years ago |
|
|
df95b61eca | 4 years ago |
|
|
181de8225e | 4 years ago |
|
|
89c3e38197 | 4 years ago |
|
|
4fc211f993 | 4 years ago |
|
|
9701904523 | 4 years ago |
|
|
4b93cba06d | 4 years ago |
|
|
0559d96c05 | 4 years ago |
|
|
255d20bee7 | 4 years ago |
@ -1,106 +1,224 @@
|
||||
import React, { Fragment, useState, useCallback } from 'react'
|
||||
import { Combobox, Transition } from '@headlessui/react'
|
||||
import React, { Fragment, useState, useCallback, useEffect, useRef } from 'react'
|
||||
import { Transition } from '@headlessui/react'
|
||||
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 Spinner() {
|
||||
return (
|
||||
<svg className="animate-spin h-4 w-4 text-indigo-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
<li
|
||||
className={className}
|
||||
id={id}
|
||||
onClick={onClick}
|
||||
onMouseEnter={onMouseEnter}
|
||||
>
|
||||
<span className="block truncate">{text}</span>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
function scrollTo(wrapper, id) {
|
||||
if (wrapper) {
|
||||
const el = wrapper.querySelector('#' + id);
|
||||
|
||||
if (el) {
|
||||
el.scrollIntoView({block: 'center'});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function optionId(index) {
|
||||
return `plausible-combobox-option-${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 visibleOptions = [...options]
|
||||
if (props.freeChoice && input.length > 0 && options.every(option => option.value !== input)) {
|
||||
visibleOptions.push({value: input, label: input, freeChoice: true})
|
||||
}
|
||||
|
||||
function highLight(index) {
|
||||
let newIndex = index
|
||||
|
||||
if (index < 0) {
|
||||
newIndex = visibleOptions.length - 1
|
||||
} else if (index >= visibleOptions.length) {
|
||||
newIndex = 0
|
||||
}
|
||||
|
||||
setHighlightedIndex(newIndex)
|
||||
scrollTo(listRef.current, optionId(newIndex))
|
||||
}
|
||||
|
||||
function onKeyDown(e) {
|
||||
if (e.key === 'Enter') {
|
||||
if (!isOpen || loading || visibleOptions.length === 0) return null
|
||||
selectOption(visibleOptions[highlightedIndex])
|
||||
e.preventDefault()
|
||||
}
|
||||
if (e.key === 'Escape') {
|
||||
if (!isOpen || loading) return null
|
||||
setOpen(false)
|
||||
searchRef.current?.focus()
|
||||
e.preventDefault()
|
||||
}
|
||||
if (e.key === 'ArrowDown') {
|
||||
if (isOpen) {
|
||||
highLight(highlightedIndex + 1)
|
||||
} else {
|
||||
setOpen(true)
|
||||
}
|
||||
}
|
||||
if (e.key === 'ArrowUp') {
|
||||
if (isOpen) {
|
||||
highLight(highlightedIndex - 1)
|
||||
} else {
|
||||
setOpen(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isDisabled(option) {
|
||||
return props.values.some((val) => val.value === option.value)
|
||||
}
|
||||
|
||||
function fetchOptions(query) {
|
||||
setLoading(true)
|
||||
|
||||
return props.fetchOptions(query).then((loadedOptions) => {
|
||||
setLoading(false)
|
||||
setHighlightedIndex(0)
|
||||
setOptions(loadedOptions)
|
||||
})
|
||||
}
|
||||
|
||||
const debouncedFetchOptions = useCallback(debounce(fetchOptions, 200), [])
|
||||
|
||||
function onOpen() {
|
||||
setOptions([])
|
||||
fetchOptions(props.selection.label)
|
||||
function onInput(e) {
|
||||
const newInput = e.target.value
|
||||
setInput(newInput)
|
||||
debouncedFetchOptions(newInput)
|
||||
}
|
||||
|
||||
function onBlur(e) {
|
||||
!props.strict && props.onChange({
|
||||
value: e.target.value,
|
||||
label: e.target.value
|
||||
})
|
||||
function toggleOpen() {
|
||||
if (!isOpen) {
|
||||
fetchOptions(input)
|
||||
searchRef.current.focus()
|
||||
setOpen(true)
|
||||
} else {
|
||||
setInput('')
|
||||
setOpen(false)
|
||||
}
|
||||
}
|
||||
|
||||
function selectOption(option) {
|
||||
if (isDisabled(option)) return
|
||||
|
||||
function renderOptions() {
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300">
|
||||
Loading options...
|
||||
</div>
|
||||
)
|
||||
} else if (!loading && options.length === 0) {
|
||||
return (
|
||||
<div className="relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300">
|
||||
No matches found in the current dashboard. Try selecting a different time range or searching for something different
|
||||
</div>
|
||||
)
|
||||
props.onChange([...props.values, option])
|
||||
setOpen(false)
|
||||
setInput('')
|
||||
searchRef.current.focus()
|
||||
}
|
||||
|
||||
} else {
|
||||
return options.map((option) => {
|
||||
return (
|
||||
<Combobox.Option
|
||||
key={option.value}
|
||||
className={({ active }) =>
|
||||
`relative cursor-default select-none py-2 px-3 ${active ? 'bg-indigo-600 text-white' : 'text-gray-900 dark:text-gray-300'
|
||||
}`
|
||||
}
|
||||
value={option}
|
||||
>
|
||||
<span className="block truncate">{option.label}</span>
|
||||
</Combobox.Option>
|
||||
)
|
||||
})
|
||||
}
|
||||
function removeOption(option, e) {
|
||||
e.stopPropagation()
|
||||
const newValues = props.values.filter((val) => val.value !== option.value)
|
||||
props.onChange(newValues)
|
||||
searchRef.current.focus()
|
||||
setOpen(false)
|
||||
}
|
||||
|
||||
const handleClick = useCallback((e) => {
|
||||
if (containerRef.current && containerRef.current.contains(e.target)) return;
|
||||
|
||||
setInput('')
|
||||
setOpen(false)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener("mousedown", handleClick, false);
|
||||
return () => { document.removeEventListener("mousedown", handleClick, false); }
|
||||
}, [])
|
||||
|
||||
const matchesFound = !loading && visibleOptions.length > 0
|
||||
const noMatchesFound = !loading && visibleOptions.length === 0
|
||||
|
||||
return (
|
||||
<Combobox value={props.selection} onChange={(val) => props.onChange(val)}>
|
||||
<div className="relative ml-2 w-full">
|
||||
<Combobox.Button as="div" className="relative dark:bg-gray-900 dark:text-gray-300 block rounded-md shadow-sm border border-gray-300 dark:border-gray-700 hover:border-gray-400 dark:hover:border-gray-200 focus-within:ring-indigo-500 focus-within:border-indigo-500 ">
|
||||
<Combobox.Input
|
||||
className="border-none rounded-md focus:outline-none focus:ring-0 pr-10 text-sm"
|
||||
style={{backgroundColor: 'inherit'}}
|
||||
placeholder={props.placeholder}
|
||||
displayValue={(item) => item && item.label}
|
||||
onChange={(event) => debouncedFetchOptions(event.target.value)}
|
||||
onBlur={onBlur}
|
||||
/>
|
||||
<div className="absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
{!loading && <ChevronDownIcon className="h-4 w-4 text-gray-500" />}
|
||||
{loading && <Spinner />}
|
||||
</div>
|
||||
</Combobox.Button>
|
||||
<Transition
|
||||
as={Fragment}
|
||||
leave="transition ease-in duration-100"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
beforeEnter={onOpen}
|
||||
>
|
||||
<Combobox.Options className="z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900">
|
||||
{renderOptions()}
|
||||
</Combobox.Options>
|
||||
</Transition>
|
||||
<div onKeyDown={onKeyDown} ref={containerRef} className="relative ml-2 w-full">
|
||||
<div onClick={toggleOpen} className={classNames('pl-2 pr-8 py-1 w-full dark:bg-gray-900 dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-700 focus-within:border-indigo-500 focus-within:ring-1 focus-within:ring-indigo-500', {'border-indigo-500 ring-1 ring-indigo-500': isOpen, '': !isOpen})}>
|
||||
{ props.values.map((value) => {
|
||||
return (
|
||||
<div key={value.value} className="bg-indigo-100 dark:bg-indigo-600 flex justify-between w-full rounded-sm px-2 py-0.5 m-0.5 text-sm">{value.label} <span onClick={(e) => removeOption(value, e)} className="cursor-pointer font-bold ml-1">×</span></div>
|
||||
)
|
||||
})
|
||||
}
|
||||
<input className="border-none py-1 px-1 p-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm" ref={searchRef} value={input} style={{backgroundColor: "inherit"}} placeholder={props.placeholder} type="text" onChange={onInput}></input>
|
||||
<div className="cursor-pointer absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
{!loading && <ChevronDownIcon className="h-4 w-4 text-gray-500" />}
|
||||
{loading && <Spinner />}
|
||||
</div>
|
||||
</div>
|
||||
</Combobox>
|
||||
<Transition
|
||||
as={Fragment}
|
||||
leave="transition ease-in duration-100"
|
||||
leaveFrom="opacity-100"
|
||||
leaveTo="opacity-0"
|
||||
show={isOpen}
|
||||
>
|
||||
<ul ref={listRef} className="z-50 absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm dark:bg-gray-900">
|
||||
{ loading && (
|
||||
<div className="relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300">
|
||||
Loading options...
|
||||
</div>
|
||||
)}
|
||||
{ noMatchesFound && (
|
||||
<div className="relative cursor-default select-none py-2 px-4 text-gray-700 dark:text-gray-300">
|
||||
No matches found in the current dashboard. Try selecting a different time range or searching for something different
|
||||
</div>
|
||||
)}
|
||||
{ matchesFound && (
|
||||
visibleOptions.map((option, i) => {
|
||||
const text = option.freeChoice ? `Filter by '${option.label}'` : option.label
|
||||
|
||||
return (
|
||||
<Option
|
||||
key={option.value}
|
||||
id={optionId(i)}
|
||||
isHighlighted={highlightedIndex === i}
|
||||
isDisabled={isDisabled(option)}
|
||||
onClick={() => selectOption(option)}
|
||||
onMouseEnter={() => setHighlightedIndex(i)}
|
||||
text={text}
|
||||
/>
|
||||
)
|
||||
})
|
||||
)}
|
||||
</ul>
|
||||
</Transition>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Spinner() {
|
||||
return (
|
||||
<svg className="animate-spin h-4 w-4 text-indigo-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
@ -0,0 +1,146 @@
|
||||
export const FILTER_GROUPS = {
|
||||
'page': ['page', 'entry_page', 'exit_page'],
|
||||
'source': ['source', 'referrer'],
|
||||
'location': ['country', 'region', 'city'],
|
||||
'screen': ['screen'],
|
||||
'browser': ['browser', 'browser_version'],
|
||||
'os': ['os', 'os_version'],
|
||||
'utm': ['utm_medium', 'utm_source', 'utm_campaign', 'utm_term', 'utm_content'],
|
||||
'goal': ['goal'],
|
||||
'props': ['prop_key', 'prop_value']
|
||||
}
|
||||
|
||||
export const FILTER_TYPES = {
|
||||
isNot: 'is not',
|
||||
contains: 'contains',
|
||||
is: 'is'
|
||||
};
|
||||
|
||||
export const FILTER_PREFIXES = {
|
||||
[FILTER_TYPES.isNot]: '!',
|
||||
[FILTER_TYPES.contains]: '~',
|
||||
[FILTER_TYPES.is]: ''
|
||||
};
|
||||
|
||||
// As of March 2023, Safari does not support negative lookbehind regexes. In case it throws an error, falls back to plain | matching. This means
|
||||
// escaping pipe characters in filters does not currently work in Safari
|
||||
let NON_ESCAPED_PIPE_REGEX;
|
||||
try {
|
||||
NON_ESCAPED_PIPE_REGEX = new RegExp("(?<!\\\\)\\|", "g")
|
||||
} catch(_e) {
|
||||
NON_ESCAPED_PIPE_REGEX = '|'
|
||||
}
|
||||
|
||||
console.log(NON_ESCAPED_PIPE_REGEX)
|
||||
|
||||
const ESCAPED_PIPE = '\\|'
|
||||
|
||||
function escapeFilterValue(value) {
|
||||
return value.replaceAll(NON_ESCAPED_PIPE_REGEX, ESCAPED_PIPE)
|
||||
}
|
||||
|
||||
export function toFilterQuery(type, clauses) {
|
||||
const prefix = FILTER_PREFIXES[type];
|
||||
const result = clauses.map(clause => escapeFilterValue(clause.value.trim())).join('|')
|
||||
return prefix + result;
|
||||
}
|
||||
|
||||
function parsePrefix(rawValue) {
|
||||
const type = Object.keys(FILTER_PREFIXES)
|
||||
.find(type => FILTER_PREFIXES[type] === rawValue[0]) || FILTER_TYPES.is;
|
||||
|
||||
const value = [FILTER_TYPES.isNot, FILTER_TYPES.contains].includes(type)
|
||||
? rawValue.substring(1)
|
||||
: rawValue;
|
||||
|
||||
const values = value
|
||||
.split(NON_ESCAPED_PIPE_REGEX)
|
||||
.filter((clause) => !!clause)
|
||||
.map((val) => val.replaceAll(ESCAPED_PIPE, '|'))
|
||||
|
||||
return {type, values}
|
||||
}
|
||||
|
||||
export function parseQueryFilter(query, filter) {
|
||||
if (filter === 'props') {
|
||||
const rawValue = query.filters['props']
|
||||
const [[_propKey, propVal]] = Object.entries(rawValue)
|
||||
const {type, values} = parsePrefix(propVal)
|
||||
const clauses = values.map(val => { return {value: val, label: val}})
|
||||
return {type, clauses}
|
||||
} else {
|
||||
const {type, values} = parsePrefix(query.filters[filter] || '')
|
||||
|
||||
let labels = values
|
||||
|
||||
if (filter === 'country' && values.length > 0) {
|
||||
const rawLabel = (new URLSearchParams(window.location.search)).get('country_labels') || ''
|
||||
labels = rawLabel.split('|').filter(label => !!label)
|
||||
}
|
||||
|
||||
if (filter === 'region' && values.length > 0) {
|
||||
const rawLabel = (new URLSearchParams(window.location.search)).get('region_labels') || ''
|
||||
labels = rawLabel.split('|').filter(label => !!label)
|
||||
}
|
||||
|
||||
if (filter === 'city' && values.length > 0) {
|
||||
const rawLabel = (new URLSearchParams(window.location.search)).get('city_labels') || ''
|
||||
labels = rawLabel.split('|').filter(label => !!label)
|
||||
}
|
||||
|
||||
const clauses = values.map((value, index) => { return {value, label: labels[index]}})
|
||||
|
||||
return {type, clauses}
|
||||
}
|
||||
}
|
||||
|
||||
export function formatFilterGroup(filterGroup) {
|
||||
if (filterGroup === 'utm') {
|
||||
return 'UTM tags'
|
||||
} else if (filterGroup === 'location') {
|
||||
return 'Location'
|
||||
} else if (filterGroup === 'props') {
|
||||
return 'Property'
|
||||
} else {
|
||||
return formattedFilters[filterGroup]
|
||||
}
|
||||
}
|
||||
|
||||
export function filterGroupForFilter(filter) {
|
||||
const map = Object.entries(FILTER_GROUPS).reduce((filterToGroupMap, [group, filtersInGroup]) => {
|
||||
const filtersToAdd = {}
|
||||
filtersInGroup.forEach((filterInGroup) => {
|
||||
filtersToAdd[filterInGroup] = group
|
||||
})
|
||||
|
||||
return { ...filterToGroupMap, ...filtersToAdd }
|
||||
}, {})
|
||||
|
||||
|
||||
return map[filter] || filter
|
||||
}
|
||||
|
||||
export const formattedFilters = {
|
||||
'goal': 'Goal',
|
||||
'props': 'Property',
|
||||
'prop_key': 'Property',
|
||||
'prop_value': 'Value',
|
||||
'source': 'Source',
|
||||
'utm_medium': 'UTM Medium',
|
||||
'utm_source': 'UTM Source',
|
||||
'utm_campaign': 'UTM Campaign',
|
||||
'utm_content': 'UTM Content',
|
||||
'utm_term': 'UTM Term',
|
||||
'referrer': 'Referrer URL',
|
||||
'screen': 'Screen size',
|
||||
'browser': 'Browser',
|
||||
'browser_version': 'Browser Version',
|
||||
'os': 'Operating System',
|
||||
'os_version': 'Operating System Version',
|
||||
'country': 'Country',
|
||||
'region': 'Region',
|
||||
'city': 'City',
|
||||
'page': 'Page',
|
||||
'entry_page': 'Entry Page',
|
||||
'exit_page': 'Exit Page'
|
||||
}
|
||||
Loading…
Reference in New Issue