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(flags): stop the flags list flashing skeleton rows on filter change (#74990)

ruby childs1e65ce3fa9b8
diff --git a/frontend/src/scenes/feature-flags/FeatureFlags.tsx b/frontend/src/scenes/feature-flags/FeatureFlags.tsx
index 757af333027f..74be190a7b35 100644
--- a/frontend/src/scenes/feature-flags/FeatureFlags.tsx
+++ b/frontend/src/scenes/feature-flags/FeatureFlags.tsx
@@ -308,7 +308,7 @@ function FeatureFlagRowActions({ featureFlag }: { featureFlag: FeatureFlagType }
void deleteWithUndo({
endpoint: `projects/${currentProjectId}/feature_flags`,
object: { name: featureFlag.key, id: featureFlag.id },
- callback: loadFeatureFlags,
+ callback: () => loadFeatureFlags(),
}).catch((e) => {
lemonToast.error(`Failed to delete feature flag: ${e.detail}`)
})
diff --git a/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
index 0de96ff1598b..7ce63227cede 100644
--- a/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
@@ -461,3 +461,101 @@ describe('updateFeatureFlagArchived', () => {
expect(logic.values.featureFlagsUpdating[2]).toBe(true)
})
})
+
+describe('displayedFlags stability while a load is in flight', () => {
+ let logic: ReturnType<typeof featureFlagsLogic.build>
+
+ beforeEach(() => {
+ useMocks({
+ get: {
+ '/api/projects/:projectId/feature_flags/': () => [
+ 200,
+ {
+ results: [
+ { id: 1, key: 'alpha', active: true },
+ { id: 2, key: 'beta', active: true },
+ ],
+ count: 2,
+ },
+ ],
+ },
+ })
+ initKeaTests()
+ logic = featureFlagsLogic()
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic?.unmount()
+ })
+
+ // Regression guard: displayedFlags must filter the loaded page by the filters it was loaded under, not
+ // the just-changed live filters. Filtering the previous page against a new search before the refetch
+ // lands used to empty the list, which flashed LemonTable's skeleton rows over a table that had data.
+ it('keeps the loaded rows until the refetch lands when a non-matching search is typed', async () => {
+ logic.actions.loadFeatureFlags()
+ await expectLogic(logic).toDispatchActions(['loadFeatureFlagsSuccess'])
+ expect(logic.values.displayedFlags.map((f) => f.key)).toEqual(['alpha', 'beta'])
+
+ await expectLogic(logic, () => {
+ logic.actions.setFeatureFlagsFilters({ search: 'no-such-flag' })
+ })
+ // setFeatureFlagsFilters debounces the refetch, so this asserts the pre-refetch render.
+ .toMatchValues({
+ filters: expect.objectContaining({ search: 'no-such-flag' }),
+ displayedFlags: [expect.objectContaining({ key: 'alpha' }), expect.objectContaining({ key: 'beta' })],
+ })
+ .toDispatchActions(['loadFeatureFlagsSuccess'])
+
+ // The mock ignores `search`, so the refetch returns alpha and beta again: only the client-side
+ // filter, now stamped with the new search, can empty the list. Asserted outside the expectLogic
+ // chain because a failing toMatchValues there times the test out instead of reporting a diff.
+ expect(logic.values.displayedFlags).toEqual([])
+ })
+
+ // Regression guard: the loader reads `values` live, so reading the filters after its await stamped the
+ // response with whatever had been typed since. displayedFlags then filtered a page against filters it
+ // was never requested under, emptying the list and flashing skeletons over data that had just landed.
+ it('stamps the response with the filters it was requested under, not ones typed while it was in flight', async () => {
+ logic.actions.loadFeatureFlags()
+ // The refetch is debounced 300ms, so this only mutates `filters` while the first request is open.
+ logic.actions.setFeatureFlagsFilters({ search: 'typed-while-loading' })
+
+ await expectLogic(logic).toDispatchActions(['loadFeatureFlagsSuccess'])
+
+ expect(logic.values.featureFlags.filters?.search).toBeUndefined()
+ expect(logic.values.displayedFlags.map((f) => f.key)).toEqual(['alpha', 'beta'])
+ })
+
+ // Regression guard: the 300ms debounce spaces out dispatches, not requests, so two loads can be open
+ // at once. Without a breakpoint the slower, older response resolved last and replaced the newer page.
+ it('discards a superseded response that resolves after a newer one', async () => {
+ let releaseStaleResponse = (): void => {}
+ const staleResponseReleased = new Promise<void>((resolve) => {
+ releaseStaleResponse = resolve
+ })
+ let requestCount = 0
+ useMocks({
+ get: {
+ '/api/projects/:projectId/feature_flags/': async () => {
+ requestCount += 1
+ if (requestCount === 1) {
+ await staleResponseReleased
+ return [200, { results: [{ id: 1, key: 'stale', active: true }], count: 1 }]
+ }
+ return [200, { results: [{ id: 2, key: 'fresh', active: true }], count: 1 }]
+ },
+ },
+ })
+
+ logic.actions.loadFeatureFlags()
+ logic.actions.loadFeatureFlags()
+ await expectLogic(logic).toDispatchActions(['loadFeatureFlagsSuccess'])
+ expect(logic.values.displayedFlags.map((f) => f.key)).toEqual(['fresh'])
+
+ releaseStaleResponse()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.displayedFlags.map((f) => f.key)).toEqual(['fresh'])
+ })
+})
diff --git a/frontend/src/scenes/feature-flags/featureFlagsLogic.ts b/frontend/src/scenes/feature-flags/featureFlagsLogic.ts
index 89f6927ca68e..a54d20277c69 100644
--- a/frontend/src/scenes/feature-flags/featureFlagsLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagsLogic.ts
@@ -192,7 +192,6 @@ export interface featureFlagsLogicValues {
featureFlagsUpdating: Record<number, boolean>
filters: FeatureFlagsFilters
filtersChanged: boolean
- localFlagsCache: FeatureFlagType[]
pagination: PaginationManual
paramsFromFilters: {
active?: string | undefined
@@ -220,7 +219,7 @@ export interface featureFlagsLogicActions {
deleteFlag: (id: number) => {
id: number
}
- loadFeatureFlags: () => any
+ loadFeatureFlags: (_: void) => void
loadFeatureFlagsFailure: (
error: string,
errorObject?: any
@@ -230,10 +229,10 @@ export interface featureFlagsLogicActions {
}
loadFeatureFlagsSuccess: (
featureFlags: FeatureFlagsResult,
- payload?: any
+ payload?: void
) => {
featureFlags: FeatureFlagsResult
- payload?: any
+ payload?: void
}
setActiveTab: (tabKey: FeatureFlagsTab) => {
tabKey: FeatureFlagsTab
@@ -392,13 +391,8 @@ export interface featureFlagsLogicMeta {
featureFlags: FeatureFlagsResult,
filters: FeatureFlagsFilters
) => boolean
- pagination: (
- filters: FeatureFlagsFilters,
- displayedFlags: FeatureFlagType[],
- featureFlags: FeatureFlagsResult,
- filtersChanged: boolean
- ) => PaginationManual
- displayedFlags: (localFlagsCache: FeatureFlagType[], filters: FeatureFlagsFilters) => FeatureFlagType[]
+ pagination: (filters: FeatureFlagsFilters, featureFlags: FeatureFlagsResult) => PaginationManual
+ displayedFlags: (featureFlags: FeatureFlagsResult) => FeatureFlagType[]
}
}
@@ -430,15 +424,23 @@ export const featureFlagsLogic = kea<featureFlagsLogicType>([
featureFlags: [
{ results: [], count: 0, filters: DEFAULT_FILTERS, offset: 0 } as FeatureFlagsResult,
{
- loadFeatureFlags: async () => {
+ loadFeatureFlags: async (_: void, breakpoint) => {
+ // Read the filters up front: `values` is live, so reading them after the await would
+ // stamp the response with whatever the user has typed since, and displayedFlags would
+ // then filter this page against filters it was never requested under.
+ const params = values.paramsFromFilters
+ const filters = values.filters
const response = await api.get(
- `api/projects/${values.currentProjectId}/feature_flags/?${toParams(values.paramsFromFilters)}`
+ `api/projects/${values.currentProjectId}/feature_flags/?${toParams(params)}`
)
+ // Drop a response that a newer request has already superseded, so slow-then-fast
+ // responses can't land out of order.
+ breakpoint()
return {
...response,
- offset: values.paramsFromFilters.offset,
- filters: values.filters,
+ offset: params.offset,
+ filters,
}
},
updateFeatureFlag: async ({ id, payload }: { id: number; payload: Partial<FeatureFlagType> }) => {
@@ -520,20 +522,6 @@ export const featureFlagsLogic = kea<featureFlagsLogicType>([
results: state.results.filter((flag) => flag.id !== id),
}),
},
- localFlagsCache: [
- [] as FeatureFlagType[],
- {
- loadFeatureFlagsSuccess: (_, { featureFlags }) => {
- return featureFlags.results
- },
- updateFeatureFlagSuccess: (_, { featureFlags }) => featureFlags.results,
- updateFeatureFlagArchivedSuccess: (_, { featureFlags }) => featureFlags.results,
- updateFlag: (state, { flag }) => state.map((f) => (f.id === flag.id ? flag : f)),
- updateFlagFromPartial: (state, { flag }) =>
- state.map((f) => (f.id === flag.id ? { ...f, ...flag } : f)),
- deleteFlag: (state, { id }) => state.filter((f) => f.id !== id),
- },
- ],
activeTab: [
FeatureFlagsTab.OVERVIEW as FeatureFlagsTab,
{
@@ -616,18 +604,13 @@ export const featureFlagsLogic = kea<featureFlagsLogicType>([
},
],
pagination: [
- (s) => [s.filters, s.displayedFlags, s.featureFlags, s.filtersChanged],
- (
- filters: FeatureFlagsFilters,
- displayedFlags: FeatureFlagType[],
- featureFlags: FeatureFlagsResult,
- filtersChanged: boolean
- ): PaginationManual => {
+ (s) => [s.filters, s.featureFlags],
+ (filters: FeatureFlagsFilters, featureFlags: FeatureFlagsResult): PaginationManual => {
return {
controlled: true,
pageSize: FLAGS_PER_PAGE,
currentPage: filters.page || 1,
- entryCount: filtersChanged ? displayedFlags.length : featureFlags.count,
+ entryCount: featureFlags.count,
}
},
],
@@ -638,9 +621,14 @@ export const featureFlagsLogic = kea<featureFlagsLogicType>([
}),
],
displayedFlags: [
- (s) => [s.localFlagsCache, s.filters],
- (cache: FeatureFlagType[], filters: FeatureFlagsFilters): FeatureFlagType[] => {
- return cache.filter((flag) => flagMatchesFilters(flag, filters))
+ (s) => [s.featureFlags],
+ (featureFlags: FeatureFlagsResult): FeatureFlagType[] => {
+ // Re-applies the page's own server-side filters so optimistic row edits that no longer match
+ // (a flag toggled off under active=true, or archived) drop out without a refetch. Filtering
+ // against the live filters instead would empty the list between a filter change and its
+ // refetch, and LemonTable renders skeleton rows over an empty dataSource.
+ const appliedFilters = featureFlags.filters ?? DEFAULT_FILTERS
+ return featureFlags.results.filter((flag) => flagMatchesFilters(flag, appliedFilters))
},
],
}),
diff --git a/frontend/src/scenes/feature-flags/flagSelectionLogic.ts b/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
index 069d779a1a8d..9b2d67b96adb 100644
--- a/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
+++ b/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
@@ -234,7 +234,7 @@ export interface flagSelectionLogicActions {
flagCount: number
projectCount: number
} // eventUsageLogic
- loadFeatureFlags: () => any // featureFlagsLogic
+ loadFeatureFlags: (_?: void | undefined) => void // featureFlagsLogic
bulkCopyFlags: () => {
value: true
}
diff --git a/frontend/src/scenes/persons/relatedFeatureFlagsLogic.ts b/frontend/src/scenes/persons/relatedFeatureFlagsLogic.ts
index ae734ab58cbf..882097150cad 100644
--- a/frontend/src/scenes/persons/relatedFeatureFlagsLogic.ts
+++ b/frontend/src/scenes/persons/relatedFeatureFlagsLogic.ts
@@ -55,7 +55,7 @@ export interface relatedFeatureFlagsLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface relatedFeatureFlagsLogicActions {
- loadFeatureFlags: () => any // featureFlagsLogic
+ loadFeatureFlags: (_?: void | undefined) => void // featureFlagsLogic
setFeatureFlagsFilters: (
filters: Partial<FeatureFlagsFilters>,
replace?: boolean | undefined