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.

7.9 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { useState } from 'react'

import { IconX } from '@posthog/icons'

import api from 'lib/api'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonInput } from 'lib/lemon-ui/LemonInput'
import { LemonLabel } from 'lib/lemon-ui/LemonLabel'
import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { LemonTag } from 'lib/lemon-ui/LemonTag/LemonTag'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import { PropertyDefinition, materializedColumnsLogic } from './materializedColumnsLogic'

export function CreateSlotModal(): JSX.Element {
    const { currentTeam } = useValues(teamLogic)
    const { availableProperties, availablePropertiesLoading } = useValues(materializedColumnsLogic)
    const { setShowCreateModal, loadSlots } = useActions(materializedColumnsLogic)
    const [selectedPropertyId, setSelectedPropertyId] = useState<number | null>(null)
    const [searchTerm, setSearchTerm] = useState('')
    const [isSubmitting, setIsSubmitting] = useState(false)

    const handleSubmit = async (): Promise<void> => {
        if (!currentTeam || !selectedPropertyId) {
            return
        }

        setIsSubmitting(true)
        try {
            // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
            await api.create(`api/projects/${currentTeam.id}/materialized_column_slots/assign_slot/`, {
                property_definition_id: selectedPropertyId,
            })
            lemonToast.success('Property queued for materialization — it will be picked up by the next weekly cycle')
            setShowCreateModal(false)
            loadSlots()
        } catch (error: any) {
            lemonToast.error(error.detail || 'Failed to queue property for materialization')
            console.error(error)
        } finally {
            setIsSubmitting(false)
        }
    }

    // Filter and sort properties based on search term
    const filteredProperties = availableProperties
        .filter((prop: PropertyDefinition) => prop.name.toLowerCase().includes(searchTerm.toLowerCase()))
        .sort((a: PropertyDefinition, b: PropertyDefinition) => a.name.localeCompare(b.name))

    const selectedProperty = availableProperties.find((prop: PropertyDefinition) => prop.id === selectedPropertyId)

    return (
        <LemonModal
            isOpen
            onClose={() => setShowCreateModal(false)}
            title="Assign Materialized Column Slot"
            width="36rem"
            footer={
                <>
                    <LemonButton type="secondary" onClick={() => setShowCreateModal(false)}>
                        Cancel
                    </LemonButton>
                    <LemonButton
                        type="primary"
                        onClick={handleSubmit}
                        loading={isSubmitting}
                        disabledReason={!selectedPropertyId ? 'Please select a property' : undefined}
                    >
                        Queue for materialization
                    </LemonButton>
                </>
            }
        >
            <div className="space-y-4">
                <p className="text-muted">
                    Select a property to queue for materialization. The slot starts in <strong>PENDING</strong> state;
                    the next weekly backfill cycle will assign it a column, run the historical backfill, and transition
                    it to <strong>READY</strong>. Until then HogQL falls back to JSON extraction for this property.
                </p>

                <div>
                    <LemonLabel>Property to materialize</LemonLabel>
                    {selectedProperty ? (
                        <LemonButton
                            fullWidth
                            onClick={() => setSelectedPropertyId(null)}
                            sideAction={{
                                icon: <IconX />,
                                tooltip: 'Clear selection',
                                onClick: () => setSelectedPropertyId(null),
                            }}
                        >
                            <span className="flex items-center justify-between gap-2 flex-1">
                                <span>{selectedProperty.name}</span>
                                <LemonTag type="default" size="small">
                                    {selectedProperty.property_type}
                                </LemonTag>
                            </span>
                        </LemonButton>
                    ) : (
                        <div className="deprecated-space-y-2">
                            <LemonInput
                                type="search"
                                placeholder="Search properties..."
                                value={searchTerm}
                                onChange={setSearchTerm}
                                fullWidth
                                autoFocus
                            />
                            <div className="max-h-60 overflow-y-auto">
                                {availablePropertiesLoading ? (
                                    <div className="p-4 text-center text-muted">Loading properties...</div>
                                ) : filteredProperties.length === 0 ? (
                                    <div className="p-4 text-center text-muted">
                                        {searchTerm ? 'No properties match your search' : 'No properties available'}
                                    </div>
                                ) : (
                                    <ul className="deprecated-space-y-px">
                                        {filteredProperties.map((prop: PropertyDefinition) => (
                                            <li key={prop.id}>
                                                <LemonButton
                                                    fullWidth
                                                    role="menuitem"
                                                    size="small"
                                                    onClick={() => {
                                                        setSelectedPropertyId(prop.id)
                                                        setSearchTerm('')
                                                    }}
                                                >
                                                    <span className="flex items-center justify-between gap-2 flex-1">
                                                        <span className="truncate">{prop.name}</span>
                                                        <LemonTag type="default" size="small">
                                                            {prop.property_type}
                                                        </LemonTag>
                                                    </span>
                                                </LemonButton>
                                            </li>
                                        ))}
                                    </ul>
                                )}
                            </div>
                        </div>
                    )}
                </div>

                {availableProperties.length === 0 && !availablePropertiesLoading && (
                    <div className="bg-warning-highlight text-warning rounded p-3 text-sm">
                        No properties available for materialization. All eligible properties have either been
                        materialized or don't have a property_type set.
                    </div>
                )}
            </div>
        </LemonModal>
    )
}