: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useValues } from 'kea'
import { LemonButton, LemonDialog, LemonModal, LemonTable } from '@posthog/lemon-ui'
import { VARIABLE_INSIGHT_COLUMNS } from 'scenes/data-management/variables/insightColumns'
import { VariableType } from '../../types'
import { variableDataLogic } from './variableDataLogic'
import { VariableForm } from './VariableForm'
import { variableModalLogic } from './variableModalLogic'
import { variablesLogic } from './variablesLogic'
export const NewVariableModal = (): JSX.Element => {
const { closeModal, updateVariable, save, openNewVariableModal, changeTypeExistingVariable } =
useActions(variableModalLogic)
const { isModalOpen, variable, modalType, insightsUsingVariable, insightsLoading, isSaving } =
useValues(variableModalLogic)
const { query } = useValues(variablesLogic)
const { deleteVariable } = useActions(variableDataLogic)
const title = modalType === 'new' ? `New ${variable.type} variable` : `Editing ${variable.name}`
const handleDelete = (): void => {
if (variable.id) {
LemonDialog.open({
title: 'Delete',
description:
'Are you sure you want to delete this variable? This cannot be undone. Queries that use this variable will no longer work.',
primaryButton: {
status: 'danger',
children: 'Delete variable',
onClick: (): void => {
deleteVariable(variable.id)
closeModal()
},
},
secondaryButton: {
children: 'Cancel',
},
})
}
}
const handleTypeChange = (variableType: VariableType): void => {
if (modalType === 'new') {
openNewVariableModal(variableType)
} else {
changeTypeExistingVariable(variableType)
}
}
return (
<LemonModal
title={title}
isOpen={isModalOpen}
onClose={closeModal}
maxWidth={modalType === 'existing' && insightsUsingVariable.length > 0 ? '60rem' : '30rem'}
footer={
<div className="flex flex-1 justify-end gap-2">
{modalType === 'existing' && (
<LemonButton type="secondary" status="danger" onClick={handleDelete} disabled={isSaving}>
Delete variable
</LemonButton>
)}
<div className="flex-1" />
<LemonButton type="secondary" onClick={closeModal} disabled={isSaving}>
Close
</LemonButton>
<LemonButton type="primary" onClick={() => save()} loading={isSaving}>
Save
</LemonButton>
</div>
}
>
<VariableForm
variable={variable}
updateVariable={updateVariable}
onSave={save}
modalType={modalType}
onTypeChange={handleTypeChange}
defaultValuesQueryConnectionId={query?.source?.connectionId ?? null}
/>
{modalType === 'existing' && (
<div className="mt-4">
<h3 className="text-base font-semibold mb-2">Insights using this variable</h3>
<LemonTable
loading={insightsLoading}
dataSource={insightsUsingVariable}
columns={VARIABLE_INSIGHT_COLUMNS}
rowKey="id"
emptyState="No insights use this variable"
size="small"
/>
</div>
)}
</LemonModal>
)
}