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

import api from 'lib/api'
import { CommentsLogicProps } from 'lib/components/Comments/commentsLogic'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'

import type { FeatureFlagsSet } from '../../../../../lib/logic/featureFlagLogic'
import { sidePanelContextLogic } from '../../sidePanelContextLogic'
import type { SidePanelSceneContext } from '../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelDiscussionLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    sceneSidePanelContext: SidePanelSceneContext // sidePanelContextLogic
    commentCount: number
    commentCountLoading: boolean
    commentsListRef: HTMLDivElement | null
    commentsLogicProps: CommentsLogicProps | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelDiscussionLogicActions {
    incrementCommentCount: () => any
    incrementCommentCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    incrementCommentCountSuccess: (
        commentCount: number,
        payload?: any
    ) => {
        commentCount: number
        payload?: any
    }
    loadCommentCount: () => {
        value: true
    }
    loadCommentCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadCommentCountSuccess: (
        commentCount: number,
        payload?: {
            value: true
        }
    ) => {
        commentCount: number
        payload?: {
            value: true
        }
    }
    resetCommentCount: () => {
        value: true
    }
    resetCommentCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    resetCommentCountSuccess: (
        commentCount: number,
        payload?: {
            value: true
        }
    ) => {
        commentCount: number
        payload?: {
            value: true
        }
    }
    scrollToLastComment: () => {
        value: true
    }
    setCommentsListRef: (ref: HTMLDivElement) => {
        ref: HTMLDivElement
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sidePanelDiscussionLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        commentsLogicProps: (sceneSidePanelContext: SidePanelSceneContext) => CommentsLogicProps | null
    }
}

export type sidePanelDiscussionLogicType = MakeLogicType<
    sidePanelDiscussionLogicValues,
    sidePanelDiscussionLogicActions,
    Record<string, any>,
    sidePanelDiscussionLogicMeta
>

export const sidePanelDiscussionLogic = kea<sidePanelDiscussionLogicType>([
    path(['scenes', 'navigation', 'sidepanel', 'sidePanelDiscussionLogic']),
    actions({
        loadCommentCount: true,
        resetCommentCount: true,
        scrollToLastComment: true,
        setCommentsListRef: (ref: HTMLDivElement) => ({ ref }),
    }),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags'], sidePanelContextLogic, ['sceneSidePanelContext']],
    })),
    reducers({
        commentsListRef: [
            null as HTMLDivElement | null,
            {
                setCommentsListRef: (_, { ref }) => ref,
            },
        ],
    }),
    loaders(({ values }) => ({
        commentCount: [
            0,
            {
                loadCommentCount: async (_, breakpoint) => {
                    if (!values.commentsLogicProps || values.commentsLogicProps.disabled) {
                        return 0
                    }

                    await breakpoint(100)
                    const response = await api.comments.getCount({
                        ...values.commentsLogicProps,
                        exclude_emoji_reactions: true,
                    })

                    breakpoint()

                    return response
                },
                incrementCommentCount: () => {
                    return values.commentCount + 1
                },
                resetCommentCount: () => {
                    return 0
                },
            },
        ],
    })),
    listeners(({ values }) => ({
        scrollToLastComment() {
            const commentsListRef = values.commentsListRef
            if (commentsListRef) {
                setTimeout(() => {
                    commentsListRef.scrollTop = commentsListRef.scrollHeight
                }, 100)
            }
        },
    })),
    selectors({
        commentsLogicProps: [
            (s) => [s.sceneSidePanelContext],
            (sceneSidePanelContext: import('../../types').SidePanelSceneContext): CommentsLogicProps | null => {
                return sceneSidePanelContext.activity_scope
                    ? {
                          scope: sceneSidePanelContext.activity_scope,
                          item_id: sceneSidePanelContext.activity_item_id,
                          item_context: sceneSidePanelContext.activity_item_context,
                          disabled: sceneSidePanelContext.discussions_disabled,
                      }
                    : null
            },
        ],
    }),

    subscriptions(({ actions }) => ({
        commentsLogicProps: (props) => {
            if (props) {
                actions.loadCommentCount()
            } else {
                actions.resetCommentCount()
            }
        },
    })),
])