Skip to content
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
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useParams } from 'react-router-dom';
import { useRecoilValue } from 'recoil';
import { useIcons } from 'twenty-ui';

import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { RecordIndexPageKanbanAddButton } from '@/object-record/record-index/components/RecordIndexPageKanbanAddButton';
import { recordIndexViewTypeState } from '@/object-record/record-index/states/recordIndexViewTypeState';
import { PageAddButton } from '@/ui/layout/page/PageAddButton';
import { PageHeader } from '@/ui/layout/page/PageHeader';
Expand All @@ -12,13 +12,15 @@ import { capitalize } from '~/utils/string/capitalize';

type RecordIndexPageHeaderProps = {
createRecord: () => void;
recordIndexId: string;
objectNamePlural: string;
};

export const RecordIndexPageHeader = ({
createRecord,
recordIndexId,
objectNamePlural,
}: RecordIndexPageHeaderProps) => {
const objectNamePlural = useParams().objectNamePlural ?? '';

const { findObjectMetadataItemByNamePlural } =
useFilteredObjectMetadataItems();

Expand All @@ -32,7 +34,7 @@ export const RecordIndexPageHeader = ({

const recordIndexViewType = useRecoilValue(recordIndexViewTypeState);

const canAddRecord =
const isTable =
recordIndexViewType === ViewType.Table && !objectMetadataItem?.isRemote;

Comment on lines +37 to 38
Copy link
Contributor

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.

const pageHeaderTitle =
Expand All @@ -41,7 +43,14 @@ export const RecordIndexPageHeader = ({
return (
<PageHeader title={pageHeaderTitle} Icon={Icon}>
<PageHotkeysEffect onAddButtonClick={createRecord} />
{canAddRecord && <PageAddButton onClick={createRecord} />}
{isTable ? (
<PageAddButton onClick={createRecord} />
) : (
<RecordIndexPageKanbanAddButton
recordIndexId={recordIndexId}
objectNamePlural={objectNamePlural}
/>
)}
</PageHeader>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 styled from '@emotion/styled';
import { useState } from 'react';

import { SingleEntitySelect } from '@/object-record/relation-picker/components/SingleEntitySelect';

import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { RecordIndexPageKanbanAddMenuItem } from '@/object-record/record-index/components/RecordIndexPageKanbanAddMenuItem';
import { useRecordIndexPageKanbanAddButton } from '@/object-record/record-index/hooks/useRecordIndexPageKanbanAddButton';
import { IconButton } from '@/ui/input/button/components/IconButton';
import { IconPlus } from 'twenty-ui';

const StyledDropdownMenuItemsContainer = styled(DropdownMenuItemsContainer)`
width: 100%;
`;

type RecordIndexPageKanbanAddButtonProps = {
recordIndexId: string;
objectNamePlural: string;
};

export const RecordIndexPageKanbanAddButton = ({
recordIndexId,
objectNamePlural,
}: RecordIndexPageKanbanAddButtonProps) => {
const [isSelectingCompany, setIsSelectingCompany] = useState(false);
lucasbordeau marked this conversation as resolved.
Show resolved Hide resolved

const {
dropdownId,
columnIds,
selectFieldMetadataItem,
isOpportunity,
handleOpportunityClick,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep the handlers in the component ? Generally it's better to use hook for logic unrelated to component management, so instead of exporting handlers let's export functions like createOpportunity or createOpportunityWithoutCompany, etc. And then in a handleItemClick we would handle the ternary logic.

That helps us keep business logic separated from component logic.

handleNonOpportunityClick,
handleEntitySelect,
handleCancel,
} = useRecordIndexPageKanbanAddButton({
recordIndexId,
objectNamePlural,
setIsSelectingCompany,
});

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={
<DropdownMenu style={{ width: '200px' }}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Inline style here contradicts the dropdownMenuWidth prop. Consider using only one method for consistency

{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={
isOpportunity
? handleOpportunityClick
: handleNonOpportunityClick
}
/>
))}
</StyledDropdownMenuItemsContainer>
)}
</DropdownMenu>
}
dropdownHotkeyScope={{ scope: dropdownId }}
/>
);
};
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)}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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 { useRecordBoardStates } from '@/object-record/record-board/hooks/internal/useRecordBoardStates';
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition';
import { recordIndexKanbanFieldMetadataIdState } from '@/object-record/record-index/states/recordIndexKanbanFieldMetadataIdState';
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 { usePreviousHotkeyScope } from '@/ui/utilities/hotkey/hooks/usePreviousHotkeyScope';
import { useCallback, useState } from 'react';
import { useRecoilValue } from 'recoil';

