|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart' as material; |
| 4 | +import 'package:querya_desktop/app/app.dart'; |
| 5 | +import 'package:querya_desktop/core/storage/app_data_root.dart'; |
| 6 | +import 'package:querya_desktop/core/storage/local_db.dart'; |
| 7 | +import 'package:querya_desktop/core/theme/theme_controller.dart'; |
| 8 | +import 'package:querya_desktop/core/ui/querya_icons.dart'; |
| 9 | +import 'package:querya_desktop/shared/widgets/widgets.dart'; |
| 10 | + |
| 11 | +/// Disaster recovery UI launched when LocalDb fails to open or migrate on startup. |
| 12 | +/// Allows the user to inspect the error, retry, remove stale locks, backup the database, |
| 13 | +/// or launch in Safe Mode. |
| 14 | +class QueryaStartupRecoveryApp extends material.StatefulWidget { |
| 15 | + const QueryaStartupRecoveryApp({ |
| 16 | + super.key, |
| 17 | + required this.error, |
| 18 | + this.stackTrace, |
| 19 | + this.onRetrySuccess, |
| 20 | + }); |
| 21 | + |
| 22 | + final Object error; |
| 23 | + final StackTrace? stackTrace; |
| 24 | + final material.VoidCallback? onRetrySuccess; |
| 25 | + |
| 26 | + @override |
| 27 | + material.State<QueryaStartupRecoveryApp> createState() => |
| 28 | + _QueryaStartupRecoveryAppState(); |
| 29 | +} |
| 30 | + |
| 31 | +class _QueryaStartupRecoveryAppState |
| 32 | + extends material.State<QueryaStartupRecoveryApp> { |
| 33 | + bool _isBusy = false; |
| 34 | + String? _statusMessage; |
| 35 | + bool _isErrorStatus = false; |
| 36 | + |
| 37 | + void _setStatus(String message, {bool isError = false}) { |
| 38 | + setState(() { |
| 39 | + _statusMessage = message; |
| 40 | + _isErrorStatus = isError; |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + Future<void> _handleRetry() async { |
| 45 | + setState(() => _isBusy = true); |
| 46 | + _setStatus('Attempting to open database...'); |
| 47 | + try { |
| 48 | + await LocalDb.instance.open(); |
| 49 | + _setStatus('Database opened successfully!'); |
| 50 | + if (mounted) { |
| 51 | + if (widget.onRetrySuccess != null) { |
| 52 | + widget.onRetrySuccess!(); |
| 53 | + } else { |
| 54 | + material.runApp(const QueryaApp()); |
| 55 | + } |
| 56 | + } |
| 57 | + } catch (e) { |
| 58 | + _setStatus('Retry failed: $e', isError: true); |
| 59 | + } finally { |
| 60 | + if (mounted) setState(() => _isBusy = false); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + Future<void> _handleUnlockWal() async { |
| 65 | + setState(() => _isBusy = true); |
| 66 | + _setStatus('Removing WAL and lock files...'); |
| 67 | + try { |
| 68 | + await LocalDb.instance.removeWalAndLockFiles(); |
| 69 | + _setStatus('Stale lock and WAL files removed. Testing database connection...'); |
| 70 | + await LocalDb.instance.open(); |
| 71 | + _setStatus('Database opened successfully!'); |
| 72 | + if (mounted) { |
| 73 | + if (widget.onRetrySuccess != null) { |
| 74 | + widget.onRetrySuccess!(); |
| 75 | + } else { |
| 76 | + material.runApp(const QueryaApp()); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (e) { |
| 80 | + _setStatus('Unlock attempted, but database failed to open: $e', |
| 81 | + isError: true); |
| 82 | + } finally { |
| 83 | + if (mounted) setState(() => _isBusy = false); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Future<void> _handleBackup() async { |
| 88 | + setState(() => _isBusy = true); |
| 89 | + _setStatus('Creating timestamped backup...'); |
| 90 | + try { |
| 91 | + final backupPath = await LocalDb.instance.backupDatabaseFile(); |
| 92 | + _setStatus('Backup saved to: $backupPath'); |
| 93 | + } catch (e) { |
| 94 | + _setStatus('Backup failed: $e', isError: true); |
| 95 | + } finally { |
| 96 | + if (mounted) setState(() => _isBusy = false); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + Future<void> _handleSafeMode() async { |
| 101 | + setState(() => _isBusy = true); |
| 102 | + _setStatus('Switching to clean safe profile...'); |
| 103 | + try { |
| 104 | + await LocalDb.instance.close(); |
| 105 | + final tempDir = await Directory.systemTemp.createTemp('querya_safe_mode_'); |
| 106 | + AppDataRoot.setSafeModeRootPath(tempDir.path); |
| 107 | + await LocalDb.instance.open(); |
| 108 | + _setStatus('Safe mode ready. Launching Querya...'); |
| 109 | + if (mounted) { |
| 110 | + if (widget.onRetrySuccess != null) { |
| 111 | + widget.onRetrySuccess!(); |
| 112 | + } else { |
| 113 | + material.runApp(const QueryaApp()); |
| 114 | + } |
| 115 | + } |
| 116 | + } catch (e) { |
| 117 | + _setStatus('Failed to enter safe mode: $e', isError: true); |
| 118 | + } finally { |
| 119 | + if (mounted) setState(() => _isBusy = false); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @override |
| 124 | + material.Widget build(material.BuildContext context) { |
| 125 | + final themeData = ThemeController.instance.darkShadcnTheme; |
| 126 | + final colorScheme = themeData.colorScheme; |
| 127 | + |
| 128 | + return ShadcnApp( |
| 129 | + title: 'Querya - Startup Recovery', |
| 130 | + theme: ThemeController.instance.darkShadcnTheme, |
| 131 | + darkTheme: ThemeController.instance.darkShadcnTheme, |
| 132 | + themeMode: ThemeMode.dark, |
| 133 | + debugShowCheckedModeBanner: false, |
| 134 | + home: material.Scaffold( |
| 135 | + backgroundColor: colorScheme.background, |
| 136 | + body: material.Center( |
| 137 | + child: material.SingleChildScrollView( |
| 138 | + padding: const material.EdgeInsets.all(32), |
| 139 | + child: material.ConstrainedBox( |
| 140 | + constraints: const material.BoxConstraints(maxWidth: 640), |
| 141 | + child: QueryaDialogCard( |
| 142 | + borderColor: colorScheme.destructive, |
| 143 | + child: material.Padding( |
| 144 | + padding: const material.EdgeInsets.all(28), |
| 145 | + child: material.Column( |
| 146 | + mainAxisSize: material.MainAxisSize.min, |
| 147 | + crossAxisAlignment: material.CrossAxisAlignment.start, |
| 148 | + children: [ |
| 149 | + material.Row( |
| 150 | + children: [ |
| 151 | + material.Icon( |
| 152 | + QueryaIcons.treeError, |
| 153 | + size: 32, |
| 154 | + color: colorScheme.destructive, |
| 155 | + ), |
| 156 | + const material.SizedBox(width: 12), |
| 157 | + const material.Expanded( |
| 158 | + child: Text( |
| 159 | + 'Database Recovery', |
| 160 | + ), |
| 161 | + ), |
| 162 | + ], |
| 163 | + ), |
| 164 | + const material.SizedBox(height: 16), |
| 165 | + const Text( |
| 166 | + 'Querya could not open the profile database (querya.db) on startup. ' |
| 167 | + 'This can happen after a crash, concurrent lock, file permission conflict, ' |
| 168 | + 'or SQLite database corruption.', |
| 169 | + ).muted(), |
| 170 | + const material.SizedBox(height: 16), |
| 171 | + material.Container( |
| 172 | + width: double.infinity, |
| 173 | + padding: const material.EdgeInsets.all(12), |
| 174 | + decoration: material.BoxDecoration( |
| 175 | + color: colorScheme.muted.withOpacity(0.3), |
| 176 | + borderRadius: material.BorderRadius.circular(6), |
| 177 | + border: material.Border.all( |
| 178 | + color: colorScheme.border, |
| 179 | + ), |
| 180 | + ), |
| 181 | + child: material.SelectableText( |
| 182 | + widget.error.toString(), |
| 183 | + style: const material.TextStyle( |
| 184 | + fontFamily: 'monospace', |
| 185 | + fontSize: 12, |
| 186 | + ), |
| 187 | + ), |
| 188 | + ), |
| 189 | + if (_statusMessage != null) ...[ |
| 190 | + const material.SizedBox(height: 16), |
| 191 | + material.Container( |
| 192 | + width: double.infinity, |
| 193 | + padding: const material.EdgeInsets.all(10), |
| 194 | + decoration: material.BoxDecoration( |
| 195 | + color: (_isErrorStatus |
| 196 | + ? colorScheme.destructive |
| 197 | + : colorScheme.primary) |
| 198 | + .withOpacity(0.15), |
| 199 | + borderRadius: material.BorderRadius.circular(6), |
| 200 | + ), |
| 201 | + child: Text( |
| 202 | + _statusMessage!, |
| 203 | + style: material.TextStyle( |
| 204 | + color: _isErrorStatus |
| 205 | + ? colorScheme.destructive |
| 206 | + : colorScheme.primary, |
| 207 | + fontSize: 13, |
| 208 | + fontWeight: material.FontWeight.w500, |
| 209 | + ), |
| 210 | + ), |
| 211 | + ), |
| 212 | + ], |
| 213 | + const material.SizedBox(height: 24), |
| 214 | + material.Wrap( |
| 215 | + spacing: 12, |
| 216 | + runSpacing: 12, |
| 217 | + children: [ |
| 218 | + PrimaryButton( |
| 219 | + onPressed: _isBusy ? null : _handleRetry, |
| 220 | + child: const Text('Retry Opening'), |
| 221 | + ), |
| 222 | + OutlineButton( |
| 223 | + onPressed: _isBusy ? null : _handleUnlockWal, |
| 224 | + child: const Text('Unlock Database (Clear WAL)'), |
| 225 | + ), |
| 226 | + OutlineButton( |
| 227 | + onPressed: _isBusy ? null : _handleBackup, |
| 228 | + child: const Text('Export / Backup querya.db'), |
| 229 | + ), |
| 230 | + GhostButton( |
| 231 | + onPressed: _isBusy ? null : _handleSafeMode, |
| 232 | + child: const Text('Start in Safe Mode'), |
| 233 | + ), |
| 234 | + ], |
| 235 | + ), |
| 236 | + ], |
| 237 | + ), |
| 238 | + ), |
| 239 | + ), |
| 240 | + ), |
| 241 | + ), |
| 242 | + ), |
| 243 | + ), |
| 244 | + ); |
| 245 | + } |
| 246 | +} |
0 commit comments