File size: 3,274 Bytes
5da4770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Project, Thread } from '@/lib/api';
import { createClient } from '@/lib/supabase/server';

export const getThread = async (threadId: string): Promise<Thread> => {
  const supabase = await createClient();
  const { data, error } = await supabase
    .from('threads')
    .select('*')
    .eq('thread_id', threadId)
    .single();

  if (error) throw error;

  return data;
};

export const getProject = async (projectId: string): Promise<Project> => {
  const supabase = await createClient();

  try {
    const { data, error } = await supabase
      .from('projects')
      .select('*')
      .eq('project_id', projectId)
      .single();

      console.log('Raw project data from database:', data);

    if (error) {
      // Handle the specific "no rows returned" error from Supabase
      if (error.code === 'PGRST116') {
        throw new Error(`Project not found or not accessible: ${projectId}`);
      }
      throw error;
    }

    console.log('Raw project data from database:', data);

    // // If project has a sandbox, ensure it's started
    // if (data.sandbox?.id) {
    //   // Fire off sandbox activation without blocking
    //   const ensureSandboxActive = async () => {
    //     try {
    //       const {
    //         data: { session },
    //       } = await supabase.auth.getSession();

    //       // For public projects, we don't need authentication
    //       const headers: Record<string, string> = {
    //         'Content-Type': 'application/json',
    //       };

    //       if (session?.access_token) {
    //         headers['Authorization'] = `Bearer ${session.access_token}`;
    //       }

    //       console.log(`Ensuring sandbox is active for project ${projectId}...`);
    //       const response = await fetch(
    //         `${API_URL}/project/${projectId}/sandbox/ensure-active`,
    //         {
    //           method: 'POST',
    //           headers,
    //         },
    //       );

    //       if (!response.ok) {
    //         const errorText = await response
    //           .text()
    //           .catch(() => 'No error details available');
    //         console.warn(
    //           `Failed to ensure sandbox is active: ${response.status} ${response.statusText}`,
    //           errorText,
    //         );
    //       } else {
    //         console.log('Sandbox activation successful');
    //       }
    //     } catch (sandboxError) {
    //       console.warn('Failed to ensure sandbox is active:', sandboxError);
    //     }
    //   };

    //   // Start the sandbox activation without awaiting
    //   ensureSandboxActive();
    // }

    // Map database fields to our Project type
    const mappedProject: Project = {
      id: data.project_id,
      name: data.name || '',
      description: data.description || '',
      account_id: data.account_id,
      is_public: data.is_public || false,
      created_at: data.created_at,
      sandbox: data.sandbox || {
        id: '',
        pass: '',
        vnc_preview: '',
        sandbox_url: '',
      },
    };

    // console.log('Mapped project data for frontend:', mappedProject);

    return mappedProject;
  } catch (error) {
    console.error(`Error fetching project ${projectId}:`, error);
    throw error;
  }
};