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.

4.0 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconInfo } from '@posthog/icons'
import { LemonButton, LemonSwitch, Tooltip } from '@posthog/lemon-ui'

import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { OrganizationMembershipLevel, TeamMembershipLevel } from 'lib/constants'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { LinkedHogFunctions } from 'scenes/hog-functions/list/LinkedHogFunctions'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'

// These settings are gated as a whole. The AUDIT_LOGS pay gate is declared on the
// `environment-activity-logs` section in SettingsMap, not on each component here.

export function ActivityLogSettings(): JSX.Element {
    const { user } = useValues(userLogic)
    const restrictedReason = useRestrictedArea({
        scope: RestrictionScope.Project,
        minimumAccessLevel: TeamMembershipLevel.Admin,
    })
    const effectiveRestriction = user?.is_impersonated ? null : restrictedReason

    return (
        <div className="flex">
            <p>
                <LemonButton to={urls.advancedActivityLogs()} type="primary" disabledReason={effectiveRestriction}>
                    Browse all activity logs
                </LemonButton>
            </p>
        </div>
    )
}

export function ActivityLogOrgLevelSettings(): JSX.Element {
    const { currentTeam } = useValues(teamLogic)
    const { updateCurrentTeam } = useActions(teamLogic)
    const { reportActivityLogSettingToggled } = useActions(eventUsageLogic)

    const restrictionReason = useRestrictedArea({ minimumAccessLevel: OrganizationMembershipLevel.Admin })

    const handleToggle = (checked: boolean): void => {
        updateCurrentTeam({ receive_org_level_activity_logs: checked })
        reportActivityLogSettingToggled(checked)
    }

    return (
        <div>
            <p className="flex items-center gap-1">
                Include organization-level activity logs in this project.
                <Tooltip
                    title={
                        <>
                            When enabled, activity logs from organization-level changes (such as organization settings,
                            domains, and members) will be included in this project's activity logs page, exports, and
                            notifications subscriptions.
                        </>
                    }
                >
                    <IconInfo className="text-lg" />
                </Tooltip>
            </p>

            <LemonSwitch
                id="posthog-activity-log-org-level-switch"
                onChange={handleToggle}
                checked={!!currentTeam?.receive_org_level_activity_logs}
                disabledReason={restrictionReason || undefined}
                label="Include organization-level activity"
                bordered
            />
        </div>
    )
}

export function ActivityLogNotifications(): JSX.Element | null {
    const restrictedReason = useRestrictedArea({
        scope: RestrictionScope.Project,
        minimumAccessLevel: TeamMembershipLevel.Admin,
    })

    if (restrictedReason) {
        return null
    }

    return (
        <div>
            <p className="flex items-center gap-1">
                Create notifications to get notified of activity logs.
                <Tooltip
                    title={
                        <>
                            You can filter by activity type, resource, and other properties to receive only the
                            notifications you need.
                        </>
                    }
                >
                    <IconInfo className="text-lg" />
                </Tooltip>
            </p>

            <LinkedHogFunctions
                type="internal_destination"
                subTemplateIds={['activity-log']}
                queryParams={{
                    returnTo: urls.settings('environment-activity-logs', 'activity-log-notifications'),
                }}
            />
        </div>
    )
}