Add error handling for organization loading in HomePage

This commit is contained in:
Leon Bösche
2026-01-09 23:14:45 +01:00
parent aac6d2eb46
commit 708d4ca790
2 changed files with 391 additions and 356 deletions

View File

@@ -12,6 +12,7 @@ import '../blocs/permission/permission_event.dart';
import '../blocs/permission/permission_state.dart';
import '../blocs/upload/upload_bloc.dart';
import '../blocs/upload/upload_event.dart';
import '../blocs/upload/upload_state.dart';
import '../models/file_item.dart';
import '../theme/app_theme.dart';
import '../theme/modern_glass_button.dart';
@@ -661,7 +662,22 @@ class _FileExplorerState extends State<FileExplorer> {
@override
Widget build(BuildContext context) {
return BlocBuilder<FileBrowserBloc, FileBrowserState>(
return BlocListener<UploadBloc, UploadState>(
listener: (context, uploadState) {
if (uploadState is UploadInProgress) {
final hasCompleted = uploadState.uploads.any((u) => u.isCompleted);
if (hasCompleted) {
final fbState = context.read<FileBrowserBloc>().state;
String currentPath = '/';
if (fbState is DirectoryLoaded) currentPath = fbState.currentPath;
if (fbState is DirectoryEmpty) currentPath = fbState.currentPath;
context.read<FileBrowserBloc>().add(
LoadDirectory(orgId: widget.orgId, path: currentPath),
);
}
}
},
child: BlocBuilder<FileBrowserBloc, FileBrowserState>(
builder: (context, state) {
if (state is DirectoryLoading) {
return Center(
@@ -705,13 +721,14 @@ class _FileExplorerState extends State<FileExplorer> {
ModernGlassButton(
onPressed: () async {
final result = await FilePicker.platform
.pickFiles();
.pickFiles(withData: true);
if (result != null && result.files.isNotEmpty) {
final files = result.files
.map(
(file) => FileItem(
name: file.name,
path: '/${file.name}',
// Parent path only; server uses filename from multipart
path: state.currentPath,
type: FileType.file,
size: file.size,
lastModified: DateTime.now(),
@@ -723,7 +740,7 @@ class _FileExplorerState extends State<FileExplorer> {
context.read<UploadBloc>().add(
StartUpload(
files: files,
targetPath: '/',
targetPath: state.currentPath,
orgId: widget.orgId,
),
);
@@ -740,10 +757,10 @@ class _FileExplorerState extends State<FileExplorer> {
const SizedBox(width: 16),
ModernGlassButton(
onPressed: () async {
final folderName = await _showCreateFolderDialog(
context,
);
if (folderName != null && folderName.isNotEmpty) {
final folderName =
await _showCreateFolderDialog(context);
if (folderName != null &&
folderName.isNotEmpty) {
context.read<FileBrowserBloc>().add(
CreateFolder(
orgId: widget.orgId,
@@ -780,7 +797,10 @@ class _FileExplorerState extends State<FileExplorer> {
onPressed: () {
final parentPath = _getParentPath(state.currentPath);
context.read<FileBrowserBloc>().add(
LoadDirectory(orgId: widget.orgId, path: parentPath),
LoadDirectory(
orgId: widget.orgId,
path: parentPath,
),
);
},
),
@@ -873,23 +893,26 @@ class _FileExplorerState extends State<FileExplorer> {
ModernGlassButton(
onPressed: () async {
final result = await FilePicker.platform
.pickFiles();
.pickFiles(withData: true);
if (result != null && result.files.isNotEmpty) {
final files = result.files
.map(
(file) => FileItem(
name: file.name,
path: '/${file.name}',
// Parent path only; server uses filename from multipart
path: state.currentPath,
type: FileType.file,
size: file.size,
lastModified: DateTime.now(),
localPath: file.path,
bytes: file.bytes,
),
)
.toList();
context.read<UploadBloc>().add(
StartUpload(
files: files,
targetPath: '/',
targetPath: state.currentPath,
orgId: widget.orgId,
),
);
@@ -906,10 +929,10 @@ class _FileExplorerState extends State<FileExplorer> {
const SizedBox(width: 16),
ModernGlassButton(
onPressed: () async {
final folderName = await _showCreateFolderDialog(
context,
);
if (folderName != null && folderName.isNotEmpty) {
final folderName =
await _showCreateFolderDialog(context);
if (folderName != null &&
folderName.isNotEmpty) {
context.read<FileBrowserBloc>().add(
CreateFolder(
orgId: widget.orgId,
@@ -1048,6 +1071,7 @@ class _FileExplorerState extends State<FileExplorer> {
return const SizedBox.shrink();
},
),
);
}

View File

@@ -345,6 +345,17 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
>(
listener: (context, state) {
if (state is OrganizationLoaded) {
// Show errors if present
if (state.error != null &&
state.error!.isNotEmpty) {
ScaffoldMessenger.of(
context,
).showSnackBar(
SnackBar(
content: Text(state.error!),
),
);
}
final orgId =
state.selectedOrg?.id ?? '';
// Reload file browser when org changes (or when falling back to personal workspace)