@ -6,13 +6,13 @@ import * as url from '../../util/url'
import { CR _METRIC , PERCENTAGE _METRIC } from "../reports/metrics" ;
import * as storage from "../../util/storage" ;
import { parsePrefix , escapeFilterValue } from "../../util/filters"
import { ApiErrorNotice } from '../../api'
export default function Properties ( props ) {
const { site , query } = props
const propKeyStorageName = ` prop_key__ ${ site . domain } `
const propKeyStorageNameForGoal = ` ${ query . filters . goal } __prop_key__ ${ site . domain } `
const [ error , setError ] = useState ( undefined )
const [ propKey , setPropKey ] = useState ( choosePropKey ( ) )
useEffect ( ( ) => {
@ -48,11 +48,17 @@ export default function Properties(props) {
function fetchProps ( ) {
return api . get ( url . apiPath ( site , ` /custom-prop-values/ ${ encodeURIComponent ( propKey ) } ` ) , query )
. catch ( ( err ) => {
setError ( err )
} )
}
const fetchPropKeyOptions = useCallback ( ( ) => {
return ( input ) => {
return api . get ( url . apiPath ( site , "/suggestions/prop_key" ) , query , { q : input . trim ( ) } )
. catch ( ( err ) => {
setError ( err )
} )
}
} , [ query ] )
@ -71,37 +77,48 @@ export default function Properties(props) {
/*global BUILD_EXTRA*/
function renderBreakdown ( ) {
return (
< ListReport
fetchData = { fetchProps }
getFilterFor = { getFilterFor }
keyLabel = { propKey }
metrics = { [
{ name : 'visitors' , label : 'Visitors' , plot : true } ,
{ name : 'events' , label : 'Events' , hiddenOnMobile : true } ,
query . filters . goal ? CR _METRIC : PERCENTAGE _METRIC ,
BUILD _EXTRA && { name : 'total_revenue' , label : 'Revenue' , hiddenOnMobile : true } ,
BUILD _EXTRA && { name : 'average_revenue' , label : 'Average' , hiddenOnMobile : true }
] }
detailsLink = { url . sitePath ( site , ` /custom-prop-values/ ${ propKey } ` ) }
maybeHideDetails = { true }
query = { query }
color = "bg-red-50"
colMinWidth = { 90 }
/ >
)
if ( error ) {
return (
< ApiErrorNotice error = { error } / >
)
} else {
return (
< ListReport
fetchData = { fetchProps }
getFilterFor = { getFilterFor }
keyLabel = { propKey }
metrics = { [
{ name : 'visitors' , label : 'Visitors' , plot : true } ,
{ name : 'events' , label : 'Events' , hiddenOnMobile : true } ,
query . filters . goal ? CR _METRIC : PERCENTAGE _METRIC ,
BUILD _EXTRA && { name : 'total_revenue' , label : 'Revenue' , hiddenOnMobile : true } ,
BUILD _EXTRA && { name : 'average_revenue' , label : 'Average' , hiddenOnMobile : true }
] }
detailsLink = { url . sitePath ( site , ` /custom-prop-values/ ${ propKey } ` ) }
maybeHideDetails = { true }
query = { query }
color = "bg-red-50"
colMinWidth = { 90 }
/ >
)
}
}
const getFilterFor = ( listItem ) => { return { 'props' : JSON . stringify ( { [ propKey ] : escapeFilterValue ( listItem . name ) } ) } }
const comboboxValues = propKey ? [ { value : propKey , label : propKey } ] : [ ]
const boxClass = 'pl-2 pr-8 py-1 bg-transparent dark:text-gray-300 rounded-md shadow-sm border border-gray-300 dark:border-gray-500'
return (
< div className = "w-full mt-4" >
< div >
< Combobox isDisabled = { ! ! query . filters . props } boxClass = { boxClass } fetchOptions = { fetchPropKeyOptions ( ) } singleOption = { true } values = { comboboxValues } onSelect = { onPropKeySelect ( ) } placeholder = { 'Select a property' } / >
if ( error ) {
return ( < ApiErrorNotice error = { error } / > )
} else {
return (
< div className = "w-full mt-4" >
< div >
< Combobox isDisabled = { ! ! query . filters . props } boxClass = { boxClass } fetchOptions = { fetchPropKeyOptions ( ) } singleOption = { true } values = { comboboxValues } onSelect = { onPropKeySelect ( ) } placeholder = { 'Select a property' } / >
< / d i v >
{ propKey && renderBreakdown ( ) }
< / d i v >
{ propKey && renderBreakdown ( ) }
< / d i v >
)
)
}
}