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.

8.5 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconDownload, IconPencil, IconRefresh, IconWarning } from '@posthog/icons'
import { LemonButton, LemonSelect, LemonTable, LemonTag, Spinner, lemonToast } from '@posthog/lemon-ui'
import { LemonTableColumns } from '@posthog/lemon-ui'
import { Button } from '@posthog/quill'

import { AccessControlAction } from 'lib/components/AccessControlAction'
import { exportedAssetBlob } from 'lib/components/ExportButton/exporter'
import { exportsLogic } from 'lib/components/ExportButton/exportsLogic'
import { getExportDisabledReason, getExportPendingLabel } from 'lib/components/ExportButton/exportStatus'
import { takeScreenshotLogic } from 'lib/components/TakeScreenshot/takeScreenshotLogic'
import { dayjs } from 'lib/dayjs'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { humanFriendlyNumber } from 'lib/utils/numbers'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene, SceneExport } from 'scenes/sceneTypes'
import { settingsSectionSceneScope } from 'scenes/settings/settingsSectionScenes'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { AccessControlLevel, AccessControlResourceType, ExportedAssetType, ExporterFormat } from '~/types'

import { exportsSceneLogic } from './exportsSceneLogic'

const ROW_LIMIT_IN_THOUSANDS = 300

export const scene: SceneExport = {
    component: ExportsScene,
    logic: exportsSceneLogic,
}

function ExportActions({ asset }: { asset: ExportedAssetType }): JSX.Element {
    const { freshUndownloadedExports } = useValues(exportsLogic)
    const { downloadExport } = useActions(exportsLogic)
    const { setBlob } = useActions(takeScreenshotLogic({ screenshotKey: 'exports' }))

    const isNotDownloaded = freshUndownloadedExports.some((fresh) => fresh.id === asset.id)
    const stillCalculating = !asset.has_content && !asset.exception
    const disabledReason = getExportDisabledReason(asset)
    const pendingLabel = getExportPendingLabel(asset)

    const handleEdit = async (): Promise<void> => {
        const r = await exportedAssetBlob(asset)
        if (!r) {
            lemonToast.error('Cannot get the file. Please try again.')
            return
        }
        setBlob(r)
    }

    return (
        <div className="flex gap-2 justify-end items-center">
            {stillCalculating && pendingLabel && (
                <span className="text-xs text-secondary" data-attr="export-pending-label">
                    {pendingLabel}
                </span>
            )}
            {asset.export_format === ExporterFormat.PNG && (
                <AccessControlAction
                    resourceType={AccessControlResourceType.Export}
                    minAccessLevel={AccessControlLevel.Editor}
                    userAccessLevel={asset.user_access_level}
                >
                    <LemonButton
                        tooltip="Edit"
                        size="xsmall"
                        data-attr="export-editor"
                        disabledReason={disabledReason}
                        type={isNotDownloaded ? 'primary' : 'secondary'}
                        icon={<IconPencil />}
                        onClick={() => {
                            void handleEdit()
                        }}
                    />
                </AccessControlAction>
            )}
            <AccessControlAction
                resourceType={AccessControlResourceType.Export}
                minAccessLevel={AccessControlLevel.Viewer}
                userAccessLevel={asset.user_access_level}
            >
                <LemonButton
                    tooltip="Download"
                    size="xsmall"
                    type={isNotDownloaded ? 'primary' : 'secondary'}
                    data-attr="export-download"
                    disabledReason={disabledReason}
                    onClick={() => downloadExport(asset)}
                    sideIcon={
                        stillCalculating ? (
                            <Spinner />
                        ) : asset.has_content ? (
                            <IconDownload className="text-link" />
                        ) : (
                            <IconWarning className="text-link" />
                        )
                    }
                />
            </AccessControlAction>
        </div>
    )
}

export function ExportsScene(): JSX.Element {
    const { exports, exportsLoading, assetFormat } = useValues(exportsLogic)
    const { loadExports, setAssetFormat } = useActions(exportsLogic)
    const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')

    const columns: LemonTableColumns<ExportedAssetType> = [
        {
            title: 'Filename',
            dataIndex: 'filename',
            render: (_, asset) => <span className="font-medium">{asset.filename}</span>,
        },
        {
            title: 'Format',
            dataIndex: 'export_format',
            render: (_, asset) => (
                <div className="flex items-center gap-1">
                    <LemonTag>{asset.export_format}</LemonTag>
                    {asset.export_format === ExporterFormat.CSV && (
                        <span className="text-xs text-secondary">
                            {asset.export_context?.row_limit
                                ? humanFriendlyNumber(asset.export_context.row_limit)
                                : `${ROW_LIMIT_IN_THOUSANDS}k`}{' '}
                            row limit
                        </span>
                    )}
                </div>
            ),
        },
        {
            title: 'Created',
            dataIndex: 'created_at',
            render: (_, asset) => (asset.created_at ? dayjs(asset.created_at).fromNow() : '–'),
            sorter: (a, b) => dayjs(a.created_at).unix() - dayjs(b.created_at).unix(),
        },
        {
            title: 'Expires',
            dataIndex: 'expires_after',
            render: (_, asset) => (asset.expires_after ? dayjs(asset.expires_after).fromNow() : '–'),
        },
        {
            title: '',
            width: 0,
            render: (_, asset) => <ExportActions asset={asset} />,
        },
    ]

    return (
        <SceneContent>
            <SceneTitleSection
                name={sceneConfigurations[Scene.Exports].name}
                description={sceneConfigurations[Scene.Exports].description}
                resourceType={{ type: 'exports' }}
                scope={settingsSectionSceneScope('environment-exports')}
                actions={
                    todayRailNav ? (
                        <Button
                            onClick={loadExports}
                            variant="primary"
                            loading={exportsLoading}
                            data-attr="export-refresh"
                        >
                            <IconRefresh />
                            Refresh
                        </Button>
                    ) : (
                        <LemonButton
                            onClick={loadExports}
                            type="primary"
                            size="small"
                            icon={<IconRefresh />}
                            loading={exportsLoading}
                            data-attr="export-refresh"
                        >
                            Refresh
                        </LemonButton>
                    )
                }
            />

            <div className="flex justify-end mb-2">
                <div className="flex items-center gap-2">
                    <span className="text-sm">Format</span>
                    <LemonSelect
                        size="small"
                        options={[
                            { label: 'All', value: null },
                            ...Object.entries(ExporterFormat).map(([key, value]) => ({
                                label: key,
                                value: value,
                            })),
                        ]}
                        value={assetFormat}
                        onChange={setAssetFormat}
                        disabledReason={exportsLoading ? 'Loading exports...' : undefined}
                    />
                </div>
            </div>

            <LemonTable
                columns={columns}
                dataSource={exports}
                loading={exportsLoading}
                rowKey="id"
                emptyState="No exports matching current filters"
                defaultSorting={{ columnKey: 'created_at', order: -1 }}
            />
        </SceneContent>
    )
}