feat(funnels): add per-step filter UI to funnel creation form

This commit is contained in:
Rudy B. committed 2026-03-16 21:51:16 +01:00
1 parent 96ae1de7fd
commit 8f0ccf84a9
2 files changed
+343 -35

No files matched your search

@@ -1,6 +1,7 @@
import {
Button,
Column,
ComboBox,
Form,
FormButtons,
FormField,
@@ -8,18 +9,319 @@ import {
FormSubmitButton,
Grid,
Icon,
ListItem,
ListSeparator,
Loading,
Row,
Select,
Text,
TextField,
useDebounce,
} from '@umami/react-zen';
import { useMessages, useReportQuery, useUpdateQuery } from '@/components/hooks';
import { endOfDay, subMonths } from 'date-fns';
import { Fragment, useState } from 'react';
import { Empty } from '@/components/common/Empty';
import { useApi, useMessages, useReportQuery, useUpdateQuery } from '@/components/hooks';
import { Plus, X } from '@/components/icons';
import { ActionSelect } from '@/components/input/ActionSelect';
import { LookupField } from '@/components/input/LookupField';
const FUNNEL_STEPS_MAX = 8;
function getEventDataDateRange() {
return {
startAt: +subMonths(endOfDay(new Date()), 6),
endAt: +endOfDay(new Date()),
};
}
function PropertySelect({
websiteId,
eventName,
value,
onChange,
onPropertyChange,
}: {
websiteId: string;
eventName?: string;
value?: string;
onChange?: (value: string) => void;
onPropertyChange?: (value: string) => void;
}) {
const { get, useQuery } = useApi();
const { formatMessage, messages } = useMessages();
const [search, setSearch] = useState(value ?? '');
const searchValue = useDebounce(search, 300);
const { startAt, endAt } = getEventDataDateRange();
const { data, isLoading } = useQuery<
Array<{ eventName: string; propertyName: string; total: number }>
>({
queryKey: ['event-data:properties', { websiteId, eventName, searchValue, startAt, endAt }],
queryFn: () =>
get(`/websites/${websiteId}/event-data/properties`, {
startAt,
endAt,
...(eventName ? { event: eventName } : {}),
...(searchValue ? { propertyName: searchValue } : {}),
}),
enabled: !!websiteId,
});
const properties = [...new Set(data?.map(d => d.propertyName) ?? [])];
return (
<ComboBox
aria-label="PropertySelect"
items={properties}
inputValue={value}
onInputChange={v => {
setSearch(v);
onChange?.(v);
onPropertyChange?.(v);
}}
formValue="text"
allowsEmptyCollection
allowsCustomValue
renderEmptyState={() =>
isLoading ? (
<Loading placement="center" icon="dots" />
) : (
<Empty message={formatMessage(messages.noResultsFound)} />
)
}
>
{properties.map(p => (
<ListItem key={p} id={p}>
{p}
</ListItem>
))}
</ComboBox>
);
}
function ValueSelect({
websiteId,
eventName,
propertyName,
value,
onChange,
}: {
websiteId: string;
eventName?: string;
propertyName?: string;
value?: string;
onChange?: (value: string) => void;
}) {
const { get, useQuery } = useApi();
const { formatMessage, messages } = useMessages();
const [search, setSearch] = useState(value ?? '');
const searchValue = useDebounce(search, 300);
const { startAt, endAt } = getEventDataDateRange();
const { data, isLoading } = useQuery<Array<{ value: string; total: number }>>({
queryKey: [
'event-data:values',
{ websiteId, eventName, propertyName, searchValue, startAt, endAt },
],
queryFn: () =>
get(`/websites/${websiteId}/event-data/values`, {
startAt,
endAt,
event: eventName,
propertyName,
}),
enabled: !!(websiteId && eventName && propertyName),
});
const values = data?.map(d => d.value) ?? [];
return (
<ComboBox
aria-label="ValueSelect"
items={values}
inputValue={value}
onInputChange={v => {
setSearch(v);
onChange?.(v);
}}
formValue="text"
allowsEmptyCollection
allowsCustomValue
renderEmptyState={() =>
isLoading ? (
<Loading placement="center" icon="dots" />
) : (
<Empty message={formatMessage(messages.noResultsFound)} />
)
}
>
{values.map(v => (
<ListItem key={v} id={v}>
{v}
</ListItem>
))}
</ComboBox>
);
}
function OperatorSelect({
value = 'eq',
onChange,
}: {
value?: string;
onChange?: (value: string) => void;
}) {
const { formatMessage, labels } = useMessages();
return (
<Select value={value} onChange={onChange}>
<ListItem id="eq">{formatMessage(labels.is)}</ListItem>
<ListItem id="neq">{formatMessage(labels.isNot)}</ListItem>
<ListItem id="c">{formatMessage(labels.contains)}</ListItem>
<ListItem id="dnc">{formatMessage(labels.doesNotContain)}</ListItem>
</Select>
);
}
function StepRow({
index,
websiteId,
initialEventName,
onRemove,
}: {
index: number;
websiteId: string;
initialEventName?: string;
onRemove: () => void;
}) {
const { formatMessage, labels } = useMessages();
const [eventName, setEventName] = useState(initialEventName ?? '');
return (
<Column gap>
<Grid columns="260px 1fr auto" gap>
<Column>
<FormField
name={`steps.${index}.type`}
rules={{ required: formatMessage(labels.required) }}
>
<ActionSelect />
</FormField>
</Column>
<Column>
<FormField
name={`steps.${index}.value`}
rules={{ required: formatMessage(labels.required) }}
>
{({ field, context }) => {
const type = context.watch(`steps.${index}.type`);
return (
<LookupField
websiteId={websiteId}
type={type}
{...field}
onValueChange={(v: string) => {
setEventName(v);
}}
/>
);
}}
</FormField>
</Column>
<Button onPress={onRemove}>
<Icon size="sm">
<X />
</Icon>
</Button>
</Grid>
<FormFieldArray name={`steps.${index}.filters`}>
{({ fields: filterFields, append: appendFilter, remove: removeFilter, watch }) => {
const stepType = watch(`steps.${index}.type`);
if (stepType !== 'event') return null;
return (
<Grid gap>
{filterFields.map(
(
{ id: filterId, property: initialProperty }: { id: string; property?: string },
filterIndex: number,
) => (
<FilterRow
key={filterId}
stepIndex={index}
filterIndex={filterIndex}
websiteId={websiteId}
eventName={eventName}
initialProperty={initialProperty}
onRemove={() => removeFilter(filterIndex)}
/>
),
)}
<Row>
<Button
variant="quiet"
onPress={() => appendFilter({ property: '', operator: 'eq', value: '' })}
>
<Icon size="sm">
<Plus />
</Icon>
<Text>{formatMessage(labels.filter)}</Text>
</Button>
</Row>
</Grid>
);
}}
</FormFieldArray>
</Column>
);
}
function FilterRow({
stepIndex,
filterIndex,
websiteId,
eventName,
initialProperty,
onRemove,
}: {
stepIndex: number;
filterIndex: number;
websiteId: string;
eventName: string;
initialProperty?: string;
onRemove: () => void;
}) {
const { formatMessage, labels } = useMessages();
const [propertyName, setPropertyName] = useState(initialProperty ?? '');
return (
<Grid columns="1fr 140px 1fr auto" gap>
<FormField
name={`steps.${stepIndex}.filters.${filterIndex}.property`}
rules={{ required: formatMessage(labels.required) }}
>
<PropertySelect
websiteId={websiteId}
eventName={eventName}
onPropertyChange={v => setPropertyName(v)}
/>
</FormField>
<FormField name={`steps.${stepIndex}.filters.${filterIndex}.operator`}>
<OperatorSelect />
</FormField>
<FormField name={`steps.${stepIndex}.filters.${filterIndex}.value`}>
<ValueSelect websiteId={websiteId} eventName={eventName} propertyName={propertyName} />
</FormField>
<Button onPress={onRemove}>
<Icon size="sm">
<X />
</Icon>
</Button>
</Grid>
);
}
export function FunnelEditForm({
id,
websiteId,
@@ -35,7 +337,13 @@ export function FunnelEditForm({
const { data } = useReportQuery(id);
const { mutateAsync, error, isPending, touch } = useUpdateQuery(`/reports${id ? `/${id}` : ''}`);
const handleSubmit = async ({ name, ...parameters }) => {
const handleSubmit = async ({
name,
...parameters
}: {
name: string;
[key: string]: unknown;
}) => {
await mutateAsync(
{ ...data, id, name, type: 'funnel', websiteId, parameters },
{
@@ -56,7 +364,10 @@ export function FunnelEditForm({
const defaultValues = {
name: data?.name || '',
window: data?.parameters?.window || 60,
steps: data?.parameters?.steps || [{ type: 'path', value: '' }],
steps: (data?.parameters?.steps || [{ type: 'path', value: '' }]).map(step => ({
...step,
filters: step.filters || [],
})),
};
return (
@@ -85,39 +396,23 @@ export function FunnelEditForm({
{({ fields, append, remove }) => {
return (
<Grid gap>
{fields.map(({ id }: { id: string }, index: number) => {
return (
<Grid key={id} columns="260px 1fr auto" gap>
<Column>
<FormField
name={`steps.${index}.type`}
rules={{ required: formatMessage(labels.required) }}
>
<ActionSelect />
</FormField>
</Column>
<Column>
<FormField
name={`steps.${index}.value`}
rules={{ required: formatMessage(labels.required) }}
>
{({ field, context }) => {
const type = context.watch(`steps.${index}.type`);
return <LookupField websiteId={websiteId} type={type} {...field} />;
}}
</FormField>
</Column>
<Button onPress={() => remove(index)}>
<Icon size="sm">
<X />
</Icon>
</Button>
</Grid>
);
})}
{fields.map(
({ id, value: initialEventName }: { id: string; value: string }, index: number) => (
<Fragment key={id}>
{index > 0 && <ListSeparator />}
<StepRow
index={index}
websiteId={websiteId}
initialEventName={initialEventName}
onRemove={() => remove(index)}
/>
</Fragment>
),
)}
<ListSeparator />
<Row>
<Button
onPress={() => append({ type: 'path', value: '' })}
onPress={() => append({ type: 'path', value: '', filters: [] })}
isDisabled={fields.length >= FUNNEL_STEPS_MAX}
>
<Icon>
+14 -1
View File
@@ -9,9 +9,21 @@ export interface LookupFieldProps extends ComboBoxProps {
type: string;
value: string;
onChange: (value: string) => void;
/**
* `FormField` injects `onChange` via `cloneElement`, overwriting any custom handler.
* Use `onValueChange` for side effects that must survive that override.
*/
onValueChange?: (value: string) => void;
}
export function LookupField({ websiteId, type, value, onChange, ...props }: LookupFieldProps) {
export function LookupField({
websiteId,
type,
value,
onChange,
onValueChange,
...props
}: LookupFieldProps) {
const { formatMessage, messages } = useMessages();
const [search, setSearch] = useState(value);
const searchValue = useDebounce(search, 300);
@@ -43,6 +55,7 @@ export function LookupField({ websiteId, type, value, onChange, ...props }: Look
onInputChange={value => {
handleSearch(value);
onChange?.(value);
onValueChange?.(value);
}}
formValue="text"
allowsEmptyCollection