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

fix(SingleEntitySelectMenuItems): extract Add New button from entitiesToSelect #8474

Merged
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { isNonEmptyString } from '@sniptt/guards';
import { Fragment, useRef } from 'react';
import { useRef } from 'react';
import { useRecoilValue } from 'recoil';
import { Key } from 'ts-key-enum';
import { IconComponent, IconPlus, MenuItem, MenuItemSelect } from 'twenty-ui';
import { IconComponent, MenuItem, MenuItemSelect } from 'twenty-ui';

import { SelectableMenuItemSelect } from '@/object-record/relation-picker/components/SelectableMenuItemSelect';
import { SINGLE_ENTITY_SELECT_BASE_LIST } from '@/object-record/relation-picker/constants/SingleEntitySelectBaseList';
import { CreateNewButton } from '@/ui/input/relation-picker/components/CreateNewButton';
import { DropdownMenuSkeletonItem } from '@/ui/input/relation-picker/components/skeletons/DropdownMenuSkeletonItem';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { SelectableList } from '@/ui/layout/selectable-list/components/SelectableList';
import { useSelectableList } from '@/ui/layout/selectable-list/hooks/useSelectableList';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
Expand All @@ -26,7 +24,6 @@ export type SingleEntitySelectMenuItemsProps = {
onCancel?: () => void;
onEntitySelected: (entity?: EntityForSelect) => void;
selectedEntity?: EntityForSelect;
onCreate?: () => void;
showCreateButton?: boolean;
nicolasrouanne marked this conversation as resolved.
Show resolved Hide resolved
SelectAllIcon?: IconComponent;
selectAllLabel?: string;
Expand All @@ -46,8 +43,6 @@ export const SingleEntitySelectMenuItems = ({
onCancel,
onEntitySelected,
selectedEntity,
onCreate,
showCreateButton,
SelectAllIcon,
selectAllLabel,
isAllEntitySelected,
Expand All @@ -59,14 +54,6 @@ export const SingleEntitySelectMenuItems = ({
}: SingleEntitySelectMenuItemsProps) => {
const containerRef = useRef<HTMLDivElement>(null);

const createNewRecord = showCreateButton
? {
__typename: '',
id: 'add-new',
name: 'Add New',
}
: null;

const selectNone = emptyLabel
? {
__typename: '',
Expand All @@ -88,7 +75,6 @@ export const SingleEntitySelectMenuItems = ({
selectNone,
selectedEntity,
...entitiesToSelect,
createNewRecord,
].filter(
(entity): entity is EntityForSelect =>
isDefined(entity) && isNonEmptyString(entity.name),
Expand All @@ -98,10 +84,6 @@ export const SingleEntitySelectMenuItems = ({
SINGLE_ENTITY_SELECT_BASE_LIST,
);

const isSelectedAddNewButton = useRecoilValue(
isSelectedItemIdSelector('add-new'),
);

const isSelectedSelectNoneButton = useRecoilValue(
isSelectedItemIdSelector('select-none'),
);
Expand Down Expand Up @@ -129,14 +111,10 @@ export const SingleEntitySelectMenuItems = ({
selectableItemIdArray={selectableItemIds}
hotkeyScope={hotkeyScope}
onEnter={(itemId) => {
if (itemId === 'add-new' && showCreateButton === true) {
onCreate?.();
} else {
const entityIndex = entitiesInDropdown.findIndex(
(entity) => entity.id === itemId,
);
onEntitySelected(entitiesInDropdown[entityIndex]);
}
const entityIndex = entitiesInDropdown.findIndex(
(entity) => entity.id === itemId,
);
onEntitySelected(entitiesInDropdown[entityIndex]);
resetSelectedItem();
}}
>
Expand All @@ -146,33 +124,10 @@ export const SingleEntitySelectMenuItems = ({
) : entitiesInDropdown.length === 0 &&
!isAllEntitySelectShown &&
!loading ? (
<>
<MenuItem text="No result" />
{entitiesToSelect.length > 0 && <DropdownMenuSeparator />}
<CreateNewButton
key="add-new"
onClick={onCreate}
LeftIcon={IconPlus}
text="Add New"
hovered={isSelectedAddNewButton}
/>
</>
<MenuItem text="No result" />
) : (
entitiesInDropdown?.map((entity) => {
switch (entity.id) {
case 'add-new': {
return (
<Fragment key={entity.id}>
{entitiesToSelect.length > 0 && <DropdownMenuSeparator />}
<CreateNewButton
onClick={onCreate}
LeftIcon={IconPlus}
text="Add New"
hovered={isSelectedAddNewButton}
/>
</Fragment>
);
}
case 'select-none': {
return (
emptyLabel && (
Expand Down
nicolasrouanne marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
} from '@/object-record/relation-picker/components/SingleEntitySelectMenuItems';
import { useEntitySelectSearch } from '@/object-record/relation-picker/hooks/useEntitySelectSearch';
import { useRelationPickerEntitiesOptions } from '@/object-record/relation-picker/hooks/useRelationPickerEntitiesOptions';
import { CreateNewButton } from '@/ui/input/relation-picker/components/CreateNewButton';
import { DropdownMenuItemsContainer } from '@/ui/layout/dropdown/components/DropdownMenuItemsContainer';
import { DropdownMenuSearchInput } from '@/ui/layout/dropdown/components/DropdownMenuSearchInput';
import { DropdownMenuSeparator } from '@/ui/layout/dropdown/components/DropdownMenuSeparator';
import { Placement } from '@floating-ui/react';
import { IconPlus } from 'twenty-ui';
import { isDefined } from '~/utils/isDefined';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';

Expand Down Expand Up @@ -49,21 +52,15 @@ export const SingleEntitySelectMenuItemsWithSearch = ({
excludedRelationRecordIds,
});

const showCreateButton = isDefined(onCreate);

let onCreateWithInput = undefined;

if (isDefined(onCreate)) {
Copy link
Member

Choose a reason for hiding this comment

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

FYI I think we should have kept that #8529 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry to have introduced that 😢
I think I didn't really understand what it was used for.
I was under the impression my change didn't impact the public API of the component... but it seems I was wrong, sorry.

It's weird that TS didn't catch it

Copy link
Member

Choose a reason for hiding this comment

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

No problem at al! I should have seen it 😅 (+ lack of good tests on our codebase). That code looks ugly I would have been tempted to remove it too!

onCreateWithInput = () => {
if (onCreate.length > 0) {
(onCreate as (searchInput?: string) => void)(
relationPickerSearchFilter,
);
} else {
(onCreate as () => void)();
}
};
}
const createNewButton = isDefined(onCreate) && (
<DropdownMenuItemsContainer>
<CreateNewButton
onClick={() => onCreate?.(relationPickerSearchFilter)}
LeftIcon={IconPlus}
text="Add New"
/>
</DropdownMenuItemsContainer>
nicolasrouanne marked this conversation as resolved.
Show resolved Hide resolved
);

const results = (
<SingleEntitySelectMenuItems
Expand All @@ -76,14 +73,12 @@ export const SingleEntitySelectMenuItemsWithSearch = ({
}
shouldSelectEmptyOption={selectedRelationRecordIds?.length === 0}
hotkeyScope={relationPickerScopeId}
onCreate={onCreateWithInput}
isFiltered={!!relationPickerSearchFilter}
{...{
EmptyIcon,
emptyLabel,
onCancel,
onEntitySelected,
showCreateButton,
}}
/>
);
Expand All @@ -104,6 +99,8 @@ export const SingleEntitySelectMenuItemsWithSearch = ({
{results}
</>
)}
{entities.entitiesToSelect.length > 0 && <DropdownMenuSeparator />}
nicolasrouanne marked this conversation as resolved.
Show resolved Hide resolved
{createNewButton}
</>
);
};
Loading