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.

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

import { sidePanelNotificationsLogic } from '~/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic'
import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'

import type { PanelLayoutNavIdentifier } from '../../../layout/panel-layout/panelLayoutLogic'

export type NotificationTab = 'all' | 'unread' | 'archived'

/** Which surface the user opened the notifications panel from. */
export type NotificationsPanelSource = 'bell' | 'critical_toast'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notificationsMenuLogicValues {
    activePanelIdentifier: string // panelLayoutLogic
    isLayoutPanelVisible: boolean // panelLayoutLogic
    inAppUnreadCount: number // sidePanelNotificationsLogic
    activeTab: NotificationTab
    isNotificationsMenuOpen: boolean
    isNotificationsPanelActive: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notificationsMenuLogicActions {
    closePanel: () => {
        value: true
    } // panelLayoutLogic
    setActivePanelIdentifier: (identifier: PanelLayoutNavIdentifier) => {
        identifier: PanelLayoutNavIdentifier
    } // panelLayoutLogic
    showLayoutPanel: (visible: boolean) => {
        visible: boolean
    } // panelLayoutLogic
    loadArchivedNotifications: () => {
        value: true
    } // sidePanelNotificationsLogic
    openNotificationsPanel: (source: NotificationsPanelSource) => {
        source: NotificationsPanelSource
    }
    openToUnread: () => {
        value: true
    }
    setActiveTab: (tab: NotificationTab) => {
        tab: NotificationTab
    }
    setNotificationsMenuOpen: (isOpen: boolean) => {
        isOpen: boolean
    }
    toggleNotificationsMenu: () => {
        value: true
    }
    toggleNotificationsPanel: (source: NotificationsPanelSource) => {
        source: NotificationsPanelSource
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface notificationsMenuLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        isNotificationsPanelActive: (isLayoutPanelVisible: boolean, activePanelIdentifier: string) => boolean
    }
}

export type notificationsMenuLogicType = MakeLogicType<
    notificationsMenuLogicValues,
    notificationsMenuLogicActions,
    Record<string, any>,
    notificationsMenuLogicMeta
>

export const notificationsMenuLogic = kea<notificationsMenuLogicType>([
    path(['lib', 'components', 'NotificationsMenu', 'notificationsMenuLogic']),
    connect(() => ({
        actions: [
            sidePanelNotificationsLogic,
            ['loadArchivedNotifications'],
            panelLayoutLogic,
            ['setActivePanelIdentifier', 'showLayoutPanel', 'closePanel'],
        ],
        values: [
            sidePanelNotificationsLogic,
            ['inAppUnreadCount'],
            panelLayoutLogic,
            ['activePanelIdentifier', 'isLayoutPanelVisible'],
        ],
    })),
    actions({
        setNotificationsMenuOpen: (isOpen: boolean) => ({ isOpen }),
        toggleNotificationsMenu: true,
        setActiveTab: (tab: NotificationTab) => ({ tab }),
        openToUnread: true,
        openNotificationsPanel: (source: NotificationsPanelSource) => ({ source }),
        toggleNotificationsPanel: (source: NotificationsPanelSource) => ({ source }),
    }),
    reducers({
        isNotificationsMenuOpen: [
            false as boolean,
            {
                setNotificationsMenuOpen: (_, { isOpen }) => isOpen,
                toggleNotificationsMenu: (state) => !state,
                openToUnread: () => true,
            },
        ],
        activeTab: [
            'all' as NotificationTab,
            {
                setActiveTab: (_, { tab }) => tab,
                openToUnread: () => 'unread' as NotificationTab,
            },
        ],
    }),
    selectors({
        isNotificationsPanelActive: [
            (s) => [s.isLayoutPanelVisible, s.activePanelIdentifier],
            (isLayoutPanelVisible: boolean, activePanelIdentifier: string): boolean =>
                isLayoutPanelVisible && activePanelIdentifier === 'Notifications',
        ],
    }),
    listeners(({ actions, values }) => ({
        setActiveTab: ({ tab }) => {
            // Archived notifications are a separate data source loaded on demand. Refetch every time
            // the tab is opened so items just archived from the other tabs always show up.
            if (tab === 'archived') {
                actions.loadArchivedNotifications()
            }
        },
        toggleNotificationsPanel: ({ source }) => {
            if (values.isNotificationsPanelActive) {
                actions.closePanel()
                return
            }
            actions.openNotificationsPanel(source)
        },
        openNotificationsPanel: ({ source }) => {
            // Captured before the panel renders, so `unread_count` is what the badge was showing.
            // Reading it after opening would race the auto-mark-read pass over the visible rows.
            posthog.capture('notification panel opened', {
                source,
                unread_count: values.inAppUnreadCount,
                had_unread: values.inAppUnreadCount > 0,
            })
            // The Notifications side panel's visibility is owned by panelLayoutLogic, so opening it
            // has to go through these actions.
            actions.setActivePanelIdentifier('Notifications')
            actions.showLayoutPanel(true)
        },
        openToUnread: () => {
            // A critical toast can be clicked while the panel is already open. That only switches
            // to the unread tab (the reducer above), so it must not report a panel open.
            if (values.isNotificationsPanelActive) {
                return
            }
            actions.openNotificationsPanel('critical_toast')
        },
    })),
])