feat: add last run store
parent
379d382914
commit
ea40cf5bcc
@ -0,0 +1,30 @@
|
|||||||
|
import type { FC } from 'react'
|
||||||
|
import { createContext, useRef } from 'react'
|
||||||
|
import { createLastRunStore } from './store'
|
||||||
|
|
||||||
|
type LastRunStoreApi = ReturnType<typeof createLastRunStore>
|
||||||
|
|
||||||
|
type LastRunContextType = LastRunStoreApi | undefined
|
||||||
|
|
||||||
|
export const LastRunContext = createContext<LastRunContextType>(undefined)
|
||||||
|
|
||||||
|
type LastRunProviderProps = {
|
||||||
|
children: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
const LastRunProvider: FC<LastRunProviderProps> = ({
|
||||||
|
children,
|
||||||
|
}) => {
|
||||||
|
const storeRef = useRef<LastRunStoreApi>()
|
||||||
|
|
||||||
|
if (!storeRef.current)
|
||||||
|
storeRef.current = createLastRunStore()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<LastRunContext.Provider value={storeRef.current!}>
|
||||||
|
{children}
|
||||||
|
</LastRunContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default LastRunProvider
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
import { useContext } from 'react'
|
||||||
|
import { createStore, useStore } from 'zustand'
|
||||||
|
import { LastRunContext } from './provider'
|
||||||
|
|
||||||
|
type NodeInfo = {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
type: string
|
||||||
|
vars: {
|
||||||
|
key: string
|
||||||
|
type: string
|
||||||
|
value: any
|
||||||
|
}[]
|
||||||
|
} & {
|
||||||
|
input: Record<string, any>
|
||||||
|
output: Record<string, any>
|
||||||
|
}
|
||||||
|
|
||||||
|
type LastRunState = {
|
||||||
|
nodes: NodeInfo[]
|
||||||
|
}
|
||||||
|
|
||||||
|
type LastRunActions = {
|
||||||
|
setInfos: (vars: NodeInfo[]) => void
|
||||||
|
getInfos: () => NodeInfo[]
|
||||||
|
getNodeInfo: (nodeId: string) => NodeInfo | undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
type LastRunStore = LastRunState & LastRunActions
|
||||||
|
|
||||||
|
export const createLastRunStore = () => {
|
||||||
|
return createStore<LastRunStore>((set, get) => ({
|
||||||
|
nodes: [{
|
||||||
|
id: '',
|
||||||
|
name: '',
|
||||||
|
type: '',
|
||||||
|
vars: [],
|
||||||
|
input: {},
|
||||||
|
output: {},
|
||||||
|
}],
|
||||||
|
setInfos: (vars) => {
|
||||||
|
set(() => ({
|
||||||
|
nodes: vars,
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
getInfos: () => {
|
||||||
|
return get().nodes
|
||||||
|
},
|
||||||
|
clearVars: () => {
|
||||||
|
set(() => ({
|
||||||
|
nodes: [],
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
getNodeInfo: (nodeId) => {
|
||||||
|
const nodes = get().nodes
|
||||||
|
return nodes.find(node => node.id === nodeId)
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useLastRunStore = <T>(selector: (state: LastRunStore) => T): T => {
|
||||||
|
const store = useContext(LastRunContext)
|
||||||
|
if (!store)
|
||||||
|
throw new Error('Missing LastRunContext.Provider in the tree')
|
||||||
|
|
||||||
|
return useStore(store, selector)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue