-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
added button in nav bar for kanban view #6829
Merged
lucasbordeau
merged 5 commits into
twentyhq:main
from
ehconitin:TopRightKanbanAddButton
Sep 10, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
48b880c
add button for kanban view
ehconitin cc0e295
Merge branch 'twentyhq:main' into TopRightKanbanAddButton
ehconitin c07f79b
Merge branch 'twentyhq:main' into TopRightKanbanAddButton
ehconitin aedb64d
kept only buisness logic in hook - acc to review
ehconitin 24107c4
Merge branch 'twentyhq:main' into TopRightKanbanAddButton
ehconitin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
158 changes: 158 additions & 0 deletions
158
...ront/src/modules/object-record/record-index/components/RecordIndexPageKanbanAddButton.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,158 @@ | ||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; | ||
import { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates'; | ||
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition'; | ||
import { RecordIndexPageKanbanAddMenuItem } from '@/object-record/record-index/components/RecordIndexPageKanbanAddMenuItem'; | ||
import { useRecordIndexPageKanbanAddButton } from '@/object-record/record-index/hooks/useRecordIndexPageKanbanAddButton'; | ||
import { SingleEntitySelect } from '@/object-record/relation-picker/components/SingleEntitySelect'; | ||
import { useEntitySelectSearch } from '@/object-record/relation-picker/hooks/useEntitySelectSearch'; | ||
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; | ||
import { RelationPickerHotkeyScope } from '@/object-record/relation-picker/types/RelationPickerHotkeyScope'; | ||
import { IconButton } from '@/ui/input/button/components/IconButton'; | ||
import { Dropdown } from '@/ui/layout/dropdown/components/Dropdown'; | ||
import { DropdownMenu } from '@/ui/layout/dropdown/components/DropdownMenu'; | ||
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer'; | ||
import { useDropdown } from '@/ui/layout/dropdown/hooks/useDropdown'; | ||
import { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope'; | ||
import styled from '@emotion/styled'; | ||
import { useCallback, useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { IconPlus, isDefined } from 'twenty-ui'; | ||
|
||
const StyledDropdownMenuItemsContainer = styled(DropdownMenuItemsContainer)` | ||
width: 100%; | ||
`; | ||
|
||
const StyledDropDownMenu = styled(DropdownMenu)` | ||
width: 200px; | ||
`; | ||
|
||
type RecordIndexPageKanbanAddButtonProps = { | ||
recordIndexId: string; | ||
objectNamePlural: string; | ||
}; | ||
|
||
export const RecordIndexPageKanbanAddButton = ({ | ||
recordIndexId, | ||
objectNamePlural, | ||
}: RecordIndexPageKanbanAddButtonProps) => { | ||
const dropdownId = `record-index-page-add-button-dropdown`; | ||
const [isSelectingCompany, setIsSelectingCompany] = useState(false); | ||
lucasbordeau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [selectedColumnDefinition, setSelectedColumnDefinition] = | ||
useState<RecordBoardColumnDefinition>(); | ||
|
||
const { columnIdsState } = useRecordBoardStates(recordIndexId); | ||
const columnIds = useRecoilValue(columnIdsState); | ||
|
||
const { | ||
setHotkeyScopeAndMemorizePreviousScope, | ||
goBackToPreviousHotkeyScope, | ||
} = usePreviousHotkeyScope(); | ||
const { resetSearchFilter } = useEntitySelectSearch({ | ||
relationPickerScopeId: 'relation-picker', | ||
}); | ||
|
||
const { closeDropdown } = useDropdown(dropdownId); | ||
|
||
const { | ||
selectFieldMetadataItem, | ||
isOpportunity, | ||
createOpportunity, | ||
createRecordWithoutCompany, | ||
} = useRecordIndexPageKanbanAddButton({ | ||
objectNamePlural, | ||
}); | ||
|
||
const handleItemClick = useCallback( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better ! |
||
(columnDefinition: RecordBoardColumnDefinition) => { | ||
if (isOpportunity) { | ||
setIsSelectingCompany(true); | ||
setSelectedColumnDefinition(columnDefinition); | ||
setHotkeyScopeAndMemorizePreviousScope( | ||
RelationPickerHotkeyScope.RelationPicker, | ||
); | ||
} else { | ||
createRecordWithoutCompany(columnDefinition); | ||
closeDropdown(); | ||
} | ||
}, | ||
[ | ||
isOpportunity, | ||
createRecordWithoutCompany, | ||
setHotkeyScopeAndMemorizePreviousScope, | ||
closeDropdown, | ||
], | ||
); | ||
|
||
const handleEntitySelect = useCallback( | ||
(company?: EntityForSelect) => { | ||
setIsSelectingCompany(false); | ||
goBackToPreviousHotkeyScope(); | ||
resetSearchFilter(); | ||
if (isDefined(company) && isDefined(selectedColumnDefinition)) { | ||
createOpportunity(company, selectedColumnDefinition); | ||
} | ||
closeDropdown(); | ||
}, | ||
[ | ||
createOpportunity, | ||
goBackToPreviousHotkeyScope, | ||
resetSearchFilter, | ||
selectedColumnDefinition, | ||
closeDropdown, | ||
], | ||
); | ||
|
||
const handleCancel = useCallback(() => { | ||
resetSearchFilter(); | ||
goBackToPreviousHotkeyScope(); | ||
setIsSelectingCompany(false); | ||
}, [goBackToPreviousHotkeyScope, resetSearchFilter]); | ||
|
||
if (!selectFieldMetadataItem) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Dropdown | ||
dropdownMenuWidth="200px" | ||
dropdownPlacement="bottom-start" | ||
clickableComponent={ | ||
<IconButton | ||
Icon={IconPlus} | ||
dataTestId="add-button" | ||
size="medium" | ||
variant="secondary" | ||
accent="default" | ||
ariaLabel="Add" | ||
/> | ||
} | ||
dropdownId={dropdownId} | ||
dropdownComponents={ | ||
<StyledDropDownMenu> | ||
{isOpportunity && isSelectingCompany ? ( | ||
<SingleEntitySelect | ||
disableBackgroundBlur | ||
onCancel={handleCancel} | ||
onEntitySelected={handleEntitySelect} | ||
relationObjectNameSingular={CoreObjectNameSingular.Company} | ||
relationPickerScopeId="relation-picker" | ||
selectedRelationRecordIds={[]} | ||
/> | ||
) : ( | ||
<StyledDropdownMenuItemsContainer> | ||
{columnIds.map((columnId) => ( | ||
<RecordIndexPageKanbanAddMenuItem | ||
key={columnId} | ||
columnId={columnId} | ||
recordIndexId={recordIndexId} | ||
onItemClick={handleItemClick} | ||
/> | ||
))} | ||
</StyledDropdownMenuItemsContainer> | ||
)} | ||
</StyledDropDownMenu> | ||
} | ||
dropdownHotkeyScope={{ scope: dropdownId }} | ||
/> | ||
); | ||
}; |
55 changes: 55 additions & 0 deletions
55
...nt/src/modules/object-record/record-index/components/RecordIndexPageKanbanAddMenuItem.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,55 @@ | ||
import { RecordBoardColumnDefinitionType } from '@/object-record/record-board/types/RecordBoardColumnDefinition'; | ||
import { useRecordIndexPageKanbanAddMenuItem } from '@/object-record/record-index/hooks/useRecordIndexPageKanbanAddMenuItem'; | ||
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem'; | ||
import styled from '@emotion/styled'; | ||
import { Tag } from 'twenty-ui'; | ||
|
||
const StyledMenuItem = styled(MenuItem)` | ||
width: 200px; | ||
`; | ||
|
||
type RecordIndexPageKanbanAddMenuItemProps = { | ||
columnId: string; | ||
recordIndexId: string; | ||
onItemClick: (columnDefinition: any) => void; | ||
}; | ||
|
||
export const RecordIndexPageKanbanAddMenuItem = ({ | ||
columnId, | ||
recordIndexId, | ||
onItemClick, | ||
}: RecordIndexPageKanbanAddMenuItemProps) => { | ||
const { columnDefinition } = useRecordIndexPageKanbanAddMenuItem( | ||
recordIndexId, | ||
columnId, | ||
); | ||
if (!columnDefinition) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<StyledMenuItem | ||
text={ | ||
<Tag | ||
variant={ | ||
columnDefinition.type === RecordBoardColumnDefinitionType.Value | ||
? 'solid' | ||
: 'outline' | ||
} | ||
color={ | ||
columnDefinition.type === RecordBoardColumnDefinitionType.Value | ||
? columnDefinition.color | ||
: 'transparent' | ||
} | ||
text={columnDefinition.title} | ||
weight={ | ||
columnDefinition.type === RecordBoardColumnDefinitionType.Value | ||
? 'regular' | ||
: 'medium' | ||
} | ||
/> | ||
} | ||
onClick={() => onItemClick(columnDefinition)} | ||
/> | ||
); | ||
}; |
65 changes: 65 additions & 0 deletions
65
...y-front/src/modules/object-record/record-index/hooks/useRecordIndexPageKanbanAddButton.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 { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { useObjectNameSingularFromPlural } from '@/object-metadata/hooks/useObjectNameSingularFromPlural'; | ||
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular'; | ||
import { useCreateOneRecord } from '@/object-record/hooks/useCreateOneRecord'; | ||
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition'; | ||
import { recordIndexKanbanFieldMetadataIdState } from '@/object-record/record-index/states/recordIndexKanbanFieldMetadataIdState'; | ||
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { isDefined } from 'twenty-ui'; | ||
|
||
type useRecordIndexPageKanbanAddButtonProps = { | ||
objectNamePlural: string; | ||
}; | ||
|
||
export const useRecordIndexPageKanbanAddButton = ({ | ||
objectNamePlural, | ||
}: useRecordIndexPageKanbanAddButtonProps) => { | ||
const { objectNameSingular } = useObjectNameSingularFromPlural({ | ||
objectNamePlural, | ||
}); | ||
const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular }); | ||
|
||
const recordIndexKanbanFieldMetadataId = useRecoilValue( | ||
recordIndexKanbanFieldMetadataIdState, | ||
); | ||
const { createOneRecord } = useCreateOneRecord({ objectNameSingular }); | ||
|
||
const selectFieldMetadataItem = objectMetadataItem.fields.find( | ||
(field) => field.id === recordIndexKanbanFieldMetadataId, | ||
); | ||
const isOpportunity = | ||
objectMetadataItem.nameSingular === CoreObjectNameSingular.Opportunity; | ||
|
||
const createOpportunity = ( | ||
company: EntityForSelect, | ||
columnDefinition: RecordBoardColumnDefinition, | ||
) => { | ||
if (isDefined(selectFieldMetadataItem)) { | ||
createOneRecord({ | ||
name: company.name, | ||
companyId: company.id, | ||
position: 'first', | ||
[selectFieldMetadataItem.name]: columnDefinition?.value, | ||
}); | ||
} | ||
}; | ||
|
||
const createRecordWithoutCompany = ( | ||
columnDefinition: RecordBoardColumnDefinition, | ||
) => { | ||
if (isDefined(selectFieldMetadataItem)) { | ||
createOneRecord({ | ||
[selectFieldMetadataItem.name]: columnDefinition?.value, | ||
position: 'first', | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
selectFieldMetadataItem, | ||
isOpportunity, | ||
createOpportunity, | ||
createRecordWithoutCompany, | ||
}; | ||
}; |
12 changes: 12 additions & 0 deletions
12
...front/src/modules/object-record/record-index/hooks/useRecordIndexPageKanbanAddMenuItem.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,12 @@ | ||
import { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates'; | ||
import { useRecoilValue } from 'recoil'; | ||
|
||
export const useRecordIndexPageKanbanAddMenuItem = ( | ||
recordIndexId: string, | ||
columnId: string, | ||
) => { | ||
const { columnsFamilySelector } = useRecordBoardStates(recordIndexId); | ||
const columnDefinition = useRecoilValue(columnsFamilySelector(columnId)); | ||
|
||
return { columnDefinition }; | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Variable name changed from 'canAddRecord' to 'isTable'. Ensure this doesn't affect the logic elsewhere in the component.