Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace zustand with jotai #167

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"@emotion/styled": "^11.10.6",
"@mui/material": "^5.11.13",
"copy-to-clipboard": "^3.3.3",
"zustand": "^4.3.6"
"fast-deep-equal": "^3.1.3",
"jotai": "^1.13.0"
},
"lint-staged": {
"!*.{ts,tsx,js,jsx}": "prettier --write --ignore-unknown",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const external = [
'@mui/material',
'@mui/material/styles',
'copy-to-clipboard',
'zustand',
'jotai',
'jotai/utils',
'react',
'react/jsx-runtime',
'react-dom',
Expand Down
44 changes: 29 additions & 15 deletions src/components/DataKeyPair.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { Box } from '@mui/material'
import { useAtomValue, useSetAtom } from 'jotai'
import type { ComponentProps, FC, MouseEvent } from 'react'
import { useCallback, useMemo, useState } from 'react'

import { useTextColor } from '../hooks/useColor'
import { useClipboard } from '../hooks/useCopyToClipboard'
import { useInspect } from '../hooks/useInspect'
import { useJsonViewerStore } from '../stores/JsonViewerStore'
import {
colorspaceAtom,
editableAtom,
enableClipboardAtom,
hoverPathAtom,
keyRendererAtom,
onChangeAtom,
quotesOnKeysAtom,
rootNameAtom,
setHoverAtom,
valueAtom
} from '../state'
import { useTypeComponents } from '../stores/typeRegistry'
import type { DataItemProps } from '../type'
import { getValueSize } from '../utils'
Expand Down Expand Up @@ -43,7 +54,7 @@ const IconBox: FC<IconBoxProps> = (props) => (
export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
const { value, path, nestedIndex } = props
const propsEditable = props.editable ?? undefined
const storeEditable = useJsonViewerStore(store => store.editable)
const storeEditable = useAtomValue(editableAtom)
const editable = useMemo(() => {
if (storeEditable === false) {
return false
Expand All @@ -60,26 +71,28 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
const [tempValue, setTempValue] = useState(typeof value === 'function' ? () => value : value)
const depth = path.length
const key = path[depth - 1]
const hoverPath = useJsonViewerStore(store => store.hoverPath)
const hoverPath = useAtomValue(hoverPathAtom)
const isHover = useMemo(() => {
return hoverPath && path.every(
(value, index) => value === hoverPath.path[index] && nestedIndex ===
hoverPath.nestedIndex)
}, [hoverPath, path, nestedIndex])
const setHover = useJsonViewerStore(store => store.setHover)
const root = useJsonViewerStore(store => store.value)
const setHover = useSetAtom(setHoverAtom)
const root = useAtomValue(valueAtom)
const [inspect, setInspect] = useInspect(path, value, nestedIndex)
const [editing, setEditing] = useState(false)
const onChange = useJsonViewerStore(store => store.onChange)
const keyColor = useTextColor()
const numberKeyColor = useJsonViewerStore(store => store.colorspace.base0C)
const onChange = useAtomValue(onChangeAtom)
const {
base07: keyColor,
base0C: numberKeyColor
} = useAtomValue(colorspaceAtom)
const { Component, PreComponent, PostComponent, Editor } = useTypeComponents(value, path)
const quotesOnKeys = useJsonViewerStore(store => store.quotesOnKeys)
const rootName = useJsonViewerStore(store => store.rootName)
const quotesOnKeys = useAtomValue(quotesOnKeysAtom)
const rootName = useAtomValue(rootNameAtom)
const isRoot = root === value
const isNumberKey = Number.isInteger(Number(key))

const enableClipboard = useJsonViewerStore(store => store.enableClipboard)
const enableClipboard = useAtomValue(enableClipboardAtom)
const { copy, copied } = useClipboard()

const actionIcons = useMemo(() => {
Expand Down Expand Up @@ -161,7 +174,7 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {

const isEmptyValue = useMemo(() => getValueSize(value) === 0, [value])
const expandable = !isEmptyValue && !!(PreComponent && PostComponent)
const KeyRenderer = useJsonViewerStore(store => store.keyRenderer)
const KeyRenderer = useAtomValue(keyRendererAtom)
const downstreamProps: DataItemProps = useMemo(() => ({
path,
inspect,
Expand All @@ -174,8 +187,9 @@ export const DataKeyPair: FC<DataKeyPairProps> = (props) => {
data-testid={'data-key-pair' + path.join('.')}
sx={{ userSelect: 'text' }}
onMouseEnter={
useCallback(() => setHover(path, nestedIndex),
[setHover, path, nestedIndex])
useCallback(() => {
setHover({ path, nestedIndex })
}, [setHover, path, nestedIndex])
}
>
<DataBox
Expand Down
5 changes: 3 additions & 2 deletions src/components/DataTypes/Function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* Because in Next.js SSR, the function will be translated to other type
*/
import { Box, NoSsr } from '@mui/material'
import { useAtomValue } from 'jotai'
import type { FC } from 'react'

import { useJsonViewerStore } from '../../stores/JsonViewerStore'
import { colorspaceAtom } from '../../state'
import type { DataItemProps } from '../../type'
import { DataTypeLabel } from '../DataTypeLabel'

Expand Down Expand Up @@ -70,7 +71,7 @@ export const PostFunctionType: FC<DataItemProps<Function>> = () => {
}

export const FunctionType: FC<DataItemProps<Function>> = (props) => {
const functionColor = useJsonViewerStore(store => store.colorspace.base05)
const { base05: functionColor } = useAtomValue(colorspaceAtom)
return (
<NoSsr>
<Box
Expand Down
40 changes: 25 additions & 15 deletions src/components/DataTypes/Object.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Box } from '@mui/material'
import { useAtomValue } from 'jotai'
import type { FC } from 'react'
import { useMemo, useState } from 'react'

import { useTextColor } from '../../hooks/useColor'
import { useIsCycleReference } from '../../hooks/useIsCycleReference'
import { useJsonViewerStore } from '../../stores/JsonViewerStore'
import {
colorspaceAtom,
displayObjectSizeAtom,
groupArraysAfterLengthAtom,
indentWidthAtom,
maxDisplayLengthAtom,
objectSortKeysAtom
} from '../../state'
import type { DataItemProps } from '../../type'
import { getValueSize, segmentArray } from '../../utils'
import { DataKeyPair } from '../DataKeyPair'
Expand All @@ -30,13 +37,14 @@ function inspectMetadata (value: object) {
}

export const PreObjectType: FC<DataItemProps<object>> = (props) => {
const metadataColor = useJsonViewerStore(store => store.colorspace.base04)
const textColor = useTextColor()
const {
base04: metadataColor,
base07: textColor
} = useAtomValue(colorspaceAtom)
const isArray = useMemo(() => Array.isArray(props.value), [props.value])
const isEmptyValue = useMemo(() => getValueSize(props.value) === 0, [props.value])
const sizeOfValue = useMemo(() => inspectMetadata(props.value), [props.value]
)
const displayObjectSize = useJsonViewerStore(store => store.displayObjectSize)
const sizeOfValue = useMemo(() => inspectMetadata(props.value), [props.value])
const displayObjectSize = useAtomValue(displayObjectSizeAtom)
const isTrap = useIsCycleReference(props.path, props.value)
return (
<Box
Expand Down Expand Up @@ -78,9 +86,9 @@ export const PreObjectType: FC<DataItemProps<object>> = (props) => {
}

export const PostObjectType: FC<DataItemProps<object>> = (props) => {
const metadataColor = useJsonViewerStore(store => store.colorspace.base04)
const { base04: metadataColor } = useAtomValue(colorspaceAtom)
const isArray = useMemo(() => Array.isArray(props.value), [props.value])
const displayObjectSize = useJsonViewerStore(store => store.displayObjectSize)
const displayObjectSize = useAtomValue(displayObjectSizeAtom)
const isEmptyValue = useMemo(() => getValueSize(props.value) === 0, [props.value])
const sizeOfValue = useMemo(() => inspectMetadata(props.value), [props.value])

Expand Down Expand Up @@ -111,12 +119,14 @@ function getIterator (value: any): value is Iterable<unknown> {
}

export const ObjectType: FC<DataItemProps<object>> = (props) => {
const keyColor = useTextColor()
const borderColor = useJsonViewerStore(store => store.colorspace.base02)
const groupArraysAfterLength = useJsonViewerStore(store => store.groupArraysAfterLength)
const {
base02: borderColor,
base07: keyColor
} = useAtomValue(colorspaceAtom)
const groupArraysAfterLength = useAtomValue(groupArraysAfterLengthAtom)
const isTrap = useIsCycleReference(props.path, props.value)
const [displayLength, setDisplayLength] = useState(useJsonViewerStore(store => store.maxDisplayLength))
const objectSortKeys = useJsonViewerStore(store => store.objectSortKeys)
const [displayLength, setDisplayLength] = useState(useAtomValue(maxDisplayLengthAtom))
const objectSortKeys = useAtomValue(objectSortKeysAtom)
const elements = useMemo(() => {
if (!props.inspect) {
return null
Expand Down Expand Up @@ -245,7 +255,7 @@ export const ObjectType: FC<DataItemProps<object>> = (props) => {
objectSortKeys
])
const marginLeft = props.inspect ? 0.6 : 0
const width = useJsonViewerStore(store => store.indentWidth)
const width = useAtomValue(indentWidthAtom)
const indentWidth = props.inspect ? width - marginLeft : width
const isEmptyValue = useMemo(() => getValueSize(props.value) === 0, [props.value])
if (isEmptyValue) {
Expand Down
14 changes: 6 additions & 8 deletions src/components/DataTypes/createEasyType.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InputBase } from '@mui/material'
import { useAtomValue } from 'jotai'
import type { ChangeEventHandler, ComponentType, FC } from 'react'
import { memo, useCallback } from 'react'

import { useJsonViewerStore } from '../../stores/JsonViewerStore'
import { colorspaceAtom, displayDataTypesAtom, onSelectAtom } from '../../state'
import type { Colorspace } from '../../theme/base16'
import type { DataItemProps, DataType, EditorProps } from '../../type'
import { DataTypeLabel } from '../DataTypeLabel'
Expand All @@ -18,13 +19,11 @@ export function createEasyType<Value> (
}
): Omit<DataType<Value>, 'is'> {
const { fromString, colorKey, displayTypeLabel = true } = config

const Render = memo(renderValue)
const EasyType: FC<DataItemProps<Value>> = (props) => {
const storeDisplayDataTypes = useJsonViewerStore(store => store.displayDataTypes)
const color = useJsonViewerStore(store => store.colorspace[colorKey])
const onSelect = useJsonViewerStore(store => store.onSelect)

const storeDisplayDataTypes = useAtomValue(displayDataTypesAtom)
const color = useAtomValue(colorspaceAtom)[colorKey]
const onSelect = useAtomValue(onSelectAtom)
return (
<DataBox onClick={() => onSelect?.(props.path, props.value)} sx={{ color }}>
{(displayTypeLabel && storeDisplayDataTypes) && <DataTypeLabel dataType={type} />}
Expand All @@ -41,9 +40,8 @@ export function createEasyType<Value> (
Component: EasyType
}
}

const EasyTypeEditor: FC<EditorProps<Value>> = ({ value, setValue }) => {
const color = useJsonViewerStore(store => store.colorspace[colorKey])
const color = useAtomValue(colorspaceAtom)[colorKey]
return (
<InputBase
value={value}
Expand Down
5 changes: 0 additions & 5 deletions src/hooks/useColor.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/hooks/useCopyToClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import copyToClipboard from 'copy-to-clipboard'
import { useAtomValue } from 'jotai'
import { useCallback, useRef, useState } from 'react'

import { useJsonViewerStore } from '../stores/JsonViewerStore'
import { onCopyAtom } from '../state'
import type { JsonViewerOnCopy } from '../type'
import { safeStringify } from '../utils'

Expand All @@ -23,7 +24,7 @@ export function useClipboard ({ timeout = 2000 } = {}) {
copyTimeout.current = window.setTimeout(() => setCopied(false), timeout)
setCopied(value)
}, [timeout])
const onCopy = useJsonViewerStore(store => store.onCopy)
const onCopy = useAtomValue(onCopyAtom)

const copy = useCallback<JsonViewerOnCopy>((path, value: unknown) => {
if (typeof onCopy === 'function') {
Expand Down
39 changes: 19 additions & 20 deletions src/hooks/useInspect.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import type {
Dispatch,
SetStateAction
} from 'react'
import { useAtom, useAtomValue } from 'jotai'
import {
Dispatch,
SetStateAction,
useCallback,
useEffect,
useState
} from 'react'

import {
useJsonViewerStore
} from '../stores/JsonViewerStore'
import { defaultInspectDepthAtom, inspectCacheAtom } from '../state'
import type { HostPath, JsonViewerProps } from '../type'
import { useIsCycleReference } from './useIsCycleReference'

export function useInspect (path: (string | number)[], value: any, nestedIndex?: number) {
export function useInspect (
path: HostPath['path'],
value: JsonViewerProps['value'],
nestedIndex?: HostPath['nestedIndex']
) {
const depth = path.length
const isTrap = useIsCycleReference(path, value)
const getInspectCache = useJsonViewerStore(store => store.getInspectCache)
const setInspectCache = useJsonViewerStore(store => store.setInspectCache)
const defaultInspectDepth = useJsonViewerStore(store => store.defaultInspectDepth)
const defaultInspectDepth = useAtomValue(defaultInspectDepthAtom)
const [inspectCache, setInspectCache] = useAtom(inspectCacheAtom({ path, nestedIndex }))
useEffect(() => {
const inspect = getInspectCache(path, nestedIndex)
if (inspect !== undefined) {
if (inspectCache !== undefined) {
return
}
if (nestedIndex !== undefined) {
setInspectCache(path, false, nestedIndex)
setInspectCache({ path, action: false, nestedIndex })
} else {
// do not inspect when it is a cycle reference, otherwise there will have a loop
const inspect = isTrap
? false
: depth < defaultInspectDepth
setInspectCache(path, inspect)
setInspectCache({ path, action: inspect })
}
}, [defaultInspectDepth, depth, getInspectCache, isTrap, nestedIndex, path, setInspectCache])
}, [defaultInspectDepth, depth, isTrap, nestedIndex, path, inspectCache, setInspectCache])
const [inspect, set] = useState<boolean>(() => {
const shouldInspect = getInspectCache(path, nestedIndex)
if (shouldInspect !== undefined) {
return shouldInspect
if (inspectCache !== undefined) {
return inspectCache
}
if (nestedIndex !== undefined) {
return false
Expand All @@ -49,7 +48,7 @@ export function useInspect (path: (string | number)[], value: any, nestedIndex?:
const setInspect = useCallback<Dispatch<SetStateAction<boolean>>>((apply) => {
set((oldState) => {
const newState = typeof apply === 'boolean' ? apply : apply(oldState)
setInspectCache(path, newState, nestedIndex)
setInspectCache({ path, action: newState, nestedIndex })
return newState
})
}, [nestedIndex, path, setInspectCache])
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useIsCycleReference.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useAtomValue } from 'jotai'
import { useMemo } from 'react'

import { useJsonViewerStore } from '../stores/JsonViewerStore'
import { valueAtom } from '../state'
import { isCycleReference } from '../utils'

export function useIsCycleReference (path: (string | number)[], value: any) {
const rootValue = useJsonViewerStore(store => store.value)
const rootValue = useAtomValue(valueAtom)
return useMemo(
() => isCycleReference(rootValue, path, value),
[path, value, rootValue])
[path, value, rootValue]
)
}
Loading