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.3 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { toParams } from 'lib/utils/url'
import { teamLogic } from 'scenes/teamLogic'

import { Group } from '~/types'

export type CreationGroupLogicProps = {
    groupTypeIndex: number | null
    groupKey: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface creationGroupLogicValues {
    currentTeamId: number | null // teamLogic
    group: Group | null
    groupLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface creationGroupLogicActions {
    loadGroup: () => any
    loadGroupFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadGroupSuccess: (
        group: Group | null,
        payload?: any
    ) => {
        group: Group | null
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface creationGroupLogicMeta {
    key: string
}

export type creationGroupLogicType = MakeLogicType<
    creationGroupLogicValues,
    creationGroupLogicActions,
    CreationGroupLogicProps,
    creationGroupLogicMeta
>

// Fetches the group a ticket was created with (its organization_id snapshot) so the related-groups
// panel can show its name and a working link even when it has dropped out of the live related list.
export const creationGroupLogic = kea<creationGroupLogicType>([
    props({} as CreationGroupLogicProps),
    key((props) => `${props.groupTypeIndex}-${props.groupKey}`),
    path((key) => ['products', 'conversations', 'frontend', 'scenes', 'ticket', 'creationGroupLogic', key]),
    connect(() => ({
        values: [teamLogic, ['currentTeamId']],
    })),
    loaders(({ values, props }) => ({
        group: [
            null as Group | null,
            {
                loadGroup: async () => {
                    if (props.groupTypeIndex === null || !props.groupKey) {
                        return null
                    }
                    // skip_create_notebook: this is a read-only display lookup; without it `groups/find`
                    // lazily creates the group's CRM notebook as a side effect.
                    const params = {
                        group_type_index: props.groupTypeIndex,
                        group_key: props.groupKey,
                        skip_create_notebook: true,
                    }
                    try {
                        // `groups/find` is a cross-product (groups) endpoint with no conversations-generated client.
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use groupsFindRetrieve() from 'products/groups/frontend/generated/api' instead.
                        return await api.get(`api/projects/${values.currentTeamId}/groups/find?${toParams(params)}`)
                    } catch {
                        // Group may no longer exist (stale snapshot) — degrade to no row rather than a broken link.
                        return null
                    }
                },
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadGroup()
    }),
])