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(surveys): allow deleting notifications from the list (#58625)

Lucas Faria77b7e24a6ae3
diff --git a/frontend/src/scenes/surveys/components/ConfirmDeleteButton.tsx b/frontend/src/scenes/surveys/components/ConfirmDeleteButton.tsx
new file mode 100644
index 000000000000..bacccadf484b
--- /dev/null
+++ b/frontend/src/scenes/surveys/components/ConfirmDeleteButton.tsx
@@ -0,0 +1,68 @@
+import { AnimatePresence, useReducedMotion } from 'motion/react'
+import * as motion from 'motion/react-client'
+import { useEffect, useState } from 'react'
+
+import { IconTrash } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+const CONFIRM_RESET_MS = 5000
+
+interface ConfirmDeleteButtonProps {
+ onDelete: () => void
+ idleLabel?: string
+ confirmLabel?: string
+ 'data-attr'?: string
+}
+
+export function ConfirmDeleteButton({
+ onDelete,
+ idleLabel = 'Delete',
+ confirmLabel = 'Confirm?',
+ 'data-attr': dataAttr,
+}: ConfirmDeleteButtonProps): JSX.Element {
+ const [armed, setArmed] = useState(false)
+ const reduceMotion = useReducedMotion()
+
+ useEffect(() => {
+ if (!armed) {
+ return
+ }
+ const handle = window.setTimeout(() => setArmed(false), CONFIRM_RESET_MS)
+ return () => window.clearTimeout(handle)
+ }, [armed])
+
+ const handleClick = (): void => {
+ if (armed) {
+ onDelete()
+ setArmed(false)
+ } else {
+ setArmed(true)
+ }
+ }
+
+ return (
+ <div className="relative isolate inline-block overflow-hidden rounded">
+ <AnimatePresence>
+ {armed && (
+ <motion.div
+ key="timer"
+ className="absolute inset-0 -z-10 bg-danger/20 origin-left"
+ initial={{ scaleX: 1 }}
+ animate={{ scaleX: reduceMotion ? 1 : 0 }}
+ exit={{ opacity: 0, transition: { duration: reduceMotion ? 0 : 0.12 } }}
+ transition={{
+ duration: reduceMotion ? 0 : CONFIRM_RESET_MS / 1000,
+ ease: 'linear',
+ }}
+ />
+ )}
+ </AnimatePresence>
+ <LemonButton size="small" icon={<IconTrash />} status="danger" data-attr={dataAttr} onClick={handleClick}>
+ {armed ? confirmLabel : idleLabel}
+ </LemonButton>
+ <span aria-live="polite" aria-atomic="true" className="sr-only">
+ {armed ? `Press again within ${CONFIRM_RESET_MS / 1000} seconds to confirm deletion.` : ''}
+ </span>
+ </div>
+ )
+}
diff --git a/frontend/src/scenes/surveys/components/SurveyNotifications.tsx b/frontend/src/scenes/surveys/components/SurveyNotifications.tsx
index 3097501921bb..4b0cfd856252 100644
--- a/frontend/src/scenes/surveys/components/SurveyNotifications.tsx
+++ b/frontend/src/scenes/surveys/components/SurveyNotifications.tsx
@@ -6,6 +6,7 @@ import { LemonButton, LemonMenu, LemonSkeleton, LemonSwitch } from '@posthog/lem
import { MailHog } from 'lib/components/hedgehogs'
import { LemonMenuItems } from 'lib/lemon-ui/LemonMenu/LemonMenu'
import { HogFunctionIcon } from 'scenes/hog-functions/configuration/HogFunctionIcon'
+import { ConfirmDeleteButton } from 'scenes/surveys/components/ConfirmDeleteButton'
import { NEW_SURVEY } from 'scenes/surveys/constants'
import { surveyLogic } from 'scenes/surveys/surveyLogic'
import { surveyNotificationModalLogic } from 'scenes/surveys/surveyNotificationModalLogic'
@@ -53,7 +54,7 @@ export function SurveyNotifications({
reusableSurveyNotifications,
reusableSurveyNotificationsLoading,
} = useValues(logic)
- const { toggleSurveyNotificationEnabled } = useActions(logic)
+ const { toggleSurveyNotificationEnabled, deleteSurveyNotification } = useActions(logic)
const { openDialog } = useActions(notificationModalLogic)
const isUnsavedSurvey = survey.id === NEW_SURVEY.id
@@ -220,7 +221,10 @@ export function SurveyNotifications({
<LemonSwitch
checked={fn.enabled}
onChange={() => toggleSurveyNotificationEnabled(fn.id, !fn.enabled)}
- size="small"
+ />
+ <ConfirmDeleteButton
+ onDelete={() => deleteSurveyNotification(fn)}
+ data-attr="survey-notification-tab-delete"
/>
</div>
)
diff --git a/frontend/src/scenes/surveys/components/SurveyNotificationsList.tsx b/frontend/src/scenes/surveys/components/SurveyNotificationsList.tsx
index 71a70b01a15b..d26798e29e4e 100644
--- a/frontend/src/scenes/surveys/components/SurveyNotificationsList.tsx
+++ b/frontend/src/scenes/surveys/components/SurveyNotificationsList.tsx
@@ -6,6 +6,7 @@ import { LemonBanner, LemonButton, LemonDropdown, LemonInput, LemonSkeleton, Lem
import { MailHog } from 'lib/components/hedgehogs'
import { HogFunctionIcon } from 'scenes/hog-functions/configuration/HogFunctionIcon'
+import { ConfirmDeleteButton } from 'scenes/surveys/components/ConfirmDeleteButton'
import {
getSurveyIdsFromNotificationFilters,
surveyNotificationsListLogic,
@@ -128,7 +129,8 @@ function NewNotificationButton({
export function SurveyNotificationsList(): JSX.Element {
const { notifications, notificationsLoading, notificationsFailed, knownSurveysFailed } =
useValues(surveyNotificationsListLogic)
- const { toggleNotificationEnabled, loadNotifications, loadKnownSurveys } = useActions(surveyNotificationsListLogic)
+ const { toggleNotificationEnabled, deleteNotification, loadNotifications, loadKnownSurveys } =
+ useActions(surveyNotificationsListLogic)
const { push } = useActions(router)
if (notificationsLoading) {
@@ -235,7 +237,10 @@ export function SurveyNotificationsList(): JSX.Element {
<LemonSwitch
checked={fn.enabled}
onChange={() => toggleNotificationEnabled(fn.id, !fn.enabled)}
- size="small"
+ />
+ <ConfirmDeleteButton
+ onDelete={() => deleteNotification(fn)}
+ data-attr="survey-notification-list-delete"
/>
</div>
)
diff --git a/frontend/src/scenes/surveys/surveyLogic.tsx b/frontend/src/scenes/surveys/surveyLogic.tsx
index ffa667ce7b92..6cc5a3aa9cda 100644
--- a/frontend/src/scenes/surveys/surveyLogic.tsx
+++ b/frontend/src/scenes/surveys/surveyLogic.tsx
@@ -12,8 +12,10 @@ import { FEATURE_FLAGS } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { FeatureFlagsSet, featureFlagLogic as enabledFlagLogic } from 'lib/logic/featureFlagLogic'
import { allOperatorsMapping, hasFormErrors, isObject, objectClean } from 'lib/utils'
+import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { maxGlobalLogic } from 'scenes/max/maxGlobalLogic'
+import { projectLogic } from 'scenes/projectLogic'
import { Scene } from 'scenes/sceneTypes'
import {
branchingConfigToDropdownValue,
@@ -705,6 +707,7 @@ export const surveyLogic = kea<surveyLogicType>([
notificationId,
enabled,
}),
+ deleteSurveyNotification: (notification: HogFunctionType) => ({ notification }),
setPersonNames: (personNames: Record<string, string>) => ({ personNames }),
generateTranslationDrafts: (language: string, overwrite: boolean = true) => ({ language, overwrite }),
setGeneratingTranslationDrafts: (generating: boolean) => ({ generating }),
@@ -1495,6 +1498,28 @@ export const surveyLogic = kea<surveyLogicType>([
})
}
},
+ deleteSurveyNotification: async ({ notification }) => {
+ const previous = values.surveyNotifications
+ // Optimistically remove the row; restore on undo or on a swallowed API error.
+ actions.loadSurveyNotificationsSuccess(previous.filter((n) => n.id !== notification.id))
+
+ let callbackFired = false
+ await deleteWithUndo({
+ endpoint: `projects/${projectLogic.values.currentProjectId}/hog_functions`,
+ object: { id: notification.id, name: notification.name },
+ callback: (undo) => {
+ callbackFired = true
+ if (undo) {
+ actions.loadSurveyNotifications()
+ }
+ },
+ })
+
+ if (!callbackFired) {
+ // deleteWithUndo swallows API errors and only fires the callback on success.
+ actions.loadSurveyNotificationsSuccess(previous)
+ }
+ },
}
}),
events(({ cache }) => ({
diff --git a/frontend/src/scenes/surveys/surveyNotificationsListLogic.ts b/frontend/src/scenes/surveys/surveyNotificationsListLogic.ts
index f04f1a5956c5..d7e29037d2e0 100644
--- a/frontend/src/scenes/surveys/surveyNotificationsListLogic.ts
+++ b/frontend/src/scenes/surveys/surveyNotificationsListLogic.ts
@@ -1,10 +1,12 @@
-import { actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
+import { actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
+import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
+import { projectLogic } from 'scenes/projectLogic'
import { CyclotronJobFiltersType, HogFunctionType, Survey, SurveyEventName, SurveyEventProperties } from '~/types'
@@ -42,8 +44,12 @@ export type KnownSurvey = Pick<Survey, 'id' | 'name' | 'archived' | 'created_at'
export const surveyNotificationsListLogic = kea<surveyNotificationsListLogicType>([
path(['scenes', 'surveys', 'surveyNotificationsListLogic']),
+ connect(() => ({
+ values: [projectLogic, ['currentProjectId']],
+ })),
actions({
toggleNotificationEnabled: (notificationId: string, enabled: boolean) => ({ notificationId, enabled }),
+ deleteNotification: (notification: HogFunctionType) => ({ notification }),
setSurveyPickerSearch: (search: string) => ({ search }),
setSurveyPickerVisible: (visible: boolean) => ({ visible }),
}),
@@ -203,6 +209,28 @@ export const surveyNotificationsListLogic = kea<surveyNotificationsListLogicType
})
}
},
+ deleteNotification: async ({ notification }) => {
+ const previous = values.allNotifications
+ // Optimistically remove the row; restore on undo or on a swallowed API error.
+ actions.loadNotificationsSuccess(previous.filter((n) => n.id !== notification.id))
+
+ let callbackFired = false
+ await deleteWithUndo({
+ endpoint: `projects/${values.currentProjectId}/hog_functions`,
+ object: { id: notification.id, name: notification.name },
+ callback: (undo) => {
+ callbackFired = true
+ if (undo) {
+ actions.loadNotifications()
+ }
+ },
+ })
+
+ if (!callbackFired) {
+ // deleteWithUndo swallows API errors and only fires the callback on success.
+ actions.loadNotificationsSuccess(previous)
+ }
+ },
})),
afterMount(({ actions }) => {
actions.loadNotifications()