From e69496933a5202c608f1d88e686e000f6cc8300d Mon Sep 17 00:00:00 2001 From: Francis Cao Date: Tue, 15 Sep 2026 13:48:55 -0700 Subject: [PATCH] Support session property filters on segments. Closes #4507 --- src/app/(main)/TopNav.tsx | 6 ++ src/app/(main)/links/LinkEditForm.tsx | 2 +- src/app/(main)/pixels/PixelEditForm.tsx | 2 +- .../preferences/PreferenceSettings.tsx | 10 +-- .../(reports)/goals/GoalEditForm.tsx | 2 +- .../[websiteId]/cohorts/CohortEditForm.tsx | 14 ++-- .../[websiteId]/segments/SegmentEditForm.tsx | 73 ++++++++++++------- .../[websiteId]/settings/ShareEditForm.tsx | 2 +- .../[websiteId]/segments/[segmentId]/route.ts | 10 ++- .../websites/[websiteId]/segments/route.ts | 6 +- src/components/common/FilterRecord.tsx | 2 +- src/components/input/FilterBar.tsx | 12 ++- src/components/input/PreferencesButton.tsx | 4 +- .../property-data/PropertyFilterRecord.tsx | 2 +- src/components/share/SimpleShareEditForm.tsx | 2 +- src/lib/request.test.ts | 48 ++++++++++++ src/lib/request.ts | 1 + src/lib/schema.ts | 16 +++- 18 files changed, 161 insertions(+), 53 deletions(-) create mode 100644 src/lib/request.test.ts diff --git a/src/app/(main)/TopNav.tsx b/src/app/(main)/TopNav.tsx index 2b41e144a..0d3e79bed 100644 --- a/src/app/(main)/TopNav.tsx +++ b/src/app/(main)/TopNav.tsx @@ -71,6 +71,8 @@ export function TopNav() { teamId={teamId} onChange={handleLinkChange} buttonProps={{ + className: + 'border-transparent bg-transparent shadow-none hover:border-transparent hover:bg-interactive active:bg-interactive-hover', style: { minHeight: 40, minWidth: 200, maxWidth: 200 }, }} /> @@ -81,6 +83,8 @@ export function TopNav() { teamId={teamId} onChange={handlePixelChange} buttonProps={{ + className: + 'border-transparent bg-transparent shadow-none hover:border-transparent hover:bg-interactive active:bg-interactive-hover', style: { minHeight: 40, minWidth: 200, maxWidth: 200 }, }} /> @@ -91,6 +95,8 @@ export function TopNav() { teamId={teamId} onChange={handleBoardChange} buttonProps={{ + className: + 'border-transparent bg-transparent shadow-none hover:border-transparent hover:bg-interactive active:bg-interactive-hover', style: { minHeight: 40, minWidth: 200, maxWidth: 200 }, }} /> diff --git a/src/app/(main)/links/LinkEditForm.tsx b/src/app/(main)/links/LinkEditForm.tsx index 8b8f476be..05235ee21 100644 --- a/src/app/(main)/links/LinkEditForm.tsx +++ b/src/app/(main)/links/LinkEditForm.tsx @@ -125,7 +125,7 @@ export function LinkEditForm({ )} - + - + - + - + - + - + - + diff --git a/src/app/(main)/websites/[websiteId]/(reports)/goals/GoalEditForm.tsx b/src/app/(main)/websites/[websiteId]/(reports)/goals/GoalEditForm.tsx index 808057411..9f3ccd92c 100644 --- a/src/app/(main)/websites/[websiteId]/(reports)/goals/GoalEditForm.tsx +++ b/src/app/(main)/websites/[websiteId]/(reports)/goals/GoalEditForm.tsx @@ -64,7 +64,7 @@ export function GoalEditForm({ - + {isMobile ? ( diff --git a/src/app/(main)/websites/[websiteId]/cohorts/CohortEditForm.tsx b/src/app/(main)/websites/[websiteId]/cohorts/CohortEditForm.tsx index 8ad13549d..9b77f0067 100644 --- a/src/app/(main)/websites/[websiteId]/cohorts/CohortEditForm.tsx +++ b/src/app/(main)/websites/[websiteId]/cohorts/CohortEditForm.tsx @@ -8,6 +8,7 @@ import { Grid, Label, Loading, + Text, TextField, } from '@umami/react-zen'; import { useEffect, useState } from 'react'; @@ -71,6 +72,7 @@ export function CohortEditForm({ } const defaultValues = { + name: '', parameters: { filters, dateRange: '30day', action: { type: 'path', value: '' } }, }; @@ -84,12 +86,12 @@ export function CohortEditForm({ const type = watch('parameters.action.type'); return ( - <> + - + @@ -110,15 +112,15 @@ export function CohortEditForm({ - + - - + + {t(labels.filters)} - + ); }} diff --git a/src/app/(main)/websites/[websiteId]/segments/SegmentEditForm.tsx b/src/app/(main)/websites/[websiteId]/segments/SegmentEditForm.tsx index 553c0c9b6..62078cf81 100644 --- a/src/app/(main)/websites/[websiteId]/segments/SegmentEditForm.tsx +++ b/src/app/(main)/websites/[websiteId]/segments/SegmentEditForm.tsx @@ -1,21 +1,25 @@ import { Button, + Column, Form, FormButtons, FormField, FormSubmitButton, - Label, Loading, + Text, TextField, } from '@umami/react-zen'; import { useEffect, useState } from 'react'; import { useMessages, useUpdateQuery, useWebsiteSegmentQuery } from '@/components/hooks'; import { FieldFilters } from '@/components/input/FieldFilters'; +import { PropertyFilters } from '@/components/property-data/PropertyFilters'; +import type { SessionPropertyFilter } from '@/lib/types'; export function SegmentEditForm({ segmentId, websiteId, filters = [], + sessionPropertyFilters = [], showFilters = true, onSave, onClose, @@ -23,6 +27,7 @@ export function SegmentEditForm({ segmentId?: string; websiteId: string; filters?: any[]; + sessionPropertyFilters?: SessionPropertyFilter[]; showFilters?: boolean; onSave?: () => void; onClose?: () => void; @@ -69,32 +74,50 @@ export function SegmentEditForm({ return (
- - - - {showFilters && ( - <> - - - - - - )} - - - - {t(labels.save)} - - + + + + + {showFilters && ( + + + {t(labels.filters)} + + + + + + + {t(labels.sessionData)} + + {({ field }) => ( + + )} + + + + )} + + + + {t(labels.save)} + + +
); } diff --git a/src/app/(main)/websites/[websiteId]/settings/ShareEditForm.tsx b/src/app/(main)/websites/[websiteId]/settings/ShareEditForm.tsx index b89e4e222..161e271ed 100644 --- a/src/app/(main)/websites/[websiteId]/settings/ShareEditForm.tsx +++ b/src/app/(main)/websites/[websiteId]/settings/ShareEditForm.tsx @@ -128,7 +128,7 @@ export function ShareEditForm({ return ( {url && ( - + diff --git a/src/app/api/websites/[websiteId]/segments/[segmentId]/route.ts b/src/app/api/websites/[websiteId]/segments/[segmentId]/route.ts index 1ed6d7955..c3f6bc619 100644 --- a/src/app/api/websites/[websiteId]/segments/[segmentId]/route.ts +++ b/src/app/api/websites/[websiteId]/segments/[segmentId]/route.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { parseRequest } from '@/lib/request'; -import { json, notFound, ok, unauthorized } from '@/lib/response'; -import { anyObjectParam, segmentTypeParam } from '@/lib/schema'; +import { badRequest, json, notFound, ok, unauthorized } from '@/lib/response'; +import { segmentParamSchema, segmentTypeParam } from '@/lib/schema'; import { canDeleteWebsite, canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions'; import { deleteSegment, getWebsiteSegment, updateSegment } from '@/queries/prisma'; @@ -37,7 +37,7 @@ export async function POST( const schema = z.object({ type: segmentTypeParam, name: z.string().max(200), - parameters: anyObjectParam, + parameters: segmentParamSchema, }); const { auth, body, error } = await parseRequest(request, schema); @@ -49,6 +49,10 @@ export async function POST( const { websiteId, segmentId } = await params; const { type, name, parameters } = body; + if (type === 'cohort' && parameters.sessionPropertyFilters?.length) { + return badRequest({ message: 'Session property filters are only supported for segments.' }); + } + if (!(await canUpdateWebsite(auth, websiteId))) { return unauthorized(); } diff --git a/src/app/api/websites/[websiteId]/segments/route.ts b/src/app/api/websites/[websiteId]/segments/route.ts index 39f326582..015dc8b6c 100644 --- a/src/app/api/websites/[websiteId]/segments/route.ts +++ b/src/app/api/websites/[websiteId]/segments/route.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { uuid } from '@/lib/crypto'; import { getQueryFilters, parseRequest } from '@/lib/request'; -import { json, unauthorized } from '@/lib/response'; +import { badRequest, json, unauthorized } from '@/lib/response'; import { searchParams, segmentParamSchema, segmentTypeParam } from '@/lib/schema'; import { canUpdateWebsite, canViewSharedWebsiteFilters } from '@/permissions'; import { createSegment, getWebsiteSegments } from '@/queries/prisma'; @@ -54,6 +54,10 @@ export async function POST( const { websiteId } = await params; const { type, name, parameters } = body; + if (type === 'cohort' && parameters.sessionPropertyFilters?.length) { + return badRequest({ message: 'Session property filters are only supported for segments.' }); + } + if (!(await canUpdateWebsite(auth, websiteId))) { return unauthorized(); } diff --git a/src/components/common/FilterRecord.tsx b/src/components/common/FilterRecord.tsx index cb2a1462d..0f8d311ff 100644 --- a/src/components/common/FilterRecord.tsx +++ b/src/components/common/FilterRecord.tsx @@ -84,7 +84,7 @@ export function FilterRecord({ }; return ( - + diff --git a/src/components/input/FilterBar.tsx b/src/components/input/FilterBar.tsx index 61b7d8387..5429dd1f6 100644 --- a/src/components/input/FilterBar.tsx +++ b/src/components/input/FilterBar.tsx @@ -35,9 +35,8 @@ export function FilterBar({ websiteId }: { websiteId?: string }) { const { data, isLoading } = useWebsiteSegmentQuery(websiteId, segment || cohort); const canSaveSegment = !!websiteId && - filters.length > 0 && + (filters.length > 0 || sessionPropertyFilters.length > 0) && eventPropertyFilters.length === 0 && - sessionPropertyFilters.length === 0 && !segment && !cohort && !pathname.includes('/share'); @@ -192,7 +191,14 @@ export function FilterBar({ websiteId }: { websiteId?: string }) { }} > {({ close }) => { - return ; + return ( + + ); }} diff --git a/src/components/input/PreferencesButton.tsx b/src/components/input/PreferencesButton.tsx index 87149bba3..439ac7b32 100644 --- a/src/components/input/PreferencesButton.tsx +++ b/src/components/input/PreferencesButton.tsx @@ -17,11 +17,11 @@ export function PreferencesButton() { - + - + diff --git a/src/components/property-data/PropertyFilterRecord.tsx b/src/components/property-data/PropertyFilterRecord.tsx index 710cb40e3..3131a3cce 100644 --- a/src/components/property-data/PropertyFilterRecord.tsx +++ b/src/components/property-data/PropertyFilterRecord.tsx @@ -148,7 +148,7 @@ export function PropertyFilterRecord({ }; return ( - +