-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAllPropsSimple.tsx
More file actions
88 lines (75 loc) · 1.86 KB
/
AllPropsSimple.tsx
File metadata and controls
88 lines (75 loc) · 1.86 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
datatypes,
JSONValue,
properties,
Resource,
useResource,
useSubject,
useTitle,
} from '@tomic/react';
import React, { useMemo } from 'react';
import styled from 'styled-components';
export interface AllPropsSimpleProps {
resource: Resource;
}
/** Renders a simple list of all properties on the resource. Will not render any link or other interactive element. */
export function AllPropsSimple({ resource }: AllPropsSimpleProps): JSX.Element {
return (
<ul>
{[...resource.getPropVals()].map(([prop, val]) => (
<Row key={prop} prop={prop} val={val} />
))}
</ul>
);
}
interface RowProps {
prop: string;
val: JSONValue;
}
function Row({ prop, val }: RowProps): JSX.Element {
const propResource = useResource(prop);
const [propName] = useTitle(propResource);
const [dataType] = useSubject(propResource, properties.datatype);
const value = useMemo(() => {
if (dataType === datatypes.atomicUrl) {
return <Value val={val as string} />;
}
if (dataType === datatypes.resourceArray) {
return <ResourceArray val={val as string[]} />;
}
return <>{val as string}</>;
}, [val, dataType]);
return (
<List>
<Key>{propName}</Key>: {value}
</List>
);
}
const Key = styled.span`
font-weight: bold;
`;
const List = styled.ul`
list-style: none;
margin: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
color: ${p => p.theme.colors.textLight};
`;
function ResourceArray({ val }: { val: string[] }): JSX.Element {
return (
<>
{val.map((v, i) => (
<>
<Value val={v} key={v} />
{i === val.length - 1 ? '' : ', '}
</>
))}
</>
);
}
function Value({ val }: { val: string }): JSX.Element {
const valueResource = useResource(val);
const [valueName] = useTitle(valueResource);
return <>{valueName}</>;
}