Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(replay): name a date range the preset list does not contain (#101027)

HaynesPostHogd3dda2bd797a
diff --git a/frontend/src/lib/components/DateFilter/dateFilterLogic.test.ts b/frontend/src/lib/components/DateFilter/dateFilterLogic.test.ts
index 78d47792e098..32d90a1272ce 100644
--- a/frontend/src/lib/components/DateFilter/dateFilterLogic.test.ts
+++ b/frontend/src/lib/components/DateFilter/dateFilterLogic.test.ts
@@ -2,7 +2,8 @@ import { expectLogic } from 'kea-test-utils'
import { DateFilterLogicProps, DateFilterView } from 'lib/components/DateFilter/types'
import { dayjs } from 'lib/dayjs'
-import { dateMapping } from 'lib/utils/dateFilters'
+import { dateMapping, dateStringToDayJs } from 'lib/utils/dateFilters'
+import { formatDateRange } from 'lib/utils/datetime'
import { dateFilterLogic } from './dateFilterLogic'
@@ -284,4 +285,56 @@ describe('dateFilterLogic', () => {
})
}
)
+
+ describe("a range outside the caller's preset list", () => {
+ // the six presets replay offers
+ const narrowDateOptions = [
+ { key: 'Custom', values: [] },
+ { key: 'Last 24 hours', values: ['-24h'] },
+ { key: 'Last 3 days', values: ['-3d'] },
+ { key: 'Last 7 days', values: ['-7d'] },
+ { key: 'Last 30 days', values: ['-30d'] },
+ { key: 'All time', values: ['-5y'] },
+ ]
+
+ const buildLogic = (dateFrom: string | null, dateTo: string | null): ReturnType<typeof dateFilterLogic.build> =>
+ dateFilterLogic({
+ key: `narrow-${dateFrom}-${dateTo}`,
+ onChange: jest.fn(),
+ dateFrom,
+ dateTo,
+ dateOptions: narrowDateOptions,
+ isDateFormatted: false,
+ })
+
+ it('falls back to the full preset mapping', async () => {
+ const narrowLogic = buildLogic('-1mStart', '-1mEnd')
+ narrowLogic.mount()
+
+ await expectLogic(narrowLogic).toMatchValues({ label: 'Last month' })
+ })
+
+ it('falls back to the resolved dates when no preset matches', async () => {
+ const narrowLogic = buildLogic('-2mStart', '-2mEnd')
+ narrowLogic.mount()
+
+ await expectLogic(narrowLogic).toMatchValues({
+ label: formatDateRange(dateStringToDayJs('-2mStart')!, dateStringToDayJs('-2mEnd')!),
+ })
+ })
+
+ it('names the upper bound when only the end of the range is set', async () => {
+ const narrowLogic = buildLogic(null, '2026-07-31')
+ narrowLogic.mount()
+
+ await expectLogic(narrowLogic).toMatchValues({ label: 'Until July 31, 2026' })
+ })
+
+ it('still shows the placeholder when no range is set', async () => {
+ const narrowLogic = buildLogic(null, null)
+ narrowLogic.mount()
+
+ await expectLogic(narrowLogic).toMatchValues({ label: 'No date range override' })
+ })
+ })
})
diff --git a/frontend/src/lib/components/DateFilter/dateFilterLogic.ts b/frontend/src/lib/components/DateFilter/dateFilterLogic.ts
index 9eea9d95b76a..822043688133 100644
--- a/frontend/src/lib/components/DateFilter/dateFilterLogic.ts
+++ b/frontend/src/lib/components/DateFilter/dateFilterLogic.ts
@@ -7,7 +7,7 @@ import {
SELECT_FIXED_VALUE_PLACEHOLDER,
} from 'lib/components/DateFilter/types'
import { Dayjs, dayjs } from 'lib/dayjs'
-import { dateFilterToText, dateStringToDayJs } from 'lib/utils/dateFilters'
+import { dateFilterToText, dateMapping, dateStringToDayJs } from 'lib/utils/dateFilters'
import { formatDate, formatDateRange, formatDateTime, formatDateTimeRange, isDate } from 'lib/utils/datetime'
import { DateMappingOption } from '~/types'
@@ -21,6 +21,24 @@ const RELATIVE_UNIT_LABEL: Record<string, string> = {
y: 'year',
}
+/** A caller can narrow the preset list, so a stored range can match none of its options. Name the
+ * range anyway, rather than let the placeholder read as though no range were set. */
+function labelForRangeOutsideOptions(
+ dateFrom: string | Dayjs | null | undefined,
+ dateTo: string | Dayjs | null | undefined
+): string | null {
+ const mappedLabel = dateFilterToText(dateFrom, dateTo, null, dateMapping, false)
+ if (mappedLabel) {
+ return mappedLabel
+ }
+ const resolvedFrom = dayjs.isDayjs(dateFrom) ? dateFrom : dateStringToDayJs(dateFrom ?? null)
+ const resolvedTo = dayjs.isDayjs(dateTo) ? dateTo : dateStringToDayJs(dateTo ?? null)
+ if (!resolvedFrom?.isValid()) {
+ return resolvedTo?.isValid() ? `Until ${formatDate(resolvedTo)}` : null
+ }
+ return resolvedTo?.isValid() ? formatDateRange(resolvedFrom, resolvedTo) : `${formatDate(resolvedFrom)} to now`
+}
+
function formatRelativeOffset(value: string): string {
const match = /^-(\d+)([hdwmqy])$/.exec(value)
if (!match) {
@@ -62,7 +80,7 @@ export interface dateFilterLogicValues {
isFixedRangeWithTime: boolean
isRollingDateRange: boolean
isVisible: boolean
- label: string | null
+ label: string
rangeDateFrom: Dayjs | null
rangeDateTo: Dayjs | null
view: DateFilterView
@@ -164,7 +182,7 @@ export interface dateFilterLogicMeta {
arg5: any,
dateFromHasTimePrecision: boolean,
dateToHasTimePrecision: boolean
- ) => string | null
+ ) => string
}
}
@@ -356,7 +374,7 @@ export const dateFilterLogic = kea<dateFilterLogicType>([
allowSingleAndRange,
dateFromHasTimePrecision: boolean,
dateToHasTimePrecision: boolean
- ) => {
+ ): string => {
// Only render the "N days ago to M days ago" label when the consumer has opted into
// the custom-relative-range picker — other call sites (e.g. trends) may legitimately
// store both dates as relative strings (e.g. "-0d"/"-0d" for "Today") without intending
@@ -398,15 +416,11 @@ export const dateFilterLogic = kea<dateFilterLogicType>([
} to now`
: isFixedDate
? formatDate(dateStringToDayJs(dateFrom) ?? dayjs(dateFrom))
- : dateFilterToText(
- dateFrom,
- dateTo,
- isFixedDateMode
- ? (placeholder ?? SELECT_FIXED_VALUE_PLACEHOLDER)
- : NO_OVERRIDE_RANGE_PLACEHOLDER,
- dateOptions,
- false
- )
+ : (dateFilterToText(dateFrom, dateTo, null, dateOptions, false) ??
+ labelForRangeOutsideOptions(dateFrom, dateTo) ??
+ (isFixedDateMode
+ ? (placeholder ?? SELECT_FIXED_VALUE_PLACEHOLDER)
+ : NO_OVERRIDE_RANGE_PLACEHOLDER))
},
],
}),