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.

3.7 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, selectors } from 'kea'
import posthog from 'posthog-js'

import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic, getFeatureFlagPayload } from 'lib/logic/featureFlagLogic'
import { readsFlagEvaluationsTable } from 'scenes/feature-flags/flagEvaluationsTable'
import { teamLogic } from 'scenes/teamLogic'

import { TeamPublicType, TeamType } from '~/types'

export type FlagCalledArtifactType = 'insight' | 'dashboard' | 'action' | 'cohort'

/** The announcement link from the flag's `{ "url": … }` payload, or null until the payload holds an https URL with a host. */
export function announcementUrlFromPayload(payload: unknown): string | null {
    const url = (payload as { url?: unknown } | null | undefined)?.url
    if (typeof url !== 'string') {
        return null
    }
    try {
        const { protocol, host } = new URL(url)
        return protocol === 'https:' && host ? url : null
    } catch {
        return null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCalledRebuildBannerLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    announcementUrl: string | null
    bannersEnabled: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCalledRebuildBannerLogicActions {
    reportBannerShown: (artifactType: FlagCalledArtifactType) => {
        artifactType: FlagCalledArtifactType
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface flagCalledRebuildBannerLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        bannersEnabled: (currentTeam: TeamPublicType | TeamType | null, featureFlags: FeatureFlagsSet) => boolean
        announcementUrl: (featureFlags: FeatureFlagsSet) => string | null
    }
}

export type flagCalledRebuildBannerLogicType = MakeLogicType<
    flagCalledRebuildBannerLogicValues,
    flagCalledRebuildBannerLogicActions,
    Record<string, any>,
    flagCalledRebuildBannerLogicMeta
>

export const flagCalledRebuildBannerLogic = kea<flagCalledRebuildBannerLogicType>([
    path(['lib', 'components', 'FlagCalledRebuildBanner', 'flagCalledRebuildBannerLogic']),
    connect(() => ({
        values: [teamLogic, ['currentTeam'], featureFlagLogic, ['featureFlags']],
    })),
    actions({
        reportBannerShown: (artifactType: FlagCalledArtifactType) => ({ artifactType }),
    }),
    selectors({
        // A team on mode 0 still builds events series in its pickers, so it has no way to rebuild yet.
        bannersEnabled: [
            (s) => [s.currentTeam, s.featureFlags],
            (currentTeam: TeamPublicType | TeamType | null, featureFlags: FeatureFlagsSet): boolean =>
                !!featureFlags[FEATURE_FLAGS.FLAG_CALLED_MOVE_NOTICES] && readsFlagEvaluationsTable(currentTeam),
        ],
        // Reads the payload when the flags change, because the payload arrives with them.
        announcementUrl: [
            (s) => [s.featureFlags],
            (featureFlags: FeatureFlagsSet): string | null =>
                featureFlags[FEATURE_FLAGS.FLAG_CALLED_MOVE_NOTICES]
                    ? announcementUrlFromPayload(getFeatureFlagPayload(FEATURE_FLAGS.FLAG_CALLED_MOVE_NOTICES))
                    : null,
        ],
    }),
    listeners({
        reportBannerShown: ({ artifactType }) => {
            // pinned: analytics event name and property. Renaming them breaks the insights that count banner views.
            posthog.capture('flag called rebuild banner shown', { artifact_type: artifactType })
        },
    }),
])