From de228be5ca02cf84d21a8259056236a1afcafef1 Mon Sep 17 00:00:00 2001 From: gitstart-twenty <140154534+gitstart-twenty@users.noreply.github.com> Date: Wed, 15 May 2024 18:57:17 +0300 Subject: [PATCH] AAU I want to re-order my views with drag & drop (#5002) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Description I want to re-order my views with drag & drop ### Refs #4782 ### Demo https://jam.dev/c/699ece8a-0467-494a-b9a3-faf666ee9c93 Fixes #4782 --------- Co-authored-by: gitstart-twenty Co-authored-by: v1b3m Co-authored-by: Matheus Co-authored-by: FellipeMTX Co-authored-by: FĂ©lix Malfait --- .../components/MenuItemDraggable.tsx | 3 + .../components/ViewPickerListContent.tsx | 85 +++++++++++++++---- 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/packages/twenty-front/src/modules/ui/navigation/menu-item/components/MenuItemDraggable.tsx b/packages/twenty-front/src/modules/ui/navigation/menu-item/components/MenuItemDraggable.tsx index 773de35e02d5..80d27886a66f 100644 --- a/packages/twenty-front/src/modules/ui/navigation/menu-item/components/MenuItemDraggable.tsx +++ b/packages/twenty-front/src/modules/ui/navigation/menu-item/components/MenuItemDraggable.tsx @@ -17,6 +17,7 @@ export type MenuItemDraggableProps = { text: string; isDragDisabled?: boolean; className?: string; + isIconDisplayedOnHoverOnly?: boolean; }; export const MenuItemDraggable = ({ LeftIcon, @@ -27,6 +28,7 @@ export const MenuItemDraggable = ({ text, isDragDisabled = false, className, + isIconDisplayedOnHoverOnly = true, }: MenuItemDraggableProps) => { const showIconButtons = Array.isArray(iconButtons) && iconButtons.length > 0; @@ -36,6 +38,7 @@ export const MenuItemDraggable = ({ accent={accent} className={className} isMenuOpen={!!isTooltipOpen} + isIconDisplayedOnHoverOnly={isIconDisplayedOnHoverOnly} > { const { closeDropdown } = useDropdown(VIEW_PICKER_DROPDOWN_ID); + const { updateView } = useHandleViews(); + const handleViewSelect = (viewId: string) => { selectView(viewId); closeDropdown(); @@ -56,29 +63,73 @@ export const ViewPickerListContent = () => { const { getIcon } = useIcons(); + const indexView = viewsOnCurrentObject.find((view) => view.key === 'INDEX'); + const viewsOnCurrentObjectWithoutIndex = viewsOnCurrentObject.filter( + (view) => view.key !== 'INDEX', + ); + + const handleDragEnd = useCallback( + (result: DropResult) => { + if (!result.destination) return; + + moveArrayItem(viewsOnCurrentObject, { + fromIndex: result.source.index, + toIndex: result.destination.index, + }).forEach((view, index) => { + if (view.position !== index) { + updateView({ ...view, position: index }); + } + }); + }, + [updateView, viewsOnCurrentObject], + ); + return ( <> - {viewsOnCurrentObject.map((view) => ( + ( + ) => + handleEditViewButtonClick(event, view.id), + }, + ].filter(isDefined)} + isIconDisplayedOnHoverOnly + onClick={() => handleViewSelect(view.id)} + LeftIcon={getIcon(view.icon)} + text={view.name} + /> + } + /> + ), + )} + /> + {indexView && ( ) => - handleEditViewButtonClick(event, view.id), - } - : { - Icon: IconLock, - }, + { + Icon: IconLock, + }, ].filter(isDefined)} - isIconDisplayedOnHoverOnly={view.key !== 'INDEX'} - onClick={() => handleViewSelect(view.id)} - LeftIcon={getIcon(view.icon)} - text={view.name} + isIconDisplayedOnHoverOnly={false} + onClick={() => handleViewSelect(indexView.id)} + LeftIcon={getIcon(indexView.icon)} + text={indexView.name} /> - ))} + )}