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.3 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { LemonButton, LemonInput, LemonTable, LemonTableColumns } from '@posthog/lemon-ui'

import { IconOpenInNew } from 'lib/lemon-ui/icons'
import { LemonTableLink } from 'lib/lemon-ui/LemonTable/LemonTableLink'
import { urls } from 'scenes/urls'

import { ActorsQuery } from '~/queries/schema/schema-general'
import { CohortType } from '~/types'

import { saveToCohortModalContentLogic } from './saveToCohortModalContentLogic'

interface SaveToCohortModalContentProps {
    closeModal: () => void
    query: ActorsQuery
}

export function SaveToCohortModalContent({ closeModal, query }: SaveToCohortModalContentProps): JSX.Element {
    const { cohorts, cohortsLoading, pagination, cohortFilters } = useValues(saveToCohortModalContentLogic)
    const { setCohortFilters, saveQueryToCohort } = useActions(saveToCohortModalContentLogic)

    const columns: LemonTableColumns<CohortType> = [
        {
            title: 'Name',
            dataIndex: 'name',
            sorter: (a, b) => (a.name || '').localeCompare(b.name || ''),
            render: function Render(name, { id, description }) {
                return (
                    <LemonTableLink
                        to={urls.cohort(id)}
                        target="_blank"
                        title={
                            name ? (
                                <>
                                    {name} <IconOpenInNew className="shrink-0" />
                                </>
                            ) : (
                                'Untitled'
                            )
                        }
                        description={description}
                    />
                )
            },
        },
        {
            title: null,
            render: function RenderActions(_, cohort) {
                return (
                    <LemonButton
                        size="xsmall"
                        type="primary"
                        disabledReason={
                            cohort.is_calculating
                                ? 'This cohort is still being populated. Try again when it finishes.'
                                : undefined
                        }
                        onClick={() => {
                            saveQueryToCohort(cohort, query)
                            closeModal()
                        }}
                    >
                        Select
                    </LemonButton>
                )
            },
        },
    ]
    return (
        <div className="text-muted mb-2 w-160">
            <LemonInput
                className="w-48 mb-2"
                type="search"
                placeholder="Search for cohorts"
                onChange={(search) => {
                    setCohortFilters({ search: search || undefined, page: 1 })
                }}
                value={cohortFilters.search}
            />
            <LemonTable
                columns={columns}
                loading={cohortsLoading}
                rowKey="id"
                pagination={pagination}
                dataSource={cohorts.results}
                nouns={['cohort', 'cohorts']}
                data-attr="static-cohorts-table"
                useURLForSorting={false}
            />
        </div>
    )
}