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.

6.6 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { Field, Form, Group } from 'kea-forms'

import { IconPlus } from '@posthog/icons'
import { LemonDivider } from '@posthog/lemon-ui'

import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonInput } from 'lib/lemon-ui/LemonInput'

import { ToolbarMenu } from '~/toolbar/floating/components/bar/ToolbarMenu'
import { actionsTabLogic } from '~/toolbar/shared/actions/actionsTabLogic'
import { ActionStepEditor } from '~/toolbar/shared/actions/ActionStepEditor'
import { SelectorEditingModal } from '~/toolbar/shared/actions/SelectorEditingModal'
import { toolbarPosthogJS } from '~/toolbar/shared/toolbarPosthogJS'

export const ActionsEditingToolbarMenu = (): JSX.Element => {
    const {
        selectedActionId,
        inspectingElement,
        editingSelector,
        elementsChainBeingEdited,
        editingSelectorValue,
        actionForm,
    } = useValues(actionsTabLogic)
    const { setActionFormValue, selectAction, inspectForElementWithIndex, setElementSelector, editSelectorWithIndex } =
        useActions(actionsTabLogic)

    return (
        <ToolbarMenu>
            <SelectorEditingModal
                isOpen={editingSelector !== null}
                setIsOpen={() => editSelectorWithIndex(null)}
                activeElementChain={elementsChainBeingEdited}
                startingSelector={editingSelectorValue}
                onChange={(selector) => {
                    if (selector && editingSelector !== null) {
                        toolbarPosthogJS.capture('toolbar_manual_selector_applied', {
                            chosenSelector: selector,
                        })
                        setElementSelector(selector, editingSelector)
                    }
                }}
            />
            <Form
                name="action_step"
                logic={actionsTabLogic}
                formKey="actionForm"
                enableFormOnSubmit
                className="flex flex-col overflow-hidden flex-1"
            >
                <ToolbarMenu.Header className="border-b">
                    <h1 className="p-1 font-bold text-sm mb-0">
                        {selectedActionId === 'new' ? 'New ' : 'Edit '}
                        action
                    </h1>
                </ToolbarMenu.Header>
                <ToolbarMenu.Body>
                    <div className="p-1">
                        <div>
                            <p>What did your user do?</p>
                            <Field name="name">
                                <LemonInput
                                    placeholder="E.g: Clicked Sign Up"
                                    className="action-title-field"
                                    stopPropagation={true}
                                />
                            </Field>
                        </div>

                        {actionForm.steps?.map((step, index) => (
                            <Group key={index} name={['steps', index]}>
                                <LemonDivider />
                                <div key={index} className="p-1 flex flex-col gap-2">
                                    <ActionStepEditor
                                        step={step}
                                        index={index}
                                        inspecting={inspectingElement === index}
                                        onInspect={() =>
                                            inspectForElementWithIndex(inspectingElement === index ? null : index)
                                        }
                                        onEditSelector={() => {
                                            toolbarPosthogJS.capture('toolbar_manual_selector_modal_opened', {
                                                selector: step?.selector ?? null,
                                            })
                                            editSelectorWithIndex(index)
                                        }}
                                        onRemove={() =>
                                            setActionFormValue(
                                                'steps',
                                                //actionForm.steps without the step at index
                                                actionForm.steps?.filter((_, i) => i !== index)
                                            )
                                        }
                                    />

                                    {index === (actionForm.steps?.length || 0) - 1 ? (
                                        <div className="text-right mt-4">
                                            <LemonButton
                                                type="secondary"
                                                size="small"
                                                sideIcon={<IconPlus />}
                                                onClick={() =>
                                                    setActionFormValue('steps', [...(actionForm.steps || []), {}])
                                                }
                                            >
                                                Add Another Element
                                            </LemonButton>
                                        </div>
                                    ) : null}
                                </div>
                            </Group>
                        ))}

                        {(actionForm.steps || []).length === 0 ? (
                            <LemonButton
                                icon={<IconPlus />}
                                size="small"
                                type="primary"
                                onClick={() => setActionFormValue('steps', [...(actionForm.steps || []), {}])}
                                className="my-2"
                            >
                                Add An Element
                            </LemonButton>
                        ) : null}
                    </div>
                </ToolbarMenu.Body>
                <ToolbarMenu.Footer>
                    <span className="flex-1" />
                    <LemonButton type="secondary" size="small" onClick={() => selectAction(null)}>
                        Cancel
                    </LemonButton>
                    <LemonButton type="primary" htmlType="submit" size="small">
                        {selectedActionId === 'new' ? 'Create ' : 'Save '}
                        action
                    </LemonButton>
                </ToolbarMenu.Footer>
            </Form>
        </ToolbarMenu>
    )
}