* wip * wip * Ok * Deleted unused code * Fixed lint * Minor fixes * Minor fixes * Minor Fixes * Minor merge fixes * Ok * Fix storybook tests * Removed console.log * Fix login * asd * Fixed storybook * Added await * Fixed await * Added sleep for failing test * Fix sleep * Fix test * Fix tests --------- Co-authored-by: Charles Bochet <charles@twenty.com>
99 lines
3.2 KiB
TypeScript
99 lines
3.2 KiB
TypeScript
import { useRef } from 'react';
|
|
import { Key } from 'ts-key-enum';
|
|
|
|
import { DropdownMenuItem } from '@/ui/dropdown/components/DropdownMenuItem';
|
|
import { DropdownMenuItemsContainer } from '@/ui/dropdown/components/DropdownMenuItemsContainer';
|
|
import { DropdownMenuSelectableItem } from '@/ui/dropdown/components/DropdownMenuSelectableItem';
|
|
import { useScopedHotkeys } from '@/ui/hotkey/hooks/useScopedHotkeys';
|
|
import { Avatar } from '@/users/components/Avatar';
|
|
import { isDefined } from '~/utils/isDefined';
|
|
import { isNonEmptyString } from '~/utils/isNonEmptyString';
|
|
|
|
import { OverflowingTextWithTooltip } from '../../tooltip/OverflowingTextWithTooltip';
|
|
import { useEntitySelectScroll } from '../hooks/useEntitySelectScroll';
|
|
import { EntityForSelect } from '../types/EntityForSelect';
|
|
import { RelationPickerHotkeyScope } from '../types/RelationPickerHotkeyScope';
|
|
|
|
import { DropdownMenuSkeletonItem } from './skeletons/DropdownMenuSkeletonItem';
|
|
|
|
export type EntitiesForSingleEntitySelect<
|
|
CustomEntityForSelect extends EntityForSelect,
|
|
> = {
|
|
selectedEntity: CustomEntityForSelect;
|
|
entitiesToSelect: CustomEntityForSelect[];
|
|
loading: boolean;
|
|
};
|
|
|
|
export function SingleEntitySelectBase<
|
|
CustomEntityForSelect extends EntityForSelect,
|
|
>({
|
|
entities,
|
|
onEntitySelected,
|
|
onCancel,
|
|
}: {
|
|
entities: EntitiesForSingleEntitySelect<CustomEntityForSelect>;
|
|
onEntitySelected: (entity: CustomEntityForSelect | null | undefined) => void;
|
|
onCancel?: () => void;
|
|
}) {
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
let entitiesInDropdown = isDefined(entities.selectedEntity)
|
|
? [entities.selectedEntity, ...(entities.entitiesToSelect ?? [])]
|
|
: entities.entitiesToSelect ?? [];
|
|
|
|
entitiesInDropdown = entitiesInDropdown.filter((entity) =>
|
|
isNonEmptyString(entity.name),
|
|
);
|
|
|
|
const { hoveredIndex, resetScroll } = useEntitySelectScroll({
|
|
entities: entitiesInDropdown,
|
|
containerRef,
|
|
});
|
|
|
|
useScopedHotkeys(
|
|
Key.Enter,
|
|
() => {
|
|
onEntitySelected(entitiesInDropdown[hoveredIndex]);
|
|
resetScroll();
|
|
},
|
|
RelationPickerHotkeyScope.RelationPicker,
|
|
[entitiesInDropdown, hoveredIndex, onEntitySelected],
|
|
);
|
|
|
|
useScopedHotkeys(
|
|
Key.Escape,
|
|
() => {
|
|
onCancel?.();
|
|
},
|
|
RelationPickerHotkeyScope.RelationPicker,
|
|
[onCancel],
|
|
);
|
|
|
|
return (
|
|
<DropdownMenuItemsContainer ref={containerRef} hasMaxHeight>
|
|
{entities.loading ? (
|
|
<DropdownMenuSkeletonItem />
|
|
) : entitiesInDropdown.length === 0 ? (
|
|
<DropdownMenuItem>No result</DropdownMenuItem>
|
|
) : (
|
|
entitiesInDropdown?.map((entity, index) => (
|
|
<DropdownMenuSelectableItem
|
|
key={entity.id}
|
|
selected={entities.selectedEntity?.id === entity.id}
|
|
hovered={hoveredIndex === index}
|
|
onClick={() => onEntitySelected(entity)}
|
|
>
|
|
<Avatar
|
|
avatarUrl={entity.avatarUrl}
|
|
colorId={entity.id}
|
|
placeholder={entity.name}
|
|
size={16}
|
|
type={entity.avatarType ?? 'rounded'}
|
|
/>
|
|
<OverflowingTextWithTooltip text={entity.name} />
|
|
</DropdownMenuSelectableItem>
|
|
))
|
|
)}
|
|
</DropdownMenuItemsContainer>
|
|
);
|
|
}
|