|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { useEffect } from 'react' |
| 3 | +import { useEffect, useState } from 'react' |
4 | 4 | import { useParams, useRouter } from 'next/navigation' |
5 | 5 | import { LoadingAgent } from '@/components/ui/loading-agent' |
6 | 6 | import { useWorkflowRegistry } from '@/stores/workflows/registry/store' |
7 | 7 |
|
8 | 8 | export default function WorkflowsPage() { |
9 | 9 | const router = useRouter() |
10 | | - const { workflows, isLoading } = useWorkflowRegistry() |
| 10 | + const { workflows, isLoading, loadWorkflows } = useWorkflowRegistry() |
| 11 | + const [hasInitialized, setHasInitialized] = useState(false) |
11 | 12 |
|
12 | 13 | const params = useParams() |
13 | | - const workspaceId = params.workspaceId |
| 14 | + const workspaceId = params.workspaceId as string |
14 | 15 |
|
| 16 | + // Initialize workspace workflows |
15 | 17 | useEffect(() => { |
16 | | - // Wait for workflows to load |
17 | | - if (isLoading) return |
| 18 | + const initializeWorkspace = async () => { |
| 19 | + try { |
| 20 | + await loadWorkflows(workspaceId) |
| 21 | + setHasInitialized(true) |
| 22 | + } catch (error) { |
| 23 | + console.error('Failed to load workflows for workspace:', error) |
| 24 | + setHasInitialized(true) // Still mark as initialized to show error state |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + if (!hasInitialized) { |
| 29 | + initializeWorkspace() |
| 30 | + } |
| 31 | + }, [workspaceId, loadWorkflows, hasInitialized]) |
| 32 | + |
| 33 | + // Handle redirection once workflows are loaded |
| 34 | + useEffect(() => { |
| 35 | + // Only proceed if we've initialized and workflows are not loading |
| 36 | + if (!hasInitialized || isLoading) return |
18 | 37 |
|
19 | 38 | const workflowIds = Object.keys(workflows) |
20 | 39 |
|
21 | | - // If we have workflows, redirect to the first one |
22 | | - if (workflowIds.length > 0) { |
23 | | - router.replace(`/workspace/${workspaceId}/w/${workflowIds[0]}`) |
24 | | - return |
25 | | - } |
| 40 | + // Validate that workflows belong to the current workspace |
| 41 | + const workspaceWorkflows = workflowIds.filter(id => { |
| 42 | + const workflow = workflows[id] |
| 43 | + return workflow.workspaceId === workspaceId |
| 44 | + }) |
26 | 45 |
|
27 | | - // If no workflows exist after loading is complete, this means the workspace creation |
28 | | - // didn't work properly or the user doesn't have any workspaces. |
29 | | - // Redirect to home to let the system handle workspace/workflow creation properly. |
30 | | - router.replace('/') |
31 | | - }, [workflows, isLoading, router, workspaceId]) |
| 46 | + // If we have valid workspace workflows, redirect to the first one |
| 47 | + if (workspaceWorkflows.length > 0) { |
| 48 | + router.replace(`/workspace/${workspaceId}/w/${workspaceWorkflows[0]}`) |
| 49 | + } |
| 50 | + }, [hasInitialized, isLoading, workflows, workspaceId, router]) |
32 | 51 |
|
33 | | - // Show loading state while determining where to redirect |
| 52 | + // Always show loading state until redirect happens |
| 53 | + // There should always be a default workflow, so we never show "no workflows found" |
34 | 54 | return ( |
35 | 55 | <div className='flex h-screen items-center justify-center'> |
36 | 56 | <div className='text-center'> |
|
0 commit comments