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.

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

import { LemonButton } from '@posthog/lemon-ui'
import { Button } from '@posthog/quill'

import { AccessControlAction } from 'lib/components/AccessControlAction'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
import { Shortcut } from 'lib/components/Shortcuts/Shortcut'
import { keyBinds } from 'lib/components/Shortcuts/shortcuts'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { LemonTabs } from 'lib/lemon-ui/LemonTabs'
import { LinkPrimitive } from 'lib/lemon-ui/Link'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene, SceneExport } from 'scenes/sceneTypes'
import { SurveyFeedbackButton } from 'scenes/surveys/components/SurveyFeedbackButton'
import { SurveyNotificationsList } from 'scenes/surveys/components/SurveyNotificationsList'
import { SurveysTable } from 'scenes/surveys/components/SurveysTable'
import { urls } from 'scenes/urls'

import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { AccessControlLevel, AccessControlResourceType, ActivityScope } from '~/types'

import { surveysEmptyState } from 'products/surveys/frontend/emptyState/surveysEmptyState'

import { SURVEY_CREATED_SOURCE } from './constants'
import { DuplicateToProjectModal } from './DuplicateToProjectModal'
import { SurveySettings, SurveysDisabledBanner } from './SurveySettings'
import { SurveysTabs, surveysLogic } from './surveysLogic'

export const scene: SceneExport = {
    component: Surveys,
    logic: surveysLogic,
    productKey: ProductKey.SURVEYS,
    emptyState: surveysEmptyState,
}

function NewSurveyButton(): JSX.Element {
    const { addProductIntent } = useActions(surveysLogic)
    const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')

    const trackAddNewClick = (): void => {
        addProductIntent({
            product_type: ProductKey.SURVEYS,
            intent_context: ProductIntentContext.SURVEY_ADD_NEW,
        })
    }

    return (
        <AccessControlAction resourceType={AccessControlResourceType.Survey} minAccessLevel={AccessControlLevel.Editor}>
            <Shortcut
                name="NewSurvey"
                keybind={[keyBinds.new]}
                intent="New survey"
                interaction="click"
                scope={Scene.Surveys}
            >
                {todayRailNav ? (
                    <Button
                        variant="primary"
                        render={<LinkPrimitive to={urls.surveyWizard()} />}
                        data-attr="new-survey"
                        tooltip="New survey"
                        onClick={trackAddNewClick}
                    >
                        New survey
                    </Button>
                ) : (
                    <LemonButton
                        size="small"
                        to={urls.surveyWizard()}
                        type="primary"
                        data-attr="new-survey"
                        tooltip="New survey"
                        onClick={trackAddNewClick}
                    >
                        New survey
                    </LemonButton>
                )}
            </Shortcut>
        </AccessControlAction>
    )
}

function Surveys(): JSX.Element {
    const { tab } = useValues(surveysLogic)
    const { setTab, handleMaxSurveyCreated } = useActions(surveysLogic)

    return (
        <SceneContent>
            <SceneTitleSection
                name={sceneConfigurations[Scene.Surveys].name}
                description={sceneConfigurations[Scene.Surveys].description}
                resourceType={{
                    type: sceneConfigurations[Scene.Surveys].iconType || 'default_icon_type',
                }}
                actions={
                    <>
                        <SurveyFeedbackButton />
                        <NewSurveyButton />
                    </>
                }
                maxToolProps={{
                    identifier: 'create_survey',
                    initialMaxPrompt: 'Create a survey to collect ',
                    suggestions: [
                        'Create an NPS survey for customers who completed checkout',
                        'Create a feedback survey asking about our new dashboard',
                        'Create a product-market fit survey for trial users',
                        'Create a quick satisfaction survey for support interactions',
                    ],
                    context: {},
                    callback: (toolOutput) => handleMaxSurveyCreated(toolOutput, SURVEY_CREATED_SOURCE.MAX_AI),
                }}
            />
            <SurveysDisabledBanner />
            <LemonTabs
                activeKey={tab}
                onChange={(newTab) => setTab(newTab as SurveysTabs)}
                tabs={[
                    { key: SurveysTabs.Active, label: 'Active' },
                    { key: SurveysTabs.Archived, label: 'Archived' },
                    { key: SurveysTabs.Notifications, label: 'Notifications' },
                    { key: SurveysTabs.History, label: 'History' },
                    { key: SurveysTabs.Settings, label: 'Settings' },
                ]}
                sceneInset={true}
            />
            {tab === SurveysTabs.Settings && <SurveySettings />}
            {tab === SurveysTabs.Notifications && <SurveyNotificationsList />}

            {tab === SurveysTabs.History && <ActivityLog scope={ActivityScope.SURVEY} />}

            {(tab === SurveysTabs.Active || tab === SurveysTabs.Archived) && <SurveysTable />}
            <DuplicateToProjectModal />
        </SceneContent>
    )
}