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 { IconRefresh } from '@posthog/icons'
import { LemonButton, LemonDialog, LemonInput, LemonLabel, LemonSkeleton } from '@posthog/lemon-ui'

import { AuthorizedUrlList } from 'lib/components/AuthorizedUrlList/AuthorizedUrlList'
import { AuthorizedUrlListType } from 'lib/components/AuthorizedUrlList/authorizedUrlListLogic'
import { CodeSnippet } from 'lib/components/CodeSnippet'
import { JSSnippet } from 'lib/components/JSSnippet'
import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { TeamMembershipLevel } from 'lib/constants'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { Link } from 'lib/lemon-ui/Link'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { userHasAccess } from 'lib/utils/accessControlUtils'
import { inStorybook, inStorybookTestRunner } from 'lib/utils/dom'
import { teamLogic } from 'scenes/teamLogic'

import { AccessControlLevel, AccessControlResourceType } from '~/types'

import { BusinessModelConfig } from './BusinessModelConfig'
import { TimezoneConfig } from './TimezoneConfig'
import { WeekStartConfig } from './WeekStartConfig'

export function WebSnippet(): JSX.Element {
    const { currentTeam, currentTeamLoading } = useValues(teamLogic)

    return currentTeamLoading && !currentTeam ? (
        <div className="deprecated-space-y-4">
            <LemonSkeleton className="w-1/2 h-4" />
            <LemonSkeleton repeat={3} />
        </div>
    ) : (
        <JSSnippet />
    )
}

export function TeamVariables(): JSX.Element {
    const { currentTeam, isTeamTokenResetAvailable } = useValues(teamLogic)
    const { resetToken } = useActions(teamLogic)
    const restrictedReason = useRestrictedArea({
        scope: RestrictionScope.Project,
        minimumAccessLevel: TeamMembershipLevel.Admin,
    })
    const { preflight } = useValues(preflightLogic)

    const region = preflight?.region

    const RESET_CONFIRMATION = 'RESET'

    const openDialog = (): void => {
        LemonDialog.openForm({
            maxWidth: 480,
            title: 'Reset project token?',
            description:
                'This will immediately invalidate your current project token. Any apps, websites, or services using it will stop sending data to PostHog until you update them with the new token. This action cannot be undone.',
            initialValues: { confirmation: '' },
            content: (
                <LemonField name="confirmation">
                    <LemonInput
                        placeholder={`Type "${RESET_CONFIRMATION}" to confirm`}
                        autoFocus
                        data-attr="reset-api-key-confirmation-input"
                    />
                </LemonField>
            ),
            errors: {
                confirmation: (value: string) =>
                    (value || '').toUpperCase() !== RESET_CONFIRMATION
                        ? `Type "${RESET_CONFIRMATION}" to confirm`
                        : undefined,
            },
            primaryButtonProps: {
                status: 'danger',
                children: 'Reset token',
            },
            onSubmit: () => {
                resetToken()
            },
        })
    }

    return (
        <div className="space-y-4 max-w-200">
            <div className="border rounded p-4 space-y-3 bg-bg-light">
                <LemonLabel className="mb-0">Project token</LemonLabel>
                <CodeSnippet
                    compact
                    thing="project token"
                    actions={
                        isTeamTokenResetAvailable ? (
                            <LemonButton
                                icon={<IconRefresh />}
                                disabledReason={restrictedReason}
                                noPadding
                                onClick={openDialog}
                                tooltip="Reset token"
                            />
                        ) : undefined
                    }
                >
                    {currentTeam?.api_token || ''}
                </CodeSnippet>
                <p className="text-muted text-xs mb-0">
                    Write-only key for use in <Link to="https://posthog.com/docs/libraries">client libraries</Link>.
                    Safe to use in public apps.
                </p>
            </div>

            <div className="flex gap-4 flex-wrap">
                <div className="border rounded p-4 space-y-3 bg-bg-light flex-1 min-w-60">
                    <LemonLabel className="mb-0">Project ID</LemonLabel>
                    <CodeSnippet compact thing="project ID">
                        {String(currentTeam?.id || '')}
                    </CodeSnippet>
                    <p className="text-muted text-xs mb-0">
                        Use this ID in the <Link to="https://posthog.com/docs/api">PostHog API</Link>.
                    </p>
                </div>
                {region ? (
                    <div className="border rounded p-4 space-y-3 bg-bg-light flex-1 min-w-60">
                        <LemonLabel className="mb-0">Region</LemonLabel>
                        <CodeSnippet compact thing="project region">
                            {`${region} Cloud`}
                        </CodeSnippet>
                        <p className="text-muted text-xs mb-0">Where your PostHog data is hosted.</p>
                    </div>
                ) : null}
            </div>
        </div>
    )
}

export function TeamTimezone({ displayWarning = true }: { displayWarning?: boolean }): JSX.Element {
    return (
        <div className="flex flex-col sm:flex-row gap-8">
            <div className="flex flex-col gap-2 flex-1 max-w-120">
                <LemonLabel id="timezone">Time zone</LemonLabel>
                <TimezoneConfig displayWarning={displayWarning} />
            </div>
            <div className="flex flex-col gap-2">
                <LemonLabel id="timezone">Week starts on</LemonLabel>
                <WeekStartConfig displayWarning={displayWarning} />
            </div>
        </div>
    )
}

export function TeamBusinessModel(): JSX.Element {
    return (
        <div className="deprecated-space-y-2">
            <LemonLabel id="business-model">Business model</LemonLabel>
            <BusinessModelConfig />
        </div>
    )
}

export function TeamAuthorizedURLs(): JSX.Element {
    // In Storybook, allow editing by default since we don't have full app context
    const canEdit =
        inStorybook() || inStorybookTestRunner()
            ? true
            : userHasAccess(AccessControlResourceType.WebAnalytics, AccessControlLevel.Editor)

    return (
        <AuthorizedUrlList
            type={AuthorizedUrlListType.WEB_ANALYTICS}
            allowWildCards={false}
            allowAdd={canEdit}
            allowDelete={canEdit}
            displaySuggestions={canEdit}
        />
    )
}