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.1 KB
Raw Blame
import './CompactList.scss'

import clsx from 'clsx'
import { useValues } from 'kea'

import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'
import { themeLogic } from 'lib/logic/themeLogic'

import { EmptyMessage, EmptyMessageProps } from '../EmptyMessage/EmptyMessage'

export interface CompactListProps {
    title?: string | JSX.Element
    viewAllURL?: string
    viewAllDataAttr?: string
    loading: boolean
    /** Whether the last load attempt failed. Rendered instead of `emptyMessage`, even if `items` is empty. */
    error?: boolean
    errorMessage?: EmptyMessageProps
    items: any[]
    emptyMessage?: EmptyMessageProps
    renderRow: (rowData: any, index: number) => JSX.Element
    /** Whether the content should have a fixed height or shrink to fit the content, with a max of the fixed height. Defaults to 'fixed'. */
    contentHeightBehavior?: 'fixed' | 'shrink' | 'fit-content'
}

export function CompactList({
    title,
    viewAllURL,
    viewAllDataAttr,
    loading,
    error,
    errorMessage,
    items,
    emptyMessage,
    renderRow,
    contentHeightBehavior = 'fixed',
}: CompactListProps): JSX.Element {
    const { theme } = useValues(themeLogic)
    return (
        <div
            className="CompactList"
            // eslint-disable-next-line react/forbid-dom-props
            style={theme?.boxStyle}
        >
            {title && (
                <>
                    <div className="CompactList__header">
                        <h3 className="px-2 truncate" title={typeof title === 'string' ? title : undefined}>
                            {title}
                        </h3>
                        {viewAllURL && (
                            <LemonButton to={viewAllURL} data-attr={viewAllDataAttr}>
                                View all
                            </LemonButton>
                        )}
                    </div>
                    <div className="mx-2">
                        {/* This divider has to be within a div, because otherwise horizontal margin ADDS to the width */}
                        <LemonDivider className="my-0" />
                    </div>
                </>
            )}
            <div
                className={clsx('CompactList__content', {
                    'max-h-[16rem] h-auto': contentHeightBehavior === 'shrink',
                    'h-auto': contentHeightBehavior === 'fit-content',
                })}
            >
                {loading ? (
                    <div className="p-2 deprecated-space-y-6">
                        {Array.from({ length: 6 }, (_, index) => (
                            <LemonSkeleton key={index} />
                        ))}
                    </div>
                ) : error && errorMessage ? (
                    <EmptyMessage {...errorMessage} />
                ) : items.length === 0 && emptyMessage ? (
                    <EmptyMessage {...emptyMessage} />
                ) : (
                    <>{items.map((item, index) => renderRow(item, index))}</>
                )}
            </div>
        </div>
    )
}