Files
sim/stores/workflows/index.ts
Emir Karabeg 76dbc4a52f Feat/db sync (#94)
* feat(db-sync): added general sync file and implemented environment sync

* improvement(workflows-store): structured workflows store system better and added getter for values across stores

* fix(stores): deleted workflows/types since unused

* improvement(db-sync): added workflow event syncs and debounce

* improvement(db-sync): clean and upgraded db-sync system; environment sync implemented

* improvement(db-sync): added batch sync with registry; init bug needs fixing

* improvement(db-sync): finalized sync system and implemented for workflow

* fix(db-sync): fixed client-side rendering

* improvement(db-sync): created backwards sync system; environment implemented

* improvement(db-sync): added colors to db

* fix(db-sync): color sync with db

* improvement(db-sync): added workflow backwards sync; fixing color bug and race condition

* fix(db-stores): color sync

* feature(db-sync): db-sync complete; need to sync history

* improvement(db-sync): added scheduling

* fix(db-sync): environment sync to db
2025-03-03 19:43:39 -08:00

74 lines
2.5 KiB
TypeScript

import { loadWorkflowState } from './persistence'
import { useWorkflowRegistry } from './registry/store'
import { useSubBlockStore } from './subblock/store'
import { mergeSubblockState } from './utils'
import { useWorkflowStore } from './workflow/store'
import { BlockState, WorkflowState } from './workflow/types'
// Get a specific block with its subblock values merged in
export function getBlockWithValues(blockId: string): BlockState | null {
const workflowState = useWorkflowStore.getState()
const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId
if (!activeWorkflowId || !workflowState.blocks[blockId]) return null
const mergedBlocks = mergeSubblockState(workflowState.blocks, activeWorkflowId, blockId)
return mergedBlocks[blockId] || null
}
// Get all workflows with their values merged
export function getAllWorkflowsWithValues() {
const { workflows } = useWorkflowRegistry.getState()
const result: Record<string, any> = {}
const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId
const currentState = useWorkflowStore.getState()
for (const [id, metadata] of Object.entries(workflows)) {
// Load the specific state for this workflow
let workflowState: WorkflowState
if (id === activeWorkflowId) {
// For the active workflow, use the current state from the store
workflowState = {
blocks: currentState.blocks,
edges: currentState.edges,
loops: currentState.loops,
isDeployed: currentState.isDeployed,
deployedAt: currentState.deployedAt,
lastSaved: currentState.lastSaved,
}
} else {
// For other workflows, load their state from localStorage
const savedState = loadWorkflowState(id)
if (!savedState) {
// Skip workflows with no saved state
console.warn(`No saved state found for workflow ${id}`)
continue
}
workflowState = savedState
}
// Merge the subblock values for this specific workflow
const mergedBlocks = mergeSubblockState(workflowState.blocks, id)
result[id] = {
id,
name: metadata.name,
description: metadata.description,
color: metadata.color || '#3972F6',
state: {
blocks: mergedBlocks,
edges: workflowState.edges,
loops: workflowState.loops,
lastSaved: workflowState.lastSaved,
isDeployed: workflowState.isDeployed,
deployedAt: workflowState.deployedAt,
},
}
}
return result
}
export { useWorkflowRegistry, useWorkflowStore, useSubBlockStore }