|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react' |
| 4 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' |
| 5 | +import { Button } from '@/components/ui/button' |
| 6 | +import { Alert, AlertDescription } from '@/components/ui/alert' |
| 7 | +import { Badge } from '@/components/ui/badge' |
| 8 | +import { CheckCircle, XCircle, AlertTriangle, Database, ExternalLink } from 'lucide-react' |
| 9 | +import { checkSupabaseConnection, checkEnhancedTablesExist } from '@/lib/user-settings' |
| 10 | + |
| 11 | +interface TableStatus { |
| 12 | + user_profiles: boolean |
| 13 | + user_preferences: boolean |
| 14 | + user_integrations: boolean |
| 15 | + user_security_settings: boolean |
| 16 | +} |
| 17 | + |
| 18 | +export default function DatabaseDiagnostic() { |
| 19 | + const [tableStatus, setTableStatus] = useState<TableStatus | null>(null) |
| 20 | + const [isChecking, setIsChecking] = useState(false) |
| 21 | + const [supabaseConnected, setSupabaseConnected] = useState(false) |
| 22 | + |
| 23 | + const checkDatabaseStatus = async () => { |
| 24 | + setIsChecking(true) |
| 25 | + try { |
| 26 | + // Check Supabase connection |
| 27 | + const connected = await checkSupabaseConnection() |
| 28 | + setSupabaseConnected(connected) |
| 29 | + |
| 30 | + if (connected) { |
| 31 | + // Check table existence |
| 32 | + const status = await checkEnhancedTablesExist() |
| 33 | + setTableStatus(status as unknown as TableStatus) |
| 34 | + } else { |
| 35 | + setTableStatus(null) |
| 36 | + } |
| 37 | + } catch (error) { |
| 38 | + console.error('Error checking database status:', error) |
| 39 | + setSupabaseConnected(false) |
| 40 | + setTableStatus(null) |
| 41 | + } finally { |
| 42 | + setIsChecking(false) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + checkDatabaseStatus() |
| 48 | + }, []) |
| 49 | + |
| 50 | + const allTablesExist = tableStatus && Object.values(tableStatus).every(exists => exists) |
| 51 | + const someTablesExist = tableStatus && Object.values(tableStatus).some(exists => exists) |
| 52 | + |
| 53 | + const renderTableRow = (tableName: string, exists: boolean) => ( |
| 54 | + <div key={tableName} className="flex items-center justify-between py-2"> |
| 55 | + <code className="text-sm bg-muted px-2 py-1 rounded">{tableName}</code> |
| 56 | + <div className="flex items-center gap-2"> |
| 57 | + {exists ? ( |
| 58 | + <> |
| 59 | + <CheckCircle className="h-4 w-4 text-green-500" /> |
| 60 | + <Badge variant="default" className="bg-green-100 text-green-800">Exists</Badge> |
| 61 | + </> |
| 62 | + ) : ( |
| 63 | + <> |
| 64 | + <XCircle className="h-4 w-4 text-red-500" /> |
| 65 | + <Badge variant="destructive">Missing</Badge> |
| 66 | + </> |
| 67 | + )} |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ) |
| 71 | + |
| 72 | + return ( |
| 73 | + <Card className="w-full max-w-2xl mx-auto"> |
| 74 | + <CardHeader> |
| 75 | + <CardTitle className="flex items-center gap-2"> |
| 76 | + <Database className="h-5 w-5" /> |
| 77 | + Database Setup Diagnostic |
| 78 | + </CardTitle> |
| 79 | + <CardDescription> |
| 80 | + Check if your Supabase database has the required tables for user settings |
| 81 | + </CardDescription> |
| 82 | + </CardHeader> |
| 83 | + <CardContent className="space-y-6"> |
| 84 | + {/* Supabase Connection Status */} |
| 85 | + <div className="space-y-2"> |
| 86 | + <h3 className="text-sm font-medium">Supabase Connection</h3> |
| 87 | + <div className="flex items-center gap-2"> |
| 88 | + {supabaseConnected ? ( |
| 89 | + <> |
| 90 | + <CheckCircle className="h-4 w-4 text-green-500" /> |
| 91 | + <span className="text-sm">Connected</span> |
| 92 | + </> |
| 93 | + ) : ( |
| 94 | + <> |
| 95 | + <XCircle className="h-4 w-4 text-red-500" /> |
| 96 | + <span className="text-sm">Not connected or configuration error</span> |
| 97 | + </> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + |
| 102 | + {/* Table Status */} |
| 103 | + {tableStatus && ( |
| 104 | + <div className="space-y-4"> |
| 105 | + <div className="flex items-center justify-between"> |
| 106 | + <h3 className="text-sm font-medium">Required Tables</h3> |
| 107 | + <Button |
| 108 | + variant="outline" |
| 109 | + size="sm" |
| 110 | + onClick={checkDatabaseStatus} |
| 111 | + disabled={isChecking} |
| 112 | + > |
| 113 | + {isChecking ? 'Checking...' : 'Refresh'} |
| 114 | + </Button> |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className="space-y-1 border rounded-lg p-3"> |
| 118 | + {Object.entries(tableStatus).map(([table, exists]) => |
| 119 | + renderTableRow(table, exists) |
| 120 | + )} |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + |
| 125 | + {/* Status Alerts */} |
| 126 | + {!supabaseConnected && ( |
| 127 | + <Alert variant="destructive"> |
| 128 | + <AlertTriangle className="h-4 w-4" /> |
| 129 | + <AlertDescription> |
| 130 | + Supabase is not properly configured. Check your environment variables: |
| 131 | + <ul className="mt-2 space-y-1"> |
| 132 | + <li><code>NEXT_PUBLIC_SUPABASE_URL</code></li> |
| 133 | + <li><code>NEXT_PUBLIC_SUPABASE_ANON_KEY</code></li> |
| 134 | + </ul> |
| 135 | + </AlertDescription> |
| 136 | + </Alert> |
| 137 | + )} |
| 138 | + |
| 139 | + {supabaseConnected && tableStatus && !allTablesExist && ( |
| 140 | + <Alert variant="destructive"> |
| 141 | + <AlertTriangle className="h-4 w-4" /> |
| 142 | + <AlertDescription> |
| 143 | + {!someTablesExist |
| 144 | + ? "Required database tables are missing. You need to run the database migration." |
| 145 | + : "Some database tables are missing. You may need to update your database migration." |
| 146 | + } |
| 147 | + </AlertDescription> |
| 148 | + </Alert> |
| 149 | + )} |
| 150 | + |
| 151 | + {allTablesExist && ( |
| 152 | + <Alert> |
| 153 | + <CheckCircle className="h-4 w-4" /> |
| 154 | + <AlertDescription> |
| 155 | + All required database tables exist! Your user settings should work properly. |
| 156 | + </AlertDescription> |
| 157 | + </Alert> |
| 158 | + )} |
| 159 | + |
| 160 | + {/* Setup Instructions */} |
| 161 | + {supabaseConnected && !allTablesExist && ( |
| 162 | + <div className="space-y-4"> |
| 163 | + <h3 className="text-sm font-medium">Setup Instructions</h3> |
| 164 | + <div className="prose prose-sm max-w-none"> |
| 165 | + <ol className="space-y-2"> |
| 166 | + <li> |
| 167 | + Open your Supabase dashboard |
| 168 | + <Button variant="link" className="p-0 h-auto ml-2" asChild> |
| 169 | + <a href="https://app.supabase.com/projects" target="_blank" rel="noopener noreferrer"> |
| 170 | + <ExternalLink className="h-3 w-3 ml-1" /> |
| 171 | + </a> |
| 172 | + </Button> |
| 173 | + </li> |
| 174 | + <li>Go to SQL Editor in your project</li> |
| 175 | + <li>Create a new query and paste the complete database migration SQL</li> |
| 176 | + <li>Run the migration to create all required tables</li> |
| 177 | + <li>Return here and click "Refresh" to verify the setup</li> |
| 178 | + </ol> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + )} |
| 182 | + |
| 183 | + {/* Migration SQL Link */} |
| 184 | + {!allTablesExist && ( |
| 185 | + <div className="border-t pt-4"> |
| 186 | + <p className="text-sm text-muted-foreground"> |
| 187 | + Need the migration SQL? The complete database schema was provided in the previous response. |
| 188 | + Copy and run it in your Supabase SQL Editor. |
| 189 | + </p> |
| 190 | + </div> |
| 191 | + )} |
| 192 | + </CardContent> |
| 193 | + </Card> |
| 194 | + ) |
| 195 | +} |
0 commit comments