From 8aa0fc7fc24fccc0795a89ee43c270711f6acdfe Mon Sep 17 00:00:00 2001 From: Boy132 Date: Mon, 29 Sep 2025 15:26:17 +0200 Subject: [PATCH] Refresh page after file updates (#1759) Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com> --- .../Resources/Files/Pages/EditFiles.php | 22 +++----- .../Resources/Files/Pages/ListFiles.php | 50 +++++++++++++------ 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/app/Filament/Server/Resources/Files/Pages/EditFiles.php b/app/Filament/Server/Resources/Files/Pages/EditFiles.php index 16bd44dca..d5835338c 100644 --- a/app/Filament/Server/Resources/Files/Pages/EditFiles.php +++ b/app/Filament/Server/Resources/Files/Pages/EditFiles.php @@ -97,8 +97,7 @@ class EditFiles extends Page ->body(fn () => $this->path) ->send(); - $url = ListFiles::getUrl(['path' => dirname($this->path)]); - $this->redirect($url, FilamentView::hasSpaMode($url)); + $this->redirectToList(); }), Action::make('save') ->label(trans('server/file.actions.edit.save')) @@ -178,39 +177,34 @@ class EditFiles extends Page ->danger() ->closable() ->send(); - - $url = ListFiles::getUrl(['path' => dirname($this->path)]); - $this->redirect($url, FilamentView::hasSpaMode($url)); } catch (FileNotFoundException) { AlertBanner::make('file_not_found') ->title(trans('server/file.alerts.file_not_found.title', ['name' => basename($this->path)])) ->danger() ->closable() ->send(); - - $url = ListFiles::getUrl(['path' => dirname($this->path)]); - $this->redirect($url, FilamentView::hasSpaMode($url)); } catch (FileNotEditableException) { AlertBanner::make('file_is_directory') ->title(trans('server/file.alerts.file_not_found.title', ['name' => basename($this->path)])) ->danger() ->closable() ->send(); - - $url = ListFiles::getUrl(['path' => dirname($this->path)]); - $this->redirect($url, FilamentView::hasSpaMode($url)); } catch (ConnectionException) { // Alert banner for this one will be handled by ListFiles - - $url = ListFiles::getUrl(['path' => dirname($this->path)]); - $this->redirect($url, FilamentView::hasSpaMode($url)); } + $this->redirectToList(); }), ]) ->columnSpanFull(), ]); } + private function redirectToList(): void + { + $url = ListFiles::getUrl(['path' => dirname($this->path)]); + $this->redirect($url, FilamentView::hasSpaMode($url)); + } + public function mount(string $path): void { $this->authorizeAccess(); diff --git a/app/Filament/Server/Resources/Files/Pages/ListFiles.php b/app/Filament/Server/Resources/Files/Pages/ListFiles.php index dcecefaaa..4b7801141 100644 --- a/app/Filament/Server/Resources/Files/Pages/ListFiles.php +++ b/app/Filament/Server/Resources/Files/Pages/ListFiles.php @@ -36,6 +36,7 @@ use Filament\Schemas\Components\Tabs; use Filament\Schemas\Components\Tabs\Tab; use Filament\Schemas\Components\Utilities\Get; use Filament\Support\Enums\IconSize; +use Filament\Support\Facades\FilamentView; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Enums\PaginationMode; use Filament\Tables\Table; @@ -58,6 +59,11 @@ class ListFiles extends ListRecords private DaemonFileRepository $fileRepository; + public function getTitle(): string + { + return trans('server/file.title'); + } + public function getBreadcrumbs(): array { $resource = static::getResource(); @@ -83,15 +89,13 @@ class ListFiles extends ListRecords /** @var Server $server */ $server = Filament::getTenant(); - $files = File::get($server, $this->path); - return $table ->paginated([25, 50, 100, 150, 200]) ->paginationMode(PaginationMode::Simple) ->defaultPaginationPageOption(50) ->deferLoading() ->searchable() - ->query(fn () => $files->orderByDesc('is_directory')) + ->query(fn () => File::get($server, $this->path)->orderByDesc('is_directory')) ->defaultSort('name') ->columns([ TextColumn::make('name') @@ -161,6 +165,8 @@ class ListFiles extends ListRecords ->body(fn () => $file->name . ' -> ' . $data['name']) ->success() ->send(); + + $this->refreshPage(); }), Action::make('copy') ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_CREATE, $server)) @@ -179,7 +185,7 @@ class ListFiles extends ListRecords ->success() ->send(); - return redirect(ListFiles::getUrl(['path' => $this->path])); + $this->refreshPage(); }), Action::make('download') ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_READ_CONTENT, $server)) @@ -221,6 +227,8 @@ class ListFiles extends ListRecords ->body($oldLocation . ' -> ' . $newLocation) ->success() ->send(); + + $this->refreshPage(); }), Action::make('permissions') ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_UPDATE, $server)) @@ -309,7 +317,7 @@ class ListFiles extends ListRecords ->success() ->send(); - return redirect(ListFiles::getUrl(['path' => $this->path])); + $this->refreshPage(); }), Action::make('unarchive') ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_ARCHIVE, $server)) @@ -329,7 +337,7 @@ class ListFiles extends ListRecords ->success() ->send(); - return redirect(ListFiles::getUrl(['path' => $this->path])); + $this->refreshPage(); }), ])->iconSize(IconSize::Large), DeleteAction::make() @@ -346,6 +354,8 @@ class ListFiles extends ListRecords ->property('directory', $this->path) ->property('files', $file->name) ->log(); + + $this->refreshPage(); }), ]) ->toolbarActions([ @@ -376,6 +386,8 @@ class ListFiles extends ListRecords ->title(trans('server/file.actions.move.bulk_notification', ['count' => count($files), 'directory' => resolve_path(join_paths($this->path, $location))])) ->success() ->send(); + + $this->refreshPage(); }), BulkAction::make('archive') ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_ARCHIVE, $server)) @@ -402,7 +414,7 @@ class ListFiles extends ListRecords ->success() ->send(); - return redirect(ListFiles::getUrl(['path' => $this->path])); + $this->refreshPage(); }), DeleteBulkAction::make() ->authorize(fn () => auth()->user()->can(Permission::ACTION_FILE_DELETE, $server)) @@ -419,6 +431,8 @@ class ListFiles extends ListRecords ->title(trans('server/file.actions.delete.bulk_notification', ['count' => count($files)])) ->success() ->send(); + + $this->refreshPage(); }), ]), @@ -436,6 +450,8 @@ class ListFiles extends ListRecords Activity::event('server:file.write') ->property('file', $path) ->log(); + + $this->refreshPage(); } catch (FileExistsException) { AlertBanner::make('file_already_exists') ->title(trans('server/file.alerts.file_already_exists.title', ['name' => $path])) @@ -443,7 +459,7 @@ class ListFiles extends ListRecords ->closable() ->send(); - $this->redirect(self::getUrl(['path' => dirname($path)])); + $this->refreshPage(true); } }) ->schema([ @@ -465,15 +481,18 @@ class ListFiles extends ListRecords Activity::event('server:file.create-directory') ->property(['directory' => $this->path, 'name' => $data['name']]) ->log(); + + $this->refreshPage(); } catch (FileExistsException) { $path = join_paths($this->path, $data['name']); + AlertBanner::make('folder_already_exists') ->title(trans('server/file.alerts.file_already_exists.title', ['name' => $path])) ->danger() ->closable() ->send(); - $this->redirect(self::getUrl(['path' => dirname($path)])); + $this->refreshPage(true); } }) ->schema([ @@ -506,7 +525,7 @@ class ListFiles extends ListRecords ->log(); } - return redirect(ListFiles::getUrl(['path' => $this->path])); + $this->refreshPage(); }) ->schema([ Tabs::make() @@ -557,6 +576,12 @@ class ListFiles extends ListRecords ]); } + private function refreshPage(bool $oneBack = false): void + { + $url = self::getUrl(['path' => $oneBack ? dirname($this->path) : $this->path]); + $this->redirect($url, FilamentView::hasSpaMode($url)); + } + /** * @return string[] */ @@ -593,9 +618,4 @@ class ListFiles extends ListRecords ->where('path', '.*'), ); } - - public function getTitle(): string - { - return trans('server/file.title'); - } }