: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
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
import { useEffect, useRef, useState } from 'react'
import { IconChevronDown, IconChevronRight, IconQuestion } from '@posthog/icons'
import {
LemonBanner,
LemonButton,
LemonCheckbox,
LemonInput,
LemonSwitch,
LemonTag,
Spinner,
Tooltip,
} from '@posthog/lemon-ui'
import { organizationLogic } from 'scenes/organizationLogic'
import { userLogic } from 'scenes/userLogic'
import { AvailableFeature, NotificationSettings, OrganizationBasicType, TeamBasicType } from '~/types'
import { LOCKED_BY_ORGANIZATION, lockedValueFor } from '../shared/notificationLocks'
import { PIPELINE_KIND_LABELS, pipelineNotificationsLogic } from './pipelineNotificationsLogic'
enum NotificationBlock {
Security = 'security',
WeeklyDigest = 'weekly-digest',
MemberJoin = 'member-join',
DataPipelineErrors = 'data-pipeline-errors',
IssueAssigned = 'issue-assigned',
EtWeeklyDigest = 'et-weekly-digest',
WaWeeklyDigest = 'wa-weekly-digest',
DataCatalogWeeklyDigest = 'data-catalog-weekly-digest',
CommentMentions = 'comment-mentions',
Approvals = 'approvals',
ApiKeyExposure = 'api-key-exposure',
MaterializedViewSync = 'materialized-view-sync',
AiEvaluationDisabled = 'ai-evaluation-disabled',
WorkflowSuggestions = 'workflow-suggestions',
}
const NOTIFICATION_BLOCK_ORDER = Object.values(NotificationBlock)
type BooleanNotificationSettings = Omit<
NotificationSettings,
| 'project_weekly_digest_disabled'
| 'error_tracking_weekly_digest_project_enabled'
| 'web_analytics_weekly_digest_project_enabled'
| 'organization_member_join_email_disabled'
>
const NOTIFICATION_DEFAULTS: BooleanNotificationSettings = {
plugin_disabled: true,
error_tracking_issue_assigned: true,
error_tracking_weekly_digest: true,
discussions_mentioned: true,
all_weekly_digest_disabled: false,
project_api_key_exposed: true,
ai_evaluation_disabled: true,
workflow_suggestions: true,
materialized_view_sync_failed: false,
materialized_view_sync_failed_daily: true,
materialized_view_sync_failed_immediate: false,
web_analytics_weekly_digest: true,
data_catalog_weekly_digest: true,
approval_requested: true,
approval_resolved: true,
}
function ProjectDigestSelector({
keyPrefix,
dataAttrPrefix,
setting,
inverse = false,
isTeamDisabled,
onToggleTeam,
onToggleAllTeams,
hint,
}: {
keyPrefix: string
dataAttrPrefix: string
/** The setting an organization admin can enforce per project. */
setting: string
/** True when the stored value means "off", so an enforced value reads inverted. */
inverse?: boolean
isTeamDisabled: (teamId: number) => boolean
onToggleTeam: (teamId: number, enabled: boolean) => void
onToggleAllTeams: (teamIds: number[], enabled: boolean) => void
hint?: string
}): JSX.Element {
const { user, userLoading } = useValues(userLogic)
const { currentOrganization } = useValues(organizationLogic)
const [expanded, setExpanded] = useState(true)
const teams = currentOrganization?.teams ?? []
const ownTeamIds = teams
.filter((team: TeamBasicType) => lockedValueFor(user?.notification_locks, setting, team.id) === null)
.map((team: TeamBasicType) => team.id)
return (
<div>
<LemonButton
icon={expanded ? <IconChevronDown /> : <IconChevronRight />}
onClick={() => setExpanded(!expanded)}
size="small"
type="tertiary"
className="p-0"
>
Select projects ({currentOrganization?.teams?.length || 0} available)
</LemonButton>
{expanded && (
<div className="mt-3 ml-6 space-y-2">
{hint && <span className="text-muted text-xs">{hint}</span>}
<div className="flex flex-col gap-2">
<div className="flex flex-row items-center gap-4">
<LemonButton
size="xsmall"
type="secondary"
onClick={() => onToggleAllTeams(ownTeamIds, true)}
disabledReason={ownTeamIds.length === 0 ? LOCKED_BY_ORGANIZATION : undefined}
>
Enable for all projects
</LemonButton>
<LemonButton
size="xsmall"
type="secondary"
onClick={() => onToggleAllTeams(ownTeamIds, false)}
disabledReason={ownTeamIds.length === 0 ? LOCKED_BY_ORGANIZATION : undefined}
>
Disable for all projects
</LemonButton>
</div>
{teams.map((team: TeamBasicType) => {
const enforced = lockedValueFor(user?.notification_locks, setting, team.id)
return (
<LemonCheckbox
key={`${keyPrefix}-${team.id}`}
id={`${keyPrefix}-${team.id}`}
data-attr={`${dataAttrPrefix}_${team.id}`}
onChange={(checked) => onToggleTeam(team.id, checked)}
checked={
enforced !== null ? (inverse ? !enforced : enforced) : !isTeamDisabled(team.id)
}
disabledReason={
enforced !== null
? LOCKED_BY_ORGANIZATION
: userLoading
? 'Loading...'
: undefined
}
label={
<div className="flex items-center gap-2">
<span>{team.name}</span>
<LemonTag type="muted">id: {team.id.toString()}</LemonTag>
{enforced !== null && (
<LemonTag type="highlight">Set by your admin</LemonTag>
)}
</div>
}
/>
)
})}
</div>
</div>
)}
</div>
)
}
function OrganizationMemberJoinSelector(): JSX.Element {
const { user, userLoading } = useValues(userLogic)
const { updateMemberJoinEmailForOrganization, updateMemberJoinEmailForAllOrganizations } = useActions(userLogic)
const [expanded, setExpanded] = useState(true)
const organizations = [...(user?.organizations || [])].sort((a, b) => a.name.localeCompare(b.name))
const isOrgDisabled = (orgId: string): boolean =>
!!user?.notification_settings?.organization_member_join_email_disabled?.[orgId]
const enforcedFor = (orgId: string): boolean | null =>
lockedValueFor(user?.notification_locks, 'organization_member_join_email_disabled', orgId)
const ownOrgIds = organizations.filter((o: OrganizationBasicType) => enforcedFor(o.id) === null).map((o) => o.id)
return (
<div>
<LemonButton
icon={expanded ? <IconChevronDown /> : <IconChevronRight />}
onClick={() => setExpanded(!expanded)}
size="small"
type="tertiary"
className="p-0"
>
Select organizations ({organizations.length} available)
</LemonButton>
{expanded && (
<div className="mt-3 ml-6 space-y-2">
<span className="text-muted text-xs">
You receive these emails by default for every organization you belong to. Turn off any
organization you do not want them for.
</span>
<div className="flex flex-col gap-2">
<div className="flex flex-row items-center gap-4">
<LemonButton
size="xsmall"
type="secondary"
onClick={() => updateMemberJoinEmailForAllOrganizations(ownOrgIds, true)}
disabledReason={ownOrgIds.length === 0 ? LOCKED_BY_ORGANIZATION : undefined}
>
Enable for all organizations
</LemonButton>
<LemonButton
size="xsmall"
type="secondary"
onClick={() => updateMemberJoinEmailForAllOrganizations(ownOrgIds, false)}
disabledReason={ownOrgIds.length === 0 ? LOCKED_BY_ORGANIZATION : undefined}
>
Disable for all organizations
</LemonButton>
</div>
{organizations.map((org) => {
const enforced = enforcedFor(org.id)
return (
<LemonCheckbox
key={`member-join-org-${org.id}`}
id={`member-join-org-${org.id}`}
data-attr={`member_join_email_org_${org.id}`}
onChange={(checked) => updateMemberJoinEmailForOrganization(org.id, checked)}
checked={enforced !== null ? !enforced : !isOrgDisabled(org.id)}
disabledReason={
enforced !== null
? LOCKED_BY_ORGANIZATION
: userLoading
? 'Loading...'
: undefined
}
label={
<span className="flex items-center gap-2">
{org.name}
{enforced !== null && (
<LemonTag type="highlight">Set by your admin</LemonTag>
)}
</span>
}
/>
)
})}
</div>
</div>
)}
</div>
)
}
function PipelineNotificationSelector(): JSX.Element {
const { user, userLoading } = useValues(userLogic)
const { updatePipelineNotification, updatePipelineNotificationForAll } = useActions(userLogic)
const { pipelines, pipelinesLoading, pipelinesByTeam, allPipelineIds, isPipelineDisabled } =
useValues(pipelineNotificationsLogic)
const [expanded, setExpanded] = useState(false)
return (
<div>
<LemonButton
icon={expanded ? <IconChevronDown /> : <IconChevronRight />}
onClick={() => setExpanded(!expanded)}
size="small"
type="tertiary"
className="p-0"
>
Select pipelines to receive notifications for
</LemonButton>
{expanded && (
<div className="mt-3 ml-6 space-y-2">
<p className="text-muted text-xs">
All pipelines are enabled by default. Uncheck any pipeline you no longer want notifications for.
</p>
{pipelinesLoading ? (
<div className="flex items-center gap-2 py-2">
<Spinner className="text-lg" />
<span className="text-muted text-sm">Loading pipelines...</span>
</div>
) : pipelines.length === 0 ? (
<p className="text-muted text-sm">No pipelines found in your projects.</p>
) : (
<div className="flex flex-col gap-2">
<div className="flex flex-row items-center gap-4">
<LemonButton
size="xsmall"
type="secondary"
onClick={() => updatePipelineNotificationForAll(allPipelineIds, true)}
>
Enable all notifications
</LemonButton>
<LemonButton
size="xsmall"
type="secondary"
onClick={() => updatePipelineNotificationForAll(allPipelineIds, false)}
>
Mute all notifications
</LemonButton>
</div>
{Object.entries(pipelinesByTeam).map(([key, { teamId, teamName, items }]) => {
const enforced = lockedValueFor(
user?.notification_locks,
'pipeline_notifications_disabled',
teamId
)
return (
<div key={key} className="flex flex-col gap-2">
<div className="flex items-center gap-2">
<span className="font-medium">{teamName}</span>
<LemonTag type="muted">id: {teamId}</LemonTag>
{enforced !== null && (
<LemonTag type="highlight">Set by your admin</LemonTag>
)}
</div>
<div className="ml-4 flex flex-col gap-1">
{items.map((pipeline) => (
<LemonCheckbox
key={pipeline.id}
id={`pipeline-${pipeline.id}`}
data-attr={`pipeline_notification_${pipeline.id}`}
onChange={(checked) =>
updatePipelineNotification(pipeline.id, checked)
}
checked={
enforced !== null ? !enforced : !isPipelineDisabled(pipeline.id)
}
disabledReason={
enforced !== null
? LOCKED_BY_ORGANIZATION
: userLoading
? 'Loading...'
: undefined
}
label={
<div className="flex items-center gap-2">
<span>{pipeline.name}</span>
<LemonTag type="default">
{PIPELINE_KIND_LABELS[pipeline.kind]}
</LemonTag>
</div>
}
/>
))}
</div>
</div>
)
})}
</div>
)}
</div>
)}
</div>
)
}
export function UpdateEmailPreferences(): JSX.Element {
const { user, userLoading, hasAvailableFeature } = useValues(userLogic)
const {
updateWeeklyDigestForTeam,
updateWeeklyDigestForAllTeams,
updateETWeeklyDigestForTeam,
updateETWeeklyDigestForAllTeams,
updateWAWeeklyDigestForTeam,
updateWAWeeklyDigestForAllTeams,
updateDataPipelineErrorThreshold,
} = useActions(userLogic)
const { currentOrganization, currentOrganizationLoading } = useValues(organizationLogic)
const { searchParams } = useValues(router)
const highlight = searchParams.highlight as NotificationBlock | undefined
const dataLoaded = !userLoading && !currentOrganizationLoading && !!user && !!currentOrganization
const weeklyDigestEnabled = !user?.notification_settings?.all_weekly_digest_disabled
const etDigestEnabled = user?.notification_settings?.error_tracking_weekly_digest !== false
const waDigestEnabled = user?.notification_settings?.web_analytics_weekly_digest !== false
const dataPipelineErrorThresholdValue = (user?.notification_settings?.data_pipeline_error_threshold ?? 0) * 100
const [localDataPipelineErrorThreshold, setLocalDataPipelineErrorThreshold] = useState(
dataPipelineErrorThresholdValue
)
const dataPipelineErrorThresholdError =
!isNaN(localDataPipelineErrorThreshold) &&
localDataPipelineErrorThreshold >= 0 &&
localDataPipelineErrorThreshold <= 100
? undefined
: 'Threshold must be between 0% and 100%'
const blocks: Record<NotificationBlock, JSX.Element> = {
[NotificationBlock.Security]: (
<div className="border rounded p-4">
<div className="space-y-2">
<LemonSwitch
data-attr="security_alerts_enabled"
checked={true}
disabled={true}
label="Security alerts"
/>
<span className="text-muted text-sm">
Account security notifications including password changes, 2FA, login activity, and personal API
key exposure. These notifications cannot be disabled.
</span>
</div>
</div>
),
[NotificationBlock.WeeklyDigest]: (
<div className="border rounded p-4 space-y-3">
<SimpleSwitch
setting="all_weekly_digest_disabled"
label="Weekly digest"
description="The weekly digest keeps you up to date with everything that's happening in your PostHog organizations"
dataAttr="weekly_digest_enabled"
inverse={true}
/>
{weeklyDigestEnabled && (
<ProjectDigestSelector
keyPrefix="weekly-digest"
dataAttrPrefix="weekly_digest"
setting="project_weekly_digest_disabled"
inverse
isTeamDisabled={(teamId) =>
!!user?.notification_settings.project_weekly_digest_disabled?.[teamId]
}
onToggleTeam={updateWeeklyDigestForTeam}
onToggleAllTeams={updateWeeklyDigestForAllTeams}
/>
)}
</div>
),
[NotificationBlock.MemberJoin]: (
<div className="border rounded p-4 space-y-3">
<div className="space-y-2">
<span className="font-medium">New member joined</span>
<span className="text-muted text-sm block">
When someone joins an organization you belong to, we email existing members. Choose which
organizations you want these notifications for.
</span>
</div>
<OrganizationMemberJoinSelector />
</div>
),
[NotificationBlock.DataPipelineErrors]: (
<div className="border rounded p-4 space-y-3">
<SimpleSwitch
setting="plugin_disabled"
label="Data pipeline errors"
description="Get notified when data pipeline components (destinations, batch exports, data warehouse sources) encounter errors for all projects"
dataAttr="pipeline_errors_enabled"
/>
{user?.notification_settings?.plugin_disabled !== false && (
<div className="space-y-2">
<label className="text-sm font-medium">Failure rate threshold</label>
<div className="flex flex-col gap-2">
<div className="flex items-center gap-2">
<LemonInput
type="number"
size="xsmall"
min={0}
max={100}
step={0.1}
value={localDataPipelineErrorThreshold}
onChange={(value) => {
const numValue = value != null && !isNaN(value) ? value : 0
setLocalDataPipelineErrorThreshold(numValue)
updateDataPipelineErrorThreshold(numValue)
}}
disabledReason={userLoading ? 'Loading...' : undefined}
status={dataPipelineErrorThresholdError ? 'danger' : 'default'}
suffix={<span>%</span>}
className="w-32"
/>
<span className="text-muted text-sm">
Only notify if failure rate exceeds this threshold. Set to 0% to notify on any
failure.
</span>
</div>
{dataPipelineErrorThresholdError && (
<div className="text-danger text-sm">{dataPipelineErrorThresholdError}</div>
)}
</div>
</div>
)}
{user?.notification_settings?.plugin_disabled !== false && <PipelineNotificationSelector />}
</div>
),
[NotificationBlock.IssueAssigned]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="error_tracking_issue_assigned"
label="Issue assigned"
description="Stay on top of your bugs with a notification every time an issue is assigned to you or your role"
dataAttr="error_tracking_issue_assigned_enabled"
/>
</div>
),
[NotificationBlock.EtWeeklyDigest]: (
<div className="border rounded p-4 space-y-3">
<SimpleSwitch
setting="error_tracking_weekly_digest"
label="Error tracking weekly digest"
description="Get a weekly summary of exceptions caught across your projects every Monday"
dataAttr="error_tracking_weekly_digest_enabled"
/>
{etDigestEnabled && (
<>
{!user?.notification_settings.error_tracking_weekly_digest_project_enabled && (
<LemonBanner type="info">
You haven't selected any projects yet, so on the first digest run we'll automatically
pick the one with the most exceptions. If you'd prefer to choose yourself, just select
your projects below and we won't override your choice.
</LemonBanner>
)}
<ProjectDigestSelector
keyPrefix="et-digest"
dataAttrPrefix="et_weekly_digest"
setting="error_tracking_weekly_digest_project_enabled"
isTeamDisabled={(teamId) =>
!user?.notification_settings.error_tracking_weekly_digest_project_enabled?.[teamId]
}
onToggleTeam={updateETWeeklyDigestForTeam}
onToggleAllTeams={updateETWeeklyDigestForAllTeams}
/>
</>
)}
</div>
),
[NotificationBlock.WaWeeklyDigest]: (
<div className="border rounded p-4 space-y-3">
<SimpleSwitch
setting="web_analytics_weekly_digest"
label="Web analytics weekly digest"
description="Get a weekly summary of web traffic across your projects every Monday"
dataAttr="web_analytics_weekly_digest_enabled"
/>
{waDigestEnabled && (
<>
{!user?.notification_settings.web_analytics_weekly_digest_project_enabled && (
<LemonBanner type="info">
You haven't selected any projects yet, so on the first digest run we'll automatically
pick the one with the most visitors. If you'd prefer to choose yourself, just select
your projects below and we won't override your choice.
</LemonBanner>
)}
<ProjectDigestSelector
keyPrefix="wa-digest"
dataAttrPrefix="wa_weekly_digest"
setting="web_analytics_weekly_digest_project_enabled"
isTeamDisabled={(teamId) =>
!user?.notification_settings.web_analytics_weekly_digest_project_enabled?.[teamId]
}
onToggleTeam={updateWAWeeklyDigestForTeam}
onToggleAllTeams={updateWAWeeklyDigestForAllTeams}
/>
</>
)}
</div>
),
[NotificationBlock.DataCatalogWeeklyDigest]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="data_catalog_weekly_digest"
label="Data catalog weekly digest"
description="Get a weekly summary of the metrics, relationships, and certifications waiting for review in your projects"
dataAttr="data_catalog_weekly_digest_enabled"
/>
</div>
),
[NotificationBlock.CommentMentions]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="discussions_mentioned"
label="Comment mentions"
description="Get notified when someone mentions you in a discussion on any project"
dataAttr="discussions_mentioned_enabled"
/>
</div>
),
[NotificationBlock.Approvals]: (
<div className="border rounded p-4 space-y-3">
<div className="flex items-center gap-1 font-semibold">
<span>Approvals</span>
<Tooltip title="These switches apply to approval emails from all your organizations.">
<IconQuestion className="text-secondary text-base" />
</Tooltip>
</div>
<SimpleSwitch
setting="approval_requested"
label="Approval requests"
description="Get an email when a change needs your approval"
dataAttr="approval_requested_enabled"
/>
<SimpleSwitch
setting="approval_resolved"
label="Change request updates"
description="Get an email when your change request is approved, declined, expires, or goes live"
dataAttr="approval_resolved_enabled"
/>
</div>
),
[NotificationBlock.ApiKeyExposure]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="project_api_key_exposed"
label="Private API key exposure"
description="Get notified when private API keys are publicly exposed"
dataAttr="project_api_key_exposure_enabled"
/>
</div>
),
[NotificationBlock.AiEvaluationDisabled]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="ai_evaluation_disabled"
label="Disabled AI evaluations"
description="Get notified when an AI observability evaluation stops because of an error"
dataAttr="ai_evaluation_disabled_enabled"
/>
</div>
),
[NotificationBlock.WorkflowSuggestions]: (
<div className="border rounded p-4">
<SimpleSwitch
setting="workflow_suggestions"
label="Workflow suggestions"
description="Get an email when PostHog suggests a change to a workflow you set up"
dataAttr="workflow_suggestions_enabled"
/>
</div>
),
[NotificationBlock.MaterializedViewSync]: (
<div className="border rounded p-4 space-y-3">
<SimpleSwitch
setting="materialized_view_sync_failed"
label="Materialized view sync failures"
description="Get notified when a materialized view fails to sync"
dataAttr="materialized_view_sync_failed_enabled"
/>
<MatviewFailureEmailOptions />
</div>
),
}
const visibleBlocks = NOTIFICATION_BLOCK_ORDER.filter(
(block) => block !== NotificationBlock.Approvals || hasAvailableFeature(AvailableFeature.APPROVALS)
)
const highlightedBlock = highlight && visibleBlocks.includes(highlight) ? highlight : null
const highlightRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (dataLoaded && highlightRef.current) {
highlightRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
}, [dataLoaded, highlightedBlock])
return (
<div className="space-y-3">
{visibleBlocks.map((key) => (
<div
key={key}
ref={key === highlightedBlock ? highlightRef : undefined}
className={key === highlightedBlock ? 'ring-2 ring-accent-primary rounded-lg' : undefined}
>
{blocks[key]}
</div>
))}
</div>
)
}
const MatviewFailureEmailOptions = (): JSX.Element | null => {
const { user } = useValues(userLogic)
if (!user?.notification_settings?.materialized_view_sync_failed) {
return null
}
return (
<div className="pl-6 space-y-3">
<SimpleSwitch
setting="materialized_view_sync_failed_daily"
label="Daily digest"
description="One email a day summarizing failing views."
dataAttr="materialized_view_sync_failed_daily"
/>
<SimpleSwitch
setting="materialized_view_sync_failed_immediate"
label="Right away"
description="An email when a view starts failing. While it keeps failing, only the daily digest lists it."
dataAttr="materialized_view_sync_failed_immediate"
/>
</div>
)
}
const SimpleSwitch = ({
dataAttr,
label,
description,
setting,
inverse = false,
}: {
dataAttr: string
label: string
description: string
setting: keyof BooleanNotificationSettings
/**
* Some settings are expressed as "disabled" but the control is expressed as "enabled" (e.g. "All weekly digests" setting).
*/
inverse?: boolean
}): JSX.Element => {
const { user, userLoading } = useValues(userLogic)
const { updateUser } = useActions(userLogic)
const enforced = lockedValueFor(user?.notification_locks, setting)
const value = enforced ?? user?.notification_settings?.[setting]
const defaultValue = NOTIFICATION_DEFAULTS[setting]
let checked: boolean = typeof value === 'boolean' ? value : typeof defaultValue === 'boolean' ? defaultValue : false
if (inverse) {
checked = !checked
}
return (
<div className="space-y-2">
<LemonSwitch
data-attr={dataAttr}
onChange={(newChecked) => {
user?.notification_settings &&
updateUser({
notification_settings: {
...user?.notification_settings,
[setting]: inverse ? !newChecked : newChecked,
},
})
}}
checked={checked}
disabledReason={enforced !== null ? LOCKED_BY_ORGANIZATION : userLoading ? 'Loading...' : undefined}
label={
<span className="flex items-center gap-2">
{label}
{enforced !== null && <LemonTag type="highlight">Set by your admin</LemonTag>}
</span>
}
/>
<span className="text-muted text-sm">{description}</span>
</div>
)
}