Skip to content

Commit 372543c

Browse files
references for leaf types
1 parent e60f8e1 commit 372543c

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

src/CollapsingType.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useCallback } from 'react';
2-
import { useSchema } from './SchemaContext';
2+
import { useSchema, useSchemaContext } from './SchemaContext';
33
import Details from './Details';
44
import CollapsingDetails from './CollapsingDetails';
55
import Type from './Type';
6+
import References from './References';
67

78
import { BaseType } from './metamodel';
89

@@ -14,6 +15,8 @@ type Props = {
1415

1516
export default function CollapsingType({ header, namespace, name }: Props) {
1617
const schema = useSchema();
18+
const { references } = useSchemaContext();
19+
const typeName = `${namespace}::${name}`
1720

1821
const renderType = useCallback(() => {
1922
for (const type of schema.types) {
@@ -22,10 +25,13 @@ export default function CollapsingType({ header, namespace, name }: Props) {
2225
return <Type type={bt} />;
2326
}
2427
}
28+
if (references[typeName]) {
29+
return <References typeName={typeName} />;
30+
}
2531
return <Details value="No additional info" />
2632
}, [name, namespace, schema.types]);
2733

2834
return (
29-
<CollapsingDetails header={header} value=<code>{`${namespace}::${name}` }</code> cb={renderType} />
35+
<CollapsingDetails header={header} value=<code>{typeName}</code> cb={renderType} />
3036
);
3137
}

src/References.tsx

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useSchemaContext } from './SchemaContext';
2+
import CollapsingDetails from './CollapsingDetails';
3+
import CollapsingEndpoint from './CollapsingEndpoint';
4+
import CollapsingType from './CollapsingType';
5+
6+
type Props = {
7+
typeName: string;
8+
}
9+
10+
export default function References({ typeName }: Props) {
11+
const { references } = useSchemaContext();
12+
let refCount = 0;
13+
14+
if (references[typeName] !== undefined) {
15+
if (references[typeName].endpoints) {
16+
refCount += references[typeName].endpoints.length;
17+
}
18+
if (references[typeName].types) {
19+
refCount += references[typeName].types.length;
20+
}
21+
}
22+
23+
return (
24+
<>
25+
{typeName in references && (references[typeName].endpoints || references[typeName].types) &&
26+
<CollapsingDetails header="References" value={`${refCount}`}>
27+
{references[typeName].endpoints &&
28+
<>
29+
{references[typeName].endpoints.map(endpointName => {
30+
const comment = endpointName.split(' ').slice(1).join(' ');
31+
const name = endpointName.split(' ')[0];
32+
return <CollapsingEndpoint key={name} header={comment} name={name} />;
33+
})}
34+
</>
35+
}
36+
{references[typeName].types &&
37+
<>
38+
{references[typeName].types.map(typeName => {
39+
const comment = typeName.split(' ').slice(1).join(' ');
40+
const [namespace, name] = typeName.split(' ')[0].split('::')
41+
return <CollapsingType key={typeName} header={comment} namespace={namespace} name={name} />;
42+
})}
43+
</>
44+
}
45+
</CollapsingDetails>
46+
}
47+
</>
48+
);
49+
}

src/SchemaContext.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const crossReference = (schema: Model): Record<string, Record<string, string[]>>
2828
if (!('endpoints' in refs[typeName])) {
2929
refs[typeName].endpoints = [];
3030
}
31-
refs[typeName].endpoints.push(`${source.name} ${comment}`);
31+
const ref = `${source.name} ${comment}`
32+
if (refs[typeName].endpoints.indexOf(ref) == -1) {
33+
refs[typeName].endpoints.push(ref);
34+
}
3235
}
3336

3437
const addTypeReference = (source: TypeName, type: TypeName, comment: string) => {
@@ -39,7 +42,10 @@ const crossReference = (schema: Model): Record<string, Record<string, string[]>>
3942
if (refs[typeName].types === undefined) {
4043
refs[typeName].types = [];
4144
}
42-
refs[typeName].types.push(`${source.namespace}::${source.name} ${comment}`);
45+
const ref = `${source.namespace}::${source.name} ${comment}`;
46+
if (refs[typeName].types.indexOf(ref) == -1) {
47+
refs[typeName].types.push(ref);
48+
}
4349
}
4450

4551
const addValueOfReferences = (source: TypeName, valueOf: ValueOf, comment: string) => {

src/Type.tsx

+3-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Request from './Request';
99
import Response from './Response';
1010
import Enum from './Enum';
1111
import TypeAlias from './TypeAlias';
12+
import References from './References';
13+
1214
import { useSchemaContext } from './SchemaContext';
1315

1416
import { BaseType, Interface as InterfaceType, Request as RequestType, Response as ResponseType, Enum as EnumType, TypeAlias as TypeAliasType } from './metamodel';
@@ -20,16 +22,6 @@ type Props = {
2022
export default function Type({ type }: Props) {
2123
const { references } = useSchemaContext();
2224
const typeName = `${type.name.namespace}::${type.name.name}`;
23-
let refCount = 0;
24-
25-
if (references[typeName] !== undefined) {
26-
if (references[typeName].endpoints) {
27-
refCount += references[typeName].endpoints.length;
28-
}
29-
if (references[typeName].types) {
30-
refCount += references[typeName].types.length;
31-
}
32-
}
3325

3426
return (
3527
<>
@@ -66,28 +58,7 @@ export default function Type({ type }: Props) {
6658
<path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.5-.5h-5a.5.5 0 0 0 0 1h3.793L6.146 9.146a.5.5 0 1 0 .708.708L15 1.707V5.5a.5.5 0 0 0 1 0z"/>
6759
</svg>
6860
</a> />
69-
{typeName in references && (references[typeName].endpoints || references[typeName].types) &&
70-
<CollapsingDetails header="References" value={`${refCount}`}>
71-
{references[typeName].endpoints &&
72-
<>
73-
{references[typeName].endpoints.map(endpointName => {
74-
const comment = endpointName.split(' ').slice(1).join(' ');
75-
const name = endpointName.split(' ')[0];
76-
return <CollapsingEndpoint key={name} header={comment} name={name} />;
77-
})}
78-
</>
79-
}
80-
{references[typeName].types &&
81-
<>
82-
{references[typeName].types.map(typeName => {
83-
const comment = typeName.split(' ').slice(1).join(' ');
84-
const [namespace, name] = typeName.split(' ')[0].split('::')
85-
return <CollapsingType key={`${namespace}::${name}`} header={comment} namespace={namespace} name={name} />;
86-
})}
87-
</>
88-
}
89-
</CollapsingDetails>
90-
}
61+
{references[typeName] && <References typeName={typeName} />}
9162
</>
9263
);
9364
}

0 commit comments

Comments
 (0)