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.

13.8 KB
Raw Blame
import type { ColumnDef } from '@tanstack/react-table'
import { useActions } from 'kea'
import { router } from 'kea-router'

import {
    IconChevronDown,
    IconEllipsis,
    IconFolder,
    IconFolderMove,
    IconFolderOpen,
    IconTrash,
    IconX,
} from '@posthog/icons'
import {
    Button,
    Chip,
    ChipGroup,
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuTrigger,
    Text,
    Tooltip,
    TooltipContent,
    TooltipTrigger,
} from '@posthog/quill'

import { TZLabel } from 'lib/components/TZLabel'
import { LinkPrimitive } from 'lib/lemon-ui/Link'
import { urls } from 'scenes/urls'

import { projectTreeDataLogic } from '~/layout/panel-layout/ProjectTree/projectTreeDataLogic'
import { escapePath } from '~/layout/panel-layout/ProjectTree/utils'

import { AnalyticsDraggableRow } from './AnalyticsDraggableRow'
import { AnalyticsFolderDropTarget } from './AnalyticsFolderDropTarget'
import type { FolderRow } from './analyticsListLogic'
import { AnalyticsOwner } from './AnalyticsOwner'
import { AnalyticsPinMenuItem } from './AnalyticsPinMenuItem'
import { analyticsShareLogic } from './analyticsShareLogic'
import { AnalyticsSpaceGlyph } from './AnalyticsSpaceGlyph'
import { AnalyticsTypeIcon } from './AnalyticsTypeIcon'
import {
    ANALYTICS_DRAG_SCOPE,
    ANALYTICS_TYPE_INFO,
    AnalyticsItem,
    analyticsOpenHref,
    isSpacePath,
} from './analyticsUtils'

export type AnalyticsListRow = { kind: 'folder'; folder: FolderRow } | { kind: 'item'; item: AnalyticsItem }

export type AnalyticsListColumns = ColumnDef<AnalyticsListRow>[]

function EmptyCell(): JSX.Element {
    return (
        <Text size="xs" variant="muted">
            –
        </Text>
    )
}

function TimeCell({ time }: { time: string | null }): JSX.Element {
    if (!time) {
        return <EmptyCell />
    }
    return (
        <Text size="xs" variant="muted" render={<span />}>
            <TZLabel time={time} />
        </Text>
    )
}

// Two chips fit the fixed tags column; the rest fold into a count.
const VISIBLE_TAGS = 2

function TagsCell({ tags }: { tags: string[] }): JSX.Element {
    if (!tags.length) {
        return (
            <Text size="xs" variant="muted">
                –
            </Text>
        )
    }
    const hidden = tags.length - VISIBLE_TAGS
    return (
        <div className="flex min-w-0 items-center gap-1">
            <ChipGroup className="min-w-0 max-w-full flex-1 flex-nowrap">
                {tags.slice(0, VISIBLE_TAGS).map((tag) => (
                    <Chip key={tag} size="xs" className="min-w-0 max-w-24 flex-1" title={tag}>
                        <span className="min-w-0 truncate">{tag}</span>
                    </Chip>
                ))}
            </ChipGroup>
            {hidden > 0 && (
                <Text size="xs" variant="muted" className="shrink-0">
                    {`+${hidden}`}
                </Text>
            )}
        </div>
    )
}

function SpaceCell({ item }: { item: AnalyticsItem }): JSX.Element {
    const { openShareToSpace, moveItem } = useActions(analyticsShareLogic)
    const target = { type: item.type, ref: item.id, name: item.name }
    if (!item.spaceName) {
        return (
            <Button
                size="sm"
                tooltip="Add to a collection"
                onClick={() => openShareToSpace(target)}
                data-attr="analytics-list-row-share"
            >
                None
                <IconChevronDown />
            </Button>
        )
    }
    return (
        <DropdownMenu>
            <DropdownMenuTrigger render={<Button size="sm" data-attr="analytics-list-row-space" />}>
                <AnalyticsSpaceGlyph name={item.spaceName} />
                <span>{item.spaceName}</span>
                <IconChevronDown />
            </DropdownMenuTrigger>
            <DropdownMenuContent align="start" className="min-w-60">
                <DropdownMenuItem
                    onClick={() =>
                        router.actions.push(urls.analyticsList({ folder: escapePath(item.spaceName ?? '') }))
                    }
                    data-attr="analytics-list-row-open-space"
                >
                    <IconFolderOpen />
                    <span>{`Open "${item.spaceName}"`}</span>
                </DropdownMenuItem>
                <DropdownMenuItem onClick={() => openShareToSpace(target)} data-attr="analytics-list-row-move">
                    <IconFolderMove />
                    Move to another collection
                </DropdownMenuItem>
                <DropdownMenuItem
                    variant="destructive"
                    onClick={() => moveItem(target, null)}
                    data-attr="analytics-list-row-remove-from-space"
                >
                    <IconX />
                    {`Remove from "${item.spaceName}"`}
                </DropdownMenuItem>
            </DropdownMenuContent>
        </DropdownMenu>
    )
}

