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.

5.2 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'

import { lemonToast } from '@posthog/lemon-ui'

import { ApiError } from 'lib/api'
import { slackChannelId } from 'lib/integrations/slackChannel'
import { teamLogic } from 'scenes/teamLogic'

import { CommentType } from '~/types'

import { commentsSendToSlackCreate } from 'products/platform_features/frontend/generated/api'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sendCommentToSlackLogicValues {
    currentProjectId: number | string // teamLogic
    channel: string | null
    channelId: string | null
    comment: CommentType | null
    integrationId: number | null
    isOpen: boolean
    isSubmitting: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sendCommentToSlackLogicActions {
    closeModal: () => {
        value: true
    }
    openModal: (comment: CommentType) => {
        comment: CommentType
    }
    setChannel: (channel: string | null) => {
        channel: string | null
    }
    setIntegrationId: (integrationId: number | null) => {
        integrationId: number | null
    }
    submit: () => {
        value: true
    }
    submitFailure: (detail?: string | null) => {
        detail: string | null
    }
    submitSuccess: (comment: CommentType) => {
        comment: CommentType
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sendCommentToSlackLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        isOpen: (comment: CommentType | null) => boolean
        channelId: (channel: string | null) => string | null
    }
}

export type sendCommentToSlackLogicType = MakeLogicType<
    sendCommentToSlackLogicValues,
    sendCommentToSlackLogicActions,
    Record<string, any>,
    sendCommentToSlackLogicMeta
>

export const sendCommentToSlackLogic = kea<sendCommentToSlackLogicType>([
    path(['lib', 'components', 'Comments', 'sendCommentToSlackLogic']),
    connect(() => ({ values: [teamLogic, ['currentProjectId']] })),
    actions({
        openModal: (comment: CommentType) => ({ comment }),
        closeModal: true,
        setIntegrationId: (integrationId: number | null) => ({ integrationId }),
        // SlackChannelPicker emits a "CHANNEL_ID|#name" composite; we keep it whole for display.
        setChannel: (channel: string | null) => ({ channel }),
        submit: true,
        submitSuccess: (comment: CommentType) => ({ comment }),
        submitFailure: (detail: string | null = null) => ({ detail }),
    }),
    reducers({
        comment: [
            null as CommentType | null,
            {
                openModal: (_, { comment }) => comment,
                closeModal: () => null,
                submitSuccess: () => null,
            },
        ],
        integrationId: [
            null as number | null,
            {
                setIntegrationId: (_, { integrationId }) => integrationId,
                openModal: () => null,
                closeModal: () => null,
            },
        ],
        channel: [
            null as string | null,
            {
                setChannel: (_, { channel }) => channel,
                setIntegrationId: () => null,
                openModal: () => null,
                closeModal: () => null,
            },
        ],
        isSubmitting: [
            false,
            {
                submit: () => true,
                submitSuccess: () => false,
                submitFailure: () => false,
                // A modal session must never inherit a stale in-flight state from a previous one.
                openModal: () => false,
                closeModal: () => false,
            },
        ],
    }),
    selectors({
        isOpen: [(s) => [s.comment], (comment: CommentType | null): boolean => comment !== null],
        channelId: [
            (s) => [s.channel],
            (channel: string | null): string | null => (channel ? slackChannelId(channel) : null),
        ],
    }),
    listeners(({ actions, values }) => ({
        submit: async () => {
            const { comment, integrationId, channelId, currentProjectId } = values
            if (!comment || !integrationId || !channelId || !currentProjectId) {
                actions.submitFailure()
                return
            }
            try {
                // The comments API is project-scoped — currentTeamId diverges from the project id
                // for non-default environments and 404s.
                await commentsSendToSlackCreate(String(currentProjectId), comment.id, {
                    integration_id: integrationId,
                    channel_id: channelId,
                })
                actions.submitSuccess(comment)
            } catch (e) {
                // Surface the backend's actionable detail (bot not in channel, integration missing…)
                actions.submitFailure(e instanceof ApiError ? e.detail : null)
            }
        },
        submitSuccess: () => {
            lemonToast.success('Discussion sent to Slack')
        },
        submitFailure: ({ detail }) => {
            lemonToast.error(detail || 'Could not send the discussion to Slack')
        },
    })),
])