mirror of
https://github.com/johndoe6345789/low-code-react-app-b.git
synced 2026-04-24 21:54:56 +00:00
Edited Spark
This commit is contained in:
@@ -1,81 +1,77 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useKV } from '@github/spark/hooks'
|
||||
|
||||
export type DataSourceType = 'kv' | 'static' | 'computed'
|
||||
|
||||
export interface DataSourceConfig<T = any> {
|
||||
id: string
|
||||
type: DataSourceType
|
||||
key?: string
|
||||
defaultValue?: T
|
||||
compute?: (allData: Record<string, any>) => T
|
||||
dependencies?: string[]
|
||||
}
|
||||
|
||||
export function useKVDataSource<T>(key: string, defaultValue: T) {
|
||||
const [value, setValue, deleteValue] = useKV<T>(key, defaultValue)
|
||||
|
||||
return {
|
||||
data: value,
|
||||
setData: setValue,
|
||||
deleteData: deleteValue,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
export function useComputedDataSource<T>(
|
||||
compute: (allData: Record<string, any>) => T,
|
||||
allData: Record<string, any>,
|
||||
dependencies: string[],
|
||||
defaultValue?: T
|
||||
) {
|
||||
const [computed, setComputed] = useState<T>(defaultValue as T)
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
const newValue = compute(allData)
|
||||
setComputed(newValue)
|
||||
} catch (error) {
|
||||
console.error('Error computing data source:', error)
|
||||
}
|
||||
}, dependencies.map(dep => allData[dep]))
|
||||
|
||||
return {
|
||||
data: computed,
|
||||
setData: () => {},
|
||||
deleteData: () => {},
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
export function useStaticDataSource<T>(value: T) {
|
||||
return {
|
||||
data: value,
|
||||
setData: () => {},
|
||||
deleteData: () => {},
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
export function useMultipleDataSources(
|
||||
configs: DataSourceConfig[],
|
||||
onUpdate?: (data: Record<string, any>) => void
|
||||
) {
|
||||
const [allData, setAllData] = useState<Record<string, any>>({})
|
||||
|
||||
const updateData = useCallback((id: string, value: any) => {
|
||||
setAllData(prev => {
|
||||
const next = { ...prev, [id]: value }
|
||||
onUpdate?.(next)
|
||||
return next
|
||||
})
|
||||
}, [onUpdate])
|
||||
|
||||
return {
|
||||
allData,
|
||||
updateData,
|
||||
}
|
||||
}
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
|
||||
|
||||
export type DataSourceType = 'kv' | 'static' | 'computed'
|
||||
|
||||
export interface DataSourceConfig<T = any> {
|
||||
defaultVal
|
||||
dependencies?: strin
|
||||
|
||||
const [value, se
|
||||
return {
|
||||
setData: setValue,
|
||||
|
||||
|
||||
|
||||
compute: (allData: Record<st
|
||||
dependencies: string[],
|
||||
) {
|
||||
|
||||
try {
|
||||
|
||||
consol
|
||||
}, dependencies.
|
||||
return {
|
||||
deleteData: deleteValue,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
if (config.type === 'computed' && config.compute) {
|
||||
const [computed, setComputed] = useState<T>(() => config.compute(allData))
|
||||
|
||||
useEffect(() => {
|
||||
const newValue = config.compute!(allData)
|
||||
setComputed(newValue)
|
||||
}, config.dependencies?.map(dep => allData[dep]) || [allData])
|
||||
|
||||
return {
|
||||
data: computed,
|
||||
setData: () => {},
|
||||
deleteData: () => {},
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}
|
||||
}
|
||||
|
||||
const [staticData] = useState<T>(config.defaultValue as T)
|
||||
|
||||
onUp
|
||||
})
|
||||
|
||||
allData,
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user