-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathComponentCodeRenderer.tsx
43 lines (40 loc) · 1.37 KB
/
ComponentCodeRenderer.tsx
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
import { Box, IconButton, Tooltip } from '@mui/material';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
const toValueString = (value: any) => {
if (['boolean', 'number', 'string'].includes(typeof value)) return `${JSON.stringify(value)}`;
if (Array.isArray(value)) return `[${value.map(toValueString).join(', ')}]`;
if (typeof value === 'object')
return `{ ${Object.entries(value)
.map(([key, entryValue]) => `${key}: ${toValueString(entryValue)}`)
.join(', ')} }`;
return value;
};
const ComponentCodeRenderer = ({
state,
component = 'MuxPlayer',
}: {
state: Record<string, any>;
component?: string;
}) => {
const stateEntries = Object.entries(state).filter(([, value]) => value != undefined);
const propsStr = stateEntries.length
? `\n${stateEntries.map(([key, value]) => ` ${key}={${toValueString(value)}}`).join('\n')}\n`
: '';
const codeStr = `<${component}${propsStr}/>`;
const copyToClipboard = () => {
navigator.clipboard?.writeText(codeStr);
};
return (
<Box style={{ position: 'relative' }}>
<Tooltip title={'Copy to clipboard'}>
<IconButton style={{ position: 'absolute', top: 0, right: 0 }} onClick={copyToClipboard}>
<ContentCopyIcon />
</IconButton>
</Tooltip>
<pre>
<code>{codeStr}</code>
</pre>
</Box>
);
};
export default ComponentCodeRenderer;