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.

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

import { UI_CONFIGURATION_VERSION, uiCustomizationLogic } from '~/layout/uiCustomizationLogic'
import { TicketSidebarConfiguration, UserUIConfiguration } from '~/queries/schema/schema-general'

import {
    DEFAULT_TICKET_SIDEBAR_LAYOUT,
    TicketSidebarLayout,
    TicketSidebarWidgetKey,
    resolveTicketSidebarLayout,
    storeTicketSidebarLayout,
    ticketSidebarLayoutsEqual,
} from './ticketSidebarLayout'

// pinned: analytics event names. Renaming them breaks insights built on them.
const EDIT_STARTED_EVENT = 'support ticket sidebar edit started'
const EDIT_CANCELED_EVENT = 'support ticket sidebar edit canceled'
const LAYOUT_SAVED_EVENT = 'support ticket sidebar layout saved'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketSidebarWidgetsLogicValues {
    uiConfiguration: UserUIConfiguration | null // uiCustomizationLogic
    draft: TicketSidebarLayout | null
    draftMatchesDefault: boolean
    isEditing: boolean
    layout: TicketSidebarLayout
    userLayout: TicketSidebarConfiguration | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketSidebarWidgetsLogicActions {
    updateUiConfiguration: (
        configuration: UserUIConfiguration,
        successCallback?: (() => void) | undefined,
        failureCallback?: (() => void) | undefined
    ) => {
        configuration: UserUIConfiguration
        failureCallback: (() => void) | undefined
        successCallback: (() => void) | undefined
    } // uiCustomizationLogic
    cancelEditing: () => {
        value: true
    }
    hideWidget: (key: TicketSidebarWidgetKey) => {
        key: TicketSidebarWidgetKey
    }
    moveWidget: (
        fromIndex: number,
        toIndex: number
    ) => {
        fromIndex: number
        toIndex: number
    }
    resetDraftToDefault: () => {
        value: true
    }
    saveLayout: () => {
        value: true
    }
    setDraft: (draft: TicketSidebarLayout | null) => {
        draft: TicketSidebarLayout | null
    }
    showWidget: (key: TicketSidebarWidgetKey) => {
        key: TicketSidebarWidgetKey
    }
    startEditing: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface ticketSidebarWidgetsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        userLayout: (uiConfiguration: UserUIConfiguration | null) => TicketSidebarConfiguration | null
        isEditing: (draft: TicketSidebarLayout | null) => boolean
        layout: (
            draft: TicketSidebarLayout | null,
            userLayout: TicketSidebarConfiguration | null
        ) => TicketSidebarLayout
        draftMatchesDefault: (draft: TicketSidebarLayout | null) => boolean
    }
}

export type ticketSidebarWidgetsLogicType = MakeLogicType<
    ticketSidebarWidgetsLogicValues,
    ticketSidebarWidgetsLogicActions,
    Record<string, any>,
    ticketSidebarWidgetsLogicMeta
>

export const ticketSidebarWidgetsLogic = kea<ticketSidebarWidgetsLogicType>([
    path(['products', 'conversations', 'frontend', 'components', 'TicketSidebarWidgets', 'ticketSidebarWidgetsLogic']),
    connect(() => ({
        values: [uiCustomizationLogic, ['uiConfiguration']],
        actions: [uiCustomizationLogic, ['updateUiConfiguration']],
    })),
    actions({
        startEditing: true,
        cancelEditing: true,
        setDraft: (draft: TicketSidebarLayout | null) => ({ draft }),
        moveWidget: (fromIndex: number, toIndex: number) => ({ fromIndex, toIndex }),
        hideWidget: (key: TicketSidebarWidgetKey) => ({ key }),
        showWidget: (key: TicketSidebarWidgetKey) => ({ key }),
        resetDraftToDefault: true,
        saveLayout: true,
    }),
    reducers({
        draft: [
            null as TicketSidebarLayout | null,
            {
                setDraft: (_, { draft }) => draft,
                cancelEditing: () => null,
                moveWidget: (state, { fromIndex, toIndex }) => {
                    const inRange = (index: number): boolean => index >= 0 && index < (state?.visible.length ?? 0)
                    if (!state || fromIndex === toIndex || !inRange(fromIndex) || !inRange(toIndex)) {
                        return state
                    }
                    const visible = [...state.visible]
                    const [moved] = visible.splice(fromIndex, 1)
                    visible.splice(toIndex, 0, moved)
                    return { ...state, visible }
                },
                hideWidget: (state, { key }) =>
                    state && state.visible.includes(key)
                        ? { visible: state.visible.filter((k) => k !== key), hidden: [...state.hidden, key] }
                        : state,
                showWidget: (state, { key }) =>
                    state && state.hidden.includes(key)
                        ? { visible: [...state.visible, key], hidden: state.hidden.filter((k) => k !== key) }
                        : state,
            },
        ],
    }),
    selectors({
        userLayout: [
            (s) => [s.uiConfiguration],
            (uiConfiguration: UserUIConfiguration | null): TicketSidebarConfiguration | null =>
                uiConfiguration?.ticket_sidebar ?? null,
        ],
        isEditing: [(s) => [s.draft], (draft: TicketSidebarLayout | null): boolean => draft !== null],
        layout: [
            (s) => [s.draft, s.userLayout],
            (draft: TicketSidebarLayout | null, userLayout: TicketSidebarConfiguration | null): TicketSidebarLayout =>
                draft ?? resolveTicketSidebarLayout(userLayout),
        ],
        draftMatchesDefault: [
            (s) => [s.draft],
            (draft: TicketSidebarLayout | null): boolean =>
                !!draft && ticketSidebarLayoutsEqual(draft, DEFAULT_TICKET_SIDEBAR_LAYOUT),
        ],
    }),
    listeners(({ actions, values }) => ({
        startEditing: () => {
            actions.setDraft(values.layout)
            posthog.capture(EDIT_STARTED_EVENT, { has_saved_layout: values.userLayout !== null })
        },
        cancelEditing: () => {
            posthog.capture(EDIT_CANCELED_EVENT)
        },
        resetDraftToDefault: () => {
            actions.setDraft(DEFAULT_TICKET_SIDEBAR_LAYOUT)
        },
        saveLayout: () => {
            const { draft, draftMatchesDefault, uiConfiguration } = values
            if (!draft) {
                return
            }
            posthog.capture(LAYOUT_SAVED_EVENT, { visible_widgets: draft.visible, hidden_widgets: draft.hidden })
            // The server replaces ui_configuration wholesale, so every other key is carried over.
            // Saving the default stores nothing, so the user keeps getting later changes to the default order.
            actions.updateUiConfiguration({
                version: UI_CONFIGURATION_VERSION,
                ...uiConfiguration,
                ticket_sidebar: draftMatchesDefault ? undefined : storeTicketSidebarLayout(draft),
            })
            actions.setDraft(null)
        },
    })),
])