function ItemMenuCell({ item }: { item: AnalyticsItem }): JSX.Element {
    const { openShareToSpace } = useActions(analyticsShareLogic)
    return (
        <DropdownMenu>
            <DropdownMenuTrigger
                render={
                    <Button
                        variant="default"
                        size="icon-sm"
                        aria-label={`Actions for ${item.name}`}
                        data-attr="analytics-list-row-menu"
                    />
                }
            >
                <IconEllipsis />
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="min-w-56">
                <AnalyticsPinMenuItem
                    target={{ type: item.type, ref: item.id, name: item.name, href: item.href }}
                    dataAttr="analytics-list-row-pin"
                />
                <DropdownMenuItem
                    onClick={() => openShareToSpace({ type: item.type, ref: item.id, name: item.name })}
                    data-attr="analytics-list-row-move"
                >
                    <IconFolderMove />
                    Move to another collection
                </DropdownMenuItem>
            </DropdownMenuContent>
        </DropdownMenu>
    )
}

function FolderMenuCell({ folder }: { folder: FolderRow }): JSX.Element | null {
    const { deleteItem } = useActions(projectTreeDataLogic)
    const entry = folder.entry
    if (!entry) {
        return null
    }
    return (
        <DropdownMenu>
            <DropdownMenuTrigger
                render={
                    <Button
                        variant="default"
                        size="icon-sm"
                        aria-label={`Actions for ${folder.name}`}
                        data-attr="analytics-list-folder-menu"
                    />
                }
            >
                <IconEllipsis />
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="min-w-48">
                <DropdownMenuItem
                    onClick={() => router.actions.push(urls.analyticsList({ folder: folder.path }))}
                    data-attr="analytics-list-folder-open"
                >
                    <IconFolderOpen />
                    Open folder
                </DropdownMenuItem>
                {isSpacePath(folder.path) && (
                    <AnalyticsPinMenuItem
                        target={{ type: 'folder', ref: folder.path, name: folder.name }}
                        dataAttr="analytics-list-folder-pin"
                    />
                )}
                <DropdownMenuItem
                    variant="destructive"
                    onClick={() => deleteItem(entry, ANALYTICS_DRAG_SCOPE)}
                    data-attr="analytics-list-folder-delete"
                >
                    <IconTrash />
                    Delete folder
                </DropdownMenuItem>
            </DropdownMenuContent>
        </DropdownMenu>
    )
}

function FolderTitle({ folder, draggable }: { folder: FolderRow; draggable: boolean }): JSX.Element {
    const title = (
        <>
            <IconFolder className="size-4 shrink-0 text-muted-foreground" aria-hidden />
            <Button
                variant="link-foreground"
                size="sm"
                nativeButton={false}
                left
                render={<LinkPrimitive to={urls.analyticsList({ folder: folder.path })} />}
                className="min-w-0 shrink"
                data-attr="analytics-list-subfolder"
            >
                <span className="truncate">{folder.name}</span>
            </Button>
        </>
    )
    return (
        <AnalyticsFolderDropTarget path={folder.path} className="flex items-center">
            {draggable && folder.entry ? (
                <AnalyticsDraggableRow entry={folder.entry}>{title}</AnalyticsDraggableRow>
            ) : (
                title
            )}
        </AnalyticsFolderDropTarget>
    )
}

