-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add available variables dropdown (#7964)
- Add variable dropdown - Insert variables on click - Save variable as `{{stepName.object.myVar}}` and display only `myVar` https://github.com/user-attachments/assets/9b49e32c-15e6-4b64-9901-0e63664bc3e8
- Loading branch information
Showing
17 changed files
with
997 additions
and
5 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
94 changes: 94 additions & 0 deletions
94
...twenty-front/src/modules/workflow/search-variables/components/SearchVariablesDropdown.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,94 @@ | ||
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown'; | ||
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu'; | ||
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; | ||
import { StyledDropdownButtonContainer } from '@/ui/layout/dropdown/components/StyledDropdownButtonContainer'; | ||
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown'; | ||
import { SearchVariablesDropdownStepItem } from '@/workflow/search-variables/components/SearchVariablesDropdownStepItem'; | ||
import SearchVariablesDropdownStepSubItem from '@/workflow/search-variables/components/SearchVariablesDropdownStepSubItem'; | ||
import { AVAILABLE_VARIABLES_MOCK } from '@/workflow/search-variables/constants/AvailableVariablesMock'; | ||
import { SEARCH_VARIABLES_DROPDOWN_ID } from '@/workflow/search-variables/constants/SearchVariablesDropdownId'; | ||
import { WorkflowStepMock } from '@/workflow/search-variables/types/WorkflowStepMock'; | ||
import { useTheme } from '@emotion/react'; | ||
import styled from '@emotion/styled'; | ||
import { Editor } from '@tiptap/react'; | ||
import { useState } from 'react'; | ||
import { IconVariable } from 'twenty-ui'; | ||
|
||
const StyledDropdownVariableButtonContainer = styled( | ||
StyledDropdownButtonContainer, | ||
)` | ||
background-color: ${({ theme }) => theme.background.transparent.lighter}; | ||
color: ${({ theme }) => theme.font.color.tertiary}; | ||
padding: ${({ theme }) => theme.spacing(0)}; | ||
margin: ${({ theme }) => theme.spacing(2)}; | ||
`; | ||
|
||
const SearchVariablesDropdown = ({ | ||
inputId, | ||
editor, | ||
}: { | ||
inputId: string; | ||
editor: Editor; | ||
}) => { | ||
const theme = useTheme(); | ||
|
||
const dropdownId = `${SEARCH_VARIABLES_DROPDOWN_ID}-${inputId}`; | ||
const { isDropdownOpen } = useDropdown(dropdownId); | ||
const [selectedStep, setSelectedStep] = useState< | ||
WorkflowStepMock | undefined | ||
>(undefined); | ||
|
||
const insertVariableTag = (variable: string) => { | ||
editor.commands.insertVariableTag(variable); | ||
}; | ||
|
||
const handleStepSelect = (stepId: string) => { | ||
setSelectedStep( | ||
AVAILABLE_VARIABLES_MOCK.find((step) => step.id === stepId), | ||
); | ||
}; | ||
|
||
const handleSubItemSelect = (subItem: string) => { | ||
insertVariableTag(subItem); | ||
}; | ||
|
||
const handleBack = () => { | ||
setSelectedStep(undefined); | ||
}; | ||
|
||
return ( | ||
<Dropdown | ||
dropdownId={dropdownId} | ||
dropdownHotkeyScope={{ | ||
scope: dropdownId, | ||
}} | ||
clickableComponent={ | ||
<StyledDropdownVariableButtonContainer isUnfolded={isDropdownOpen}> | ||
<IconVariable size={theme.icon.size.sm} /> | ||
</StyledDropdownVariableButtonContainer> | ||
} | ||
dropdownComponents={ | ||
<DropdownMenu> | ||
<DropdownMenuItemsContainer> | ||
{selectedStep ? ( | ||
<SearchVariablesDropdownStepSubItem | ||
step={selectedStep} | ||
onSelect={handleSubItemSelect} | ||
onBack={handleBack} | ||
/> | ||
) : ( | ||
<SearchVariablesDropdownStepItem | ||
steps={AVAILABLE_VARIABLES_MOCK} | ||
onSelect={handleStepSelect} | ||
/> | ||
)} | ||
</DropdownMenuItemsContainer> | ||
</DropdownMenu> | ||
} | ||
dropdownPlacement="bottom-end" | ||
dropdownOffset={{ x: 0, y: 4 }} | ||
/> | ||
); | ||
}; | ||
|
||
export default SearchVariablesDropdown; |
28 changes: 28 additions & 0 deletions
28
...ront/src/modules/workflow/search-variables/components/SearchVariablesDropdownStepItem.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,28 @@ | ||
import { MenuItemSelect } from '@/ui/navigation/menu-item/components/MenuItemSelect'; | ||
import { WorkflowStepMock } from '@/workflow/search-variables/types/WorkflowStepMock'; | ||
|
||
type SearchVariablesDropdownStepItemProps = { | ||
steps: WorkflowStepMock[]; | ||
onSelect: (value: string) => void; | ||
}; | ||
|
||
export const SearchVariablesDropdownStepItem = ({ | ||
steps, | ||
onSelect, | ||
}: SearchVariablesDropdownStepItemProps) => { | ||
return ( | ||
<> | ||
{steps.map((item, _index) => ( | ||
<MenuItemSelect | ||
key={`step-${item.id}`} | ||
selected={false} | ||
hovered={false} | ||
onClick={() => onSelect(item.id)} | ||
text={item.name} | ||
LeftIcon={undefined} | ||
hasSubMenu | ||
/> | ||
))} | ||
</> | ||
); | ||
}; |
68 changes: 68 additions & 0 deletions
68
...t/src/modules/workflow/search-variables/components/SearchVariablesDropdownStepSubItem.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,68 @@ | ||
import { DropdownMenuHeader } from '@/ui/layout/dropdown/components/DropdownMenuHeader'; | ||
import { MenuItemSelect } from '@/ui/navigation/menu-item/components/MenuItemSelect'; | ||
import { WorkflowStepMock } from '@/workflow/search-variables/types/WorkflowStepMock'; | ||
import { isObject } from '@sniptt/guards'; | ||
import { useState } from 'react'; | ||
import { IconChevronLeft } from 'twenty-ui'; | ||
|
||
type SearchVariablesDropdownStepSubItemProps = { | ||
step: WorkflowStepMock; | ||
onSelect: (value: string) => void; | ||
onBack: () => void; | ||
}; | ||
|
||
const SearchVariablesDropdownStepSubItem = ({ | ||
step, | ||
onSelect, | ||
onBack, | ||
}: SearchVariablesDropdownStepSubItemProps) => { | ||
const [currentPath, setCurrentPath] = useState<string[]>([]); | ||
|
||
const getSelectedObject = () => { | ||
let selected = step.output; | ||
for (const key of currentPath) { | ||
selected = selected[key]; | ||
} | ||
return selected; | ||
}; | ||
|
||
const handleSelect = (key: string) => { | ||
const selectedObject = getSelectedObject(); | ||
if (isObject(selectedObject[key])) { | ||
setCurrentPath([...currentPath, key]); | ||
} else { | ||
onSelect(`{{${step.id}.${[...currentPath, key].join('.')}}}`); | ||
} | ||
}; | ||
|
||
const goBack = () => { | ||
if (currentPath.length === 0) { | ||
onBack(); | ||
} else { | ||
setCurrentPath(currentPath.slice(0, -1)); | ||
} | ||
}; | ||
|
||
const headerLabel = currentPath.length === 0 ? step.name : currentPath.at(-1); | ||
|
||
return ( | ||
<> | ||
<DropdownMenuHeader StartIcon={IconChevronLeft} onClick={goBack}> | ||
{headerLabel} | ||
</DropdownMenuHeader> | ||
{Object.entries(getSelectedObject()).map(([key, value]) => ( | ||
<MenuItemSelect | ||
key={key} | ||
selected={false} | ||
hovered={false} | ||
onClick={() => handleSelect(key)} | ||
text={key} | ||
hasSubMenu={isObject(value)} | ||
LeftIcon={undefined} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default SearchVariablesDropdownStepSubItem; |
Oops, something went wrong.