import { createClient } from '@/lib/supabase/server' import { notFound } from 'next/navigation' import Link from 'next/link' import FlowchartEditor from './FlowchartEditor' import type { FlowchartData } from '@/types/flowchart' type PageProps = { params: Promise<{ projectId: string }> } export default async function EditorPage({ params }: PageProps) { const { projectId } = await params const supabase = await createClient() const { data: { user }, } = await supabase.auth.getUser() if (!user) { return null } // Fetch user's display name for presence const { data: profile } = await supabase .from('profiles') .select('display_name') .eq('id', user.id) .single() const userDisplayName = profile?.display_name || user.email || 'Anonymous' // Try to load as owner first const { data: ownedProject } = await supabase .from('projects') .select('id, name, flowchart_data') .eq('id', projectId) .eq('user_id', user.id) .single() let project = ownedProject let isOwner = true // If not the owner, check if the user is a collaborator if (!project) { // RLS on projects allows collaborators to SELECT shared projects const { data: collab } = await supabase .from('project_collaborators') .select('id, role') .eq('project_id', projectId) .eq('user_id', user.id) .single() if (!collab) { notFound() } const { data: sharedProject } = await supabase .from('projects') .select('id, name, flowchart_data') .eq('id', projectId) .single() if (!sharedProject) { notFound() } project = sharedProject isOwner = false } const rawData = project.flowchart_data || {} const flowchartData: FlowchartData = { nodes: rawData.nodes || [], edges: rawData.edges || [], characters: rawData.characters || [], variables: rawData.variables || [], } // Migration flag: if the raw data doesn't have characters/variables arrays, // the project was created before these features existed and may need auto-migration const needsMigration = !rawData.characters && !rawData.variables return (