()
| 160 | } |
| 161 | |
| 162 | export 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 | } |
no test coverage detected