Skip to content

Commit c541d20

Browse files
committed
cleaned up
1 parent 9994ae4 commit c541d20

File tree

4 files changed

+172
-193
lines changed

4 files changed

+172
-193
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/workflow-list/components/workflow-item/workflow-item.tsx

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,15 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
4646
const userPermissions = useUserPermissionsContext()
4747
const isSelected = selectedWorkflows.has(workflow.id)
4848

49-
// Can delete check hook
5049
const { canDeleteWorkflows } = useCanDelete({ workspaceId })
5150

52-
// Delete modal state
5351
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)
5452
const [workflowIdsToDelete, setWorkflowIdsToDelete] = useState<string[]>([])
5553
const [deleteModalNames, setDeleteModalNames] = useState<string | string[]>('')
5654
const [canDeleteCaptured, setCanDeleteCaptured] = useState(true)
5755

58-
// Presence avatars state
5956
const [hasAvatars, setHasAvatars] = useState(false)
6057

61-
// Capture selection at right-click time (using ref to persist across renders)
6258
const capturedSelectionRef = useRef<{
6359
workflowIds: string[]
6460
workflowNames: string | string[]
@@ -68,44 +64,39 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
6864
* Handle opening the delete modal - uses pre-captured selection state
6965
*/
7066
const handleOpenDeleteModal = useCallback(() => {
71-
// Use the selection captured at right-click time
7267
if (capturedSelectionRef.current) {
7368
setWorkflowIdsToDelete(capturedSelectionRef.current.workflowIds)
7469
setDeleteModalNames(capturedSelectionRef.current.workflowNames)
7570
setIsDeleteModalOpen(true)
7671
}
7772
}, [])
7873

79-
// Delete workflow hook
8074
const { isDeleting, handleDeleteWorkflow } = useDeleteWorkflow({
8175
workspaceId,
8276
workflowIds: workflowIdsToDelete,
8377
isActive: (workflowIds) => workflowIds.includes(params.workflowId as string),
8478
onSuccess: () => setIsDeleteModalOpen(false),
8579
})
8680

87-
// Duplicate workflow hook (uses captured selection from right-click)
88-
const { handleDuplicateWorkflow } = useDuplicateWorkflow({
89-
workspaceId,
90-
workflowIds: capturedSelectionRef.current?.workflowIds || [],
91-
})
81+
const { handleDuplicateWorkflow: duplicateWorkflow } = useDuplicateWorkflow({ workspaceId })
9282

93-
// Export workflow hook (uses captured selection from right-click)
94-
const { handleExportWorkflow } = useExportWorkflow({
95-
workspaceId,
96-
workflowIds: capturedSelectionRef.current?.workflowIds || [],
97-
})
83+
const { handleExportWorkflow: exportWorkflow } = useExportWorkflow({ workspaceId })
84+
const handleDuplicateWorkflow = useCallback(() => {
85+
const workflowIds = capturedSelectionRef.current?.workflowIds || []
86+
if (workflowIds.length === 0) return
87+
duplicateWorkflow(workflowIds)
88+
}, [duplicateWorkflow])
89+
90+
const handleExportWorkflow = useCallback(() => {
91+
const workflowIds = capturedSelectionRef.current?.workflowIds || []
92+
if (workflowIds.length === 0) return
93+
exportWorkflow(workflowIds)
94+
}, [exportWorkflow])
9895

99-
/**
100-
* Opens the workflow in a new browser tab
101-
*/
10296
const handleOpenInNewTab = useCallback(() => {
10397
window.open(`/workspace/${workspaceId}/w/${workflow.id}`, '_blank')
10498
}, [workspaceId, workflow.id])
10599

106-
/**
107-
* Changes the workflow color
108-
*/
109100
const handleColorChange = useCallback(
110101
(color: string) => {
111102
updateWorkflow(workflow.id, { color })
@@ -120,7 +111,6 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
120111
*/
121112
const onDragStart = useCallback(
122113
(e: React.DragEvent) => {
123-
// Don't start drag if editing
124114
if (isEditing) {
125115
e.preventDefault()
126116
return
@@ -135,12 +125,10 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
135125
[isSelected, selectedWorkflows, workflow.id]
136126
)
137127

138-
// Item drag hook
139128
const { isDragging, shouldPreventClickRef, handleDragStart, handleDragEnd } = useItemDrag({
140129
onDragStart,
141130
})
142131

143-
// Context menu hook
144132
const {
145133
isOpen: isContextMenuOpen,
146134
position,
@@ -209,14 +197,12 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
209197
e.preventDefault()
210198
e.stopPropagation()
211199

212-
// Toggle: close if open, open if closed
213200
if (isContextMenuOpen) {
214201
closeMenu()
215202
return
216203
}
217204

218205
captureSelectionState()
219-
// Open context menu aligned with the button
220206
const rect = e.currentTarget.getBoundingClientRect()
221207
handleContextMenuBase({
222208
preventDefault: () => {},
@@ -228,7 +214,6 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
228214
[isContextMenuOpen, closeMenu, captureSelectionState, handleContextMenuBase]
229215
)
230216

231-
// Rename hook
232217
const {
233218
isEditing,
234219
editValue,
@@ -275,12 +260,10 @@ export function WorkflowItem({ workflow, active, level, onWorkflowClick }: Workf
275260

276261
const isModifierClick = e.shiftKey || e.metaKey || e.ctrlKey
277262

278-
// Prevent default link behavior when using modifier keys
279263
if (isModifierClick) {
280264
e.preventDefault()
281265
}
282266

283-
// Use metaKey (Cmd on Mac) or ctrlKey (Ctrl on Windows/Linux)
284267
onWorkflowClick(workflow.id, e.shiftKey, e.metaKey || e.ctrlKey)
285268
},
286269
[shouldPreventClickRef, workflow.id, onWorkflowClick, isEditing]

apps/sim/app/workspace/[workspaceId]/w/hooks/use-duplicate-workflow.ts

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ interface UseDuplicateWorkflowProps {
1313
* Current workspace ID
1414
*/
1515
workspaceId: string
16-
/**
17-
* Workflow ID(s) to duplicate
18-
*/
19-
workflowIds: string | string[]
2016
/**
2117
* Optional callback after successful duplication
2218
*/
@@ -29,69 +25,69 @@ interface UseDuplicateWorkflowProps {
2925
* @param props - Hook configuration
3026
* @returns Duplicate workflow handlers and state
3127
*/
32-
export function useDuplicateWorkflow({
33-
workspaceId,
34-
workflowIds,
35-
onSuccess,
36-
}: UseDuplicateWorkflowProps) {
28+
export function useDuplicateWorkflow({ workspaceId, onSuccess }: UseDuplicateWorkflowProps) {
3729
const router = useRouter()
3830
const { workflows } = useWorkflowRegistry()
3931
const duplicateMutation = useDuplicateWorkflowMutation()
4032

4133
/**
4234
* Duplicate the workflow(s)
35+
* @param workflowIds - The workflow ID(s) to duplicate
4336
*/
44-
const handleDuplicateWorkflow = useCallback(async () => {
45-
if (!workflowIds) {
46-
return
47-
}
37+
const handleDuplicateWorkflow = useCallback(
38+
async (workflowIds: string | string[]) => {
39+
if (!workflowIds || (Array.isArray(workflowIds) && workflowIds.length === 0)) {
40+
return
41+
}
4842

49-
if (duplicateMutation.isPending) {
50-
return
51-
}
43+
if (duplicateMutation.isPending) {
44+
return
45+
}
5246

53-
const workflowIdsToDuplicate = Array.isArray(workflowIds) ? workflowIds : [workflowIds]
47+
const workflowIdsToDuplicate = Array.isArray(workflowIds) ? workflowIds : [workflowIds]
5448

55-
const duplicatedIds: string[] = []
49+
const duplicatedIds: string[] = []
5650

57-
try {
58-
for (const sourceId of workflowIdsToDuplicate) {
59-
const sourceWorkflow = workflows[sourceId]
60-
if (!sourceWorkflow) {
61-
logger.warn(`Workflow ${sourceId} not found, skipping`)
62-
continue
63-
}
51+
try {
52+
for (const sourceId of workflowIdsToDuplicate) {
53+
const sourceWorkflow = workflows[sourceId]
54+
if (!sourceWorkflow) {
55+
logger.warn(`Workflow ${sourceId} not found, skipping`)
56+
continue
57+
}
6458

65-
const result = await duplicateMutation.mutateAsync({
66-
workspaceId,
67-
sourceId,
68-
name: `${sourceWorkflow.name} (Copy)`,
69-
description: sourceWorkflow.description,
70-
color: getNextWorkflowColor(),
71-
folderId: sourceWorkflow.folderId,
72-
})
59+
const result = await duplicateMutation.mutateAsync({
60+
workspaceId,
61+
sourceId,
62+
name: `${sourceWorkflow.name} (Copy)`,
63+
description: sourceWorkflow.description,
64+
color: getNextWorkflowColor(),
65+
folderId: sourceWorkflow.folderId,
66+
})
7367

74-
duplicatedIds.push(result.id)
75-
}
68+
duplicatedIds.push(result.id)
69+
}
70+
71+
const { clearSelection } = useFolderStore.getState()
72+
clearSelection()
7673

77-
const { clearSelection } = useFolderStore.getState()
78-
clearSelection()
74+
logger.info('Workflow(s) duplicated successfully', {
75+
workflowIds: workflowIdsToDuplicate,
76+
duplicatedIds,
77+
})
7978

80-
logger.info('Workflow(s) duplicated successfully', {
81-
workflowIds: workflowIdsToDuplicate,
82-
duplicatedIds,
83-
})
79+
if (duplicatedIds.length === 1) {
80+
router.push(`/workspace/${workspaceId}/w/${duplicatedIds[0]}`)
81+
}
8482

85-
if (duplicatedIds.length === 1) {
86-
router.push(`/workspace/${workspaceId}/w/${duplicatedIds[0]}`)
83+
onSuccess?.()
84+
} catch (error) {
85+
logger.error('Error duplicating workflow(s):', { error })
86+
throw error
8787
}
88-
89-
onSuccess?.()
90-
} catch (error) {
91-
logger.error('Error duplicating workflow(s):', { error })
92-
throw error
93-
}
94-
}, [workflowIds, duplicateMutation, workflows, workspaceId, router, onSuccess])
88+
},
89+
[duplicateMutation, workflows, workspaceId, router, onSuccess]
90+
)
9591

9692
return {
9793
isDuplicating: duplicateMutation.isPending,

apps/sim/app/workspace/[workspaceId]/w/hooks/use-export-folder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export function useExportFolder({ workspaceId, folderId, onSuccess }: UseExportF
227227
} finally {
228228
setIsExporting(false)
229229
}
230-
}, [folderId, isExporting, workflows, onSuccess])
230+
}, [folderId, isExporting, workflows, folders, onSuccess])
231231

232232
return {
233233
isExporting,

0 commit comments

Comments
 (0)