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(experiments): add loading state to end experiment button (#62029)

Paul D'Ambra7bea27204173
diff --git a/frontend/src/scenes/experiments/ExperimentView/ExperimentModals.tsx b/frontend/src/scenes/experiments/ExperimentView/ExperimentModals.tsx
index 454f84233810..f95c1c92a1a8 100644
--- a/frontend/src/scenes/experiments/ExperimentView/ExperimentModals.tsx
+++ b/frontend/src/scenes/experiments/ExperimentView/ExperimentModals.tsx
@@ -198,7 +198,7 @@ export function ResumeExperimentModal(): JSX.Element {
}
export function FinishExperimentModal(): JSX.Element {
- const { experiment, isSingleVariantShipped, shippedVariantKey } = useValues(experimentLogic)
+ const { experiment, isSingleVariantShipped, shippedVariantKey, endExperimentLoading } = useValues(experimentLogic)
const { finishExperiment, endExperimentWithoutShipping, restoreUnmodifiedExperiment } = useActions(experimentLogic)
const { closeFinishExperimentModal } = useActions(modalsLogic)
const { isFinishExperimentModalOpen } = useValues(modalsLogic)
@@ -277,6 +277,7 @@ export function FinishExperimentModal(): JSX.Element {
<LemonButton
onClick={handleEndExperiment}
type="primary"
+ loading={endExperimentLoading}
disabledReason={!experiment.conclusion && 'Select a conclusion'}
>
End experiment
diff --git a/frontend/src/scenes/experiments/experimentLogic.test.ts b/frontend/src/scenes/experiments/experimentLogic.test.ts
index 4ef25c957bce..4a1e8759ac77 100644
--- a/frontend/src/scenes/experiments/experimentLogic.test.ts
+++ b/frontend/src/scenes/experiments/experimentLogic.test.ts
@@ -1253,6 +1253,60 @@ describe('experimentLogic', () => {
})
})
+ describe('endExperimentLoading', () => {
+ // The end and ship-variant lifecycle calls both drive the single endExperimentLoading flag.
+ const triggers: { name: string; run: (l: ReturnType<typeof experimentLogic>) => void }[] = [
+ { name: 'endExperiment', run: (l) => l.actions.endExperiment() },
+ {
+ name: 'finishExperiment',
+ run: (l) => l.actions.finishExperiment({ selectedVariantKey: 'test', releaseToEveryone: false }),
+ },
+ ]
+
+ it.each(triggers)('flips the flag on then off around a successful $name', async ({ run }) => {
+ const runningExperiment = {
+ ...experiment,
+ start_date: '2026-03-17T10:00:00Z',
+ status: 'running',
+ conclusion: 'won',
+ } as Experiment
+ const createSpy = jest.spyOn(api, 'create').mockResolvedValue({
+ ...runningExperiment,
+ end_date: '2026-03-24T10:00:00Z',
+ status: 'stopped',
+ })
+
+ const keyed = experimentLogic({ experimentId: experiment.id })
+ keyed.mount()
+ keyed.actions.setExperiment(runningExperiment)
+
+ await expectLogic(keyed, () => {
+ run(keyed)
+ })
+ // loading flips on before the request and off after it resolves
+ .toDispatchActions(['setEndExperimentLoading', 'setExperiment', 'setEndExperimentLoading'])
+ .toFinishAllListeners()
+ .toMatchValues({ endExperimentLoading: false })
+
+ createSpy.mockRestore()
+ keyed.unmount()
+ })
+
+ it.each(triggers)('resets the flag after a failed $name', async ({ run }) => {
+ const createSpy = jest.spyOn(api, 'create').mockRejectedValue({ detail: 'boom' })
+
+ logic.actions.setExperiment(experiment)
+
+ await expectLogic(logic, () => {
+ run(logic)
+ })
+ .toFinishAllListeners()
+ .toMatchValues({ endExperimentLoading: false })
+
+ createSpy.mockRestore()
+ })
+ })
+
describe('updateDistribution', () => {
beforeEach(() => {
jest.spyOn(api, 'update')
diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsx
index 6a1394389d5d..52fead6245b4 100644
--- a/frontend/src/scenes/experiments/experimentLogic.tsx
+++ b/frontend/src/scenes/experiments/experimentLogic.tsx
@@ -638,6 +638,7 @@ export const experimentLogic = kea<experimentLogicType>([
createExperiment: (draft?: boolean, folder?: string | null) => ({ draft, folder }),
setCreateExperimentLoading: (loading: boolean) => ({ loading }),
setLaunchExperimentLoading: (loading: boolean) => ({ loading }),
+ setEndExperimentLoading: (loading: boolean) => ({ loading }),
setEditExperiment: (editing: boolean) => ({ editing }),
refreshExperimentResults: (forceRefresh?: boolean, triggeredBy?: ExperimentTriggeredBy) => ({
forceRefresh,
@@ -1216,6 +1217,12 @@ export const experimentLogic = kea<experimentLogicType>([
setLaunchExperimentLoading: (_, { loading }) => loading,
},
],
+ endExperimentLoading: [
+ false,
+ {
+ setEndExperimentLoading: (_, { loading }) => loading,
+ },
+ ],
hogfettiTrigger: [
null as (() => void) | null,
{
@@ -1444,6 +1451,7 @@ export const experimentLogic = kea<experimentLogicType>([
actions.refreshExperimentResults(true, 'config_change')
},
endExperiment: async () => {
+ actions.setEndExperimentLoading(true)
try {
const response: Experiment = await api.create(
`/api/projects/${values.currentProjectId}/experiments/${values.experimentId}/end`,
@@ -1456,6 +1464,8 @@ export const experimentLogic = kea<experimentLogicType>([
refreshTreeItem('experiment', String(values.experimentId))
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to end experiment')
+ } finally {
+ actions.setEndExperimentLoading(false)
}
},
endExperimentWithoutShipping: async () => {
@@ -1706,6 +1716,7 @@ export const experimentLogic = kea<experimentLogicType>([
}
},
finishExperiment: async ({ selectedVariantKey, releaseToEveryone }) => {
+ actions.setEndExperimentLoading(true)
try {
const response: Experiment = await api.create(
`/api/projects/${values.currentProjectId}/experiments/${values.experimentId}/ship_variant`,
@@ -1746,6 +1757,8 @@ export const experimentLogic = kea<experimentLogicType>([
} else {
lemonToast.error(error.detail || 'Failed to ship variant')
}
+ } finally {
+ actions.setEndExperimentLoading(false)
}
},
updateExperimentVariantImages: async ({ variantPreviewMediaIds }) => {