The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useFilteredObjectMetadataItems } from '@/object-metadata/hooks/useFilteredObjectMetadataItems';
import { type EnrichedObjectMetadataItem } from '@/object-metadata/types/EnrichedObjectMetadataItem';
import { type FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { isHiddenSystemField } from '@/object-metadata/utils/isHiddenSystemField';
import { StyledSettingsDataModelTableBodyContainer } from '@/settings/data-model/components/SettingsDataModelTableBodyContainer';
import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';
import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { SortableTableHeader } from '@/ui/layout/table/components/SortableTableHeader';
import { Table } from '@/ui/layout/table/components/Table';
import { TableBody } from '@/ui/layout/table/components/TableBody';
import { TableCell } from '@/ui/layout/table/components/TableCell';
import { TableHeader } from '@/ui/layout/table/components/TableHeader';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { useSortedArray } from '@/ui/layout/table/hooks/useSortedArray';
import { type TableMetadata } from '@/ui/layout/table/types/TableMetadata';
import { styled } from '@linaria/react';
import { msg, plural } from '@lingui/core/macro';
import { useLingui } from '@lingui/react/macro';
import { useMemo, useState } from 'react';
import { FieldMetadataType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { Dropdown } from 'twenty-ui/components/navigation';
import { SettingsRow } from 'twenty-ui/components/settings';
import {
IconArchive,
IconFilter,
IconSearch,
IconSettings,
} from 'twenty-ui/icon';
import { Button } from 'twenty-ui/primitives/input';
import { themeCssVariables } from 'twenty-ui/theme';
import {
OBJECT_RELATION_TABLE_ROW_GRID_TEMPLATE_COLUMNS,
SettingsObjectRelationItemTableRow,
} from './SettingsObjectRelationItemTableRow';
import { normalizeSearchText } from 'twenty-ui/utilities';
const StyledSearchAndFilterContainer = styled.div`
display: flex;
gap: ${themeCssVariables.spacing[2]};
padding-bottom: ${themeCssVariables.spacing[2]};
width: 100%;
`;
const StyledSearchInputContainer = styled.div`
flex: 1;
`;
const SETTINGS_OBJECT_RELATION_TABLE_METADATA: TableMetadata<FieldMetadataItem> =
{
tableId: 'settingsObjectRelations',
fields: [
{
fieldLabel: msg`Name`,
fieldName: 'label',
fieldType: 'string',
align: 'left',
},
{
fieldLabel: msg`App`,
fieldName: 'applicationId',
fieldType: 'string',
align: 'left',
},
{
fieldLabel: msg`Type`,
fieldName: 'type',
fieldType: 'string',
align: 'left',
},
],
initialSort: {
fieldName: 'label',
direction: 'asc',
},
};
type SettingsObjectRelationsTableProps = {
objectMetadataItem: EnrichedObjectMetadataItem;
};
const getRelationTargetObjectMetadataIds = (field: FieldMetadataItem) => {
if (field.type === FieldMetadataType.MORPH_RELATION) {
return (
field.morphRelations?.map(
(relation) => relation.targetObjectMetadata.id,
) ?? []
);
}
return isDefined(field.relation?.targetObjectMetadata.id)
? [field.relation.targetObjectMetadata.id]
: [];
};
const getMorphRelationTargetLabel = (field: FieldMetadataItem) => {
const morphRelationCount = field.morphRelations?.length ?? 0;
return plural(morphRelationCount, {
one: '# Object',
other: '# Objects',
});
};
const getMorphRelationFieldLabel = (field: FieldMetadataItem) => {
return field.label;
};
export const SettingsObjectRelationsTable = ({
objectMetadataItem,
}: SettingsObjectRelationsTableProps) => {
const { t } = useLingui();
const [searchTerm, setSearchTerm] = useState('');
const [showInactive, setShowInactive] = useState(true);
const [showSystemRelations, setShowSystemRelations] = useState(false);
const tableMetadata = SETTINGS_OBJECT_RELATION_TABLE_METADATA;
const { objectMetadataItems } = useFilteredObjectMetadataItems();
const relationFields = useMemo(() => {
return objectMetadataItem.fields.filter((field) => {
const isRelationField =
field.type === FieldMetadataType.RELATION ||
field.type === FieldMetadataType.MORPH_RELATION;
if (!isRelationField) {
return false;
}
const relationTargetObjectMetadataIds =
getRelationTargetObjectMetadataIds(field);
const isRelationToSystemObject = relationTargetObjectMetadataIds.some(
(objectMetadataId) =>
objectMetadataItems.some(
(objectMetadataItem) =>
objectMetadataItem.id === objectMetadataId &&
objectMetadataItem.isSystem,
),
);
return (
showSystemRelations ||
(!isHiddenSystemField(field) && !isRelationToSystemObject)
);
});
}, [objectMetadataItem.fields, showSystemRelations, objectMetadataItems]);
const sortedRelationFields = useSortedArray(relationFields, tableMetadata);
const filteredRelationFields = useMemo(() => {
const searchNormalized = normalizeSearchText(searchTerm);
return sortedRelationFields.filter((field) => {
const matchesActiveFilter = showInactive || field.isActive;
const relationTargetObjectLabels = getRelationTargetObjectMetadataIds(
field,
).flatMap((objectMetadataId) => {
const relationObjectMetadataItem = objectMetadataItems.find(
(objectMetadataItem) => objectMetadataItem.id === objectMetadataId,
);
return isDefined(relationObjectMetadataItem)
? [
relationObjectMetadataItem.labelSingular,
relationObjectMetadataItem.labelPlural,
]
: [];
});
const morphRelationLabels =
field.type === FieldMetadataType.MORPH_RELATION
? [
getMorphRelationTargetLabel(field),
getMorphRelationFieldLabel(field),
]
: [];
const searchableText = [
field.label,
...relationTargetObjectLabels,
...morphRelationLabels,
]
.map(normalizeSearchText)
.join(' ');
const matchesSearch = searchableText.includes(searchNormalized);
return matchesActiveFilter && matchesSearch;
});
}, [objectMetadataItems, sortedRelationFields, searchTerm, showInactive]);
return (
<>
<StyledSearchAndFilterContainer>
<StyledSearchInputContainer>
<SettingsTextInput
instanceId="object-relation-table-search"
LeftIcon={IconSearch}
placeholder={t`Search a field...`}
value={searchTerm}
onChange={setSearchTerm}
/>
</StyledSearchInputContainer>
<DropdownRoot
dropdownId="settings-relations-filter-dropdown"
type="panel"
>
<Dropdown.Trigger
render={
<Button
startIcon={<IconFilter />}
size="md"
aria-label={t`Filter`}
variant="outline"
/>
}
/>
<DropdownContent
side="bottom"
align="end"
sideOffset={8}
alignOffset={0}
>
<Dropdown.Section>
<SettingsRow
startElement={<IconArchive />}
onCheckedChange={() => setShowInactive(!showInactive)}
checked={showInactive}
>{t`Inactive`}</SettingsRow>
<SettingsRow
startElement={<IconSettings />}
onCheckedChange={() =>
setShowSystemRelations(!showSystemRelations)
}
checked={showSystemRelations}
>{t`System relations`}</SettingsRow>
</Dropdown.Section>
</DropdownContent>
</DropdownRoot>
</StyledSearchAndFilterContainer>
<Table>
<TableRow
gridTemplateColumns={OBJECT_RELATION_TABLE_ROW_GRID_TEMPLATE_COLUMNS}
>
{tableMetadata.fields.map((item) => (
<SortableTableHeader
key={item.fieldName}
fieldName={item.fieldName}
label={t(item.fieldLabel)}
tableId={tableMetadata.tableId}
initialSort={tableMetadata.initialSort}
/>
))}
<TableHeader></TableHeader>
</TableRow>
<StyledSettingsDataModelTableBodyContainer>
<TableBody>
{filteredRelationFields.length > 0 ? (
filteredRelationFields.map((fieldMetadataItem) => (
<SettingsObjectRelationItemTableRow
key={fieldMetadataItem.id}
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
))
) : (
<TableCell color={themeCssVariables.font.color.tertiary}>
{t`No relations found`}
</TableCell>
)}
</TableBody>
</StyledSettingsDataModelTableBodyContainer>
</Table>
</>
);
};