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

import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { objectsEqual } from 'lib/utils/objects'
import { sceneLogic } from 'scenes/sceneLogic'

import { newestByName } from './todayNewestByName'
import { TODAY_PEOPLE_RECENT_KEY, TodayPeopleRecent } from './todayPeople'
import { TODAY_USER_PERSIST_OPTIONS } from './todayUserPersistOptions'

export const TODAY_PEOPLE_RECENTS_LIMIT = 10

/** Puts a visit first. Reloading the same page changes nothing, so the list stays stable while a page loads. */
export function addPeopleRecent(recents: TodayPeopleRecent[], recent: TodayPeopleRecent): TodayPeopleRecent[] {
    const [first] = recents
    if (first?.href === recent.href && first.name === recent.name) {
        return recents
    }
    return [recent, ...recents.filter((item) => item.href !== recent.href)].slice(0, TODAY_PEOPLE_RECENTS_LIMIT)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPeopleRecentsLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentRecent: TodayPeopleRecent | null
    recentRows: TodayPeopleRecent[]
    recents: TodayPeopleRecent[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPeopleRecentsLogicActions {
    addRecent: (recent: TodayPeopleRecent) => {
        recent: TodayPeopleRecent
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPeopleRecentsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        currentRecent: (featureFlags: FeatureFlagsSet, arg: TodayPeopleRecent | null) => TodayPeopleRecent | null
        recentRows: (recents: TodayPeopleRecent[]) => TodayPeopleRecent[]
    }
}

export type todayPeopleRecentsLogicType = MakeLogicType<
    todayPeopleRecentsLogicValues,
    todayPeopleRecentsLogicActions,
    Record<string, any>,
    todayPeopleRecentsLogicMeta
>

/** Mounted with the shell, so a person opened from any pane counts. */
export const todayPeopleRecentsLogic = kea<todayPeopleRecentsLogicType>([
    path(['layout', 'today', 'todayPeopleRecentsLogic']),
    connect(() => ({
        values: [featureFlagLogic, ['featureFlags']],
        logic: [sceneLogic],
    })),
    actions({
        addRecent: (recent: TodayPeopleRecent) => ({ recent }),
    }),
    reducers({
        recents: [
            [] as TodayPeopleRecent[],
            TODAY_USER_PERSIST_OPTIONS,
            {
                addRecent: (state, { recent }) => addPeopleRecent(state, recent),
            },
        ],
    }),
    selectors({
        // Similar to "breadcrumbs" in breadcrumbsLogic: the selector of whichever scene logic is active.
        currentRecent: [
            (s) => [
                s.featureFlags,
                (state, props): TodayPeopleRecent | null => {
                    const activeSceneLogic = sceneLogic.selectors.activeSceneLogic(state, props)
                    if (!activeSceneLogic || !(TODAY_PEOPLE_RECENT_KEY in activeSceneLogic.selectors)) {
                        return null
                    }
                    const activeLoadedScene = sceneLogic.selectors.activeLoadedScene(state, props)
                    try {
                        return activeSceneLogic.selectors[TODAY_PEOPLE_RECENT_KEY](
                            state,
                            activeLoadedScene?.paramsToProps?.(activeLoadedScene?.sceneParams) || props
                        )
                    } catch {
                        // The scene logic may not be mounted yet.
                        return null
                    }
                },
            ],
            (featureFlags: FeatureFlagsSet, recent: TodayPeopleRecent | null): TodayPeopleRecent | null =>
                featureFlags[FEATURE_FLAGS.TODAY_RAIL_NAV] ? recent : null,
            { resultEqualityCheck: objectsEqual },
        ],
        recentRows: [
            (s) => [s.recents],
            (recents: TodayPeopleRecent[]): TodayPeopleRecent[] =>
                newestByName(
                    recents,
                    (item) => item.name,
                    (item) => item.href,
                    TODAY_PEOPLE_RECENTS_LIMIT
                ),
        ],
    }),
    subscriptions(({ actions }) => ({
        currentRecent: (recent: TodayPeopleRecent | null) => {
            if (recent) {
                actions.addRecent(recent)
            }
        },
    })),
])