: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 { KeyboardEvent, ReactNode } from 'react'
import { IconExternal, IconTrash } from '@posthog/icons'
import {
LemonButton,
LemonBanner,
LemonInput,
LemonSelect,
LemonSelectOptionLeaf,
LemonSelectOptions,
LemonSkeleton,
LemonTag,
LemonTagType,
Tooltip,
} from '@posthog/lemon-ui'
import { IntegrationChoice } from 'lib/components/CyclotronJob/integrations/IntegrationChoice'
import { SlackChannelPicker, SlackNotConfiguredBanner } from 'lib/integrations/SlackIntegrationHelpers'
import { IntegrationType } from '~/types'
interface AlertNotificationDestinationIconAction {
kind: 'icon'
url: string
tooltip: string
targetBlank?: boolean
}
interface AlertNotificationDestinationButtonAction {
kind: 'button'
label: string
url?: string
disabledReason?: string
dataAttr?: string
}
export interface AlertNotificationDestinationView {
key: string
title: ReactNode
detail?: ReactNode
tags?: { label: string; type?: LemonTagType; tooltip?: string }[]
viewAction?: AlertNotificationDestinationIconAction | AlertNotificationDestinationButtonAction
onDelete: () => void
deleting?: boolean
}
export interface PendingAlertNotificationDestinationView {
key: string
title: ReactNode
detail?: ReactNode
onRemove: () => void
}
export interface AlertNotificationUrlInput {
placeholder: string
helpText?: ReactNode
}
export type AlertPagerDutySeverity = 'critical' | 'error' | 'warning' | 'info'
export type AlertPagerDutyRegion = 'us' | 'eu'
export const ALERT_PAGERDUTY_SEVERITY_OPTIONS: LemonSelectOptionLeaf<AlertPagerDutySeverity>[] = [
{ value: 'critical', label: 'Critical' },
{ value: 'error', label: 'Error' },
{ value: 'warning', label: 'Warning' },
{ value: 'info', label: 'Info' },
]
export const ALERT_PAGERDUTY_REGION_OPTIONS: LemonSelectOptionLeaf<AlertPagerDutyRegion>[] = [
{ value: 'us', label: 'US' },
{ value: 'eu', label: 'EU' },
]
export interface AlertNotificationPagerDutyInput {
routingKey: string
onRoutingKeyChange: (value: string) => void
severity: AlertPagerDutySeverity
onSeverityChange: (value: AlertPagerDutySeverity) => void
region: AlertPagerDutyRegion
onRegionChange: (value: AlertPagerDutyRegion) => void
}
interface AlertNotificationDestinationEditorProps<NotificationType extends string> {
description?: ReactNode
destinations: {
showExisting: boolean
existingLoading: boolean
existing: AlertNotificationDestinationView[]
pending: PendingAlertNotificationDestinationView[]
pendingTooltip?: string
}
notificationType: {
options: LemonSelectOptions<NotificationType>
value: NotificationType
onChange: (type: NotificationType) => void
/** Where the type dropdown opens. Set 'top-start' when the editor sits near the bottom of the page. */
dropdownPlacement?: 'top-start' | 'bottom-start'
}
slack: {
notificationType: NotificationType
integrationsLoading: boolean
integrationsFailed: boolean
onRetryIntegrations: () => void
integrations?: IntegrationType[]
integration?: IntegrationType
onIntegrationChange?: (integrationId: number | null) => void
channelValue: string | null
onChannelValueChange: (value: string | null) => void
}
url?: {
input: AlertNotificationUrlInput
value: string
onChange: (value: string) => void
}
pagerduty?: AlertNotificationPagerDutyInput
add: {
onClick: () => void
disabledReason?: string
}
}
function DestinationViewAction({
action,
}: {
action: AlertNotificationDestinationView['viewAction']
}): JSX.Element | null {
if (!action) {
return null
}
switch (action.kind) {
case 'icon':
return (
<LemonButton
icon={<IconExternal />}
size="xsmall"
to={action.url}
targetBlank={action.targetBlank}
hideExternalLinkIcon
tooltip={action.tooltip}
/>
)
case 'button':
return (
<LemonButton
size="xsmall"
type="secondary"
to={action.url}
disabledReason={action.disabledReason}
data-attr={action.dataAttr}
>
{action.label}
</LemonButton>
)
default: {
const exhaustiveCheck: never = action
return exhaustiveCheck
}
}
}
function ExistingDestinations({
loading,
destinations,
}: {
loading: boolean
destinations: AlertNotificationDestinationView[]
}): JSX.Element | null {
if (loading) {
return <LemonSkeleton className="h-8" repeat={2} />
}
if (destinations.length === 0) {
return null
}
return (
<div className="space-y-2">
{destinations.map((destination) => (
<div key={destination.key} className="flex items-center justify-between border rounded p-2 gap-2">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium truncate">{destination.title}</span>
{destination.tags?.map((tag) => (
<Tooltip key={tag.label} title={tag.tooltip}>
<LemonTag type={tag.type} size="small">
{tag.label}
</LemonTag>
</Tooltip>
))}
</div>
{destination.detail ? (
<span className="text-xs text-muted-alt truncate block">{destination.detail}</span>
) : null}
</div>
<div className="flex items-center gap-1 shrink-0">
<DestinationViewAction action={destination.viewAction} />
<LemonButton
icon={<IconTrash />}
size="xsmall"
status="danger"
onClick={destination.onDelete}
loading={destination.deleting}
disabledReason={destination.deleting ? 'Deleting notification.' : undefined}
tooltip="Delete notification"
/>
</div>
</div>
))}
</div>
)
}
function PendingDestinations({
destinations,
tooltip = 'Save this alert to add this destination.',
}: {
destinations: PendingAlertNotificationDestinationView[]
tooltip?: string
}): JSX.Element | null {
if (destinations.length === 0) {
return null
}
return (
<div className="space-y-2">
{destinations.map((destination) => (
<div key={destination.key} className="flex items-center justify-between border rounded p-2 gap-2">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="text-sm font-medium truncate">{destination.title}</span>
<Tooltip title={tooltip}>
<LemonTag type="warning" size="small">
Pending
</LemonTag>
</Tooltip>
</div>
{destination.detail ? (
<span className="text-xs text-muted-alt truncate block">{destination.detail}</span>
) : null}
</div>
<LemonButton
icon={<IconTrash />}
size="xsmall"
status="danger"
onClick={destination.onRemove}
tooltip="Remove notification"
/>
</div>
))}
</div>
)
}
export function AlertNotificationDestinationEditor<NotificationType extends string>({
description,
destinations,
notificationType,
slack,
url,
pagerduty,
add,
}: AlertNotificationDestinationEditorProps<NotificationType>): JSX.Element {
const addDestinationButton = (
<LemonButton
type="primary"
size="small"
onClick={add.onClick}
disabledReason={add.disabledReason}
className="shrink-0"
>
Add
</LemonButton>
)
const addDestinationButtonIsInline =
notificationType.value === slack.notificationType || Boolean(url) || Boolean(pagerduty)
const addOnEnter = (event: KeyboardEvent<HTMLInputElement>): void => {
if (event.nativeEvent.isComposing) {
event.stopPropagation()
return
}
event.preventDefault()
event.stopPropagation()
if (!add.disabledReason) {
add.onClick()
}
}
let slackDestinationInput: JSX.Element | null = null
if (notificationType.value === slack.notificationType) {
if (slack.integrationsLoading || slack.integrations === undefined) {
slackDestinationInput = <LemonSkeleton className="h-10" />
} else if (slack.integrationsFailed) {
slackDestinationInput = (
<LemonBanner
type="error"
action={{
children: 'Try again',
onClick: slack.onRetryIntegrations,
}}
>
Couldn't load Slack workspaces.
</LemonBanner>
)
} else if (slack.integration) {
slackDestinationInput = (
<div className="space-y-3">
{(slack.integrations?.length ?? 0) > 1 ? (
<fieldset className="space-y-1">
<legend className="text-sm font-medium">Slack workspace</legend>
<IntegrationChoice
integration="slack"
value={slack.integration.id}
onChange={(integrationId) => {
if (integrationId !== slack.integration?.id) {
slack.onIntegrationChange?.(integrationId)
}
}}
allowClear={false}
/>
</fieldset>
) : null}
<fieldset className="space-y-1">
<legend className="text-sm font-medium">Channel</legend>
<div className="flex flex-col sm:flex-row items-start gap-2">
<div className="flex-1 min-w-0 w-full">
<SlackChannelPicker
value={slack.channelValue ?? undefined}
onChange={slack.onChannelValueChange}
integration={slack.integration}
/>
</div>
{addDestinationButton}
</div>
</fieldset>
</div>
)
} else {
slackDestinationInput = (
<SlackNotConfiguredBanner
type="warning"
className="max-w-4xl"
onConnected={slack.onIntegrationChange}
/>
)
}
}
const hasDestinations =
(destinations.showExisting && (destinations.existingLoading || destinations.existing.length > 0)) ||
destinations.pending.length > 0
return (
<div className="flex flex-col gap-4" data-prevent-wizard-submit>
{description ? <p className="text-xs text-muted-alt m-0">{description}</p> : null}
{destinations.showExisting ? (
<ExistingDestinations loading={destinations.existingLoading} destinations={destinations.existing} />
) : null}
<PendingDestinations destinations={destinations.pending} tooltip={destinations.pendingTooltip} />
{hasDestinations ? <hr className="border-border m-0" /> : null}
<div className="space-y-3 max-w-xl">
<LemonSelect
fullWidth
dropdownPlacement={notificationType.dropdownPlacement}
options={notificationType.options}
value={notificationType.value}
onChange={notificationType.onChange}
/>
{slackDestinationInput}
{url ? (
<div className="space-y-1">
<div className="flex flex-col sm:flex-row items-start gap-2">
<LemonInput
placeholder={url.input.placeholder}
value={url.value}
onChange={url.onChange}
onPressEnter={addOnEnter}
fullWidth
/>
{addDestinationButton}
</div>
{url.input.helpText ? <p className="text-xs text-muted-alt m-0">{url.input.helpText}</p> : null}
</div>
) : null}
{pagerduty ? (
<div className="space-y-3">
<fieldset className="space-y-1">
<legend className="text-sm font-medium">Integration key</legend>
<div className="flex flex-col sm:flex-row items-start gap-2">
{/* Autocapture records copied text, which the password type does not stop. */}
<LemonInput
type="password"
autoComplete="off"
className="ph-no-capture"
placeholder="32-character Events API v2 integration key"
value={pagerduty.routingKey}
onChange={pagerduty.onRoutingKeyChange}
onPressEnter={addOnEnter}
fullWidth
data-attr="alert-pagerduty-routing-key"
/>
{addDestinationButton}
</div>
<p className="text-xs text-muted-alt m-0">
In PagerDuty, add an "Events API v2" integration to the service you want to page and
paste its integration key here. An incident opens when the alert starts firing and
resolves when it stops.
</p>
</fieldset>
<div className="flex flex-col sm:flex-row gap-3">
<fieldset className="space-y-1 flex-1 min-w-0">
<legend className="text-sm font-medium">Severity</legend>
<LemonSelect
fullWidth
options={ALERT_PAGERDUTY_SEVERITY_OPTIONS}
value={pagerduty.severity}
onChange={pagerduty.onSeverityChange}
data-attr="alert-pagerduty-severity"
/>
</fieldset>
<fieldset className="space-y-1 flex-1 min-w-0">
<legend className="text-sm font-medium">Service region</legend>
<LemonSelect
fullWidth
options={ALERT_PAGERDUTY_REGION_OPTIONS}
value={pagerduty.region}
onChange={pagerduty.onRegionChange}
data-attr="alert-pagerduty-region"
/>
</fieldset>
</div>
</div>
) : null}
{!addDestinationButtonIsInline ? addDestinationButton : null}
</div>
</div>
)
}