-
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.
- Loading branch information
Showing
13 changed files
with
306 additions
and
161 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
87 changes: 87 additions & 0 deletions
87
packages/twenty-front/src/modules/object-record/record-group/hooks/useRecordGroupActions.ts
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,87 @@ | ||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { getObjectSlug } from '@/object-metadata/utils/getObjectSlug'; | ||
import { RecordBoardColumnContext } from '@/object-record/record-board/record-board-column/contexts/RecordBoardColumnContext'; | ||
import { useRecordGroupStates } from '@/object-record/record-group/hooks/useRecordGroupStates'; | ||
import { useRecordGroupVisibility } from '@/object-record/record-group/hooks/useRecordGroupVisibility'; | ||
import { RecordGroupAction } from '@/object-record/record-group/types/RecordGroupActions'; | ||
import { RecordIndexRootPropsContext } from '@/object-record/record-index/contexts/RecordIndexRootPropsContext'; | ||
import { navigationMemorizedUrlState } from '@/ui/navigation/states/navigationMemorizedUrlState'; | ||
import { useCallback, useContext, useMemo } from 'react'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import { useSetRecoilState } from 'recoil'; | ||
import { IconEyeOff, IconSettings } from 'twenty-ui'; | ||
|
||
export const useRecordGroupActions = () => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
|
||
const { objectNameSingular, recordIndexId } = useContext( | ||
RecordIndexRootPropsContext, | ||
); | ||
|
||
const { columnDefinition: recordGroupDefinition } = useContext( | ||
RecordBoardColumnContext, | ||
); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItem({ | ||
objectNameSingular, | ||
}); | ||
|
||
const { viewGroupFieldMetadataItem } = useRecordGroupStates({ | ||
objectNameSingular, | ||
viewBarId: recordIndexId, | ||
}); | ||
|
||
const { handleVisibilityChange: handleRecordGroupVisibilityChange } = | ||
useRecordGroupVisibility({ | ||
viewBarId: recordIndexId, | ||
}); | ||
|
||
const setNavigationMemorizedUrl = useSetRecoilState( | ||
navigationMemorizedUrlState, | ||
); | ||
|
||
const navigateToSelectSettings = useCallback(() => { | ||
setNavigationMemorizedUrl(location.pathname + location.search); | ||
navigate( | ||
`/settings/objects/${getObjectSlug(objectMetadataItem)}/${viewGroupFieldMetadataItem?.name}`, | ||
); | ||
}, [ | ||
setNavigationMemorizedUrl, | ||
location.pathname, | ||
location.search, | ||
navigate, | ||
objectMetadataItem, | ||
viewGroupFieldMetadataItem?.name, | ||
]); | ||
|
||
const recordGroupActions: RecordGroupAction[] = useMemo( | ||
() => [ | ||
{ | ||
id: 'edit', | ||
label: 'Edit', | ||
icon: IconSettings, | ||
position: 0, | ||
callback: () => { | ||
navigateToSelectSettings(); | ||
}, | ||
}, | ||
{ | ||
id: 'hide', | ||
label: 'Hide', | ||
icon: IconEyeOff, | ||
position: 1, | ||
callback: () => { | ||
handleRecordGroupVisibilityChange(recordGroupDefinition); | ||
}, | ||
}, | ||
], | ||
[ | ||
handleRecordGroupVisibilityChange, | ||
navigateToSelectSettings, | ||
recordGroupDefinition, | ||
], | ||
); | ||
|
||
return recordGroupActions; | ||
}; |
60 changes: 60 additions & 0 deletions
60
packages/twenty-front/src/modules/object-record/record-group/hooks/useRecordGroupReorder.ts
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,60 @@ | ||
import { OnDragEndResponder } from '@hello-pangea/dnd'; | ||
import { useCallback } from 'react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
|
||
import { moveArrayItem } from '~/utils/array/moveArrayItem'; | ||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual'; | ||
import { useSaveCurrentViewGroups } from '@/views/hooks/useSaveCurrentViewGroups'; | ||
import { recordIndexGroupDefinitionsState } from '@/object-record/record-index/states/recordIndexGroupDefinitionsState'; | ||
import { mapGroupDefinitionsToViewGroups } from '@/views/utils/mapGroupDefinitionsToViewGroups'; | ||
import { useRecordGroupStates } from '@/object-record/record-group/hooks/useRecordGroupStates'; | ||
|
||
type UseRecordGroupHandlersParams = { | ||
objectNameSingular: string; | ||
viewBarId: string; | ||
}; | ||
|
||
export const useRecordGroupReoder = ({ | ||
objectNameSingular, | ||
viewBarId, | ||
}: UseRecordGroupHandlersParams) => { | ||
const setRecordIndexGroupDefinitions = useSetRecoilState( | ||
recordIndexGroupDefinitionsState, | ||
); | ||
|
||
const { visibleRecordGroups } = useRecordGroupStates({ | ||
objectNameSingular, | ||
viewBarId, | ||
}); | ||
|
||
const { saveViewGroups } = useSaveCurrentViewGroups(viewBarId); | ||
|
||
const handleOrderChange: OnDragEndResponder = useCallback( | ||
(result) => { | ||
if (!result.destination) { | ||
return; | ||
} | ||
|
||
const reorderedVisibleBoardGroups = moveArrayItem(visibleRecordGroups, { | ||
fromIndex: result.source.index - 1, | ||
toIndex: result.destination.index - 1, | ||
}); | ||
|
||
if (isDeeplyEqual(visibleRecordGroups, reorderedVisibleBoardGroups)) | ||
return; | ||
|
||
const updatedGroups = [...reorderedVisibleBoardGroups].map( | ||
(group, index) => ({ ...group, position: index }), | ||
); | ||
|
||
setRecordIndexGroupDefinitions(updatedGroups); | ||
saveViewGroups(mapGroupDefinitionsToViewGroups(updatedGroups)); | ||
}, | ||
[saveViewGroups, setRecordIndexGroupDefinitions, visibleRecordGroups], | ||
); | ||
|
||
return { | ||
visibleRecordGroups, | ||
handleOrderChange, | ||
}; | ||
}; |
65 changes: 65 additions & 0 deletions
65
packages/twenty-front/src/modules/object-record/record-group/hooks/useRecordGroupStates.ts
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,65 @@ | ||
import { useMemo } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { recordIndexGroupDefinitionsState } from '@/object-record/record-index/states/recordIndexGroupDefinitionsState'; | ||
|
||
type UseRecordGroupStatesParams = { | ||
objectNameSingular: string; | ||
viewBarId: string; | ||
}; | ||
|
||
export const useRecordGroupStates = ({ | ||
objectNameSingular, | ||
}: UseRecordGroupStatesParams) => { | ||
const recordIndexGroupDefinitions = useRecoilValue( | ||
recordIndexGroupDefinitionsState, | ||
); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItem({ | ||
objectNameSingular, | ||
}); | ||
|
||
const viewGroupFieldMetadataItem = useMemo(() => { | ||
if (recordIndexGroupDefinitions.length === 0) return null; | ||
// We're assuming that all groups have the same fieldMetadataId for now | ||
const fieldMetadataId = | ||
'fieldMetadataId' in recordIndexGroupDefinitions[0] | ||
? recordIndexGroupDefinitions[0].fieldMetadataId | ||
: null; | ||
|
||
if (!fieldMetadataId) return null; | ||
|
||
return objectMetadataItem.fields.find( | ||
(field) => field.id === fieldMetadataId, | ||
); | ||
}, [objectMetadataItem, recordIndexGroupDefinitions]); | ||
|
||
const visibleRecordGroups = useMemo( | ||
() => | ||
recordIndexGroupDefinitions | ||
.filter((boardGroup) => boardGroup.isVisible) | ||
.sort( | ||
(boardGroupA, boardGroupB) => | ||
boardGroupA.position - boardGroupB.position, | ||
), | ||
[recordIndexGroupDefinitions], | ||
); | ||
|
||
const hiddenRecordGroups = useMemo( | ||
() => | ||
recordIndexGroupDefinitions | ||
.filter((boardGroup) => !boardGroup.isVisible) | ||
.map((boardGroup) => ({ | ||
...boardGroup, | ||
isVisible: false, | ||
})), | ||
[recordIndexGroupDefinitions], | ||
); | ||
|
||
return { | ||
hiddenRecordGroups, | ||
visibleRecordGroups, | ||
viewGroupFieldMetadataItem, | ||
}; | ||
}; |
47 changes: 47 additions & 0 deletions
47
...ges/twenty-front/src/modules/object-record/record-group/hooks/useRecordGroupVisibility.ts
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,47 @@ | ||
import { useCallback } from 'react'; | ||
import { useRecoilState } from 'recoil'; | ||
|
||
import { useSaveCurrentViewGroups } from '@/views/hooks/useSaveCurrentViewGroups'; | ||
import { recordIndexGroupDefinitionsState } from '@/object-record/record-index/states/recordIndexGroupDefinitionsState'; | ||
import { mapGroupDefinitionsToViewGroups } from '@/views/utils/mapGroupDefinitionsToViewGroups'; | ||
import { RecordGroupDefinition } from '@/object-record/record-group/types/RecordGroupDefinition'; | ||
|
||
type UseRecordGroupVisibilityParams = { | ||
viewBarId: string; | ||
}; | ||
|
||
export const useRecordGroupVisibility = ({ | ||
viewBarId, | ||
}: UseRecordGroupVisibilityParams) => { | ||
const [recordIndexGroupDefinitions, setRecordIndexGroupDefinitions] = | ||
useRecoilState(recordIndexGroupDefinitionsState); | ||
|
||
const { saveViewGroups } = useSaveCurrentViewGroups(viewBarId); | ||
|
||
const handleVisibilityChange = useCallback( | ||
async (updatedGroupDefinition: RecordGroupDefinition) => { | ||
const updatedGroupsDefinitions = recordIndexGroupDefinitions.map( | ||
(groupDefinition) => | ||
groupDefinition.id === updatedGroupDefinition.id | ||
? { | ||
...groupDefinition, | ||
isVisible: !groupDefinition.isVisible, | ||
} | ||
: groupDefinition, | ||
); | ||
|
||
setRecordIndexGroupDefinitions(updatedGroupsDefinitions); | ||
|
||
saveViewGroups(mapGroupDefinitionsToViewGroups(updatedGroupsDefinitions)); | ||
}, | ||
[ | ||
recordIndexGroupDefinitions, | ||
setRecordIndexGroupDefinitions, | ||
saveViewGroups, | ||
], | ||
); | ||
|
||
return { | ||
handleVisibilityChange, | ||
}; | ||
}; |
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
Oops, something went wrong.