450 lines
12 KiB
TypeScript
Raw Normal View History

import React from "react"
import {
Box,
Flex,
Input,
NumberDecrementStepper,
NumberIncrementStepper,
NumberInput,
NumberInputField,
NumberInputStepper,
Select,
Switch,
} from "@chakra-ui/react"
2023-06-28 08:25:17 +00:00
2023-07-21 01:13:02 +00:00
import { Icon } from "../Icon/Icon"
import { Safe } from "../Safety/Safe"
import { useThingtime } from "./useThingtime"
export const Thingtime = (props) => {
// TODO: Add a circular reference seen prop check
// and add button to expand circular reference
// up to 1 level deep
2023-07-21 13:33:47 +00:00
const { thingtime, setThingtime } = useThingtime()
2023-07-21 01:13:02 +00:00
const [uuid, setUuid] = React.useState()
2023-06-30 01:46:42 +00:00
2023-07-21 01:13:02 +00:00
const [circular, setCircular] = React.useState(props?.circular)
const depth = React.useMemo(() => {
return props?.depth || 1
}, [props?.depth])
const pl = React.useMemo(() => {
return props?.pl || [4, 6]
}, [props?.pl])
const pr = React.useMemo(() => {
return props?.pr || (depth === 1 ? [4, 6] : 0)
2023-07-15 08:08:36 +00:00
}, [props?.pr, depth])
// will only run on the client
2023-06-30 01:46:42 +00:00
React.useEffect(() => {
setUuid(Math.random().toString(36).substring(7))
}, [])
2023-06-28 08:25:17 +00:00
const thing = React.useMemo(() => {
return props.thing
}, [props.thing])
const fullPath = React.useMemo(() => {
return props?.fullPath || props?.path
}, [props?.fullPath, props?.path])
2023-07-15 08:08:36 +00:00
const seen = React.useMemo(() => {
if (props?.seen instanceof Array) {
if (props?.seen?.includes(thing)) {
return props?.seen
} else if (typeof thing === "object") {
return [...props.seen, thing]
}
return props?.seen || []
}
if (typeof thing === "object") {
return [thing]
}
return []
}, [props?.seen, thing])
2023-06-29 11:06:58 +00:00
const mode = React.useMemo(() => {
return "view"
2023-06-29 11:06:58 +00:00
}, [])
const validKeyTypes = React.useMemo(() => {
return ["object", "array"]
2023-06-29 11:06:58 +00:00
}, [])
const keys = React.useMemo(() => {
if (validKeyTypes?.includes(typeof thing)) {
try {
const keysRet = Object.keys(thing)
return keysRet
} catch {
// nothing
}
2023-06-29 11:06:58 +00:00
} else {
return []
}
}, [thing, validKeyTypes])
const type = React.useMemo(() => {
return typeof thing
}, [thing])
2023-07-21 13:33:47 +00:00
const typeIcon = React.useMemo(() => {
const size = 7
if (thing instanceof Array) {
return <Icon name="array" size={size}></Icon>
} else if (type === "object") {
return <Icon name="object" size={size}></Icon>
} else if (type === "string") {
return <Icon name="string" size={size}></Icon>
} else if (type === "number") {
return <Icon name="number" size={size}></Icon>
} else if (type === "boolean") {
return <Icon name="boolean" size={size}></Icon>
} else {
return <Icon name="box" size={size}></Icon>
}
}, [type, thing])
const valuePl = React.useMemo(() => {
if (typeof props?.valuePl === "number") {
return props?.valuePl
}
return props?.path ? [4, 6] : [0, 0]
}, [props?.valuePl, props?.path])
2023-06-29 11:06:58 +00:00
const renderableValue = React.useMemo(() => {
if (type === "string") {
const trimmed = thing.trim()
if (!trimmed) {
2023-07-15 08:08:36 +00:00
return ""
}
return trimmed
} else if (type === "number") {
2023-06-29 11:06:58 +00:00
return thing
} else if (type === "boolean") {
return thing ? "true" : "false"
} else if (type === "object") {
if (thing === null) {
return "null"
}
if (!keys?.length) {
return "Something!"
}
try {
return JSON.stringify(thing, null, 2)
} catch (err) {
2023-07-15 08:08:36 +00:00
// console.error(
// "Caught error making renderableValue of thing",
// err,
// thing
// )
return (
<Box cursor="pointer" onClick={() => setCircular(false)}>
Click to Expand
</Box>
)
}
2023-06-29 11:06:58 +00:00
} else {
return "Something!"
2023-06-29 11:06:58 +00:00
}
2023-07-21 01:13:02 +00:00
}, [thing, type, keys])
2023-06-29 11:06:58 +00:00
2023-07-21 01:13:02 +00:00
const keysToUse = React.useMemo(() => {
return keys
}, [keys])
// const keysToUse = flattenedKeys
2023-06-29 11:06:58 +00:00
2023-07-21 01:13:02 +00:00
const template1Modes = React.useMemo(() => {
return ["view", "edit"]
}, [])
2023-06-29 11:06:58 +00:00
2023-07-21 13:33:47 +00:00
const thingtimeChildren = React.useMemo(() => {
2023-07-21 01:13:02 +00:00
if (template1Modes?.includes(mode)) {
if (keys?.length && !circular) {
return (
<Safe {...props}>
<Flex
position="relative"
flexDirection="column"
// w={'500px'}
// w={['200px', '500px']}
maxWidth="100%"
paddingLeft={valuePl}
paddingY={props?.path ? 3 : 0}
>
{keysToUse?.length &&
keysToUse.map((key, idx) => {
if (!key?.human) {
key = {
human: key,
key: key,
}
}
2023-06-29 11:06:58 +00:00
2023-07-21 01:13:02 +00:00
const nextThing = thing[key?.key]
const nextSeen = [...seen]
2023-06-29 11:06:58 +00:00
2023-07-21 01:13:02 +00:00
if (typeof nextThing === "object") {
nextSeen.push(nextThing)
}
2023-07-21 01:13:02 +00:00
return (
<Thingtime
key={idx}
seen={nextSeen}
2023-07-21 13:33:47 +00:00
edit={props?.edit}
2023-07-21 01:13:02 +00:00
circular={seen?.includes?.(nextThing)}
depth={depth + 1}
parent={thing}
2023-07-21 13:33:47 +00:00
fullPath={fullPath + "." + key?.key}
2023-07-21 01:13:02 +00:00
path={key}
thing={nextThing}
// thing={{ infinite: { yes: true } }}
valuePl={pl}
></Thingtime>
)
})}
</Flex>
</Safe>
)
}
}
}, [
keysToUse,
mode,
circular,
seen,
depth,
thing,
props,
valuePl,
pl,
keys,
template1Modes,
])
2023-07-21 13:33:47 +00:00
const AtomicWrapper = React.useCallback(
(props) => {
2023-07-21 01:13:02 +00:00
return (
2023-07-21 13:33:47 +00:00
<Flex
flexDirection="row"
flexShrink={1}
paddingLeft={pl}
fontSize="20px"
border="none"
whiteSpace="pre-line"
outline="none"
paddingY={2}
// dangerouslySetInnerHTML={{ __html: renderableValue }}
>
2023-07-21 13:33:47 +00:00
{props?.children}
</Flex>
2023-06-29 11:06:58 +00:00
)
2023-07-21 13:33:47 +00:00
},
[pl]
)
const updateValue = React.useCallback(
(args) => {
const { value } = args
console.log("nik value", value)
console.log("nik props?.fullPath", fullPath)
2023-07-21 13:33:47 +00:00
setThingtime(fullPath, value)
2023-07-21 13:33:47 +00:00
},
[fullPath, setThingtime]
2023-07-21 13:33:47 +00:00
)
const atomicValue = React.useMemo(() => {
if (props?.edit) {
if (type === "boolean") {
return (
<AtomicWrapper>
<Box
onClick={(e) => {
e?.preventDefault?.()
e?.stopPropagation?.()
// cancel bubble
e?.nativeEvent?.stopImmediatePropagation?.()
console.log("nik 123123 clicked", !thing)
setTimeout(() => {
updateValue({ value: !thing })
console.log("nik 123123 changed", e)
}, 1)
}}
>
<Switch isChecked={thing}></Switch>
2023-07-21 13:33:47 +00:00
</Box>
</AtomicWrapper>
)
}
if (type === "number") {
const numberPxLength = thing?.toString()?.length * 13 + 30
return (
<AtomicWrapper>
<Flex>
<NumberInput
alignItems="center"
justifyContent="center"
onChange={(value) => {
setTimeout(() => {
try {
const number = Number(value)
console.log("typeof number", typeof number)
updateValue({ value: number })
} catch {
// something went wrong casting to number
}
}, 1)
}}
value={thing}
>
<NumberInputField width={numberPxLength + "px"} />
<NumberInputStepper transform="scale(0.9)">
<NumberIncrementStepper
// transform="scale(0.7)"
/>
<NumberDecrementStepper
// transform="scale(0.7)"
/>
</NumberInputStepper>
</NumberInput>
</Flex>
2023-07-21 13:33:47 +00:00
</AtomicWrapper>
)
}
2023-06-29 11:06:58 +00:00
}
2023-07-21 13:33:47 +00:00
return <AtomicWrapper>{renderableValue}</AtomicWrapper>
}, [renderableValue, AtomicWrapper, type, props?.edit, thing, updateValue])
2023-06-29 11:06:58 +00:00
const contextMenu = (
<Flex
position="absolute"
top={0}
right={0}
paddingRight={4}
userSelect="none"
>
2023-06-29 11:06:58 +00:00
Settings
</Flex>
)
const [showContextMenu, setShowContextMenu] = React.useState(false)
const humanPath = React.useMemo(() => {
if (typeof props?.path === "string") {
return props?.path
}
return props?.path?.human || ""
}, [props?.path])
2023-07-10 00:49:30 +00:00
const renderedPath = React.useMemo(() => {
2023-07-21 13:33:47 +00:00
if (props?.edit) {
return humanPath
}
if (humanPath?.includes?.("hidden")) {
return null
}
if (humanPath?.includes?.("unique")) {
// take only path from before the string unique
return humanPath.split?.("unique")?.[0]
}
2023-07-10 00:49:30 +00:00
return humanPath
2023-07-21 13:33:47 +00:00
}, [humanPath, props?.edit])
2023-07-10 00:49:30 +00:00
2023-07-21 13:33:47 +00:00
const pathDom = React.useMemo(() => {
if (renderedPath) {
return (
<Flex
maxWidth="100%"
paddingLeft={props?.pathPl || pl}
fontSize="12px"
wordBreak="break-all"
>
{renderedPath}
</Flex>
)
}
2023-07-10 00:49:30 +00:00
}, [renderedPath, pl, props?.pathPl])
const handleMouseEvent = React.useCallback(
(e) => {
const target = e?.target
// extract uuid from className
const className = target?.className
if (className?.includes(uuid?.current)) {
setShowContextMenu(e?.type === "mouseenter")
}
},
[uuid]
)
2023-07-21 13:33:47 +00:00
const [showContextIcon, setShowContextIcon] = React.useState(false)
return (
<Safe {...props} depth={depth} uuid={uuid?.current}>
<Flex
position="relative"
flexDirection="column"
// width="500px"
width={props?.width || props?.w || "100%"}
maxWidth="100%"
paddingRight={pr}
onMouseEnter={handleMouseEvent}
onMouseLeave={handleMouseEvent}
// minW={depth === 1 ? '120px' : null}
paddingY={3}
2023-07-15 08:08:36 +00:00
{...(props.chakras || {})}
className={`thing-${uuid?.current}`}
>
{/* {uuid?.current} */}
2023-07-21 01:13:02 +00:00
<Flex position="relative" flexDirection="row">
2023-07-21 13:33:47 +00:00
<Flex
alignItems="center"
flexDirection="row"
marginRight="auto"
onMouseEnter={() => setShowContextIcon(true)}
onMouseLeave={() => setShowContextIcon(false)}
>
<Flex>{pathDom}</Flex>
{props?.edit && (
<Box
// marginTop={-3}
marginTop={-1}
2023-07-21 13:33:47 +00:00
paddingLeft={1}
opacity={0.5}
cursor="pointer"
>
{typeIcon}
</Box>
)}
{pathDom && (
<Flex
paddingLeft={1}
opacity={showContextIcon ? 1 : 0}
cursor="pointer"
transition="all 0.2s ease-in-out"
>
<Icon name="magic" size={10}></Icon>
</Flex>
)}
2023-07-21 01:13:02 +00:00
</Flex>
</Flex>
{/* {showContextMenu && contextMenu} */}
2023-07-21 13:33:47 +00:00
{!thingtimeChildren && atomicValue}
{thingtimeChildren}
</Flex>
</Safe>
2023-06-29 11:06:58 +00:00
)
}