Files
twenty/packages/twenty-front/src/modules/settings/data-model/graph-overview/components/SettingsDataModelOverviewObject.tsx
Paul Rastoin aa0d8546a8 [REFACTOR][FRONT]: Remove objectMetadata and fieldMetadata sluggification (#9441)
# Introduction
For motivations and context please have a look to
https://github.com/twentyhq/twenty/pull/9394 whom this PR results from.
In this pull-request we remove any `metadataField` and `objectMetadata`
sluggification. We directly consume `objectMetadata.namePlural` and
`metadataField.name`, ***it seems like that historically the consumed
`metadataField.name`*** are we sure that we wanna change this behavior ?

## Notes
Unless I'm mistaken by reverting the `kebabcase` url formatting we might
be creating deadlinks that user could have save beforehand => Discussed
with Charles said it's controlled risk.

---------

Co-authored-by: Paul Rastoin <paulrastoin@Pauls-MacBook-Pro.local>
2025-01-08 11:31:53 +01:00

171 lines
5.6 KiB
TypeScript

import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { Node, NodeProps } from '@xyflow/react';
import { Link } from 'react-router-dom';
import { IconChevronDown, IconChevronUp, useIcons } from 'twenty-ui';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { ObjectFieldRow } from '@/settings/data-model/graph-overview/components/SettingsDataModelOverviewField';
import { SettingsDataModelObjectTypeTag } from '@/settings/data-model/objects/components/SettingsDataModelObjectTypeTag';
import { getObjectTypeLabel } from '@/settings/data-model/utils/getObjectTypeLabel';
import { capitalize } from 'twenty-shared';
import { FieldMetadataType } from '~/generated/graphql';
import { ObjectFieldRowWithoutRelation } from '@/settings/data-model/graph-overview/components/SettingsDataModelOverviewFieldWithoutRelation';
import '@xyflow/react/dist/style.css';
import { useState } from 'react';
type SettingsDataModelOverviewObjectNode = Node<ObjectMetadataItem, 'object'>;
type SettingsDataModelOverviewObjectProps =
NodeProps<SettingsDataModelOverviewObjectNode>;
const StyledNode = styled.div`
background-color: ${({ theme }) => theme.background.secondary};
border-radius: ${({ theme }) => theme.border.radius.md};
display: flex;
flex-direction: column;
width: 220px;
padding: ${({ theme }) => theme.spacing(2)};
gap: ${({ theme }) => theme.spacing(2)};
border: 1px solid ${({ theme }) => theme.border.color.medium};
box-shadow: ${({ theme }) => theme.boxShadow.light};
`;
const StyledHeader = styled.div`
align-items: center;
display: flex;
justify-content: space-between;
`;
const StyledObjectName = styled.div`
border: 0;
border-radius: 4px 4px 0 0;
display: flex;
font-weight: ${({ theme }) => theme.font.weight.medium};
gap: ${({ theme }) => theme.spacing(1)};
position: relative;
text-align: center;
`;
const StyledInnerCard = styled.div`
border: 1px solid ${({ theme }) => theme.border.color.light};
background-color: ${({ theme }) => theme.background.primary};
border-radius: ${({ theme }) => theme.border.radius.sm};
padding: ${({ theme }) => theme.spacing(2)} 0
${({ theme }) => theme.spacing(2)} 0;
display: flex;
flex-flow: column nowrap;
gap: ${({ theme }) => theme.spacing(0.5)};
color: ${({ theme }) => theme.font.color.tertiary};
`;
const StyledCardRow = styled.div`
align-items: center;
display: flex;
height: 24px;
gap: ${({ theme }) => theme.spacing(1)};
`;
const StyledCardRowOther = styled.div`
align-items: center;
cursor: pointer;
display: flex;
height: 24px;
padding: 0 ${({ theme }) => theme.spacing(2)};
gap: ${({ theme }) => theme.spacing(2)};
&:hover {
background-color: ${({ theme }) => theme.background.tertiary};
}
`;
const StyledCardRowText = styled.div``;
const StyledObjectInstanceCount = styled.div`
color: ${({ theme }) => theme.font.color.tertiary};
`;
const StyledObjectLink = styled(Link)`
align-items: center;
display: flex;
gap: ${({ theme }) => theme.spacing(1)};
text-decoration: none;
color: ${({ theme }) => theme.font.color.primary};
&:hover {
color: ${({ theme }) => theme.font.color.secondary};
}
`;
export const SettingsDataModelOverviewObject = ({
data: objectMetadataItem,
}: SettingsDataModelOverviewObjectProps) => {
const theme = useTheme();
const { getIcon } = useIcons();
const [otherFieldsExpanded, setOtherFieldsExpanded] = useState(false);
const { totalCount } = useFindManyRecords({
objectNameSingular: objectMetadataItem.nameSingular,
});
const fields = objectMetadataItem.fields.filter((x) => !x.isSystem);
const countNonRelation = fields.filter(
(x) => x.type !== FieldMetadataType.Relation,
).length;
const Icon = getIcon(objectMetadataItem.icon);
return (
<StyledNode>
<StyledHeader>
<StyledObjectName onMouseEnter={() => {}} onMouseLeave={() => {}}>
<StyledObjectLink
to={`/settings/objects/${objectMetadataItem.namePlural}`}
>
{Icon && <Icon size={theme.icon.size.md} />}
{capitalize(objectMetadataItem.namePlural)}
</StyledObjectLink>
<StyledObjectInstanceCount> · {totalCount}</StyledObjectInstanceCount>
</StyledObjectName>
<SettingsDataModelObjectTypeTag
objectTypeLabel={getObjectTypeLabel(objectMetadataItem)}
></SettingsDataModelObjectTypeTag>
</StyledHeader>
<StyledInnerCard>
{fields
.filter((x) => x.type === FieldMetadataType.Relation)
.map((field) => (
<StyledCardRow key={field.id}>
<ObjectFieldRow field={field} />
</StyledCardRow>
))}
{countNonRelation > 0 && (
<>
<StyledCardRowOther
onClick={() => setOtherFieldsExpanded(!otherFieldsExpanded)}
>
{otherFieldsExpanded ? (
<IconChevronUp size={theme.icon.size.md} />
) : (
<IconChevronDown size={theme.icon.size.md} />
)}
<StyledCardRowText>{countNonRelation} fields</StyledCardRowText>
</StyledCardRowOther>
{otherFieldsExpanded &&
fields
.filter((x) => x.type !== FieldMetadataType.Relation)
.map((field) => (
<StyledCardRow>
<ObjectFieldRowWithoutRelation field={field} />
</StyledCardRow>
))}
</>
)}
</StyledInnerCard>
</StyledNode>
);
};