type useRecordIndexPageKanbanAddButtonProps = {
recordIndexId: string;
objectNamePlural: string;
setIsSelectingCompany: React.Dispatch<React.SetStateAction<boolean>>;
};

export const useRecordIndexPageKanbanAddButton = ({
recordIndexId,
objectNamePlural,
setIsSelectingCompany,
}: useRecordIndexPageKanbanAddButtonProps) => {
const dropdownId = `record-index-page-add-button-dropdown`;
const { objectNameSingular } = useObjectNameSingularFromPlural({
objectNamePlural,
});
const [selectedColumnDefinition, setSelectedColumnDefinition] =
useState<RecordBoardColumnDefinition>();
const { objectMetadataItem } = useObjectMetadataItem({ objectNameSingular });
const { columnIdsState } = useRecordBoardStates(recordIndexId);
const columnIds = useRecoilValue(columnIdsState);
const recordIndexKanbanFieldMetadataId = useRecoilValue(
recordIndexKanbanFieldMetadataIdState,
);
const { createOneRecord } = useCreateOneRecord({ objectNameSingular });
const {
setHotkeyScopeAndMemorizePreviousScope,
goBackToPreviousHotkeyScope,
} = usePreviousHotkeyScope();

const selectFieldMetadataItem = objectMetadataItem.fields.find(
(field) => field.id === recordIndexKanbanFieldMetadataId,
);
const isOpportunity =
objectMetadataItem.nameSingular === CoreObjectNameSingular.Opportunity;

const handleOpportunityClick = useCallback(
(columnDefinition: RecordBoardColumnDefinition) => {
setIsSelectingCompany(true);
setSelectedColumnDefinition(columnDefinition);
setHotkeyScopeAndMemorizePreviousScope(
RelationPickerHotkeyScope.RelationPicker,
);
},
[
setIsSelectingCompany,
setSelectedColumnDefinition,
setHotkeyScopeAndMemorizePreviousScope,
],
);
const handleNonOpportunityClick = useCallback(
(columnDefinition: RecordBoardColumnDefinition) => {
if (selectFieldMetadataItem !== undefined)
createOneRecord({
[selectFieldMetadataItem.name]: columnDefinition?.value,
position: 'first',
});
},
[createOneRecord, selectFieldMetadataItem],
);
const { resetSearchFilter } = useEntitySelectSearch({
relationPickerScopeId: 'relation-picker',
});

const handleEntitySelect = useCallback(
(company?: EntityForSelect) => {
setIsSelectingCompany(false);
goBackToPreviousHotkeyScope();
resetSearchFilter();
if (company !== undefined && selectFieldMetadataItem !== undefined) {
createOneRecord({
name: company.name,
companyId: company.id,
position: 'first',
[selectFieldMetadataItem.name]: selectedColumnDefinition?.value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: selectedColumnDefinition?.value might be undefined. Consider adding a fallback value

});
}
},
[
createOneRecord,
goBackToPreviousHotkeyScope,
selectFieldMetadataItem,
selectedColumnDefinition,
setIsSelectingCompany,
resetSearchFilter,
],
);
const handleCancel = useCallback(() => {
resetSearchFilter();
goBackToPreviousHotkeyScope();
setIsSelectingCompany(false);
}, [goBackToPreviousHotkeyScope, resetSearchFilter, setIsSelectingCompany]);

return {
dropdownId,
columnIds,
selectFieldMetadataItem,
isOpportunity,
handleOpportunityClick,
handleNonOpportunityClick,
handleEntitySelect,
handleCancel,
};
};
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 };
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export const RecordIndexPage = () => {
return (
<PageContainer>
<PageTitle title={`${capitalize(objectNamePlural)}`} />
<RecordIndexPageHeader createRecord={handleAddButtonClick} />
<RecordIndexPageHeader
createRecord={handleAddButtonClick}
recordIndexId={recordIndexId}
objectNamePlural={objectNamePlural}
/>
<PageBody>
<StyledIndexContainer>
<RecordIndexContainer
Expand Down
Loading