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

import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { FileSystemIconType } from '~/queries/schema/schema-general'
import { Breadcrumb } from '~/types'

import { messageTemplateLogic } from './messageTemplateLogic'
import { TemplateSection, templateLibraryUrl, templateUrlInSection } from './templateSection'

export interface MessageTemplateSceneLogicProps {
    id: string
    messageId?: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface messageTemplateSceneLogicValues {
    isTransactional: boolean // messageTemplateLogic
    section: TemplateSection // messageTemplateLogic
    breadcrumbs: Breadcrumb[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface messageTemplateSceneLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        breadcrumbs: (id: string, isTransactional: boolean, section: TemplateSection) => Breadcrumb[]
    }
}

export type messageTemplateSceneLogicType = MakeLogicType<
    messageTemplateSceneLogicValues,
    {},
    MessageTemplateSceneLogicProps,
    messageTemplateSceneLogicMeta
>

export const messageTemplateSceneLogic = kea<messageTemplateSceneLogicType>([
    props({} as MessageTemplateSceneLogicProps),
    // Keyed like messageTemplateLogic, so the connection below follows the template on screen.
    key(({ id }) => id ?? 'new'),
    path((key) => ['products', 'workflows', 'frontend', 'messageTemplateSceneLogic', key]),
    connect((props: MessageTemplateSceneLogicProps) => ({
        values: [messageTemplateLogic(props), ['isTransactional', 'section']],
    })),
    selectors({
        breadcrumbs: [
            (s, p) => [p.id, s.isTransactional, s.section],
            (id: string, isTransactional: boolean, section: TemplateSection): Breadcrumb[] => {
                const sectionCrumbs: Breadcrumb[] =
                    section === 'transactional' || (section === 'workflows' && isTransactional)
                        ? [
                              {
                                  key: Scene.Transactional,
                                  name: 'Transactional',
                                  path: urls.transactional(),
                                  iconType: 'transactional',
                              },
                          ]
                        : section === 'broadcasts'
                          ? [
                                {
                                    key: Scene.Broadcasts,
                                    name: 'Broadcasts',
                                    path: urls.broadcasts(),
                                    iconType: 'broadcasts',
                                },
                            ]
                          : []
                // A transactional email is listed on the Transactional tab itself, so it skips the Templates crumb.
                const libraryCrumb: Breadcrumb[] =
                    section === 'workflows'
                        ? isTransactional
                            ? []
                            : [
                                  {
                                      key: [Scene.Workflows, 'library'],
                                      name: 'Library',
                                      path: urls.workflows('library'),
                                      iconType: 'workflows',
                                  },
                              ]
                        : isTransactional && section === 'transactional'
                          ? []
                          : [
                                {
                                    key: [section, 'library'],
                                    name: 'Templates',
                                    path: templateLibraryUrl(section),
                                    iconType: section as FileSystemIconType,
                                },
                            ]
                return [
                    ...sectionCrumbs,
                    ...libraryCrumb,
                    id === 'new'
                        ? {
                              key: 'new-template',
                              name: isTransactional ? 'New transactional email' : 'New template',
                              path: urls.workflowsLibraryTemplateNew(),
                              iconType: 'workflows' as FileSystemIconType,
                          }
                        : {
                              key: 'edit-template',
                              name: isTransactional ? 'Manage transactional email' : 'Manage template',
                              path: templateUrlInSection(id, section),
                              iconType: 'workflows' as FileSystemIconType,
                          },
                ]
            },
        ],
    }),
])