Skip to content

Commit

Permalink
Update ShowPageMoreButton to open the command menu when clicked (#8565)
Browse files Browse the repository at this point in the history
  • Loading branch information
bosiraphael authored Nov 18, 2024
1 parent e1a730a commit 9810c5b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,130 +1,18 @@
import styled from '@emotion/styled';
import { useNavigate } from 'react-router-dom';
import { useRecoilState, useRecoilValue } from 'recoil';
import {
IconButton,
IconDotsVertical,
IconRestore,
IconTrash,
MenuItem,
} from 'twenty-ui';
import { IconButton, IconDotsVertical } from 'twenty-ui';

import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
import { PageHotkeyScope } from '@/types/PageHotkeyScope';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown';
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState';
import { useCommandMenu } from '@/command-menu/hooks/useCommandMenu';

import { isObjectMetadataReadOnly } from '@/object-metadata/utils/isObjectMetadataReadOnly';
import { useDestroyOneRecord } from '@/object-record/hooks/useDestroyOneRecord';
import { useRestoreManyRecords } from '@/object-record/hooks/useRestoreManyRecords';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { Dropdown } from '../../dropdown/components/Dropdown';
import { DropdownMenu } from '../../dropdown/components/DropdownMenu';

const StyledContainer = styled.div`
z-index: 1;
`;

export const ShowPageMoreButton = ({
recordId,
objectNameSingular,
isRemote,
}: {
recordId: string;
objectNameSingular: string;
isRemote: boolean;
}) => {
const { closeDropdown, toggleDropdown } = useDropdown('more-show-page');
const navigationMemorizedUrl = useRecoilValue(navigationMemorizedUrlState);
const navigate = useNavigate();

const { deleteOneRecord } = useDeleteOneRecord({
objectNameSingular,
});
const { destroyOneRecord } = useDestroyOneRecord({
objectNameSingular,
});
const { restoreManyRecords } = useRestoreManyRecords({
objectNameSingular,
});

const handleDelete = () => {
deleteOneRecord(recordId);
closeDropdown();
};

const handleDestroy = () => {
destroyOneRecord(recordId);
closeDropdown();
navigate(navigationMemorizedUrl, { replace: true });
};

const handleRestore = () => {
restoreManyRecords([recordId]);
closeDropdown();
};

const [recordFromStore] = useRecoilState<any>(
recordStoreFamilyState(recordId),
);

if (
isObjectMetadataReadOnly({
isRemote,
nameSingular: objectNameSingular,
})
) {
return;
}
export const ShowPageMoreButton = () => {
const { openCommandMenu } = useCommandMenu();

return (
<StyledContainer>
<Dropdown
dropdownId="more-show-page"
clickableComponent={
<IconButton
Icon={IconDotsVertical}
size="medium"
dataTestId="more-showpage-button"
accent="default"
variant="secondary"
onClick={toggleDropdown}
/>
}
dropdownComponents={
<DropdownMenu>
<DropdownMenuItemsContainer>
{recordFromStore && !recordFromStore.deletedAt && (
<MenuItem
onClick={handleDelete}
accent="danger"
LeftIcon={IconTrash}
text="Delete"
/>
)}
{recordFromStore && recordFromStore.deletedAt && (
<>
<MenuItem
onClick={handleDestroy}
accent="danger"
LeftIcon={IconTrash}
text="Destroy"
/>
<MenuItem
onClick={handleRestore}
LeftIcon={IconRestore}
text="Restore"
/>
</>
)}
</DropdownMenuItemsContainer>
</DropdownMenu>
}
dropdownHotkeyScope={{
scope: PageHotkeyScope.ShowPage,
}}
/>
</StyledContainer>
<IconButton
Icon={IconDotsVertical}
size="medium"
dataTestId="more-showpage-button"
accent="default"
variant="secondary"
onClick={openCommandMenu}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ export const RecordShowPageBaseHeader = ({
targetObjectNameSingular: objectMetadataItem.nameSingular,
}}
/>
<ShowPageMoreButton
key="more"
recordId={record?.id ?? '0'}
objectNameSingular={objectNameSingular}
isRemote={objectMetadataItem.isRemote}
/>
<ShowPageMoreButton key="more" />
</>
);
};

0 comments on commit 9810c5b

Please sign in to comment.