|
| 1 | + |
| 2 | +import { useState, useEffect } from 'react'; |
| 3 | +import { supabase } from '@/integrations/supabase/client'; |
| 4 | +import { toast } from 'sonner'; |
| 5 | +import { Server } from '@/types/server'; |
| 6 | + |
| 7 | +interface UseServerManifestResult { |
| 8 | + server: Server | null; |
| 9 | + isLoading: boolean; |
| 10 | + manifestId: bigint | null; |
| 11 | + isActionsOpen: boolean; |
| 12 | + setIsActionsOpen: (open: boolean) => void; |
| 13 | + showCodeView: boolean; |
| 14 | + setShowCodeView: (show: boolean) => void; |
| 15 | + handleImport: () => Promise<void>; |
| 16 | + handleExport: () => Promise<void>; |
| 17 | + handleDeploy: () => Promise<void>; |
| 18 | + handleUndeploy: () => Promise<void>; |
| 19 | +} |
| 20 | + |
| 21 | +export function useServerManifest(id: string | undefined): UseServerManifestResult { |
| 22 | + const [server, setServer] = useState<Server | null>(null); |
| 23 | + const [isLoading, setIsLoading] = useState(true); |
| 24 | + const [isActionsOpen, setIsActionsOpen] = useState(false); |
| 25 | + const [showCodeView, setShowCodeView] = useState(false); |
| 26 | + const [manifestId, setManifestId] = useState<bigint | null>(null); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + const fetchServerDetails = async () => { |
| 30 | + if (!id) return; |
| 31 | + |
| 32 | + setIsLoading(true); |
| 33 | + try { |
| 34 | + console.log('Fetching server details for ID/slug:', id); |
| 35 | + |
| 36 | + let query = supabase.from('servers') |
| 37 | + .select(` |
| 38 | + *, |
| 39 | + project_servers!inner ( |
| 40 | + project_id |
| 41 | + ) |
| 42 | + `); |
| 43 | + |
| 44 | + // Check if the ID is a UUID format or a slug |
| 45 | + const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id); |
| 46 | + if (isUuid) { |
| 47 | + query = query.eq('id', id); |
| 48 | + } else { |
| 49 | + query = query.eq('slug', id); |
| 50 | + } |
| 51 | + |
| 52 | + let { data, error } = await query; |
| 53 | + console.log('Initial query result:', data, error); |
| 54 | + |
| 55 | + if (error || !data || data.length === 0) { |
| 56 | + // If no results found with inner join, try without the join |
| 57 | + query = supabase.from('servers').select('*'); |
| 58 | + if (isUuid) { |
| 59 | + query = query.eq('id', id); |
| 60 | + } else { |
| 61 | + query = query.eq('slug', id); |
| 62 | + } |
| 63 | + |
| 64 | + const result = await query.single(); |
| 65 | + data = result.data; |
| 66 | + error = result.error; |
| 67 | + } |
| 68 | + console.log('Fallback query result:', data, error); |
| 69 | + |
| 70 | + if (error) { |
| 71 | + console.error('Error fetching server details:', error); |
| 72 | + toast.error(`Failed to load server: ${error.message}`); |
| 73 | + throw error; |
| 74 | + } |
| 75 | + |
| 76 | + // Process the data to extract project_id if available |
| 77 | + let serverData; |
| 78 | + if (Array.isArray(data) && data.length > 0) { |
| 79 | + const firstResult = data[0]; |
| 80 | + // Extract the project_id if available |
| 81 | + const projectId = firstResult.project_servers?.length > 0 |
| 82 | + ? firstResult.project_servers[0].project_id |
| 83 | + : null; |
| 84 | + |
| 85 | + // Create the server object with project_id |
| 86 | + serverData = { |
| 87 | + ...firstResult, |
| 88 | + project_id: projectId |
| 89 | + }; |
| 90 | + delete serverData.project_servers; |
| 91 | + } else { |
| 92 | + serverData = data; |
| 93 | + } |
| 94 | + |
| 95 | + console.log('Fetched server details:', serverData); |
| 96 | + setServer(serverData as Server); |
| 97 | + const { data: dataCode, error: dataCodeError } = await supabase |
| 98 | + .rpc('generate_manifest', { server_id: serverData.id }); |
| 99 | + |
| 100 | + if (dataCodeError) { |
| 101 | + console.error('Error fetching server code:', dataCodeError); |
| 102 | + toast.error(`Failed to load server code: ${dataCodeError.message}`); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + setManifestId(dataCode); |
| 107 | + } catch (error) { |
| 108 | + console.error('Error in server fetch:', error); |
| 109 | + toast.error('Failed to load server details'); |
| 110 | + } finally { |
| 111 | + setIsLoading(false); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + fetchServerDetails(); |
| 116 | + }, [id]); |
| 117 | + |
| 118 | + const handleImport = async () => { |
| 119 | + if (!server) return; |
| 120 | + // simulate import process |
| 121 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 122 | + toast.success('Server imported successfully'); |
| 123 | + }; |
| 124 | + |
| 125 | + const handleExport = async () => { |
| 126 | + if (!server) return; |
| 127 | + // simulate export process |
| 128 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 129 | + toast.success('Server exported successfully'); |
| 130 | + }; |
| 131 | + |
| 132 | + const handleDeploy = async () => { |
| 133 | + if (!server) return; |
| 134 | + // simulate deploy process |
| 135 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 136 | + toast.success('Server deployed successfully'); |
| 137 | + }; |
| 138 | + |
| 139 | + const handleUndeploy = async () => { |
| 140 | + if (!server) return; |
| 141 | + // simulate undeploy process |
| 142 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 143 | + toast.success('Server undeployed successfully'); |
| 144 | + }; |
| 145 | + |
| 146 | + return { |
| 147 | + server, |
| 148 | + isLoading, |
| 149 | + manifestId, |
| 150 | + isActionsOpen, |
| 151 | + setIsActionsOpen, |
| 152 | + showCodeView, |
| 153 | + setShowCodeView, |
| 154 | + handleImport, |
| 155 | + handleExport, |
| 156 | + handleDeploy, |
| 157 | + handleUndeploy |
| 158 | + }; |
| 159 | +} |
0 commit comments