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.8 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { router } from 'kea-router'
import type { LocationChangedPayload } from 'kea-router/lib/types'
import posthog from 'posthog-js'

import { urls } from 'scenes/urls'

import { forYouReportIds, type TodayForYouRows } from './todayForYouRows'
import { type TodayMoreReports, type TodayReportOpenContext, todayLogic } from './todayLogic'
import { navigateWithCrossfade } from './todayViewTransition'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayReportQueueLogicValues {
    canLoadMoreReports: boolean // todayLogic
    forYouRows: TodayForYouRows // todayLogic
    moreReportsInInbox: number // todayLogic
    moreReportsLoading: boolean // todayLogic
    reportId: string | null // todayLogic
    adjacentReportIds: string[]
    atQueueEnd: boolean
    reportPosition: number | null
    reportQueue: string[] | null
    reportQueueIds: string[]
    restInInbox: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayReportQueueLogicActions {
    locationChanged: ({
        method,
        pathname,
        search,
        searchParams,
        hash,
        hashParams,
        initial,
        url,
        routerState,
    }: LocationChangedPayload) => {
        hash: string
        hashParams: Record<string, any>
        initial: boolean
        method: 'POP' | 'PUSH' | 'REPLACE'
        pathname: string
        routerState: Record<string, any>
        search: string
        searchParams: Record<string, any>
        url: string
    } // router
    loadMoreReports: () => any // todayLogic
    loadMoreReportsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    } // todayLogic
    loadMoreReportsSuccess: (
        moreReports: TodayMoreReports | null,
        payload?: any
    ) => {
        moreReports: TodayMoreReports | null
        payload?: any
    } // todayLogic
    setReportOpenContext: (context: TodayReportOpenContext | null) => {
        context: TodayReportOpenContext | null
    } // todayLogic
    leaveReport: () => {
        value: true
    }
    openAdjacentReport: (step: 1 | -1) => {
        step: 1 | -1
    }
    setReportQueue: (reportIds: string[] | null) => {
        reportIds: string[] | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayReportQueueLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        reportQueueIds: (reportQueue: string[] | null, forYouRows: TodayForYouRows) => string[]
        reportPosition: (reportId: string | null, reportQueueIds: string[]) => number | null
        adjacentReportIds: (reportPosition: number | null, reportQueueIds: string[]) => string[]
        atQueueEnd: (reportPosition: number | null, reportQueueIds: string[], canLoadMoreReports: boolean) => boolean
        restInInbox: (atQueueEnd: boolean, moreReportsInInbox: number) => boolean
    }
}

export type todayReportQueueLogicType = MakeLogicType<
    todayReportQueueLogicValues,
    todayReportQueueLogicActions,
    Record<string, any>,
    todayReportQueueLogicMeta
>

export const todayReportQueueLogic = kea<todayReportQueueLogicType>([
    path(['scenes', 'project-homepage', 'today', 'todayReportQueueLogic']),
    connect(() => ({
        values: [
            todayLogic,
            ['reportId', 'forYouRows', 'canLoadMoreReports', 'moreReportsLoading', 'moreReportsInInbox'],
        ],
        actions: [
            router,
            ['locationChanged'],
            todayLogic,
            ['loadMoreReports', 'loadMoreReportsSuccess', 'loadMoreReportsFailure', 'setReportOpenContext'],
        ],
    })),
    actions({
        setReportQueue: (reportIds: string[] | null) => ({ reportIds }),
        openAdjacentReport: (step: 1 | -1) => ({ step }),
        leaveReport: true,
    }),
    reducers({
        reportQueue: [null as string[] | null, { setReportQueue: (_, { reportIds }) => reportIds }],
    }),
    selectors({
        reportQueueIds: [
            (s) => [s.reportQueue, s.forYouRows],
            (reportQueue: string[] | null, rows: TodayForYouRows): string[] => {
                const live = forYouReportIds(rows.rows)
                if (!reportQueue) {
                    return live
                }
                const queued = new Set(reportQueue)
                return [...reportQueue, ...live.filter((id) => !queued.has(id))]
            },
        ],
        reportPosition: [
            (s) => [s.reportId, s.reportQueueIds],
            (reportId: string | null, reportQueueIds: string[]): number | null => {
                const index = reportId ? reportQueueIds.indexOf(reportId) : -1
                return index === -1 ? null : index + 1
            },
        ],
        adjacentReportIds: [
            (s) => [s.reportPosition, s.reportQueueIds],
            (reportPosition: number | null, reportQueueIds: string[]): string[] =>
                reportPosition === null
                    ? []
                    : [reportQueueIds[reportPosition - 2], reportQueueIds[reportPosition]].filter(
                          (id): id is string => !!id
                      ),
        ],
        atQueueEnd: [
            (s) => [s.reportPosition, s.reportQueueIds, s.canLoadMoreReports],
            (reportPosition: number | null, reportQueueIds: string[], canLoadMoreReports: boolean): boolean =>
                reportPosition !== null && reportPosition >= reportQueueIds.length && !canLoadMoreReports,
        ],
        restInInbox: [
            (s) => [s.atQueueEnd, s.moreReportsInInbox],
            (atQueueEnd: boolean, moreReportsInInbox: number): boolean => atQueueEnd && moreReportsInInbox > 0,
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        openAdjacentReport: ({ step }) => {
            const { reportQueueIds, reportPosition, canLoadMoreReports, moreReportsLoading } = values
            if (reportPosition === null || moreReportsLoading) {
                return
            }
            const target = reportQueueIds[reportPosition - 1 + step]
            if (target) {
                posthog.capture('today report stepped', {
                    step,
                    position: reportPosition,
                    total: reportQueueIds.length,
                })
                actions.setReportOpenContext({
                    reportId: target,
                    list: null,
                    rank: reportPosition + step,
                    listSize: reportQueueIds.length,
                    source: 'step',
                })
                router.actions.push(urls.todayReport(target))
            } else if (step === 1 && canLoadMoreReports) {
                cache.openNextAfterLoad = values.reportId
                actions.loadMoreReports()
            } else if (step === 1 && values.moreReportsInInbox > 0) {
                router.actions.push(urls.inbox())
            }
        },
        leaveReport: () => {
            navigateWithCrossfade(urls.projectHomepage())
        },
        locationChanged: () => {
            if (values.reportId === null) {
                if (values.reportQueue !== null) {
                    actions.setReportQueue(null)
                }
            } else if (values.reportQueue === null) {
                actions.setReportQueue(values.reportQueueIds)
            }
        },
        loadMoreReportsSuccess: () => {
            const openNextFrom = cache.openNextAfterLoad
            cache.openNextAfterLoad = null
            if (openNextFrom && openNextFrom === values.reportId) {
                actions.openAdjacentReport(1)
            }
        },
        loadMoreReportsFailure: () => {
            cache.openNextAfterLoad = null
        },
    })),
])