Files
twenty/front/src/modules/object-metadata/components/ObjectMetadataItemsRelationPickerEffect.tsx
gitstart-twenty 0fa55b0634 Fix Frontend modules tests (#2688)
* Fix naming issue

Co-authored-by: gitstart-twenty <twenty@gitstart.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

* Fix more tests

Co-authored-by: gitstart-twenty <twenty@gitstart.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

* Revert unnecessary changes

Co-authored-by: gitstart-twenty <twenty@gitstart.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

* Refactor according to self-review

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: gitstart-twenty <twenty@gitstart.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>

* Fix graphql mocks not working anymore

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
2023-11-28 23:33:34 +01:00

83 lines
2.1 KiB
TypeScript

import { useEffect } from 'react';
import { useRelationPicker } from '@/ui/input/components/internal/relation-picker/hooks/useRelationPicker';
import { IdentifiersMapper } from '@/ui/input/components/internal/relation-picker/types/IdentifiersMapper';
import { getLogoUrlFromDomainName } from '~/utils';
export const ObjectMetadataItemsRelationPickerEffect = () => {
const { setIdentifiersMapper, setSearchQuery } = useRelationPicker({
relationPickerScopeId: 'relation-picker',
});
const computeFilterFields = (relationPickerType: string) => {
if (relationPickerType === 'company') {
return ['name'];
}
if (['workspaceMember', 'person'].includes(relationPickerType)) {
return ['name.firstName', 'name.lastName'];
}
return ['name'];
};
const identifierMapper: IdentifiersMapper = (
record: any,
objectMetadataItemSingularName: string,
) => {
if (!record) {
return;
}
if (objectMetadataItemSingularName === 'company') {
return {
id: record.id,
name: record.name,
avatarUrl: getLogoUrlFromDomainName(record.domainName ?? ''),
avatarType: 'squared',
record: record,
};
}
if (
['workspaceMember', 'person'].includes(objectMetadataItemSingularName)
) {
return {
id: record.id,
name:
(record.name?.firstName ?? '') + ' ' + (record.name?.lastName ?? ''),
avatarUrl: record.avatarUrl,
avatarType: 'rounded',
record: record,
};
}
if (['opportunity'].includes(objectMetadataItemSingularName)) {
return {
id: record.id,
name: record?.company?.name,
avatarUrl: record.avatarUrl,
avatarType: 'rounded',
record: record,
};
}
return {
id: record.id,
name: record.name,
avatarUrl: record.avatarUrl,
avatarType: 'rounded',
record,
};
};
useEffect(() => {
setIdentifiersMapper(() => identifierMapper);
setSearchQuery({
computeFilterFields,
});
}, [setIdentifiersMapper, setSearchQuery]);
return <></>;
};