Skip to content

Commit

Permalink
persist globals in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Dec 19, 2024
1 parent 06ecef8 commit df793be
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
71 changes: 63 additions & 8 deletions frontend/src/scenes/pipeline/hogfunctions/HogFunctionTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,25 @@ export function HogFunctionTestPlaceholder({
}

export function HogFunctionTest(props: HogFunctionTestLogicProps): JSX.Element {
const { isTestInvocationSubmitting, testResult, expanded, sampleGlobalsLoading, sampleGlobalsError, type } =
useValues(hogFunctionTestLogic(props))
const { submitTestInvocation, setTestResult, toggleExpanded, loadSampleGlobals } = useActions(
hogFunctionTestLogic(props)
)
const {
isTestInvocationSubmitting,
testResult,
expanded,
sampleGlobalsLoading,
sampleGlobalsError,
type,
savedGlobals,
testInvocation,
} = useValues(hogFunctionTestLogic(props))
const {
submitTestInvocation,
setTestResult,
toggleExpanded,
loadSampleGlobals,
deleteSavedGlobals,
setSampleGlobals,
saveGlobals,
} = useActions(hogFunctionTestLogic(props))

return (
<Form logic={hogFunctionTestLogic} props={props} formKey="testInvocation" enableFormOnSubmit>
Expand All @@ -85,10 +99,10 @@ export function HogFunctionTest(props: HogFunctionTestLogicProps): JSX.Element {
>
<div className="flex items-center gap-2 justify-end">
<div className="flex-1 space-y-2">
<div className="mb-0 flex gap-2">
<h2>Testing</h2>
<h2 className="mb-0 flex gap-2 items-center">
<span>Testing</span>
{sampleGlobalsLoading ? <Spinner /> : null}
</div>
</h2>
{!expanded &&
(type === 'email' ? (
<p>Click here to test the provider with a sample e-mail</p>
Expand Down Expand Up @@ -153,6 +167,47 @@ export function HogFunctionTest(props: HogFunctionTestLogicProps): JSX.Element {
>
Fetch new event
</LemonButton>
<LemonDivider />
{savedGlobals.map(({ name, globals }, index) => (
<div className="flex w-full justify-between" key={index}>
<LemonButton
key={index}
onClick={() => setSampleGlobals(globals)}
fullWidth
className="flex-1"
>
{name}
</LemonButton>
<LemonButton
size="small"
icon={<IconX />}
onClick={() => deleteSavedGlobals(index)}
tooltip="Delete this saved set of globals"
/>
</div>
))}
{testInvocation.globals && (
<LemonButton
fullWidth
onClick={() => {
const name = prompt('Name this set of globals')
if (name) {
saveGlobals(name, JSON.parse(testInvocation.globals))
}
}}
disabledReason={(() => {
try {
JSON.parse(testInvocation.globals)
} catch (e) {
return 'Invalid globals JSON'
}
return undefined
})()}
tooltip="Perist the globals into a named set, so you can reuse them later."
>
Save globals
</LemonButton>
)}
</>
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,15 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
setUnsavedConfiguration: (configuration: HogFunctionConfigurationType | null) => ({ configuration }),
persistForUnload: true,
setSampleGlobalsError: (error) => ({ error }),
setSampleGlobals: (sampleGlobals: HogFunctionInvocationGlobals | null) => ({ sampleGlobals }),
}),
reducers(({ props }) => ({
sampleGlobals: [
null as HogFunctionInvocationGlobals | null,
{
setSampleGlobals: (_, { sampleGlobals }) => sampleGlobals,
},
],
showSource: [
// Show source by default for blank templates when creating a new function
!!(!props.id && props.templateId?.startsWith('template-blank-')),
Expand Down
21 changes: 18 additions & 3 deletions frontend/src/scenes/pipeline/hogfunctions/hogFunctionTestLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { actions, afterMount, connect, kea, key, listeners, path, props, reducer
import { forms } from 'kea-forms'
import api from 'lib/api'
import { tryJsonParse } from 'lib/utils'
import { getCurrentTeamId } from 'lib/utils/getAppContext'

import { groupsModel } from '~/models/groupsModel'
import { LogEntry } from '~/types'
import { HogFunctionInvocationGlobals, LogEntry } from '~/types'

import { hogFunctionConfigurationLogic, sanitizeConfiguration } from './hogFunctionConfigurationLogic'
import type { hogFunctionTestLogicType } from './hogFunctionTestLogicType'
Expand Down Expand Up @@ -45,18 +46,20 @@ export const hogFunctionTestLogic = kea<hogFunctionTestLogicType>([
],
actions: [
hogFunctionConfigurationLogic({ id: props.id }),
['touchConfigurationField', 'loadSampleGlobalsSuccess', 'loadSampleGlobals'],
['touchConfigurationField', 'loadSampleGlobalsSuccess', 'loadSampleGlobals', 'setSampleGlobals'],
],
})),
actions({
setTestResult: (result: HogFunctionTestInvocationResult | null) => ({ result }),
toggleExpanded: (expanded?: boolean) => ({ expanded }),
saveGlobals: (name: string, globals: HogFunctionInvocationGlobals) => ({ name, globals }),
deleteSavedGlobals: (index: number) => ({ index }),
}),
reducers({
expanded: [
false as boolean,
{
toggleExpanded: (_, { expanded }) => (expanded === undefined ? !_ : expanded),
toggleExpanded: (state, { expanded }) => (expanded === undefined ? !state : expanded),
},
],

Expand All @@ -66,11 +69,23 @@ export const hogFunctionTestLogic = kea<hogFunctionTestLogicType>([
setTestResult: (_, { result }) => result,
},
],

savedGlobals: [
[] as { name: string; globals: HogFunctionInvocationGlobals }[],
{ persist: true, prefix: `${getCurrentTeamId()}__` },
{
saveGlobals: (state, { name, globals }) => [...state, { name, globals }],
deleteSavedGlobals: (state, { index }) => state.filter((_, i) => i !== index),
},
],
}),
listeners(({ values, actions }) => ({
loadSampleGlobalsSuccess: () => {
actions.setTestInvocationValue('globals', JSON.stringify(values.sampleGlobals, null, 2))
},
setSampleGlobals: ({ sampleGlobals }) => {
actions.setTestInvocationValue('globals', JSON.stringify(sampleGlobals, null, 2))
},
})),
forms(({ props, actions, values }) => ({
testInvocation: {
Expand Down
1 change: 1 addition & 0 deletions plugin-server/src/cdp/cdp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export class CdpApi {
id: team.id,
name: team.name,
url: `${this.hub.SITE_URL ?? 'http://localhost:8000'}/project/${team.id}`,
...globals.project,
},
},
compoundConfiguration,
Expand Down
4 changes: 2 additions & 2 deletions posthog/api/hog_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ def invocations(self, request: Request, *args, **kwargs):
# Remove the team from the config
configuration.pop("team")

globals = serializer.validated_data["globals"]
hog_globals = serializer.validated_data["globals"]
mock_async_functions = serializer.validated_data["mock_async_functions"]

res = create_hog_invocation_test(
team_id=hog_function.team_id,
hog_function_id=hog_function.id,
globals=globals,
globals=hog_globals,
configuration=configuration,
mock_async_functions=mock_async_functions,
)
Expand Down

0 comments on commit df793be

Please sign in to comment.