forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TWNTY-6808 - Ability to Filter by Creation Source (twentyhq#7078)
### Description - Ability to Filter by Creation Source ### Demo LOOM: <https://www.loom.com/share/dba9c3d37a4242fe90f977b1babffbde?sid=59b07c51-d245-43cc-bb38-7d898ef72878> ### Refs twentyhq#6808 Fixes twentyhq#6808 --------- Co-authored-by: gitstart-twenty <[email protected]> Co-authored-by: gitstart-twenty <[email protected]> Co-authored-by: bosiraphael <[email protected]>
- Loading branch information
1 parent
2cd3219
commit 35788af
Showing
27 changed files
with
686 additions
and
155 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
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
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
137 changes: 137 additions & 0 deletions
137
...ules/object-record/object-filter-dropdown/components/ObjectFilterDropdownSourceSelect.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,137 @@ | ||
import { useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { useFilterDropdown } from '@/object-record/object-filter-dropdown/hooks/useFilterDropdown'; | ||
import { getSourceEnumOptions } from '@/object-record/object-filter-dropdown/utils/getSourceEnumOptions'; | ||
import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope'; | ||
import { MultipleSelectDropdown } from '@/object-record/select/components/MultipleSelectDropdown'; | ||
import { SelectableItem } from '@/object-record/select/types/SelectableItem'; | ||
import { useDeleteCombinedViewFilters } from '@/views/hooks/useDeleteCombinedViewFilters'; | ||
import { useGetCurrentView } from '@/views/hooks/useGetCurrentView'; | ||
import { ViewFilterOperand } from '@/views/types/ViewFilterOperand'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
export const EMPTY_FILTER_VALUE = '[]'; | ||
export const MAX_ITEMS_TO_DISPLAY = 3; | ||
|
||
type ObjectFilterDropdownSourceSelectProps = { | ||
viewComponentId?: string; | ||
}; | ||
|
||
export const ObjectFilterDropdownSourceSelect = ({ | ||
viewComponentId, | ||
}: ObjectFilterDropdownSourceSelectProps) => { | ||
const { | ||
filterDefinitionUsedInDropdownState, | ||
objectFilterDropdownSearchInputState, | ||
selectedOperandInDropdownState, | ||
selectedFilterState, | ||
setObjectFilterDropdownSelectedRecordIds, | ||
objectFilterDropdownSelectedRecordIdsState, | ||
selectFilter, | ||
emptyFilterButKeepDefinition, | ||
} = useFilterDropdown(); | ||
|
||
const { deleteCombinedViewFilter } = | ||
useDeleteCombinedViewFilters(viewComponentId); | ||
|
||
const { currentViewWithCombinedFiltersAndSorts } = | ||
useGetCurrentView(viewComponentId); | ||
|
||
const filterDefinitionUsedInDropdown = useRecoilValue( | ||
filterDefinitionUsedInDropdownState, | ||
); | ||
const objectFilterDropdownSearchInput = useRecoilValue( | ||
objectFilterDropdownSearchInputState, | ||
); | ||
const selectedOperandInDropdown = useRecoilValue( | ||
selectedOperandInDropdownState, | ||
); | ||
const objectFilterDropdownSelectedRecordIds = useRecoilValue( | ||
objectFilterDropdownSelectedRecordIdsState, | ||
); | ||
const [fieldId] = useState(v4()); | ||
|
||
const selectedFilter = useRecoilValue(selectedFilterState); | ||
|
||
const sourceTypes = getSourceEnumOptions( | ||
objectFilterDropdownSelectedRecordIds, | ||
); | ||
|
||
const filteredSelectedItems = sourceTypes.filter((option) => | ||
objectFilterDropdownSelectedRecordIds.includes(option.id), | ||
); | ||
|
||
const handleMultipleItemSelectChange = ( | ||
itemToSelect: SelectableItem, | ||
newSelectedValue: boolean, | ||
) => { | ||
const newSelectedItemIds = newSelectedValue | ||
? [...objectFilterDropdownSelectedRecordIds, itemToSelect.id] | ||
: objectFilterDropdownSelectedRecordIds.filter( | ||
(id) => id !== itemToSelect.id, | ||
); | ||
|
||
if (newSelectedItemIds.length === 0) { | ||
emptyFilterButKeepDefinition(); | ||
deleteCombinedViewFilter(fieldId); | ||
return; | ||
} | ||
|
||
setObjectFilterDropdownSelectedRecordIds(newSelectedItemIds); | ||
|
||
const selectedItemNames = sourceTypes | ||
.filter((option) => newSelectedItemIds.includes(option.id)) | ||
.map((option) => option.name); | ||
|
||
const filterDisplayValue = | ||
selectedItemNames.length > MAX_ITEMS_TO_DISPLAY | ||
? `${selectedItemNames.length} source types` | ||
: selectedItemNames.join(', '); | ||
|
||
if ( | ||
isDefined(filterDefinitionUsedInDropdown) && | ||
isDefined(selectedOperandInDropdown) | ||
) { | ||
const newFilterValue = | ||
newSelectedItemIds.length > 0 | ||
? JSON.stringify(newSelectedItemIds) | ||
: EMPTY_FILTER_VALUE; | ||
|
||
const viewFilter = | ||
currentViewWithCombinedFiltersAndSorts?.viewFilters.find( | ||
(viewFilter) => | ||
viewFilter.fieldMetadataId === | ||
filterDefinitionUsedInDropdown.fieldMetadataId, | ||
); | ||
|
||
const filterId = viewFilter?.id ?? fieldId; | ||
|
||
selectFilter({ | ||
id: selectedFilter?.id ? selectedFilter.id : filterId, | ||
definition: filterDefinitionUsedInDropdown, | ||
operand: selectedOperandInDropdown || ViewFilterOperand.Is, | ||
displayValue: filterDisplayValue, | ||
fieldMetadataId: filterDefinitionUsedInDropdown.fieldMetadataId, | ||
value: newFilterValue, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<MultipleSelectDropdown | ||
selectableListId="object-filter-source-select-id" | ||
hotkeyScope={RelationPickerHotkeyScope.RelationPicker} | ||
itemsToSelect={sourceTypes.filter( | ||
(item) => | ||
!filteredSelectedItems.some((selected) => selected.id === item.id), | ||
)} | ||
filteredSelectedItems={filteredSelectedItems} | ||
selectedItems={filteredSelectedItems} | ||
onChange={handleMultipleItemSelectChange} | ||
searchFilter={objectFilterDropdownSearchInput} | ||
loadingItems={false} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.