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.

2.0 KB
Raw Blame
import { useEffect } from 'react'

import { LemonLabel } from '@posthog/lemon-ui'

import { LemonField } from 'lib/lemon-ui/LemonField'

import { OrganizationSelector } from './OrganizationSelector'
import type { OrganizationOption } from './types'

type RequiredOrganizationAccessSelectorProps = {
    organizations: OrganizationOption[]
    autoSelectFirst?: boolean
}

export const RequiredOrganizationAccessSelector = ({
    organizations,
    autoSelectFirst = false,
}: RequiredOrganizationAccessSelectorProps): JSX.Element => {
    return (
        <div className="flex flex-col gap-2">
            <LemonLabel>Select organization</LemonLabel>
            <p className="text-sm text-muted mb-2">This application requires access to a specific organization.</p>
            <LemonField name="scoped_organizations">
                {({ value, onChange }) => {
                    const arrayValue = Array.isArray(value) ? value : []

                    // eslint-disable-next-line react-hooks/rules-of-hooks -- effect lives in a LemonField render prop, which is always invoked
                    useEffect(() => {
                        if (autoSelectFirst && arrayValue.length === 0 && organizations.length > 0) {
                            onChange([organizations[0].id])
                        }
                        // eslint-disable-next-line react-hooks/exhaustive-deps -- autoSelectFirst is a reactive prop; keep it so the effect re-runs if it changes
                    }, [autoSelectFirst, arrayValue.length, onChange])

                    return (
                        <OrganizationSelector
                            organizations={organizations}
                            mode="single"
                            value={arrayValue.length > 0 ? [arrayValue[0]] : []}
                            onChange={(val: string[]) => onChange(val.length > 0 ? [val[0]] : [])}
                        />
                    )
                }}
            </LemonField>
        </div>
    )
}