2023-06-29 11:06:58 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { Box, Flex } from '@chakra-ui/react'
|
2023-06-29 23:41:06 +00:00
|
|
|
import { Safe } from '../Safety/Safe'
|
2023-07-01 09:46:36 +00:00
|
|
|
import { useThingtime } from './useThingtime'
|
2023-06-28 08:25:17 +00:00
|
|
|
|
|
|
|
export const Thingtime = props => {
|
2023-07-01 14:22:57 +00:00
|
|
|
// TODO: Add a circular reference seen prop check
|
|
|
|
// and add button to expand circular reference
|
|
|
|
// up to 1 level deep
|
2023-06-29 23:41:06 +00:00
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
const [uuid, setUuid] = React.useState()
|
2023-06-30 01:46:42 +00:00
|
|
|
|
2023-07-01 14:13:27 +00:00
|
|
|
const pl = React.useMemo(() => {
|
|
|
|
return props?.pl || [4, 6]
|
|
|
|
}, [props?.pl])
|
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
// will only run on the client
|
2023-06-30 01:46:42 +00:00
|
|
|
React.useEffect(() => {
|
2023-07-01 09:46:36 +00:00
|
|
|
setUuid(Math.random().toString(36).substring(7))
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const { thingtime } = useThingtime()
|
|
|
|
|
2023-06-29 23:41:06 +00:00
|
|
|
const depth = React.useMemo(() => {
|
|
|
|
return props?.depth || 1
|
|
|
|
}, [props?.depth])
|
2023-06-29 11:39:40 +00:00
|
|
|
|
2023-06-28 08:25:17 +00:00
|
|
|
const thing = React.useMemo(() => {
|
|
|
|
return props.thing
|
|
|
|
}, [props.thing])
|
|
|
|
|
2023-06-29 11:06:58 +00:00
|
|
|
const mode = React.useMemo(() => {
|
|
|
|
return 'view'
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const validKeyTypes = React.useMemo(() => {
|
|
|
|
return ['object', 'array']
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const keys = React.useMemo(() => {
|
|
|
|
if (validKeyTypes?.includes(typeof thing)) {
|
2023-07-01 09:46:36 +00:00
|
|
|
const keysRet = Object.keys(thing)
|
|
|
|
return keysRet
|
2023-06-29 11:06:58 +00:00
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}, [thing, validKeyTypes])
|
|
|
|
|
|
|
|
const type = React.useMemo(() => {
|
|
|
|
return typeof thing
|
|
|
|
}, [thing])
|
|
|
|
|
|
|
|
const renderableValue = React.useMemo(() => {
|
|
|
|
if (type === 'string') {
|
2023-07-01 14:13:27 +00:00
|
|
|
if (!thing) {
|
|
|
|
return 'Empty string'
|
|
|
|
}
|
2023-06-29 11:06:58 +00:00
|
|
|
return thing
|
|
|
|
} else if (type === 'number') {
|
|
|
|
return thing
|
|
|
|
} else if (type === 'boolean') {
|
|
|
|
return thing ? 'true' : 'false'
|
|
|
|
} else if (type === 'object') {
|
2023-07-01 14:13:27 +00:00
|
|
|
if (thing === null) {
|
|
|
|
return 'null'
|
|
|
|
}
|
2023-07-01 09:46:36 +00:00
|
|
|
if (!keys?.length) {
|
|
|
|
return 'Empty object'
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return JSON.stringify(thing, null, 2)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(
|
|
|
|
'Caught error making renderableValue of thing',
|
|
|
|
err,
|
|
|
|
thing
|
|
|
|
)
|
|
|
|
return 'Circular reference in object.'
|
|
|
|
}
|
2023-06-29 11:06:58 +00:00
|
|
|
} else {
|
2023-07-01 14:13:27 +00:00
|
|
|
return 'Undefined'
|
2023-06-29 11:06:58 +00:00
|
|
|
}
|
|
|
|
}, [thing, type])
|
|
|
|
|
|
|
|
const flattenedKeys = React.useMemo(() => {
|
|
|
|
// create an array of all keys on object so that if object is
|
|
|
|
// { my: { child: {} } }
|
|
|
|
// the array looks like
|
|
|
|
// ['my', 'my.child']
|
|
|
|
|
|
|
|
const ret = []
|
|
|
|
|
|
|
|
try {
|
|
|
|
const randId = Math.random().toString(36).substring(7)
|
|
|
|
|
|
|
|
window.thingtime.tmp[randId] = 0
|
|
|
|
|
|
|
|
const recurse = (obj, prefix) => {
|
|
|
|
Object.keys(obj).forEach(key => {
|
|
|
|
if (typeof obj[key] === 'object') {
|
|
|
|
if (window?.thingtime?.tmp[randId] < 1000) {
|
|
|
|
window.thingtime.tmp[randId]++
|
|
|
|
recurse(obj[key], `${prefix}${prefix && '.'}${key}`)
|
|
|
|
} else {
|
|
|
|
console.error('Recursion limit reached in Thingtime.tsx')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret.push({
|
|
|
|
key: `${prefix}${prefix && '.'}${key}`,
|
|
|
|
human: `${prefix}${prefix && ' '}${key}`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
recurse(thing, '')
|
|
|
|
} catch (err) {
|
|
|
|
// console.error('Error in Thingtime.tsx creating flattenedKeys', err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}, [thing])
|
|
|
|
|
2023-06-29 11:39:40 +00:00
|
|
|
let value = null
|
|
|
|
let editableValue = null
|
2023-06-29 11:06:58 +00:00
|
|
|
|
|
|
|
const keysToUse = keys
|
|
|
|
// const keysToUse = flattenedKeys
|
|
|
|
|
|
|
|
const template1Modes = ['view', 'edit']
|
|
|
|
|
|
|
|
if (template1Modes?.includes(mode)) {
|
|
|
|
if (keys?.length) {
|
2023-06-29 11:39:40 +00:00
|
|
|
value = (
|
2023-06-29 23:41:06 +00:00
|
|
|
<Safe {...props}>
|
|
|
|
<Flex
|
|
|
|
position='relative'
|
|
|
|
flexDir='column'
|
|
|
|
// w={'500px'}
|
|
|
|
// w={['200px', '500px']}
|
|
|
|
maxW='100%'
|
2023-07-01 09:46:36 +00:00
|
|
|
py={props?.path ? 3 : 0}
|
2023-07-01 14:13:27 +00:00
|
|
|
pl={props?.valuePl}
|
2023-06-29 23:41:06 +00:00
|
|
|
>
|
|
|
|
{keysToUse?.length &&
|
|
|
|
keysToUse.map((key, idx) => {
|
|
|
|
if (!key?.human) {
|
|
|
|
key = {
|
|
|
|
human: key,
|
|
|
|
key: key
|
|
|
|
}
|
2023-06-29 11:06:58 +00:00
|
|
|
}
|
2023-06-29 23:41:06 +00:00
|
|
|
|
|
|
|
const nextThing = thing[key?.key]
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Thingtime
|
|
|
|
key={idx}
|
|
|
|
depth={depth + 1}
|
|
|
|
parent={thing}
|
|
|
|
path={key}
|
|
|
|
thing={nextThing}
|
|
|
|
// thing={{ infinite: { yes: true } }}
|
2023-07-01 14:13:27 +00:00
|
|
|
valuePl={pl}
|
2023-06-29 23:41:06 +00:00
|
|
|
></Thingtime>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Flex>
|
|
|
|
</Safe>
|
2023-06-29 11:06:58 +00:00
|
|
|
)
|
|
|
|
} else {
|
2023-06-29 11:39:40 +00:00
|
|
|
editableValue = (
|
|
|
|
<Box
|
|
|
|
contentEditable={mode === 'edit'}
|
|
|
|
border='none'
|
|
|
|
outline={'none'}
|
|
|
|
py={2}
|
2023-07-01 14:13:27 +00:00
|
|
|
pl={pl}
|
2023-06-29 11:39:40 +00:00
|
|
|
fontSize={'20px'}
|
|
|
|
>
|
|
|
|
{renderableValue}
|
|
|
|
</Box>
|
2023-06-29 11:06:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const contextMenu = (
|
2023-07-01 14:13:27 +00:00
|
|
|
<Flex pr={4} userSelect={'none'} position='absolute' top={0} right={0}>
|
2023-06-29 11:06:58 +00:00
|
|
|
Settings
|
|
|
|
</Flex>
|
|
|
|
)
|
|
|
|
|
|
|
|
const [showContextMenu, setShowContextMenu] = React.useState(false)
|
|
|
|
|
2023-06-29 11:39:40 +00:00
|
|
|
const path = React.useMemo(() => {
|
2023-07-01 14:22:04 +00:00
|
|
|
if (props?.path?.human?.includes?.('hidden')) {
|
|
|
|
return null
|
|
|
|
}
|
2023-07-01 14:39:02 +00:00
|
|
|
if (props?.path?.human?.includes?.('unique')) {
|
|
|
|
// take only path from before the string unique
|
|
|
|
return props?.path?.human?.split?.('unique')?.[0]
|
|
|
|
}
|
2023-06-29 23:41:06 +00:00
|
|
|
return (
|
2023-07-01 14:13:27 +00:00
|
|
|
<Flex maxW='100%' pl={pl} wordBreak={'break-all'} fontSize='12px'>
|
2023-06-29 23:41:06 +00:00
|
|
|
{props?.path?.human}
|
|
|
|
</Flex>
|
|
|
|
)
|
2023-06-29 11:39:40 +00:00
|
|
|
}, [props?.path])
|
|
|
|
|
|
|
|
const handleMouseEvent = React.useCallback(
|
|
|
|
e => {
|
|
|
|
const target = e?.target
|
2023-06-29 23:41:06 +00:00
|
|
|
// extract uuid from className
|
2023-06-29 11:39:40 +00:00
|
|
|
const className = target?.className
|
2023-06-29 23:41:06 +00:00
|
|
|
if (className?.includes(uuid?.current)) {
|
2023-06-29 11:39:40 +00:00
|
|
|
setShowContextMenu(e?.type === 'mouseenter')
|
|
|
|
}
|
|
|
|
},
|
2023-06-29 23:41:06 +00:00
|
|
|
[uuid]
|
2023-06-29 11:39:40 +00:00
|
|
|
)
|
|
|
|
|
2023-06-29 23:41:06 +00:00
|
|
|
return (
|
|
|
|
<Safe {...props} depth={depth} uuid={uuid?.current}>
|
|
|
|
<Flex
|
|
|
|
onMouseEnter={handleMouseEvent}
|
|
|
|
onMouseLeave={handleMouseEvent}
|
|
|
|
position='relative'
|
|
|
|
flexDir='column'
|
|
|
|
py={3}
|
|
|
|
w='500px'
|
|
|
|
// minW={depth === 1 ? '120px' : null}
|
|
|
|
maxW='100%'
|
|
|
|
{...props}
|
|
|
|
className={`thing-${uuid?.current}`}
|
|
|
|
>
|
|
|
|
{/* {uuid?.current} */}
|
|
|
|
{path}
|
2023-07-01 14:13:27 +00:00
|
|
|
{/* {showContextMenu && contextMenu} */}
|
2023-06-29 23:41:06 +00:00
|
|
|
{editableValue}
|
|
|
|
{value}
|
|
|
|
</Flex>
|
|
|
|
</Safe>
|
2023-06-29 11:06:58 +00:00
|
|
|
)
|
|
|
|
}
|