aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/RelationName.tsx
blob: e651cb878ff4c1ded0c41cffbfd35fc87e020fd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import { styled } from '@mui/material/styles';
import { observer } from 'mobx-react-lite';

import { RelationMetadata } from '../xtext/xtextServiceResults';

const ErrorPredicateName = styled('span', {
  name: 'RelationName-Error',
})(({ theme }) => ({
  color: theme.palette.error.main,
}));

const Qualifier = styled('span', {
  name: 'RelationName-Qualifier',
})(({ theme }) => ({
  color: theme.palette.text.secondary,
}));

const FormattedName = observer(function FormattedName({
  name,
  metadata,
}: {
  name: string;
  metadata: RelationMetadata;
}): React.ReactNode {
  const { detail } = metadata;
  if (detail.type === 'class' && detail.abstractClass) {
    return <i>{name}</i>;
  }
  if (detail.type === 'reference' && detail.containment) {
    return <b>{name}</b>;
  }
  if (detail.type === 'predicate' && detail.error) {
    return <ErrorPredicateName>{name}</ErrorPredicateName>;
  }
  return name;
});

function RelationName({
  metadata,
  abbreviate,
}: {
  metadata: RelationMetadata;
  abbreviate?: boolean;
}): JSX.Element {
  const { name, simpleName } = metadata;
  if (abbreviate ?? RelationName.defaultProps.abbreviate) {
    return <FormattedName name={simpleName} metadata={metadata} />;
  }
  if (name.endsWith(simpleName)) {
    return (
      <>
        <Qualifier>
          {name.substring(0, name.length - simpleName.length)}
        </Qualifier>
        <FormattedName name={simpleName} metadata={metadata} />
      </>
    );
  }
  return <FormattedName name={name} metadata={metadata} />;
}

RelationName.defaultProps = {
  abbreviate: false,
};

export default observer(RelationName);