Files
pipelines/app/lib/features/chat/files/files_panel.dart
T
alan b8fb4d350c Upload files into the shown folder from the chat Files panel
The header gains an Upload button that uses the host file picker (disabled
with its reason where no picker exists), and the panel body becomes a drop
target: on web, document drag events are observed through a dart:js_interop
seam and a file drag over the panel shows a highlight naming the folder.
Each file is posted to POST /files?path=<folder> through an injectable
transport; refusals (exists, sign in, too large) stay visible until dismissed
and the folder reloads afterwards. The list/grid builders move to
files_entries.dart to keep the panel small.
2026-11-09 00:09:01 +00:00

206 lines
6.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../../../ui/adaptive/host.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'files_controller.dart';
import 'files_drop.dart';
import 'files_drop_area.dart';
import 'files_entries.dart';
import 'files_upload_notice.dart';
import 'files_viewer.dart';
import 'files_header.dart';
import '../../../ui/layout/app_overflow_menu.dart';
const double filesPanelMinWidth = 280;
const double filesPanelMaxWidth = 420;
const double filesPanelFraction = .30;
double filesPanelWidth(double available) => (available * filesPanelFraction)
.clamp(filesPanelMinWidth, filesPanelMaxWidth);
/// The two explicit actions of a file's row or tile menu.
enum FileAction { open, download }
class FilesPanel extends StatefulWidget {
const FilesPanel({
super.key,
required this.controller,
this.onOpen,
this.viewer = const FilesViewer(),
this.picker,
this.drop,
});
final FilesController controller;
final ValueChanged<DirectoryEntry>? onOpen;
/// Opens and downloads files; tests inject one that never fetches.
final FilesViewer viewer;
/// The Upload button's file picker and the host drag-and-drop seam; null
/// means the running host's. Tests inject fakes.
final ChatAttachmentPicker? picker;
final FilesDrop? drop;
@override
State<FilesPanel> createState() => _FilesPanelState();
}
class _FilesPanelState extends State<FilesPanel> {
late final TextEditingController _filter = TextEditingController();
late final ChatAttachmentPicker _picker =
widget.picker ?? hostChatAttachmentPicker;
late final FilesDrop _drop = widget.drop ?? hostFilesDrop;
FilesController get controller => widget.controller;
String get _endpoint => configuredGatewayEndpoint(controller.gateway);
Future<void> _pickAndUpload() async {
final List<ChatPickedFile>? files;
try {
files = await _picker.pick();
} catch (error) {
controller.reportUploadError('$error');
return;
}
if (files == null || files.isEmpty) return;
await controller.upload(files);
}
@override
void initState() {
super.initState();
controller.addListener(_changed);
controller.start();
}
void _changed() {
if (mounted) {
if (_filter.text != controller.filter) _filter.text = controller.filter;
setState(() {});
}
}
@override
void dispose() {
controller.removeListener(_changed);
_filter.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final listing = controller.listing;
final entries = controller.entries;
if (controller.missingDirectory) {
return const _FilesMessage('This chat has no working directory.');
}
return Theme(
data: usesCupertinoHost(context)
? ThemeData(
brightness: CupertinoTheme.brightnessOf(context),
colorSchemeSeed: CupertinoTheme.of(context).primaryColor,
)
: Theme.of(context),
child: Builder(
builder: (context) => Material(
color: Theme.of(context).colorScheme.surface,
child: FilesDropArea(
drop: _drop,
folderLabel: listing == null
? null
: homeRelativeLabel(listing.home, listing.path),
onFiles: controller.upload,
onError: (error) => controller.reportUploadError(
'A dropped item could not be read: $error',
),
child: Column(
children: [
FilesHeader(
controller: controller,
filter: _filter,
picker: _picker,
onUpload: _pickAndUpload,
),
if (controller.error != null)
ChatErrorNotice(
message: controller.error!,
onRetry: controller.retry,
),
FilesUploadNotice(controller: controller),
if (controller.loading && listing == null)
const Expanded(child: ChatLoadingIndicator())
else if (listing != null &&
entries.isEmpty &&
!controller.loading)
Expanded(
child: _FilesMessage(
controller.filter.isEmpty
? 'This folder is empty.'
: 'No files match your filter.',
),
)
else
Expanded(
child: FilesEntries(
entries: entries,
view: controller.view,
endpoint: _endpoint,
onTap: (entry) => entry.isDir
? controller.open(entry.path)
: _open(entry),
menu: _menu,
),
),
if (listing?.truncated == true)
const Padding(
padding: EdgeInsets.all(8),
child: Text('Some entries are not shown.'),
),
],
),
),
),
),
);
}
/// Open and Download for one file; a folder has no menu (nothing to save).
Widget _menu(DirectoryEntry entry) => AppOverflowMenu<FileAction>(
label: 'File actions',
sheetTitle: entry.name,
onSelected: (_, action) => switch (action) {
FileAction.open => _open(entry),
FileAction.download => widget.viewer.download(
context,
endpoint: _endpoint,
entry: entry,
),
},
actions: const [
AppMenuAction(value: FileAction.open, label: 'Open'),
AppMenuAction(value: FileAction.download, label: 'Download'),
],
);
void _open(DirectoryEntry entry) {
if (widget.onOpen != null) {
widget.onOpen!(entry);
} else {
widget.viewer.open(context, endpoint: _endpoint, entry: entry);
}
}
}
class _FilesMessage extends StatelessWidget {
const _FilesMessage(this.text);
final String text;
@override
Widget build(BuildContext context) => Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(text, textAlign: TextAlign.center),
),
);
}