Files
low-code-react-app-b/src/hooks/data/use-json-data.ts
johndoe6345789 4dfded3533 Generated by Spark: @johndoe6345789 ➜ /workspaces/low-code-react-app-b (main) $ npm run build
> spark-template@0.0.0 prebuild
> mkdir -p /tmp/dist || true

> spark-template@0.0.0 build
> tsc -b --noCheck && vite build

vite v7.3.1 building client environment for production...
<script src="/runtime-config.js"> in "/index.html" can't be bundled without type="module" attribute
✓ 37 modules transformed.
✗ Build failed in 1.07s
error during build:
[vite]: Rollup failed to resolve import "@github/spark/hooks" from "/workspaces/low-code-react-app-b/src/hooks/use-project-state.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at viteLog (file:///workspaces/low-code-react-app-b/node_modules/vite/dist/node/chunks/config.js:33635:57)
    at file:///workspaces/low-code-react-app-b/node_modules/vite/dist/node/chunks/config.js:33669:73
    at onwarn (file:///workspaces/low-code-react-app-b/node_modules/@vitejs/plugin-react-swc/index.js:76:7)
    at file:///workspaces/low-code-react-app-b/node_modules/vite/dist/node/chunks/config.js:33669:28
    at onRollupLog (file:///workspaces/low-code-react-app-b/node_modules/vite/dist/node/chunks/config.js:33664:63)
    at onLog (file:///workspaces/low-code-react-app-b/node_modules/vite/dist/node/chunks/config.js:33467:4)
    at file:///workspaces/low-code-react-app-b/node_modules/rollup/dist/es/shared/node-entry.js:20961:32
    at Object.logger [as onLog] (file:///workspaces/low-code-react-app-b/node_modules/rollup/dist/es/shared/node-entry.js:22848:9)
    at ModuleLoader.handleInvalidResolvedId (file:///workspaces/low-code-react-app-b/node_modules/rollup/dist/es/shared/node-entry.js:21592:26)
    at file:///workspaces/low-code-react-app-b/node_modules/rollup/dist/es/shared/node-entry.js:21550:26
@johndoe6345789 ➜ /workspaces/low-code-react-app-b (main) $
2026-01-17 19:39:46 +00:00

55 lines
1.3 KiB
TypeScript

import { useState, useCallback } from 'react'
import { useKV } from '@/hooks/use-kv'
export interface UseJSONDataOptions {
key: string
defaultValue: any
persist?: boolean
}
export function useJSONData(options: UseJSONDataOptions) {
const { key, defaultValue, persist = true } = options
const [kvValue, setKvValue] = useKV(key, defaultValue)
const [localValue, setLocalValue] = useState(defaultValue)
const value = persist ? kvValue : localValue
const setValue = persist ? setKvValue : setLocalValue
const update = useCallback((updater: ((prev: any) => any) | any) => {
if (typeof updater === 'function') {
setValue(updater)
} else {
setValue(updater)
}
}, [setValue])
const updatePath = useCallback((path: string, newValue: any) => {
setValue((current: any) => {
const keys = path.split('.')
const result = { ...current }
let target: any = result
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i]
target[key] = { ...target[key] }
target = target[key]
}
target[keys[keys.length - 1]] = newValue
return result
})
}, [setValue])
const reset = useCallback(() => {
setValue(defaultValue)
}, [setValue, defaultValue])
return {
value,
setValue: update,
updatePath,
reset,
}
}