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.

21.8 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'

import {
    IconArchive,
    IconCheckCircle,
    IconCircleDashed,
    IconCopy,
    IconCursorClick,
    IconEye,
    IconMegaphone,
    IconRefresh,
    IconRocket,
    IconStopFilled,
    IconTrash,
} from '@posthog/icons'
import { LemonButton, LemonDialog, LemonDivider, LemonInput, LemonTable, LemonTag, Spinner } from '@posthog/lemon-ui'

import { dayjs } from 'lib/dayjs'
import { More } from 'lib/lemon-ui/LemonButton/More'
import { LemonTableColumn } from 'lib/lemon-ui/LemonTable'
import { createdAtColumn } from 'lib/lemon-ui/LemonTable/columnUtils'
import { LemonTableLink } from 'lib/lemon-ui/LemonTable/LemonTableLink'
import { cn } from 'lib/utils/css-classes'
import stringWithWBR from 'lib/utils/stringWithWBR'
import { urls } from 'scenes/urls'

import { ProductTour, ProgressStatus } from '~/types'

import {
    ProductToursTabs,
    getProductTourStatus,
    isAnnouncement,
    isProductTourRunning,
    productToursLogic,
} from '../productToursLogic'

export function ProductTourStatusTag({
    tour,
    isEditing,
    draftSaveStatus,
}: {
    tour: ProductTour
    isEditing?: boolean
    draftSaveStatus?: 'unsaved' | 'saving' | 'saved' | null
}): JSX.Element {
    const status = getProductTourStatus(tour)

    const statusConfig: Record<
        ProgressStatus,
        { label: string; type: 'success' | 'warning' | 'default' | 'completion' }
    > = {
        [ProgressStatus.Draft]: { label: 'Draft', type: 'default' },
        [ProgressStatus.Running]: { label: 'Running', type: 'success' },
        [ProgressStatus.Complete]: { label: 'Complete', type: 'completion' },
    }

    const config = statusConfig[status]
    return (
        <LemonTag type={config.type}>
            {isEditing && (
                <>
                    {draftSaveStatus === 'unsaved' && <IconCircleDashed />}
                    {draftSaveStatus === 'saving' && <Spinner />}
                    {draftSaveStatus === 'saved' && <IconCheckCircle className="text-success" />}
                </>
            )}
            {config.label}
        </LemonTag>
    )
}

