: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(customer-analytics): confirm announcement sends (#78382)
Christophe Eynius Tranberg199b8d375bfc
diff --git a/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.test.tsx b/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.test.tsxnew file mode 100644index 000000000000..1a210a212d85--- /dev/null+++ b/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.test.tsx@@ -0,0 +1,84 @@+import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'++import '@testing-library/jest-dom'++import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'+import { Provider } from 'kea'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { announcementsLogic } from './announcementsLogic'+import { AnnouncementsTabContent } from './AnnouncementsTabContent'++jest.mock('~/queries/query', () => ({ performQuery: jest.fn() }))++describe('AnnouncementsTabContent', () => {+ let logic: ReturnType<typeof announcementsLogic.build>+ let postedBodies: any[]++ beforeEach(() => {+ postedBodies = []+ useMocks({+ get: {+ '/api/projects/:team_id/announcements/': { results: [], count: 0 },+ '/api/projects/:team_id/announcements/channels/': [+ { id: 'C1', name: 'acme', is_member: true, customer_name: 'Acme' },+ { id: 'C2', name: 'globex', is_member: true, customer_name: 'Globex' },+ ],+ },+ post: {+ '/api/projects/:team_id/announcements/': async ({ request }) => {+ postedBodies.push(await request.json())+ return [200, { results: [], count: 0 }]+ },+ },+ })+ initKeaTests(true, { ...MOCK_DEFAULT_TEAM, conversations_settings: { slack_enabled: true } })+ logic = announcementsLogic()+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ cleanup()+ })++ function renderComposer(): void {+ logic.actions.setMessage('Offsite this week')+ logic.actions.setSelectedChannelIds(['C1', 'C2'])+ render(+ <Provider>+ <AnnouncementsTabContent />+ </Provider>+ )+ }++ function clickSend(): void {+ fireEvent.click(document.querySelector('[data-attr="send-announcement"]')!)+ }++ // Sending posts to customer channels and can't be undone, so the send button must+ // never reach the API on its own — it has to go through the confirmation first.+ it('asks for confirmation instead of sending, showing the channel count and message', async () => {+ renderComposer()++ clickSend()++ await waitFor(() => expect(screen.getByText('Send this announcement to 2 channels?')).toBeInTheDocument())+ expect(screen.getByText('Acme (#acme)')).toBeInTheDocument()+ expect(screen.getByText('Globex (#globex)')).toBeInTheDocument()+ expect(screen.getAllByText('Offsite this week').length).toBeGreaterThan(0)+ expect(postedBodies).toEqual([])+ })++ it('sends once the confirmation is accepted', async () => {+ renderComposer()++ clickSend()+ await waitFor(() => expect(document.querySelector('[data-attr="confirm-send-announcement"]')).not.toBeNull())+ fireEvent.click(document.querySelector('[data-attr="confirm-send-announcement"]')!)++ await waitFor(() => expect(postedBodies).toEqual([{ message: 'Offsite this week', channels: ['C1', 'C2'] }]))+ })+})diff --git a/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.tsx b/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.tsxindex 584a9771b7f5..a258477da748 100644--- a/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.tsx+++ b/products/customer_analytics/frontend/components/Announcements/AnnouncementsTabContent.tsx@@ -3,7 +3,9 @@ import { useActions, useValues } from 'kea'import {LemonBanner,LemonButton,+ LemonDialog,LemonInputSelect,+ LemonSnack,LemonTable,LemonTableColumns,LemonTag,@@ -12,6 +14,7 @@ import {} from '@posthog/lemon-ui'import { TZLabel } from 'lib/components/TZLabel'+import { pluralize } from 'lib/utils/strings'import { urls } from 'scenes/urls'import type { AnnouncementApi, AnnouncementDeliveryApi } from '../../generated/api.schemas'@@ -35,10 +38,30 @@ function announcementStatusTag(status: AnnouncementApi['status']): { type: TagTy}}+function ConfirmSendContent({ message, channelLabels }: { message: string; channelLabels: string[] }): JSX.Element {+ return (+ <div className="flex flex-col gap-2 max-w-[500px]">+ <p className="m-0">+ This message will be posted to {pluralize(channelLabels.length, 'customer channel')}. Sending cannot be+ undone.+ </p>+ <div className="border rounded p-2 bg-surface-secondary whitespace-pre-wrap max-h-40 overflow-y-auto">+ {message}+ </div>+ <div className="flex flex-wrap gap-1 max-h-32 overflow-y-auto">+ {channelLabels.map((label) => (+ <LemonSnack key={label}>{label}</LemonSnack>+ ))}+ </div>+ </div>+ )+}+function AnnouncementComposer(): JSX.Element {const {messageDraft,selectedChannelIds,+ selectedChannelLabels,channelOptions,memberChannelsLoading,submitting,@@ -46,6 +69,21 @@ function AnnouncementComposer(): JSX.Element {} = useValues(announcementsLogic)const { setMessage, setSelectedChannelIds, submitAnnouncement, loadMemberChannels } = useActions(announcementsLogic)+ const confirmSend = (): void => {+ LemonDialog.open({+ title: `Send this announcement to ${pluralize(selectedChannelLabels.length, 'channel')}?`,+ content: <ConfirmSendContent message={messageDraft.trim()} channelLabels={selectedChannelLabels} />,+ primaryButton: {+ children: 'Send',+ onClick: submitAnnouncement,+ 'data-attr': 'confirm-send-announcement',+ },+ secondaryButton: {+ children: 'Cancel',+ },+ })+ }+return (<div className="flex flex-col gap-2 max-w-[800px]"><LemonTextArea@@ -77,7 +115,7 @@ function AnnouncementComposer(): JSX.Element {<div><LemonButtontype="primary"- onClick={submitAnnouncement}+ onClick={confirmSend}loading={submitting}disabledReason={submitDisabledReason}data-attr="send-announcement"diff --git a/products/customer_analytics/frontend/components/Announcements/announcementsLogic.ts b/products/customer_analytics/frontend/components/Announcements/announcementsLogic.tsindex af1d1cfce3e7..226fc76e1ace 100644--- a/products/customer_analytics/frontend/components/Announcements/announcementsLogic.ts+++ b/products/customer_analytics/frontend/components/Announcements/announcementsLogic.ts@@ -54,6 +54,7 @@ export interface announcementsLogicValues {memberChannelsLoading: booleanmessageDraft: stringselectedChannelIds: string[]+ selectedChannelLabels: string[]slackConnected: booleansubmitDisabledReason: string | undefinedsubmitting: boolean@@ -185,6 +186,7 @@ export interface announcementsLogicMeta {key: stringlabel: string}[]+ selectedChannelLabels: (memberChannels: AnnouncementChannelApi[], selectedChannelIds: string[]) => string[]submitDisabledReason: (messageDraft: string,selectedChannelIds: string[],@@ -358,6 +360,15 @@ export const announcementsLogic = kea<announcementsLogicType>([return channels.map((channel) => ({ key: channel.id, label: channelLabel(channel) }))},],+ // Labels for the confirmation dialog. Falls back to the raw id so a channel the+ // picker no longer knows about still shows up in the list the user confirms.+ selectedChannelLabels: [+ (s) => [s.memberChannels, s.selectedChannelIds],+ (memberChannels: AnnouncementChannelApi[], selectedChannelIds: string[]): string[] => {+ const labelsById = new Map(memberChannels.map((channel) => [channel.id, channelLabel(channel)]))+ return selectedChannelIds.map((id) => labelsById.get(id) ?? id)+ },+ ],submitDisabledReason: [(s) => [s.messageDraft, s.selectedChannelIds, s.submitting],(messageDraft: string, selectedChannelIds: string[], submitting: boolean): string | undefined => {