-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces effects to set the actionMenuEntries
- Loading branch information
1 parent
9b9b34f
commit 3761fbf
Showing
26 changed files
with
447 additions
and
319 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...t/src/modules/action-menu/actions/record-actions/components/DeleteRecordsActionEffect.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 { useActionMenuEntries } from '@/action-menu/hooks/useActionMenuEntries'; | ||
import { contextStoreCurrentObjectMetadataIdState } from '@/context-store/states/contextStoreCurrentObjectMetadataIdState'; | ||
import { contextStoreTargetedRecordIdsState } from '@/context-store/states/contextStoreTargetedRecordIdsState'; | ||
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById'; | ||
import { DELETE_MAX_COUNT } from '@/object-record/constants/DeleteMaxCount'; | ||
import { useDeleteTableData } from '@/object-record/record-index/options/hooks/useDeleteTableData'; | ||
import { ConfirmationModal } from '@/ui/layout/modal/components/ConfirmationModal'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { IconTrash } from 'twenty-ui'; | ||
|
||
export const DeleteRecordsActionEffect = ({ | ||
position, | ||
}: { | ||
position: number; | ||
}) => { | ||
const { addActionMenuEntry, removeActionMenuEntry } = useActionMenuEntries(); | ||
|
||
const contextStoreTargetedRecordIds = useRecoilValue( | ||
contextStoreTargetedRecordIdsState, | ||
); | ||
|
||
const contextStoreCurrentObjectMetadataId = useRecoilValue( | ||
contextStoreCurrentObjectMetadataIdState, | ||
); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItemById({ | ||
objectId: contextStoreCurrentObjectMetadataId, | ||
}); | ||
|
||
const [isDeleteRecordsModalOpen, setIsDeleteRecordsModalOpen] = | ||
useState(false); | ||
|
||
const { deleteTableData } = useDeleteTableData({ | ||
objectNameSingular: objectMetadataItem?.nameSingular ?? '', | ||
recordIndexId: objectMetadataItem?.namePlural ?? '', | ||
}); | ||
|
||
const handleDeleteClick = useCallback(() => { | ||
deleteTableData(contextStoreTargetedRecordIds); | ||
}, [deleteTableData, contextStoreTargetedRecordIds]); | ||
|
||
const isRemoteObject = objectMetadataItem?.isRemote ?? false; | ||
|
||
const numberOfSelectedRecords = contextStoreTargetedRecordIds.length; | ||
|
||
const canDelete = | ||
!isRemoteObject && numberOfSelectedRecords < DELETE_MAX_COUNT; | ||
|
||
useEffect(() => { | ||
if (canDelete) { | ||
addActionMenuEntry({ | ||
key: 'delete', | ||
label: 'Delete', | ||
position, | ||
Icon: IconTrash, | ||
accent: 'danger', | ||
onClick: () => { | ||
setIsDeleteRecordsModalOpen(true); | ||
}, | ||
ConfirmationModal: ( | ||
<ConfirmationModal | ||
isOpen={isDeleteRecordsModalOpen} | ||
setIsOpen={setIsDeleteRecordsModalOpen} | ||
title={`Delete ${numberOfSelectedRecords} ${ | ||
numberOfSelectedRecords === 1 ? `record` : 'records' | ||
}`} | ||
subtitle={`Are you sure you want to delete ${ | ||
numberOfSelectedRecords === 1 ? 'this record' : 'these records' | ||
}? ${ | ||
numberOfSelectedRecords === 1 ? 'It' : 'They' | ||
} can be recovered from the Options menu.`} | ||
onConfirmClick={() => handleDeleteClick()} | ||
deleteButtonText={`Delete ${ | ||
numberOfSelectedRecords > 1 ? 'Records' : 'Record' | ||
}`} | ||
/> | ||
), | ||
}); | ||
} else { | ||
removeActionMenuEntry('delete'); | ||
} | ||
}, [ | ||
canDelete, | ||
addActionMenuEntry, | ||
removeActionMenuEntry, | ||
isDeleteRecordsModalOpen, | ||
numberOfSelectedRecords, | ||
handleDeleteClick, | ||
position, | ||
]); | ||
|
||
return null; | ||
}; |
53 changes: 53 additions & 0 deletions
53
...t/src/modules/action-menu/actions/record-actions/components/ExportRecordsActionEffect.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,53 @@ | ||
import { useActionMenuEntries } from '@/action-menu/hooks/useActionMenuEntries'; | ||
import { contextStoreCurrentObjectMetadataIdState } from '@/context-store/states/contextStoreCurrentObjectMetadataIdState'; | ||
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById'; | ||
import { | ||
displayedExportProgress, | ||
useExportTableData, | ||
} from '@/object-record/record-index/options/hooks/useExportTableData'; | ||
import { useEffect } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { IconFileExport } from 'twenty-ui'; | ||
|
||
export const ExportRecordsActionEffect = ({ | ||
position, | ||
}: { | ||
position: number; | ||
}) => { | ||
const { addActionMenuEntry, removeActionMenuEntry } = useActionMenuEntries(); | ||
|
||
const contextStoreCurrentObjectMetadataId = useRecoilValue( | ||
contextStoreCurrentObjectMetadataIdState, | ||
); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItemById({ | ||
objectId: contextStoreCurrentObjectMetadataId, | ||
}); | ||
|
||
const baseTableDataParams = { | ||
delayMs: 100, | ||
objectNameSingular: objectMetadataItem?.nameSingular ?? '', | ||
recordIndexId: objectMetadataItem?.namePlural ?? '', | ||
}; | ||
|
||
const { progress, download } = useExportTableData({ | ||
...baseTableDataParams, | ||
filename: `${objectMetadataItem?.nameSingular}.csv`, | ||
}); | ||
|
||
useEffect(() => { | ||
addActionMenuEntry({ | ||
key: 'export', | ||
position, | ||
label: displayedExportProgress(progress), | ||
Icon: IconFileExport, | ||
accent: 'default', | ||
onClick: () => download(), | ||
}); | ||
|
||
return () => { | ||
removeActionMenuEntry('export'); | ||
}; | ||
}, [download, progress, addActionMenuEntry, removeActionMenuEntry, position]); | ||
return null; | ||
}; |
78 changes: 78 additions & 0 deletions
78
...src/modules/action-menu/actions/record-actions/components/ManageFavoritesActionEffect.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,78 @@ | ||
import { useActionMenuEntries } from '@/action-menu/hooks/useActionMenuEntries'; | ||
import { contextStoreCurrentObjectMetadataIdState } from '@/context-store/states/contextStoreCurrentObjectMetadataIdState'; | ||
import { contextStoreTargetedRecordIdsState } from '@/context-store/states/contextStoreTargetedRecordIdsState'; | ||
import { useFavorites } from '@/favorites/hooks/useFavorites'; | ||
import { useObjectMetadataItemById } from '@/object-metadata/hooks/useObjectMetadataItemById'; | ||
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState'; | ||
import { useEffect } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { IconHeart, IconHeartOff, isDefined } from 'twenty-ui'; | ||
|
||
export const ManageFavoritesActionEffect = ({ | ||
position, | ||
}: { | ||
position: number; | ||
}) => { | ||
const { addActionMenuEntry, removeActionMenuEntry } = useActionMenuEntries(); | ||
|
||
const contextStoreTargetedRecordIds = useRecoilValue( | ||
contextStoreTargetedRecordIdsState, | ||
); | ||
const contextStoreCurrentObjectMetadataId = useRecoilValue( | ||
contextStoreCurrentObjectMetadataIdState, | ||
); | ||
|
||
const { favorites, createFavorite, deleteFavorite } = useFavorites(); | ||
|
||
const selectedRecordId = contextStoreTargetedRecordIds[0]; | ||
|
||
const selectedRecord = useRecoilValue( | ||
recordStoreFamilyState(selectedRecordId), | ||
); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItemById({ | ||
objectId: contextStoreCurrentObjectMetadataId, | ||
}); | ||
|
||
const foundFavorite = favorites?.find( | ||
(favorite) => favorite.recordId === selectedRecordId, | ||
); | ||
|
||
const isFavorite = !!selectedRecordId && !!foundFavorite; | ||
|
||
useEffect(() => { | ||
if (!isDefined(objectMetadataItem) || objectMetadataItem.isRemote) { | ||
return; | ||
} | ||
|
||
addActionMenuEntry({ | ||
key: 'manage-favorites', | ||
label: isFavorite ? 'Remove from favorites' : 'Add to favorites', | ||
position, | ||
Icon: isFavorite ? IconHeartOff : IconHeart, | ||
onClick: () => { | ||
if (isFavorite && isDefined(foundFavorite?.id)) { | ||
deleteFavorite(foundFavorite.id); | ||
} else if (isDefined(selectedRecord)) { | ||
createFavorite(selectedRecord, objectMetadataItem.nameSingular); | ||
} | ||
}, | ||
}); | ||
|
||
return () => { | ||
removeActionMenuEntry('manage-favorites'); | ||
}; | ||
}, [ | ||
addActionMenuEntry, | ||
createFavorite, | ||
deleteFavorite, | ||
foundFavorite?.id, | ||
isFavorite, | ||
objectMetadataItem, | ||
position, | ||
removeActionMenuEntry, | ||
selectedRecord, | ||
]); | ||
|
||
return null; | ||
}; |
14 changes: 14 additions & 0 deletions
14
.../action-menu/actions/record-actions/components/MultipleRecordsActionMenuEntriesSetter.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,14 @@ | ||
import { DeleteRecordsActionEffect } from '@/action-menu/actions/record-actions/components/DeleteRecordsActionEffect'; | ||
import { ExportRecordsActionEffect } from '@/action-menu/actions/record-actions/components/ExportRecordsActionEffect'; | ||
|
||
const actionEffects = [ExportRecordsActionEffect, DeleteRecordsActionEffect]; | ||
|
||
export const MultipleRecordsActionMenuEntriesSetter = () => { | ||
return ( | ||
<> | ||
{actionEffects.map((ActionEffect, index) => ( | ||
<ActionEffect key={index} position={index} /> | ||
))} | ||
</> | ||
); | ||
}; |
20 changes: 20 additions & 0 deletions
20
...c/modules/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter.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,20 @@ | ||
import { MultipleRecordsActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/MultipleRecordsActionMenuEntriesSetter'; | ||
import { SingleRecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/SingleRecordActionMenuEntriesSetter'; | ||
import { contextStoreTargetedRecordIdsState } from '@/context-store/states/contextStoreTargetedRecordIdsState'; | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
export const RecordActionMenuEntriesSetter = () => { | ||
const contextStoreTargetedRecordIds = useRecoilValue( | ||
contextStoreTargetedRecordIdsState, | ||
); | ||
|
||
if (contextStoreTargetedRecordIds.length === 0) { | ||
return null; | ||
} | ||
|
||
if (contextStoreTargetedRecordIds.length === 1) { | ||
return <SingleRecordActionMenuEntriesSetter />; | ||
} | ||
|
||
return <MultipleRecordsActionMenuEntriesSetter />; | ||
}; |
18 changes: 18 additions & 0 deletions
18
...les/action-menu/actions/record-actions/components/SingleRecordActionMenuEntriesSetter.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,18 @@ | ||
import { DeleteRecordsActionEffect } from '@/action-menu/actions/record-actions/components/DeleteRecordsActionEffect'; | ||
import { ExportRecordsActionEffect } from '@/action-menu/actions/record-actions/components/ExportRecordsActionEffect'; | ||
import { ManageFavoritesActionEffect } from '@/action-menu/actions/record-actions/components/ManageFavoritesActionEffect'; | ||
|
||
export const SingleRecordActionMenuEntriesSetter = () => { | ||
const actionEffects = [ | ||
ExportRecordsActionEffect, | ||
DeleteRecordsActionEffect, | ||
ManageFavoritesActionEffect, | ||
]; | ||
return ( | ||
<> | ||
{actionEffects.map((ActionEffect, index) => ( | ||
<ActionEffect key={index} position={index} /> | ||
))} | ||
</> | ||
); | ||
}; |
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
4 changes: 2 additions & 2 deletions
4
packages/twenty-front/src/modules/action-menu/components/ActionMenuConfirmationModals.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
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
25 changes: 0 additions & 25 deletions
25
packages/twenty-front/src/modules/action-menu/components/ActionMenuEntriesProvider.tsx
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
packages/twenty-front/src/modules/action-menu/components/EmptyActionMenuEntriesEffect.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.