Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AAU I want to re-order my views with drag & drop #5002

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type MenuItemDraggableProps = {
text: string;
isDragDisabled?: boolean;
className?: string;
isIconDisplayedOnHoverOnly?: boolean;
};
export const MenuItemDraggable = ({
LeftIcon,
Expand All @@ -27,6 +28,7 @@ export const MenuItemDraggable = ({
text,
isDragDisabled = false,
className,
isIconDisplayedOnHoverOnly = true,
}: MenuItemDraggableProps) => {
const showIconButtons = Array.isArray(iconButtons) && iconButtons.length > 0;

Expand All @@ -36,6 +38,7 @@ export const MenuItemDraggable = ({
accent={accent}
className={className}
isMenuOpen={!!isTooltipOpen}
isIconDisplayedOnHoverOnly={isIconDisplayedOnHoverOnly}
>
<MenuItemLeftContent
LeftIcon={LeftIcon}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { MouseEvent } from 'react';
import { MouseEvent, useCallback } from 'react';
import styled from '@emotion/styled';
import { DropResult } from '@hello-pangea/dnd';
import { useSetRecoilState } from 'recoil';
import { IconLock, IconPencil, IconPlus, useIcons } from 'twenty-ui';

import { DraggableItem } from '@/ui/layout/draggable-list/components/DraggableItem';
import { DraggableList } from '@/ui/layout/draggable-list/components/DraggableList';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { MenuItemDraggable } from '@/ui/navigation/menu-item/components/MenuItemDraggable';
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView';
import { useHandleViews } from '@/views/hooks/useHandleViews';
import { VIEW_PICKER_DROPDOWN_ID } from '@/views/view-picker/constants/ViewPickerDropdownId';
import { useViewPickerMode } from '@/views/view-picker/hooks/useViewPickerMode';
import { useViewPickerStates } from '@/views/view-picker/hooks/useViewPickerStates';
import { moveArrayItem } from '~/utils/array/moveArrayItem';
import { isDefined } from '~/utils/isDefined';

const StyledBoldDropdownMenuItemsContainer = styled(DropdownMenuItemsContainer)`
Expand All @@ -33,6 +38,8 @@ export const ViewPickerListContent = () => {

const { closeDropdown } = useDropdown(VIEW_PICKER_DROPDOWN_ID);

const { updateView } = useHandleViews();

const handleViewSelect = (viewId: string) => {
selectView(viewId);
closeDropdown();
Expand All @@ -56,29 +63,61 @@ export const ViewPickerListContent = () => {

const { getIcon } = useIcons();

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],
);

const getSortedViews = () => {
return viewsOnCurrentObject.sort((a, b) => a.position - b.position);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not recode this logic. It's already in: getObjectMetadataItemViews function

};
const sortedViews = getSortedViews();

return (
<>
<DropdownMenuItemsContainer>
{viewsOnCurrentObject.map((view) => (
<MenuItem
key={view.id}
iconButtons={[
view.key !== 'INDEX'
? {
Icon: IconPencil,
onClick: (event: MouseEvent<HTMLButtonElement>) =>
handleEditViewButtonClick(event, view.id),
}
: {
Icon: IconLock,
},
].filter(isDefined)}
isIconDisplayedOnHoverOnly={view.key !== 'INDEX'}
onClick={() => handleViewSelect(view.id)}
LeftIcon={getIcon(view.icon)}
text={view.name}
/>
))}
<DraggableList
onDragEnd={handleDragEnd}
draggableItems={sortedViews.map((view, index) => (
<DraggableItem
key={view.id}
draggableId={view.id}
index={index}
isDragDisabled={viewsOnCurrentObject.length === 1}
itemComponent={
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's the INDEX view too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @charlesBochet
This has been updated!

<MenuItemDraggable
key={view.id}
iconButtons={[
view.key !== 'INDEX'
? {
Icon: IconPencil,
onClick: (event: MouseEvent<HTMLButtonElement>) =>
handleEditViewButtonClick(event, view.id),
}
: {
Icon: IconLock,
},
].filter(isDefined)}
isIconDisplayedOnHoverOnly={view.key !== 'INDEX'}
onClick={() => handleViewSelect(view.id)}
LeftIcon={getIcon(view.icon)}
text={view.name}
/>
}
/>
))}
/>
</DropdownMenuItemsContainer>
<DropdownMenuSeparator />
<StyledBoldDropdownMenuItemsContainer>
Expand Down