-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bi): Added foundations of insight variables (#25146)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6f9ea30
commit 4ebdea6
Showing
19 changed files
with
594 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
frontend/src/queries/nodes/DataVisualization/Components/Variables/NewVariableModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { | ||
LemonButton, | ||
LemonInput, | ||
LemonInputSelect, | ||
LemonModal, | ||
LemonSegmentedButton, | ||
LemonSelect, | ||
} from '@posthog/lemon-ui' | ||
import { useActions, useValues } from 'kea' | ||
import { LemonField } from 'lib/lemon-ui/LemonField' | ||
|
||
import { Variable } from '../../types' | ||
import { addVariableLogic } from './addVariableLogic' | ||
|
||
const renderVariableSpecificFields = ( | ||
variable: Variable, | ||
updateVariable: (variable: Variable) => void | ||
): JSX.Element => { | ||
if (variable.type === 'String') { | ||
return ( | ||
<LemonField.Pure label="Default value" className="gap-1"> | ||
<LemonInput | ||
placeholder="Default value" | ||
value={variable.default_value} | ||
onChange={(value) => updateVariable({ ...variable, default_value: value })} | ||
/> | ||
</LemonField.Pure> | ||
) | ||
} | ||
|
||
if (variable.type === 'Number') { | ||
return ( | ||
<LemonField.Pure label="Default value" className="gap-1"> | ||
<LemonInput | ||
placeholder="Default value" | ||
type="number" | ||
value={variable.default_value} | ||
onChange={(value) => updateVariable({ ...variable, default_value: value ?? 0 })} | ||
/> | ||
</LemonField.Pure> | ||
) | ||
} | ||
|
||
if (variable.type === 'Boolean') { | ||
return ( | ||
<LemonField.Pure label="Default value" className="gap-1"> | ||
<LemonSegmentedButton | ||
className="w-full" | ||
value={variable.default_value ? 'true' : 'false'} | ||
onChange={(value) => updateVariable({ ...variable, default_value: value === 'true' })} | ||
options={[ | ||
{ | ||
value: 'true', | ||
label: 'true', | ||
}, | ||
{ | ||
value: 'false', | ||
label: 'false', | ||
}, | ||
]} | ||
/> | ||
</LemonField.Pure> | ||
) | ||
} | ||
|
||
if (variable.type === 'List') { | ||
return ( | ||
<> | ||
<LemonField.Pure label="Values" className="gap-1"> | ||
<LemonInputSelect | ||
value={variable.values} | ||
onChange={(value) => updateVariable({ ...variable, values: value })} | ||
placeholder="Options..." | ||
mode="multiple" | ||
allowCustomValues={true} | ||
options={[]} | ||
/> | ||
</LemonField.Pure> | ||
<LemonField.Pure label="Default value" className="gap-1"> | ||
<LemonSelect | ||
className="w-full" | ||
placeholder="Select default value" | ||
value={variable.default_value} | ||
options={variable.values.map((n) => ({ label: n, value: n }))} | ||
onChange={(value) => updateVariable({ ...variable, default_value: value ?? '' })} | ||
allowClear | ||
dropdownMaxContentWidth | ||
/> | ||
</LemonField.Pure> | ||
</> | ||
) | ||
} | ||
|
||
throw new Error(`Unsupported variable type: ${(variable as Variable).type}`) | ||
} | ||
|
||
export const NewVariableModal = (): JSX.Element => { | ||
const { closeModal, updateVariable, save } = useActions(addVariableLogic) | ||
const { isModalOpen, variable } = useValues(addVariableLogic) | ||
|
||
return ( | ||
<LemonModal | ||
title={`New ${variable.type} variable`} | ||
isOpen={isModalOpen} | ||
onClose={closeModal} | ||
maxWidth="30rem" | ||
footer={ | ||
<div className="flex flex-1 justify-end gap-2"> | ||
<LemonButton type="secondary" onClick={closeModal}> | ||
Close | ||
</LemonButton> | ||
<LemonButton type="primary" onClick={() => save()}> | ||
Save | ||
</LemonButton> | ||
</div> | ||
} | ||
> | ||
<div className="gap-4 flex flex-col"> | ||
<LemonField.Pure label="Name" className="gap-1"> | ||
<LemonInput | ||
placeholder="Name" | ||
value={variable.name} | ||
onChange={(value) => updateVariable({ ...variable, name: value })} | ||
/> | ||
</LemonField.Pure> | ||
{renderVariableSpecificFields(variable, updateVariable)} | ||
</div> | ||
</LemonModal> | ||
) | ||
} |
81 changes: 81 additions & 0 deletions
81
frontend/src/queries/nodes/DataVisualization/Components/Variables/Variables.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { IconPlus } from '@posthog/icons' | ||
import { LemonButton, LemonMenu } from '@posthog/lemon-ui' | ||
import { useActions, useValues } from 'kea' | ||
import { FEATURE_FLAGS } from 'lib/constants' | ||
import { featureFlagLogic } from 'lib/logic/featureFlagLogic' | ||
|
||
import { dataVisualizationLogic } from '../../dataVisualizationLogic' | ||
import { addVariableLogic } from './addVariableLogic' | ||
import { NewVariableModal } from './NewVariableModal' | ||
import { variablesLogic } from './variablesLogic' | ||
|
||
export const Variables = (): JSX.Element => { | ||
const { dataVisualizationProps, showEditingUI } = useValues(dataVisualizationLogic) | ||
|
||
const { featureFlags } = useValues(featureFlagLogic) | ||
const { openModal } = useActions(addVariableLogic) | ||
|
||
const builtVariablesLogic = variablesLogic({ key: dataVisualizationProps.key }) | ||
const { variables, variablesLoading, variablesForInsight } = useValues(builtVariablesLogic) | ||
const { addVariable } = useActions(builtVariablesLogic) | ||
|
||
if (!featureFlags[FEATURE_FLAGS.INSIGHT_VARIABLES]) { | ||
return <></> | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="flex gap-4 justify-between flex-wrap px-px"> | ||
{showEditingUI && ( | ||
<LemonMenu | ||
items={[ | ||
{ | ||
title: 'New variable', | ||
items: [ | ||
{ | ||
label: 'String', | ||
onClick: () => openModal('String'), | ||
}, | ||
{ | ||
label: 'Number', | ||
onClick: () => openModal('Number'), | ||
}, | ||
{ | ||
label: 'Boolean', | ||
onClick: () => openModal('Boolean'), | ||
}, | ||
{ | ||
label: 'List', | ||
onClick: () => openModal('List'), | ||
}, | ||
], | ||
}, | ||
{ | ||
label: 'Existing variable', | ||
items: variablesLoading | ||
? [ | ||
{ | ||
label: 'Loading...', | ||
onClick: () => {}, | ||
}, | ||
] | ||
: variables.map((n) => ({ | ||
label: n.name, | ||
onClick: () => addVariable(n.id), | ||
})), | ||
}, | ||
]} | ||
> | ||
<LemonButton type="secondary" sideIcon={<IconPlus />}> | ||
Add variable | ||
</LemonButton> | ||
</LemonMenu> | ||
)} | ||
{variablesForInsight.map((n) => ( | ||
<div key={n.id}>{n.name}</div> | ||
))} | ||
</div> | ||
<NewVariableModal /> | ||
</> | ||
) | ||
} |
94 changes: 94 additions & 0 deletions
94
frontend/src/queries/nodes/DataVisualization/Components/Variables/addVariableLogic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { actions, kea, path, reducers } from 'kea' | ||
import { loaders } from 'kea-loaders' | ||
import api from 'lib/api' | ||
|
||
import { BooleanVariable, ListVariable, NumberVariable, StringVariable, Variable, VariableType } from '../../types' | ||
import type { addVariableLogicType } from './addVariableLogicType' | ||
|
||
const DEFAULT_VARIABLE: StringVariable = { | ||
id: '', | ||
type: 'String', | ||
name: '', | ||
default_value: '', | ||
} | ||
|
||
export const addVariableLogic = kea<addVariableLogicType>([ | ||
path(['queries', 'nodes', 'DataVisualization', 'Components', 'Variables', 'variableLogic']), | ||
actions({ | ||
openModal: (variableType: VariableType) => ({ variableType }), | ||
closeModal: true, | ||
updateVariable: (variable: Variable) => ({ variable }), | ||
}), | ||
reducers({ | ||
variableType: [ | ||
'string' as VariableType, | ||
{ | ||
openModal: (_, { variableType }) => variableType, | ||
}, | ||
], | ||
isModalOpen: [ | ||
false as boolean, | ||
{ | ||
openModal: () => true, | ||
closeModal: () => false, | ||
}, | ||
], | ||
variable: [ | ||
DEFAULT_VARIABLE as Variable, | ||
{ | ||
openModal: (_, { variableType }) => { | ||
if (variableType === 'String') { | ||
return { | ||
id: '', | ||
type: 'String', | ||
name: '', | ||
default_value: '', | ||
} as StringVariable | ||
} | ||
|
||
if (variableType === 'Number') { | ||
return { | ||
id: '', | ||
type: 'Number', | ||
name: '', | ||
default_value: 0, | ||
} as NumberVariable | ||
} | ||
|
||
if (variableType === 'Boolean') { | ||
return { | ||
id: '', | ||
type: 'Boolean', | ||
name: '', | ||
default_value: false, | ||
} as BooleanVariable | ||
} | ||
|
||
if (variableType === 'List') { | ||
return { | ||
id: '', | ||
type: 'List', | ||
name: '', | ||
values: [], | ||
default_value: '', | ||
} as ListVariable | ||
} | ||
|
||
throw new Error(`Unsupported variable type ${variableType}`) | ||
}, | ||
updateVariable: (state, { variable }) => ({ ...state, ...variable }), | ||
closeModal: () => DEFAULT_VARIABLE, | ||
}, | ||
], | ||
}), | ||
loaders(({ values }) => ({ | ||
savedVariable: [ | ||
null as null | Variable, | ||
{ | ||
save: async () => { | ||
return await api.insightVariables.create(values.variable) | ||
}, | ||
}, | ||
], | ||
})), | ||
]) |
Oops, something went wrong.