export function ProductToursTable(): JSX.Element {
    const { filteredProductTours, productToursLoading, searchTerm, tab } = useValues(productToursLogic)
    const { deleteProductTour, updateProductTour, duplicateProductTour, setSearchTerm } = useActions(productToursLogic)

    return (
        <>
            <div className={cn('flex flex-wrap gap-2 justify-between mb-0')}>
                <LemonInput
                    type="search"
                    placeholder="Search for product tours"
                    onChange={setSearchTerm}
                    value={searchTerm || ''}
                />
            </div>
            <LemonTable
                dataSource={filteredProductTours}
                defaultSorting={{
                    columnKey: 'created_at',
                    order: -1,
                }}
                rowKey="id"
                nouns={['product tour', 'product tours']}
                data-attr="product-tours-table"
                emptyState={
                    tab === ProductToursTabs.Active
                        ? 'No product tours. Create a new tour to get started!'
                        : 'No archived product tours found'
                }
                loading={productToursLoading}
                columns={[
                    {
                        dataIndex: 'name',
                        title: 'Name',
                        render: function RenderName(_, tour) {
                            return (
                                <div className="flex gap-2 items-center justify-start">
                                    <LemonTag
                                        type="option"
                                        icon={isAnnouncement(tour) ? <IconMegaphone /> : <IconCursorClick />}
                                    >
                                        {isAnnouncement(tour) ? 'Announcement' : 'Tour'}
                                    </LemonTag>
                                    <LemonTableLink
                                        to={urls.productTour(tour.id)}
                                        title={stringWithWBR(tour.name, 17)}
                                    />
                                </div>
                            )
                        },
                    },
                    {
                        title: 'Steps',
                        render: function RenderSteps(_, tour) {
                            return isAnnouncement(tour) ? '-' : (tour.content?.steps?.length ?? 0)
                        },
                    },
                    ...(tab === ProductToursTabs.Active
                        ? [
                              createdAtColumn<ProductTour>() as LemonTableColumn<
                                  ProductTour,
                                  keyof ProductTour | undefined
                              >,
                              {
                                  title: 'Status',
                                  width: 100,
                                  render: function Render(_: any, tour: ProductTour) {
                                      return <ProductTourStatusTag tour={tour} />
                                  },
                              } as LemonTableColumn<ProductTour, keyof ProductTour | undefined>,
                          ]
                        : []),
                    {
                        width: 0,
                        render: function Render(_, tour: ProductTour) {
                            return (
                                <More
                                    overlay={
                                        <>
                                            <LemonButton
                                                fullWidth
                                                icon={<IconEye className="w-4" />}
                                                onClick={() => router.actions.push(urls.productTour(tour.id))}
                                            >
                                                View
                                            </LemonButton>
                                            <LemonButton
                                                fullWidth
                                                icon={<IconCopy className="w-4" />}
                                                onClick={() => duplicateProductTour(tour)}
                                            >
                                                Duplicate
                                            </LemonButton>
                                            {!tour.start_date && (
                                                <LemonButton
                                                    fullWidth
                                                    icon={<IconRocket className="w-4" />}
                                                    disabledReason={
                                                        tour.archived
                                                            ? 'Restore your tour before launching.'
                                                            : undefined
                                                    }
                                                    onClick={() => {
                                                        LemonDialog.open({
                                                            title: 'Launch this product tour?',
                                                            content: (
                                                                <div className="text-sm text-secondary">
                                                                    The tour will immediately start displaying to users
                                                                    matching the display conditions.
                                                                </div>
                                                            ),
                                                            primaryButton: {
                                                                children: 'Launch',
                                                                type: 'primary',
                                                                onClick: () => {
                                                                    updateProductTour({
                                                                        id: tour.id,
                                                                        updatePayload: {
                                                                            start_date: dayjs().toISOString(),
                                                                        },
                                                                    })
                                                                },
                                                                size: 'small',
                                                            },
                                                            secondaryButton: {
                                                                children: 'Cancel',
                                                                type: 'tertiary',
                                                                size: 'small',
                                                            },
                                                        })
                                                    }}
                                                >
                                                    Launch tour
                                                </LemonButton>
                                            )}
                                            {isProductTourRunning(tour) && (
                                                <LemonButton
                                                    fullWidth
                                                    icon={<IconStopFilled className="w-4" />}
                                                    onClick={() => {
                                                        LemonDialog.open({
                                                            title: 'Stop this product tour?',
                                                            content: (
                                                                <div className="text-sm text-secondary">
                                                                    The tour will no longer be visible to your users.
                                                                </div>
                                                            ),
                                                            primaryButton: {
                                                                children: 'Stop',
                                                                type: 'primary',
                                                                onClick: () => {
                                                                    updateProductTour({
                                                                        id: tour.id,
                                                                        updatePayload: {
                                                                            end_date: dayjs().toISOString(),
                                                                        },
                                                                    })
                                                                },
                                                                size: 'small',
                                                            },
                                                            secondaryButton: {
                                                                children: 'Cancel',
                                                                type: 'tertiary',
                                                                size: 'small',
                                                            },
                                                        })
                                                    }}
                                                >
                                                    Stop tour
                                                </LemonButton>
                                            )}
                                            {tour.end_date && !tour.archived && (
                                                <LemonButton
                                                    fullWidth
                                                    icon={<IconRefresh className="w-4" />}
                                                    onClick={() => {
                                                        LemonDialog.open({
                                                            title: 'Resume this product tour?',
                                                            content: (
                                                                <div className="text-sm text-secondary">
                                                                    Once resumed, the tour will be visible to your users
                                                                    again.
                                                                </div>
                                                            ),
                                                            primaryButton: {
                                                                children: 'Resume',
                                                                type: 'primary',
                                                                onClick: () => {
                                                                    updateProductTour({
                                                                        id: tour.id,
                                                                        updatePayload: {
                                                                            end_date: null,
                                                                        },
                                                                    })
                                                                },
                                                                size: 'small',
                                                            },
                                                            secondaryButton: {
                                                                children: 'Cancel',
                                                                type: 'tertiary',
                                                                size: 'small',
                                                            },
                                                        })
                                                    }}
                                                >
                                                    Resume tour
                                                </LemonButton>
                                            )}
                                            <LemonDivider />
                                            {tour.archived && (
                                                <LemonButton
                                                    fullWidth
                                                    icon={<IconArchive className="w-4" />}
                                                    onClick={() => {
                                                        updateProductTour({
                                                            id: tour.id,
                                                            updatePayload: { archived: false },
                                                        })
                                                    }}
                                                >
                                                    Restore
                                                </LemonButton>
                                            )}
                                            {!tour.archived && (
                                                <LemonButton
                                                    fullWidth
                                                    icon={<IconArchive className="w-4" />}
                                                    disabledReason={
                                                        isProductTourRunning(tour)
                                                            ? 'Stop your tour before archiving.'
                                                            : undefined
                                                    }
                                                    onClick={() => {
                                                        LemonDialog.open({
                                                            title: `Archive tour ${tour.name}?`,
                                                            content: (
                                                                <div className="text-sm text-secondary">
                                                                    This action will remove the tour from your active
                                                                    tours list. It can be restored at any time.
                                                                </div>
                                                            ),
                                                            primaryButton: {
                                                                children: 'Archive',
                                                                type: 'primary',
                                                                onClick: () => {
                                                                    updateProductTour({
                                                                        id: tour.id,
                                                                        updatePayload: {
                                                                            archived: true,
                                                                        },
                                                                    })
                                                                },
                                                                size: 'small',
                                                            },
                                                            secondaryButton: {
                                                                children: 'Cancel',
                                                                type: 'tertiary',
                                                                size: 'small',
                                                            },
                                                        })
                                                    }}
                                                >
                                                    Archive
                                                </LemonButton>
                                            )}
                                            <LemonButton
                                                status="danger"
                                                icon={<IconTrash className="w-4" />}
                                                disabledReason={
                                                    isProductTourRunning(tour)
                                                        ? 'Stop your tour before deleting.'
                                                        : undefined
                                                }
                                                onClick={() => {
                                                    LemonDialog.open({
                                                        title: `Delete tour ${tour.name}?`,
                                                        content: (
                                                            <div className="text-sm text-secondary">
                                                                This action cannot be undone. All tour data will be
                                                                permanently removed.
                                                            </div>
                                                        ),
                                                        primaryButton: {
                                                            children: 'Delete',
                                                            type: 'primary',
                                                            onClick: () => deleteProductTour(tour.id),
                                                            size: 'small',
                                                        },
                                                        secondaryButton: {
                                                            children: 'Cancel',
                                                            type: 'tertiary',
                                                            size: 'small',
                                                        },
                                                    })
                                                }}
                                                fullWidth
                                            >
                                                Delete
                                            </LemonButton>
                                        </>
                                    }
                                />
                            )
                        },
                    },
                ]}
            />
        </>
    )
}