Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | import React, { useState, useContext } from "react"; import { AuthContext } from "@common/contexts/AuthContext"; import { useReadingQueue } from "@common/hooks/useReadingQueue"; import { dbCleanupUtils, DataIntegrityReport, } from "@common/utils/database/cleanupUtils"; interface CleanupPanelState { isLoading: boolean; lastReport: DataIntegrityReport | null; lastCleanupResult: { type: "quick" | "full" | "validation"; result: any; } | null; error: string | null; operationInProgress: string | null; } export const DatabaseCleanupPanel: React.FC = () => { const auth = useContext(AuthContext); const user = auth?.user; const { cleanupOrphanedItems, isCleaningUp } = useReadingQueue(); const [state, setState] = useState<CleanupPanelState>({ isLoading: false, lastReport: null, lastCleanupResult: null, error: null, operationInProgress: null, }); const setLoading = (loading: boolean, operation?: string) => { setState((prev) => ({ ...prev, isLoading: loading, operationInProgress: loading ? operation || null : null, error: loading ? null : prev.error, })); }; const setError = (error: string) => { setState((prev) => ({ ...prev, error, isLoading: false, operationInProgress: null, })); }; const handleGenerateReport = async () => { if (!user?.id) { setError("User not authenticated"); return; } setLoading(true, "Generating integrity report"); try { const report = await dbCleanupUtils.generateIntegrityReport(user.id); setState((prev) => ({ ...prev, lastReport: report, isLoading: false, operationInProgress: null, error: null, })); } catch (error) { setError( error instanceof Error ? error.message : "Failed to generate report", ); } }; const handleQuickCleanup = async () => { if (!user?.id) { setError("User not authenticated"); return; } setLoading(true, "Running quick cleanup"); try { const result = await cleanupOrphanedItems(); setState((prev) => ({ ...prev, lastCleanupResult: { type: "quick", result }, isLoading: false, operationInProgress: null, error: null, })); } catch (error) { setError(error instanceof Error ? error.message : "Quick cleanup failed"); } }; const handleFullCleanup = async () => { if (!user?.id) { setError("User not authenticated"); return; } setLoading(true, "Running full cleanup"); try { const result = await dbCleanupUtils.performFullCleanup(user.id); setState((prev) => ({ ...prev, lastCleanupResult: { type: "full", result }, isLoading: false, operationInProgress: null, error: null, })); } catch (error) { setError(error instanceof Error ? error.message : "Full cleanup failed"); } }; const handleValidateIntegrity = async () => { if (!user?.id) { setError("User not authenticated"); return; } setLoading(true, "Validating database integrity"); try { const validation = await dbCleanupUtils.validateDatabaseIntegrity( user.id, ); setState((prev) => ({ ...prev, lastCleanupResult: { type: "validation", result: validation }, isLoading: false, operationInProgress: null, error: null, })); } catch (error) { setError(error instanceof Error ? error.message : "Validation failed"); } }; if (!user) { return ( <div className="p-4 bg-yellow-50 border border-yellow-200 rounded-lg"> <p className="text-yellow-800"> Please log in to access database cleanup tools. </p> </div> ); } return ( <div className="space-y-6 p-6 bg-white rounded-lg shadow-md"> <div className="border-b pb-4"> <h2 className="text-2xl font-bold text-gray-900"> Database Cleanup & Integrity </h2> <p className="text-gray-600 mt-1"> Tools for maintaining database integrity and cleaning up orphaned data. </p> </div> {/* Action Buttons */} <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4"> <button onClick={handleGenerateReport} disabled={state.isLoading} className="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed" > {state.operationInProgress === "Generating integrity report" ? "Generating..." : "Generate Report"} </button> <button onClick={handleQuickCleanup} disabled={state.isLoading || isCleaningUp} className="px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed" > {state.operationInProgress === "Running quick cleanup" || isCleaningUp ? "Cleaning..." : "Quick Cleanup"} </button> <button onClick={handleFullCleanup} disabled={state.isLoading} className="px-4 py-2 bg-orange-600 text-white rounded-md hover:bg-orange-700 disabled:opacity-50 disabled:cursor-not-allowed" > {state.operationInProgress === "Running full cleanup" ? "Cleaning..." : "Full Cleanup"} </button> <button onClick={handleValidateIntegrity} disabled={state.isLoading} className="px-4 py-2 bg-purple-600 text-white rounded-md hover:bg-purple-700 disabled:opacity-50 disabled:cursor-not-allowed" > {state.operationInProgress === "Validating database integrity" ? "Validating..." : "Validate Integrity"} </button> </div> {/* Loading Indicator */} {state.isLoading && ( <div className="bg-blue-50 border border-blue-200 rounded-lg p-4"> <div className="flex items-center"> <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600 mr-2"></div> <span className="text-blue-800">{state.operationInProgress}</span> </div> </div> )} {/* Error Display */} {state.error && ( <div className="bg-red-50 border border-red-200 rounded-lg p-4"> <h3 className="text-red-800 font-medium">Error</h3> <p className="text-red-700 mt-1">{state.error}</p> <button onClick={() => setState((prev) => ({ ...prev, error: null }))} className="mt-2 text-sm text-red-600 hover:text-red-800 underline" > Dismiss </button> </div> )} {/* Integrity Report */} {state.lastReport && ( <div className="bg-gray-50 border border-gray-200 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 mb-3"> Data Integrity Report </h3> <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-4"> <div className="text-center"> <div className="text-2xl font-bold text-red-600"> {state.lastReport.orphanedReadingQueueItems} </div> <div className="text-sm text-gray-600">Orphaned Queue Items</div> </div> <div className="text-center"> <div className="text-2xl font-bold text-orange-600"> {state.lastReport.orphanedNewsletterTags} </div> <div className="text-sm text-gray-600">Orphaned Tags</div> </div> <div className="text-center"> <div className="text-2xl font-bold text-yellow-600"> {state.lastReport.orphanedNewsletterSources} </div> <div className="text-sm text-gray-600">Unused Sources</div> </div> <div className="text-center"> <div className={`text-2xl font-bold ${state.lastReport.totalIssues > 0 ? "text-red-600" : "text-green-600"}`} > {state.lastReport.totalIssues} </div> <div className="text-sm text-gray-600">Total Issues</div> </div> </div> <p className="text-xs text-gray-500"> Generated: {new Date(state.lastReport.timestamp).toLocaleString()} </p> </div> )} {/* Cleanup Results */} {state.lastCleanupResult && ( <div className="bg-green-50 border border-green-200 rounded-lg p-4"> <h3 className="text-lg font-medium text-green-900 mb-3"> {state.lastCleanupResult.type === "quick" && "Quick Cleanup Results"} {state.lastCleanupResult.type === "full" && "Full Cleanup Results"} {state.lastCleanupResult.type === "validation" && "Validation Results"} </h3> {state.lastCleanupResult.type === "quick" && ( <div> <p className="text-green-800"> Removed {state.lastCleanupResult.result.removedCount} orphaned reading queue items </p> </div> )} {state.lastCleanupResult.type === "full" && ( <div className="space-y-2"> <p className="text-green-800"> <strong>Reading Queue:</strong> Removed{" "} {state.lastCleanupResult.result.readingQueue.itemsRemoved} items </p> <p className="text-green-800"> <strong>Newsletter Tags:</strong> Removed{" "} {state.lastCleanupResult.result.newsletterTags.itemsRemoved}{" "} items </p> <p className="text-green-800"> <strong>Total:</strong>{" "} {state.lastCleanupResult.result.totalItemsRemoved} items removed </p> <p className="text-xs text-green-700"> Completed in{" "} {state.lastCleanupResult.result.duration.toFixed(2)}ms </p> {state.lastCleanupResult.result.totalErrors.length > 0 && ( <div className="mt-2 p-2 bg-red-100 rounded"> <p className="text-red-800 text-sm font-medium">Errors:</p> <ul className="text-red-700 text-sm mt-1"> {state.lastCleanupResult.result.totalErrors.map( (error: string, index: number) => ( <li key={index}>• {error}</li> ), )} </ul> </div> )} </div> )} {state.lastCleanupResult.type === "validation" && ( <div className="space-y-2"> <p className={`font-medium ${state.lastCleanupResult.result.isValid ? "text-green-800" : "text-red-800"}`} > Database Integrity:{" "} {state.lastCleanupResult.result.isValid ? "VALID" : "ISSUES FOUND"} </p> {state.lastCleanupResult.result.issues.length > 0 && ( <div> <p className="text-red-800 text-sm font-medium">Issues:</p> <ul className="text-red-700 text-sm mt-1"> {state.lastCleanupResult.result.issues.map( (issue: string, index: number) => ( <li key={index}>• {issue}</li> ), )} </ul> </div> )} {state.lastCleanupResult.result.recommendations.length > 0 && ( <div> <p className="text-orange-800 text-sm font-medium"> Recommendations: </p> <ul className="text-orange-700 text-sm mt-1"> {state.lastCleanupResult.result.recommendations.map( (rec: string, index: number) => ( <li key={index}>• {rec}</li> ), )} </ul> </div> )} </div> )} </div> )} {/* Help Text */} <div className="bg-blue-50 border border-blue-200 rounded-lg p-4"> <h3 className="text-blue-900 font-medium mb-2">About These Tools</h3> <ul className="text-blue-800 text-sm space-y-1"> <li> <strong>Generate Report:</strong> Analyzes your data for integrity issues without making changes </li> <li> <strong>Quick Cleanup:</strong> Removes orphaned reading queue items only </li> <li> <strong>Full Cleanup:</strong> Comprehensive cleanup of all orphaned data types </li> <li> <strong>Validate Integrity:</strong> Checks database constraints and provides recommendations </li> </ul> <p className="text-blue-700 text-xs mt-2"> <strong>Note:</strong> These operations are safe and only remove data that references deleted items. </p> </div> </div> ); }; export default DatabaseCleanupPanel; |