mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-05-07 03:59:35 +00:00
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { useState, useCallback } from 'react'
|
|
import { DataSource, DataSourceType } from '@/types/json-ui'
|
|
|
|
export function useDataSourceManager(initialSources: DataSource[] = []) {
|
|
const [dataSources, setDataSources] = useState<DataSource[]>(initialSources)
|
|
|
|
const addDataSource = useCallback((type: DataSourceType) => {
|
|
const newSource: DataSource = {
|
|
id: `ds-${Date.now()}`,
|
|
type,
|
|
...(type === 'kv' && { key: '', defaultValue: null }),
|
|
...(type === 'static' && { defaultValue: null }),
|
|
}
|
|
|
|
setDataSources(prev => [...prev, newSource])
|
|
return newSource
|
|
}, [])
|
|
|
|
const updateDataSource = useCallback((id: string, updates: Partial<DataSource>) => {
|
|
setDataSources(prev =>
|
|
prev.map(ds => ds.id === id ? { ...ds, ...updates } : ds)
|
|
)
|
|
}, [])
|
|
|
|
const deleteDataSource = useCallback((id: string) => {
|
|
setDataSources(prev => prev.filter(ds => ds.id !== id))
|
|
}, [])
|
|
|
|
const getDataSource = useCallback((id: string) => {
|
|
return dataSources.find(ds => ds.id === id)
|
|
}, [dataSources])
|
|
|
|
const getDependents = useCallback((sourceId: string) => {
|
|
return dataSources.filter(ds =>
|
|
ds.dependencies?.includes(sourceId)
|
|
)
|
|
}, [dataSources])
|
|
|
|
return {
|
|
dataSources,
|
|
addDataSource,
|
|
updateDataSource,
|
|
deleteDataSource,
|
|
getDataSource,
|
|
getDependents,
|
|
}
|
|
}
|