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(workflows): keep send later chosen before a broadcast's first save (#124828)

Daniel Marchuk01e16bf3bc18
diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
index 12a7f833d91c..08c552c097ee 100644
--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.test.ts
@@ -624,7 +624,20 @@ describe('broadcastWizardLogic', () => {
expect(router.values.location.pathname).toContain('/workflows/broadcast-1/workflow')
})
- it('creates the draft and stores its send time when the workflow editor opens before any Continue', async () => {
+ it.each([
+ {
+ firstSave: 'the workflow editor opens',
+ step: 'review' as const,
+ save: (): void => logic.actions.openInWorkflowEditorConfirmed(),
+ path: '/workflows/broadcast-1/workflow',
+ },
+ {
+ firstSave: 'Continue saves the schedule step',
+ step: 'schedule' as const,
+ save: (): void => logic.actions.continueStep(),
+ path: '/broadcasts/broadcast-1',
+ },
+ ])('creates the draft and stores its send time when $firstSave before any other save', async (testCase) => {
const createdSchedules: Record<string, any>[] = []
useMocks({
post: {
@@ -639,17 +652,13 @@ describe('broadcastWizardLogic', () => {
router.actions.push('/broadcasts/new')
logic.actions.setScheduleMode('later')
logic.actions.setSendAt(sendAt)
- logic.actions.setStep('review')
+ logic.actions.setStep(testCase.step)
releaseCreate()
- await expectLogic(logic, () => {
- logic.actions.openInWorkflowEditorConfirmed()
- })
- .toDispatchActions(['saveBroadcastFinished'])
- .toFinishAllListeners()
+ await expectLogic(logic, testCase.save).toDispatchActions(['saveBroadcastFinished']).toFinishAllListeners()
expect(createdSchedules).toEqual([expect.objectContaining({ starts_at: sendAt, rrule: expect.any(String) })])
- expect(router.values.location.pathname).toContain('/workflows/broadcast-1/workflow')
+ expect(router.values.location.pathname).toContain(testCase.path)
})
it('keeps an email edit made while Continue is saving and moves to the draft URL after it saves', async () => {
diff --git a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
index 27a46c677390..1525a454c74c 100644
--- a/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
+++ b/products/workflows/frontend/Broadcasts/broadcastWizardLogic.ts
@@ -1915,7 +1915,9 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
const projectId = String(values.currentProjectId)
try {
await saves.run(async () => {
- actions.draftAutosaved(await createDraft(projectId, values))
+ const created = await createDraft(projectId, values)
+ actions.draftAutosaved(created)
+ actions.draftAutosaved(await saveDraftSchedule(projectId, created, values))
})
actions.showSavedDraftUrl()
} catch (error: any) {
@@ -2169,11 +2171,16 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
const projectId = String(values.currentProjectId)
let savedEditGeneration: number | undefined
try {
- const saved = await saves.run(() => {
+ const saved = await saves.run(async () => {
savedEditGeneration = cache.emailEditGeneration
- return values.broadcastId
- ? saveWithoutClobbering(projectId, values.broadcastId, values)
- : createDraft(projectId, values)
+ const draft = values.broadcastId
+ ? await saveWithoutClobbering(projectId, values.broadcastId, values)
+ : await createDraft(projectId, values)
+ // Store the draft before its schedule, so a failed schedule save can't make the next Continue
+ // create a second draft. The draft URL reloads the wizard from the server, which forgets
+ // a send time that only the page held.
+ actions.draftAutosaved(draft)
+ return await saveDraftSchedule(projectId, draft, values)
})
actions.saveBroadcastFinished(saved)
// This save carried the email edits made before it started. A later one keeps its own autosave.
@@ -2448,8 +2455,9 @@ export const broadcastWizardLogic = kea<broadcastWizardLogicType>([
actions.draftAutosaved(saved)
} while (cache.emailEditGeneration !== savedEditGeneration)
cache.emailEditPending = false
- await saveDraftSchedule(projectId, broadcastId, values)
- actions.saveBroadcastFinished(values.broadcast)
+ actions.saveBroadcastFinished(
+ await saves.run(() => saveDraftSchedule(projectId, values.broadcast!, values))
+ )
} catch (error: any) {
actions.saveBroadcastFinished(null)
if (error instanceof EditedElsewhereError) {
@@ -2662,16 +2670,19 @@ function wizardSchedule(values: broadcastWizardLogicType['values']): Partial<Hog
}
}
-/** Stores the timing picked in the wizard on the draft, so the workflow editor opens with it. The scheduler skips drafts. */
+/**
+ * Stores the timing picked in the wizard on the draft, so the workflow editor and a reload open with it.
+ * The scheduler skips drafts. Returns the draft with the schedules it now has.
+ */
async function saveDraftSchedule(
projectId: string,
- broadcastId: string,
+ broadcast: HogFlowApi,
values: broadcastWizardLogicType['values']
-): Promise<void> {
+): Promise<HogFlowApi> {
if (values.stepValidationErrors.schedule.length > 0) {
- return
+ return broadcast
}
- const existing = values.broadcast?.schedules ?? []
+ const existing = broadcast.schedules ?? []
const wanted = values.scheduleMode === 'now' ? null : wizardSchedule(values)
const unchanged = wanted
? existing.length === 1 &&
@@ -2680,14 +2691,13 @@ async function saveDraftSchedule(
existing[0].timezone === wanted.timezone
: existing.length === 0
if (unchanged) {
- return
- }
- if (wanted) {
- await hogFlowsSchedulesCreate(projectId, broadcastId, wanted as any)
+ return broadcast
}
+ const created = wanted ? await hogFlowsSchedulesCreate(projectId, broadcast.id, wanted as any) : null
for (const schedule of existing) {
- await hogFlowsSchedulesDestroy(projectId, broadcastId, schedule.id)
+ await hogFlowsSchedulesDestroy(projectId, broadcast.id, schedule.id)
}
+ return { ...broadcast, schedules: created ? [created] : [] }
}
function seedTeamUtmDefaults(