function ItemTitle({
    item,
    backUrl,
    draggable,
}: {
    item: AnalyticsItem
    backUrl: string
    draggable: boolean
}): JSX.Element {
    const title = (
        <>
            <Tooltip>
                <TooltipTrigger
                    render={
                        <span
                            className="flex size-4 shrink-0 items-center justify-center [&_svg]:size-4"
                            // eslint-disable-next-line react/forbid-dom-props
                            style={{ color: ANALYTICS_TYPE_INFO[item.type].color }}
                        />
                    }
                >
                    <AnalyticsTypeIcon type={item.type} />
                </TooltipTrigger>
                <TooltipContent>{ANALYTICS_TYPE_INFO[item.type].label}</TooltipContent>
            </Tooltip>
            <Button
                variant="link-foreground"
                size="sm"
                nativeButton={false}
                left
                render={<LinkPrimitive to={analyticsOpenHref(item, backUrl)} />}
                className="min-w-0 shrink"
                data-attr={`analytics-list-row-${item.type}`}
            >
                <span className="truncate">{item.name}</span>
            </Button>
        </>
    )
    return (
        <span className="flex items-center">
            {draggable && item.entry ? (
                <AnalyticsDraggableRow entry={item.entry}>{title}</AnalyticsDraggableRow>
            ) : (
                title
            )}
        </span>
    )
}

/** The list's columns. The type shows as the title's icon. */
export function analyticsListColumns({
    backUrl,
    showSpace,
    showOwner,
}: {
    backUrl: string
    showSpace: boolean
    showOwner: boolean
}): AnalyticsListColumns {
    const draggable = !showSpace
    return [
        {
            id: 'name',
            minSize: 256,
            header: 'Title',
            meta: { expand: true },
            enableSorting: false,
            cell: ({ row }) =>
                row.original.kind === 'folder' ? (
                    <FolderTitle folder={row.original.folder} draggable={draggable} />
                ) : (
                    <ItemTitle item={row.original.item} backUrl={backUrl} draggable={draggable} />
                ),
        },
        ...(showSpace
            ? ([
                  {
                      id: 'space',
                      header: 'Collection',
                      enableSorting: false,
                      cell: ({ row }) =>
                          row.original.kind === 'item' ? <SpaceCell item={row.original.item} /> : <EmptyCell />,
                  },
              ] satisfies AnalyticsListColumns)
            : []),
        {
            id: 'tags',
            header: 'Tags',
            enableSorting: false,
            cell: ({ row }) =>
                row.original.kind === 'item' ? <TagsCell tags={row.original.item.tags} /> : <EmptyCell />,
        },
        ...(showOwner
            ? ([
                  {
                      id: 'owner',
                      header: 'Owner',
                      enableSorting: false,
                      cell: ({ row }) => (
                          <AnalyticsOwner
                              owner={
                                  row.original.kind === 'item'
                                      ? row.original.item.createdBy
                                      : (row.original.folder.createdBy ?? null)
                              }
                          />
                      ),
                  },
              ] satisfies AnalyticsListColumns)
            : []),
        ...(showSpace
            ? ([
                  {
                      id: 'last_viewed_at',
                      header: 'Last viewed',
                      accessorFn: (row) => (row.kind === 'item' ? (row.item.entry?.last_viewed_at ?? null) : null),
                      sortDescFirst: true,
                      enableMultiSort: false,
                      meta: { sortDataAttr: 'analytics-list-sort-last-viewed' },
                      cell: ({ getValue }) => <TimeCell time={getValue<string | null>()} />,
                  },
              ] satisfies AnalyticsListColumns)
            : []),
        {
            id: 'created_at',
            header: 'Created',
            accessorFn: (row) => (row.kind === 'item' ? row.item.createdAt : (row.folder.createdAt ?? null)),
            enableSorting: showSpace,
            sortDescFirst: true,
            enableMultiSort: false,
            meta: { sortDataAttr: 'analytics-list-sort-created' },
            cell: ({ getValue }) => <TimeCell time={getValue<string | null>()} />,
        },
        {
            id: 'actions',
            enableResizing: false,
            header: '',
            enableSorting: false,
            meta: { align: 'right' },
            cell: ({ row }) =>
                row.original.kind === 'item' ? (
                    <ItemMenuCell item={row.original.item} />
                ) : (
                    <FolderMenuCell folder={row.original.folder} />
                ),
        },
    ]
}