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(cohorts): bring a cohort back in the list when its delete is refused (#117019)

Harley Alexander12fe7b1f50ba
diff --git a/frontend/src/lib/utils/deleteWithUndo.tsx b/frontend/src/lib/utils/deleteWithUndo.tsx
index bb4cee657a51..e6f6ee9d6ea3 100644
--- a/frontend/src/lib/utils/deleteWithUndo.tsx
+++ b/frontend/src/lib/utils/deleteWithUndo.tsx
@@ -17,6 +17,9 @@ export async function deleteWithUndo<T extends Record<string, any>>({
/** The name the toast shows. It defaults to `object.name`, which the request body also sends. */
label?: string
callback?: (undo: boolean, object: T) => void
+ /** Runs when the request fails, so a caller that removed the object from its state before the
+ * request can put it back. */
+ onError?: (undo: boolean, object: T) => void
}): Promise<void> {
try {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
@@ -44,6 +47,7 @@ export async function deleteWithUndo<T extends Record<string, any>>({
// Show error toast with the error message from the API
const errorMessage = error.detail || error.message || 'Failed to delete'
lemonToast.error(errorMessage)
+ props.onError?.(undo, props.object)
}
}
diff --git a/frontend/src/models/cohortsModel.test.ts b/frontend/src/models/cohortsModel.test.ts
index 3528b5f291d2..77b35cff6af4 100644
--- a/frontend/src/models/cohortsModel.test.ts
+++ b/frontend/src/models/cohortsModel.test.ts
@@ -417,6 +417,26 @@ describe('cohortsModel', () => {
}),
})
})
+
+ it('puts the cohort back in cohortsById when the server refuses the delete', async () => {
+ await expectLogic(logic).toDispatchActions(['loadAllCohortsSuccess'])
+ useMocks({
+ patch: {
+ '/api/projects/:team/cohorts/:id': () => [
+ 400,
+ { detail: 'This cohort is used in 1 active feature flag(s).' },
+ ],
+ },
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.deleteCohort({ id: 1 })
+ })
+ .toDispatchActions(['deleteCohort', 'loadAllCohortsSuccess'])
+ .toMatchValues({
+ cohortsById: expect.objectContaining({ 1: expect.objectContaining({ id: 1 }) }),
+ })
+ })
})
describe('selectors', () => {
diff --git a/frontend/src/models/cohortsModel.ts b/frontend/src/models/cohortsModel.ts
index 5c53162e29b8..0fd894031209 100644
--- a/frontend/src/models/cohortsModel.ts
+++ b/frontend/src/models/cohortsModel.ts
@@ -594,6 +594,8 @@ export const cohortsModel = kea<cohortsModelType>([
}
}
},
+ // The reducers already removed the cohort, so reload to bring back its name in breadcrumbs and pickers.
+ onError: () => actions.loadAllCohorts(),
})
},
})),
diff --git a/frontend/src/scenes/cohorts/cohortsSceneLogic.test.ts b/frontend/src/scenes/cohorts/cohortsSceneLogic.test.ts
index e814b36918ef..b5fdadf244fd 100644
--- a/frontend/src/scenes/cohorts/cohortsSceneLogic.test.ts
+++ b/frontend/src/scenes/cohorts/cohortsSceneLogic.test.ts
@@ -152,6 +152,28 @@ describe('cohortsSceneLogic', () => {
}),
})
})
+
+ it('puts the cohort back when the server refuses the delete', async () => {
+ await expectLogic(logic).toDispatchActions(['loadCohortsSuccess'])
+ useMocks({
+ patch: {
+ '/api/projects/:team/cohorts/:id': () => [
+ 400,
+ { detail: 'This cohort is used in 1 active feature flag(s).' },
+ ],
+ },
+ })
+
+ await expectLogic(logic, () => {
+ logic.actions.deleteCohort({ id: 1 })
+ })
+ .toDispatchActions(['deleteCohort', 'loadCohortsSuccess'])
+ .toMatchValues({
+ cohorts: expect.objectContaining({
+ results: expect.arrayContaining([expect.objectContaining({ id: 1 })]),
+ }),
+ })
+ })
})
describe('selectors', () => {
diff --git a/frontend/src/scenes/cohorts/cohortsSceneLogic.ts b/frontend/src/scenes/cohorts/cohortsSceneLogic.ts
index 158ae198fa70..80c333ca352c 100644
--- a/frontend/src/scenes/cohorts/cohortsSceneLogic.ts
+++ b/frontend/src/scenes/cohorts/cohortsSceneLogic.ts
@@ -302,6 +302,8 @@ export const cohortsSceneLogic = kea<cohortsSceneLogicType>([
}
}
},
+ // The reducer already removed the row, so reload to show the cohort the server kept.
+ onError: () => actions.loadCohorts(),
})
},
exportCohortPersons: async ({ id, columns }) => {