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.6 KB
Raw Blame
import {
    LogicWrapper,
    MakeLogicType,
    actions,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    reducers,
    selectors,
} from 'kea'

import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { slackChannelDisplayName } from 'lib/integrations/slackChannel'

import { IntegrationType } from '~/types'

import type { TurnSuggestion } from '../types/streamTypes'
import { captureTurnSuggestionEvent } from '../utils/turnSuggestionEvents'
import { TurnSuggestionLogicProps, turnSuggestionLogic } from './turnSuggestionLogic'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface slackDestinationLogicValues {
    slackIntegrations: IntegrationType[] | undefined // integrationsLogic
    suggestion: TurnSuggestion | null // turnSuggestionLogic
    hasSlackWorkspace: boolean
    slackChannel: string | null
    slackChannelLabel: string
    slackDestinationDisabledReason: string | null
    slackIntegrationId: number | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface slackDestinationLogicActions {
    connectSlackClicked: () => {
        value: true
    }
    setSlackChannel: (channel: string | null) => {
        channel: string | null
    }
    setSlackIntegrationId: (integrationId: number | null) => {
        integrationId: number | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface slackDestinationLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        hasSlackWorkspace: (slackIntegrations: IntegrationType[] | undefined) => boolean
        slackChannelLabel: (slackChannel: string | null) => string
        slackDestinationDisabledReason: (
            slackIntegrationId: number | null,
            slackChannel: string | null,
            hasSlackWorkspace: boolean
        ) => string | null
    }
}

export type slackDestinationLogicType = MakeLogicType<
    slackDestinationLogicValues,
    slackDestinationLogicActions,
    TurnSuggestionLogicProps,
    slackDestinationLogicMeta
>

/** The Slack workspace and channel a card posts to, shared by every kind that ends in Slack. */
export const slackDestinationLogic: LogicWrapper<slackDestinationLogicType> = kea<slackDestinationLogicType>([
    props({} as TurnSuggestionLogicProps),
    key((props) => `${props.streamKey}:${props.turnIndex}`),
    path((key) => ['products', 'posthog_ai', 'frontend', 'logics', 'slackDestinationLogic', key]),
    connect((props: TurnSuggestionLogicProps) => ({
        values: [turnSuggestionLogic(props), ['suggestion'], integrationsLogic, ['slackIntegrations']],
    })),
    actions({
        setSlackIntegrationId: (integrationId: number | null) => ({ integrationId }),
        setSlackChannel: (channel: string | null) => ({ channel }),
        connectSlackClicked: true,
    }),
    reducers({
        slackIntegrationId: [
            null as number | null,
            {
                setSlackIntegrationId: (_, { integrationId }) => integrationId,
            },
        ],
        slackChannel: [
            null as string | null,
            {
                setSlackChannel: (_, { channel }) => channel,
                // A channel belongs to one workspace, so switching workspace clears it.
                setSlackIntegrationId: () => null,
            },
        ],
    }),
    selectors({
        hasSlackWorkspace: [
            (s) => [s.slackIntegrations],
            (slackIntegrations: IntegrationType[] | undefined): boolean => !!slackIntegrations?.length,
        ],
        slackChannelLabel: [
            (s) => [s.slackChannel],
            (slackChannel: string | null): string => (slackChannel ? slackChannelDisplayName(slackChannel) : 'Slack'),
        ],
        slackDestinationDisabledReason: [
            (s) => [s.slackIntegrationId, s.slackChannel, s.hasSlackWorkspace],
            (
                slackIntegrationId: number | null,
                slackChannel: string | null,
                hasSlackWorkspace: boolean
            ): string | null => {
                if (slackIntegrationId === null) {
                    return hasSlackWorkspace ? 'Choose a Slack workspace first' : 'Connect Slack first'
                }
                if (!slackChannel) {
                    return 'Choose a Slack channel first'
                }
                return null
            },
        ],
    }),
    listeners(({ values, props }) => ({
        connectSlackClicked: () => {
            if (values.suggestion) {
                captureTurnSuggestionEvent('connect slack clicked', props, values.suggestion)
            }
        },
    })),
])