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.

feat(insights): send a broadcast from the cohort saved toast (#113011)

Daniel Marchuk57d705ac46a3
diff --git a/frontend/src/lib/components/ExportButton/exportsLogic.ts b/frontend/src/lib/components/ExportButton/exportsLogic.ts
index f40fcc209a75..592e70206d40 100644
--- a/frontend/src/lib/components/ExportButton/exportsLogic.ts
+++ b/frontend/src/lib/components/ExportButton/exportsLogic.ts
@@ -1,6 +1,5 @@
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
-import { router } from 'kea-router'
import posthog from 'posthog-js'
import api from 'lib/api'
@@ -18,8 +17,8 @@ import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { ToastButton } from 'lib/lemon-ui/LemonToast/LemonToast'
import { delay } from 'lib/utils/async'
import { uuid } from 'lib/utils/dom'
+import { cohortSavedToast } from 'scenes/cohorts/cohortSavedToast'
import type { SessionRecordingPlayerMode } from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'
-import { urls } from 'scenes/urls'
import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePanelStateLogic'
import { cohortsModel } from '~/models/cohortsModel'
@@ -389,16 +388,10 @@ export const exportsLogic = kea<exportsLogicType>([
name: name || 'Query cohort',
query: query,
})
- cohortsModel.actions.cohortCreated(cohort)
+ cohortsModel.findMounted()?.actions.cohortCreated(cohort)
await delay(500) // just in case the toast is too fast
lemonToast.dismiss(toastId)
- lemonToast.success('Cohort saved', {
- toastId: `${toastId}-success`,
- button: {
- label: 'View cohort',
- action: () => router.actions.push(urls.cohort(cohort.id)),
- },
- })
+ cohortSavedToast(cohort, 'query_results', `${toastId}-success`)
} catch {
lemonToast.dismiss(toastId)
lemonToast.error('Cohort save failed')
diff --git a/frontend/src/lib/components/SaveToCohortModalContent/saveToCohortModalContentLogic.ts b/frontend/src/lib/components/SaveToCohortModalContent/saveToCohortModalContentLogic.ts
index b61e714aadcb..33be0bb953b9 100644
--- a/frontend/src/lib/components/SaveToCohortModalContent/saveToCohortModalContentLogic.ts
+++ b/frontend/src/lib/components/SaveToCohortModalContent/saveToCohortModalContentLogic.ts
@@ -1,14 +1,13 @@
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
-import { router } from 'kea-router'
import { PaginationManual, lemonToast } from '@posthog/lemon-ui'
import api, { CountedPaginatedResponse } from 'lib/api'
import { delay } from 'lib/utils/async'
import { cohortEditLogic } from 'scenes/cohorts/cohortEditLogic'
+import { cohortSavedToast } from 'scenes/cohorts/cohortSavedToast'
import { projectLogic } from 'scenes/projectLogic'
-import { urls } from 'scenes/urls'
import { ActorsQuery } from '~/queries/schema/schema-general'
import { CohortType } from '~/types'
@@ -177,13 +176,7 @@ export const saveToCohortModalContentLogic = kea<saveToCohortModalContentLogicTy
await delay(500) // just in case the toast is too fast
lemonToast.dismiss(toastId)
- lemonToast.success('Cohort saved', {
- toastId: `${toastId}-success`,
- button: {
- label: 'View cohort',
- action: () => router.actions.push(urls.cohort(cohort.id)),
- },
- })
+ cohortSavedToast(cohort, 'query_results', `${toastId}-success`)
} catch (error) {
console.error('Save to cohort failed:', error)
lemonToast.dismiss(toastId)
diff --git a/frontend/src/lib/lemon-ui/LemonToast/LemonToast.tsx b/frontend/src/lib/lemon-ui/LemonToast/LemonToast.tsx
index 73a8fbcf8be5..7e729e1625c9 100644
--- a/frontend/src/lib/lemon-ui/LemonToast/LemonToast.tsx
+++ b/frontend/src/lib/lemon-ui/LemonToast/LemonToast.tsx
@@ -40,6 +40,11 @@ interface ToastOptionsWithButton<T = string> extends ToastOptions<T> {
hideButton?: boolean
}
+interface SuccessToastOptions<T = string> extends ToastOptionsWithButton<T> {
+ /** A second action, shown after `button` on a row under the message. */
+ secondaryButton?: ToastButton
+}
+
export const GET_HELP_BUTTON: ToastButton = {
label: 'Get help',
action: getHelp,
@@ -60,6 +65,7 @@ export interface ToastContentProps {
type: 'info' | 'success' | 'warning' | 'error'
message: string | JSX.Element
button?: ToastButton
+ secondaryButton?: ToastButton
id?: number | string
}
@@ -122,9 +128,22 @@ function ToastCopyButton({ getMessageText }: { getMessageText: () => string }):
)
}
-export function ToastContent({ type, message, button, id }: ToastContentProps): JSX.Element {
+export function ToastContent({ type, message, button, secondaryButton, id }: ToastContentProps): JSX.Element {
const messageRef = useRef<HTMLSpanElement>(null)
+ if (secondaryButton) {
+ // Two actions leave the message no room on one line, so they go on a row under it.
+ return (
+ <div className="flex flex-col gap-2" data-attr={`${type}-toast`}>
+ <span>{message}</span>
+ <div className="flex gap-2 [&_button]:!mx-0">
+ {button && <ToastActionButton button={button} toastId={id} />}
+ <ToastActionButton button={secondaryButton} toastId={id} />
+ </div>
+ </div>
+ )
+ }
+
return (
<div className="flex items-center" data-attr={`${type}-toast`}>
<span ref={messageRef} className="grow min-w-0 overflow-hidden text-ellipsis">
@@ -227,17 +246,26 @@ export const lemonToast = {
})
return id
},
- success(message: string | JSX.Element, { button, ...toastOptions }: ToastOptionsWithButton = {}) {
+ success(message: string | JSX.Element, { button, secondaryButton, ...toastOptions }: SuccessToastOptions = {}) {
const options = ensureToastId(toastOptions, 'success', message)
const id = options.toastId!
queueMicrotask(() => {
if (cancelledIds.delete(id)) {
return
}
- toast.success(<ToastContent type="success" message={message} button={button} id={id} />, {
- icon: successIcon(),
- ...options,
- })
+ toast.success(
+ <ToastContent
+ type="success"
+ message={message}
+ button={button}
+ secondaryButton={secondaryButton}
+ id={id}
+ />,
+ {
+ icon: successIcon(),
+ ...options,
+ }
+ )
})
return id
},
diff --git a/frontend/src/scenes/cohorts/cohortSavedToast.ts b/frontend/src/scenes/cohorts/cohortSavedToast.ts
new file mode 100644
index 000000000000..0a3afcf5ed5b
--- /dev/null
+++ b/frontend/src/scenes/cohorts/cohortSavedToast.ts
@@ -0,0 +1,34 @@
+import { router } from 'kea-router'
+
+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
+import { urls } from 'scenes/urls'
+
+import { CohortType } from '~/types'
+
+import {
+ captureMessageAudienceClicked,
+ cohortAudienceProperties,
+ messageAudienceUrl,
+} from 'products/workflows/frontend/MessageAudience/messageAudience'
+
+/** Confirms a static cohort saved from a list of people, with links to the cohort and to a broadcast for it. */
+export function cohortSavedToast(cohort: Pick<CohortType, 'id' | 'name'>, source: string, toastId?: string): void {
+ const savedCohort = { id: cohort.id as number, name: cohort.name }
+ lemonToast.success('Cohort saved', {
+ toastId,
+ button: {
+ label: 'View cohort',
+ action: () => router.actions.push(urls.cohort(savedCohort.id)),
+ },
+ secondaryButton: {
+ label: 'Send a broadcast',
+ dataAttr: `message-audience-${source}-broadcast`,
+ action: () => {
+ captureMessageAudienceClicked(source, 'broadcast')
+ router.actions.push(
+ messageAudienceUrl({ properties: cohortAudienceProperties(savedCohort), source }, 'broadcast')
+ )
+ },
+ },
+ })
+}
diff --git a/frontend/src/scenes/trends/persons-modal/personsModalLogic.ts b/frontend/src/scenes/trends/persons-modal/personsModalLogic.ts
index c72903ebbb8f..f3b04c50085d 100644
--- a/frontend/src/scenes/trends/persons-modal/personsModalLogic.ts
+++ b/frontend/src/scenes/trends/persons-modal/personsModalLogic.ts
@@ -12,13 +12,12 @@ import {
selectors,
} from 'kea'
import { loaders } from 'kea-loaders'
-import { combineUrl, router, urlToAction } from 'kea-router'
-
-import { lemonToast } from '@posthog/lemon-ui'
+import { combineUrl, urlToAction } from 'kea-router'
import api from 'lib/api'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { assignField, isGroupType, isSessionType } from 'lib/utils/guards'
+import { cohortSavedToast } from 'scenes/cohorts/cohortSavedToast'
import { cleanFilters } from 'scenes/insights/utils/cleanFilters'
import { sceneLogic } from 'scenes/sceneLogic'
import { teamLogic } from 'scenes/teamLogic'
@@ -545,13 +544,7 @@ export const personsModalLogic = kea<personsModalLogicType>([
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use cohortsCreate() from 'products/cohorts/frontend/generated/api' instead.
const cohort = await api.create('api/cohort', { ...cohortParams, query: values.actorsQuery })
cohortsModel.actions.cohortCreated(cohort)
- lemonToast.success('Cohort saved', {
- toastId: `cohort-saved-${cohort.id}`,
- button: {
- label: 'View cohort',
- action: () => router.actions.push(urls.cohort(cohort.id)),
- },
- })
+ cohortSavedToast(cohort, 'persons_modal', `cohort-saved-${cohort.id}`)
actions.setIsCohortModalOpen(false)
},
loadNextActors: () => {
diff --git a/products/product_analytics/frontend/insights/retention/retentionModalLogic.ts b/products/product_analytics/frontend/insights/retention/retentionModalLogic.ts
index 069a5f093d83..de002564ace3 100644
--- a/products/product_analytics/frontend/insights/retention/retentionModalLogic.ts
+++ b/products/product_analytics/frontend/insights/retention/retentionModalLogic.ts
@@ -1,13 +1,11 @@
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
-import { router } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
-import { lemonToast } from '@posthog/lemon-ui'
-
import api from 'lib/api'
import type { DataColorTheme } from 'lib/colors'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import type { FeatureFlagsSet } from 'lib/logic/featureFlagLogic'
+import { cohortSavedToast } from 'scenes/cohorts/cohortSavedToast'
import { AGGREGATION_LABEL_FOR_CUSTOM_DATA_WAREHOUSE } from 'scenes/insights/filters/aggregationTargetUtils'
import { insightVizDataLogic } from 'scenes/insights/insightVizDataLogic'
import { keyForInsightLogicProps } from 'scenes/insights/sharedUtils'
@@ -369,13 +367,7 @@ export const retentionModalLogic = kea<retentionModalLogicType>([
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use cohortsCreate() from 'products/cohorts/frontend/generated/api' instead.
const cohort = await api.create('api/cohort', { ...cohortParams, query: values.actorsQuery })
cohortsModel.actions.cohortCreated(cohort)
- lemonToast.success('Cohort saved', {
- toastId: `cohort-saved-${cohort.id}`,
- button: {
- label: 'View cohort',
- action: () => router.actions.push(urls.cohort(cohort.id)),
- },
- })
+ cohortSavedToast(cohort, 'retention_modal', `cohort-saved-${cohort.id}`)
actions.setIsCohortModalOpen(false)
},
})),