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.

3.5 KB
Raw Blame
import { MakeLogicType, actions, kea, path, reducers, selectors } from 'kea'

import { now } from 'lib/dayjs'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'

export const defaultQuickEmojis = ['💖', '👍', '🤔', '👎', '🌶️']

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface emojiUsageLogicValues {
    favouriteEmojis: string[]
    usedEmojis: Record<string, number[]>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface emojiUsageLogicActions {
    emojiUsed: (emoji: string) => {
        emoji: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface emojiUsageLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        favouriteEmojis: (usedEmojis: Record<string, number[]>) => string[]
    }
}

export type emojiUsageLogicType = MakeLogicType<
    emojiUsageLogicValues,
    emojiUsageLogicActions,
    Record<string, any>,
    emojiUsageLogicMeta
>

export const emojiUsageLogic = kea<emojiUsageLogicType>([
    path(['lib', 'lemon-ui', 'LemonTextArea', 'emojiUsage', 'logic']),
    actions({
        emojiUsed: (emoji: string) => ({ emoji }),
    }),
    reducers({
        usedEmojis: [
            {} as Record<string, number[]>,
            { persist: true },
            {
                emojiUsed: (state, { emoji }) => {
                    const currentTime = now().valueOf()
                    const thirtyDaysAgo = currentTime - 30 * 24 * 60 * 60 * 1000

                    const newState = { ...state, [emoji]: state[emoji] || [] }
                    newState[emoji] = [...newState[emoji], currentTime]

                    newState[emoji] = newState[emoji].filter((timestamp: number) => timestamp > thirtyDaysAgo)

                    if (newState[emoji].length === 0) {
                        delete newState[emoji]
                    } else {
                        // Limit to max 10 timestamps per emoji to prevent memory issues
                        if (newState[emoji].length > 10) {
                            newState[emoji] = newState[emoji].slice(-10)
                        }
                    }

                    return newState
                },
            },
        ],
    }),
    selectors({
        favouriteEmojis: [
            (s) => [s.usedEmojis],
            (usedEmojis: Record<string, number[]>): string[] => {
                // Get user's favorite emojis sorted by usage count
                const userFavorites = Object.entries(usedEmojis)
                    .map(([emoji, timestamps]) => ({
                        emoji,
                        count: timestamps.length,
                    }))
                    .sort((a, b) => b.count - a.count) // Sort by usage count descending
                    .slice(0, 5) // Take top 5
                    .map(({ emoji }) => emoji) // Extract just the emoji strings

                // If we have fewer than 5 favorites, fill with quickEmojis (avoiding duplicates)
                if (userFavorites.length < 5) {
                    const remainingSlots = 5 - userFavorites.length
                    const availableQuickEmojis = defaultQuickEmojis.filter((emoji) => !userFavorites.includes(emoji))
                    const fillEmojis = availableQuickEmojis.slice(0, remainingSlots)
                    return [...userFavorites, ...fillEmojis]
                }

                return userFavorites
            },
        ],
    }),
    permanentlyMount(),
])