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(feature-flags): answer the empty-state gate with one request (#127063)

Paul D'Ambrad81f8e297be7
diff --git a/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.test.ts b/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.test.ts
index 46dcb830871b..9e3ef86355e8 100644
--- a/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.test.ts
+++ b/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.test.ts
@@ -25,30 +25,34 @@ describe('featureFlagsSetupLogic', () => {
initKeaTests()
})
- // The list answers archived and non-archived separately, so the count is the sum of both.
+ // The list answers archived and non-archived separately. Archived flags only matter when no live flag exists.
it.each([
- [0, 0, 'needs-setup'],
- [1, 0, 'has-data'],
- [42, 0, 'has-data'],
- [0, 3, 'has-data'],
- ])('pushes %i live and %i archived flags as status %s', async (live, archived, expected) => {
- mockFeatureFlagsList.mockImplementation((_projectId, params) =>
- Promise.resolve({ count: params?.archived ? archived : live, next: null, previous: null, results: [] })
- )
- const logic = featureFlagsSetupLogic()
- logic.mount()
- await expectLogic(logic).toFinishAllListeners()
- expect(productSetupStatusLogic({ productKey: ProductKey.FEATURE_FLAGS }).values.status).toBe(expected)
- })
+ [0, 0, 'needs-setup', 2],
+ [1, 0, 'has-data', 1],
+ [42, 0, 'has-data', 1],
+ [0, 3, 'has-data', 2],
+ [1, 3, 'has-data', 1],
+ ])(
+ 'pushes %i live and %i archived flags as status %s after %i requests',
+ async (live, archived, expected, requests) => {
+ mockFeatureFlagsList.mockImplementation((_projectId, params) =>
+ Promise.resolve({ count: params?.archived ? archived : live, next: null, previous: null, results: [] })
+ )
+ const logic = featureFlagsSetupLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(productSetupStatusLogic({ productKey: ProductKey.FEATURE_FLAGS }).values.status).toBe(expected)
+ expect(mockFeatureFlagsList).toHaveBeenCalledTimes(requests)
+ }
+ )
// Undo of a project's only deleted flag reloads the flag list without remounting the gate.
- // Once a flag has been seen, a list reload must not send more count requests.
it.each([
- [0, 'needs-setup', 4],
- [1, 'has-data', 2],
+ [0, 'needs-setup', 2],
+ [1, 'has-data', 1],
])(
- 'on a flag list reload after mounting with %i live flags (%s), makes %i count requests and ends has-data',
- async (liveAtMount, statusAtMount, countRequests) => {
+ 'on a flag list reload after mounting with %i live flags (%s), makes %i requests and ends has-data',
+ async (liveAtMount, statusAtMount, requests) => {
let live = liveAtMount
mockFeatureFlagsList.mockImplementation((_projectId, params) =>
Promise.resolve({ count: params?.archived ? 0 : live, next: null, previous: null, results: [] })
@@ -68,12 +72,12 @@ describe('featureFlagsSetupLogic', () => {
})
)
await expectLogic(logic).toFinishAllListeners()
- expect(mockFeatureFlagsList).toHaveBeenCalledTimes(countRequests)
+ expect(mockFeatureFlagsList).toHaveBeenCalledTimes(requests)
expect(productSetupStatusLogic({ productKey: ProductKey.FEATURE_FLAGS }).values.status).toBe('has-data')
}
)
- it('fails open to unknown when the count query fails before any answer', async () => {
+ it('fails open to unknown when the flag check fails before any answer', async () => {
mockFeatureFlagsList.mockRejectedValue(new Error('network down'))
const logic = featureFlagsSetupLogic()
logic.mount()
diff --git a/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.ts b/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.ts
index 264368037551..b63ea8a6c4b7 100644
--- a/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.ts
+++ b/products/feature_flags/frontend/emptyState/featureFlagsSetupLogic.ts
@@ -15,8 +15,8 @@ import { FeatureFlagsListArchived } from 'products/feature_flags/frontend/genera
export interface featureFlagsSetupLogicValues {
setupStatus: ProductSetupStatus // productSetupStatusLogic
currentProjectId: number | string // teamLogic
- flagCount: number | null
- flagCountLoading: boolean
+ hasAnyFlag: boolean | null
+ hasAnyFlagLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -24,19 +24,19 @@ export interface featureFlagsSetupLogicActions {
setDetectedStatus: (status: ProductSetupStatus) => {
status: ProductSetupStatus
} // productSetupStatusLogic
- loadFlagCount: (_: void) => void
- loadFlagCountFailure: (
+ loadHasAnyFlag: (_: void) => void
+ loadHasAnyFlagFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
- loadFlagCountSuccess: (
- flagCount: number,
+ loadHasAnyFlagSuccess: (
+ hasAnyFlag: boolean,
payload?: void
) => {
- flagCount: number
+ hasAnyFlag: boolean
payload?: void
}
}
@@ -61,38 +61,45 @@ export const featureFlagsSetupLogic = kea<featureFlagsSetupLogicType>([
],
})),
loaders(({ values }) => ({
- flagCount: [
- null as number | null,
+ hasAnyFlag: [
+ null as boolean | null,
{
- loadFlagCount: async (_: void, breakpoint): Promise<number> => {
+ loadHasAnyFlag: async (_: void, breakpoint): Promise<boolean> => {
+ const live = await featureFlagsList(String(values.currentProjectId), { limit: 1 })
+ breakpoint()
+ if (live.count > 0) {
+ return true
+ }
// The list excludes archived flags unless asked for them, and no value of
// `archived` returns both, so a project that archived all of its flags would
// count zero and lose the scene it unarchives them from.
- const [live, archived] = await Promise.all([
- featureFlagsList(String(values.currentProjectId), { limit: 1 }),
- featureFlagsList(String(values.currentProjectId), {
- limit: 1,
- archived: FeatureFlagsListArchived.True,
- }),
- ])
+ const archived = await featureFlagsList(String(values.currentProjectId), {
+ limit: 1,
+ archived: FeatureFlagsListArchived.True,
+ })
breakpoint()
- return live.count + archived.count
+ return archived.count > 0
},
},
],
})),
listeners(({ actions, values }) => ({
- // Undo of a deleted flag brings it back without remounting the gate, so no count runs on its own.
- // The flag list reloads after Undo, and that reload is the signal to count again.
- [featureFlagsLogic.actionTypes.loadFeatureFlagsSuccess]: () => {
- if (values.setupStatus !== 'has-data') {
- actions.loadFlagCount()
+ // Undo of a deleted flag brings it back without remounting the gate, so no flag check runs on its own.
+ // The flag list reloads after Undo, and that reload is the signal to check again.
+ [featureFlagsLogic.actionTypes.loadFeatureFlagsSuccess]: ({ featureFlags }) => {
+ if (values.setupStatus === 'has-data') {
+ return
+ }
+ if (featureFlags.count > 0) {
+ actions.setDetectedStatus('has-data')
+ } else {
+ actions.loadHasAnyFlag()
}
},
- loadFlagCountSuccess: ({ flagCount }) => {
- actions.setDetectedStatus(flagCount !== null && flagCount > 0 ? 'has-data' : 'needs-setup')
+ loadHasAnyFlagSuccess: ({ hasAnyFlag }) => {
+ actions.setDetectedStatus(hasAnyFlag ? 'has-data' : 'needs-setup')
},
- loadFlagCountFailure: () => {
+ loadHasAnyFlagFailure: () => {
// Never strand the gate on its spinner: if nothing has answered yet, fail
// open to the real scene. Don't downgrade an existing answer on a blip.
if (values.setupStatus === 'loading') {
@@ -101,6 +108,6 @@ export const featureFlagsSetupLogic = kea<featureFlagsSetupLogicType>([
},
})),
afterMount(({ actions }) => {
- actions.loadFlagCount()
+ actions.loadHasAnyFlag()
}),
])