MCPcopy Index your code
hub / github.com/simstudioai/sim / useCreateWorkflow

Function useCreateWorkflow

apps/sim/hooks/queries/workflows.ts:162–303  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

160}
161
162export function useCreateWorkflow() {
163 const queryClient = useQueryClient()
164
165 return useMutation({
166 mutationFn: async (variables: CreateWorkflowVariables): Promise<CreateWorkflowMutationData> => {
167 const { workspaceId, name, description, folderId, sortOrder, id, deduplicate } = variables
168
169 logger.info(`Creating new workflow in workspace: ${workspaceId}`)
170
171 const createdWorkflow = await requestJson(createWorkflowContract, {
172 body: {
173 id,
174 name: name || generateCreativeWorkflowName(),
175 description: description || 'New workflow',
176 workspaceId,
177 folderId: folderId || null,
178 sortOrder,
179 deduplicate,
180 },
181 })
182 const workflowId = createdWorkflow.id
183
184 logger.info(`Successfully created workflow ${workflowId}`)
185
186 return {
187 id: workflowId,
188 name: createdWorkflow.name,
189 description: createdWorkflow.description,
190 workspaceId,
191 folderId: createdWorkflow.folderId,
192 sortOrder: createdWorkflow.sortOrder ?? 0,
193 subBlockValues: createdWorkflow.subBlockValues,
194 }
195 },
196 onMutate: async (variables) => {
197 await queryClient.cancelQueries({
198 queryKey: workflowKeys.list(variables.workspaceId, 'active'),
199 })
200
201 const snapshot = queryClient.getQueryData<WorkflowMetadata[]>(
202 workflowKeys.list(variables.workspaceId, 'active')
203 )
204
205 const tempId = variables.id ?? generateId()
206 let sortOrder: number
207 if (variables.sortOrder !== undefined) {
208 sortOrder = variables.sortOrder
209 } else {
210 const currentWorkflows = Object.fromEntries(
211 getWorkflows(variables.workspaceId).map((w) => [w.id, w])
212 )
213 sortOrder = getTopInsertionSortOrder(
214 currentWorkflows,
215 getFolderMap(variables.workspaceId),
216 variables.workspaceId,
217 variables.folderId
218 )
219 }

Callers 3

FolderItemFunction · 0.90
useWorkflowOperationsFunction · 0.90
useImportWorkflowFunction · 0.90

Calls 10

requestJsonFunction · 0.90
generateIdFunction · 0.90
getWorkflowsFunction · 0.90
getTopInsertionSortOrderFunction · 0.90
getFolderMapFunction · 0.90
invalidateWorkflowListsFunction · 0.90
infoMethod · 0.80
deleteMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected