From c27eb6d3def022912cecf104c0313f233a02f470 Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sat, 17 Jan 2026 11:38:55 +0000 Subject: [PATCH] Edited Spark --- src/hooks/data/use-data-source.ts | 158 +++++++++++++++--------------- 1 file changed, 77 insertions(+), 81 deletions(-) diff --git a/src/hooks/data/use-data-source.ts b/src/hooks/data/use-data-source.ts index fd2dcb6..34b924e 100644 --- a/src/hooks/data/use-data-source.ts +++ b/src/hooks/data/use-data-source.ts @@ -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 { - id: string - type: DataSourceType - key?: string - defaultValue?: T - compute?: (allData: Record) => T - dependencies?: string[] -} - -export function useKVDataSource(key: string, defaultValue: T) { - const [value, setValue, deleteValue] = useKV(key, defaultValue) - - return { - data: value, - setData: setValue, - deleteData: deleteValue, - isLoading: false, - error: null, - } -} - -export function useComputedDataSource( - compute: (allData: Record) => T, - allData: Record, - dependencies: string[], - defaultValue?: T -) { - const [computed, setComputed] = useState(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(value: T) { - return { - data: value, - setData: () => {}, - deleteData: () => {}, - isLoading: false, - error: null, - } -} - -export function useMultipleDataSources( - configs: DataSourceConfig[], - onUpdate?: (data: Record) => void -) { - const [allData, setAllData] = useState>({}) - - 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 { + defaultVal + dependencies?: strin + + const [value, se + return { + setData: setValue, + + + + compute: (allData: Record(() => 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(config.defaultValue as T) + + onUp + }) + + allData, + } + + + + + + + + + + + + + + + + + + + + + + +