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.

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

import { IconArrowLeft, IconArrowRight, IconTrash } from '@posthog/icons'
import { LemonBanner, LemonButton, LemonCard, LemonDialog } from '@posthog/lemon-ui'

import { GuidedWizardStep, GuidedWizardStepper } from 'lib/components/GuidedWizard/GuidedWizardStepper'

import { ContentAutopilotOnboardingStep, contentAutopilotLogic } from './contentAutopilotLogic'
import { ContentAutopilotSiteFields } from './ContentAutopilotSiteFields'
import { ContentAutopilotSourceFields } from './ContentAutopilotSourceFields'

const SETUP_STEPS: GuidedWizardStep<ContentAutopilotOnboardingStep>[] = [
    { step: 'site', label: 'Site' },
    { step: 'sources', label: 'Sources' },
]

export const ContentAutopilotSetup = ({ onboarding = false }: { onboarding?: boolean }): JSX.Element => {
    const {
        siteProfiles,
        profile,
        profileDraft,
        onboardingStep,
        discoveredSite,
        discoveredSiteLoading,
        savedProfileLoading,
        deletedProfileLoading,
    } = useValues(contentAutopilotLogic)
    const { cancelOnboarding, deleteProfile, discoverSite, saveProfile, setOnboardingStep } =
        useActions(contentAutopilotLogic)
    const saveDisabledReason = !profileDraft.domain.trim()
        ? 'Enter a site URL'
        : splitHasNoValues(profileDraft.sourceUrls)
          ? 'Add at least one sitemap or source URL'
          : undefined
    const confirmDeleteSite = (): void => {
        if (!profile) {
            return
        }
        LemonDialog.open({
            title: `Delete ${profile.name || profile.domain}?`,
            description:
                'PostHog stops any run in progress and removes this site and its proposals from the workspace. You can add the site again later.',
            primaryButton: {
                children: 'Delete site',
                status: 'danger',
                onClick: () => deleteProfile(profile.id),
            },
            secondaryButton: { children: 'Keep site' },
        })
    }

    if (!onboarding) {
        return (
            <LemonCard hoverEffect={false} className="p-4">
                <div className="mb-4">
                    <h2 className="m-0">Site settings</h2>
                    <p className="m-0 mt-1 text-muted">Change this site's sources and research boundaries.</p>
                </div>
                <div className="flex flex-col gap-5">
                    <ContentAutopilotSiteFields draft={profileDraft} />
                    <ContentAutopilotSourceFields draft={profileDraft} />
                </div>
                <div className="mt-4 flex items-center justify-between gap-2">
                    <LemonButton
                        type="secondary"
                        status="danger"
                        icon={<IconTrash />}
                        onClick={confirmDeleteSite}
                        loading={deletedProfileLoading}
                        data-attr="content-autopilot-delete-site"
                    >
                        Delete site
                    </LemonButton>
                    <LemonButton
                        type="primary"
                        onClick={saveProfile}
                        loading={savedProfileLoading}
                        disabledReason={saveDisabledReason}
                    >
                        Save settings
                    </LemonButton>
                </div>
            </LemonCard>
        )
    }

    return (
        <LemonCard hoverEffect={false} className="mx-auto w-full max-w-3xl p-6">
            <GuidedWizardStepper steps={SETUP_STEPS} currentStep={onboardingStep} aria-label="Setup progress" />
            <div className="my-6 border-t" />

            {onboardingStep === 'site' ? (
                <>
                    <div className="mb-5">
                        <h2 className="m-0">Add a site</h2>
                        <p className="m-0 mt-1 text-muted">
                            PostHog will inspect the public site to fill in the details it can find.
                        </p>
                    </div>
                    <ContentAutopilotSiteFields draft={profileDraft} />
                    <div className="mt-6 flex justify-between">
                        {siteProfiles.length > 0 ? (
                            <LemonButton type="secondary" onClick={cancelOnboarding} disabled={discoveredSiteLoading}>
                                Cancel
                            </LemonButton>
                        ) : (
                            <span />
                        )}
                        <LemonButton
                            type="primary"
                            onClick={discoverSite}
                            loading={discoveredSiteLoading}
                            disabledReason={!profileDraft.domain.trim() ? 'Enter a site URL' : undefined}
                            sideIcon={<IconArrowRight />}
                        >
                            Detect site details
                        </LemonButton>
                    </div>
                </>
            ) : (
                <>
                    <div className="mb-5">
                        <h2 className="m-0">Review sources</h2>
                        <p className="m-0 mt-1 text-muted">Confirm where PostHog may research content.</p>
                    </div>
                    {discoveredSite ? (
                        <LemonBanner type={discoveredSite.sitemap_detected ? 'success' : 'warning'} className="mb-4">
                            {discoveredSite.sitemap_detected
                                ? `Detected ${discoveredSite.source_urls.length} sitemap${
                                      discoveredSite.source_urls.length === 1 ? '' : 's'
                                  }.`
                                : discoveredSite.warnings[0]}
                        </LemonBanner>
                    ) : null}
                    <ContentAutopilotSourceFields draft={profileDraft} />
                    <div className="mt-6 flex justify-between">
                        <LemonButton
                            type="secondary"
                            onClick={() => setOnboardingStep('site')}
                            icon={<IconArrowLeft />}
                        >
                            Back
                        </LemonButton>
                        <LemonButton
                            type="primary"
                            onClick={saveProfile}
                            loading={savedProfileLoading}
                            disabledReason={saveDisabledReason}
                        >
                            Add site
                        </LemonButton>
                    </div>
                </>
            )}
        </LemonCard>
    )
}

const splitHasNoValues = (value: string): boolean => !value.split('\n').some((line) => line.trim())