From 71225bd2dc716e772052430f3b5d96b21b56a60b Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 31 Jul 2025 23:54:53 +0200 Subject: [PATCH] Refactor `AlertBanner` to be `ViewComponent` (#1555) --- .../FileResource/Pages/EditFiles.php | 15 ++----- .../FileResource/Pages/ListFiles.php | 4 +- app/Livewire/AlertBanner.php | 44 ++++++++++++------- app/Livewire/AlertBannerContainer.php | 15 ++++--- app/Models/File.php | 2 +- .../alerts/alert-banner-container.blade.php | 4 +- .../livewire/alerts/alert-banner.blade.php | 16 +++---- 7 files changed, 50 insertions(+), 50 deletions(-) diff --git a/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php b/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php index faec69c34..089d895db 100644 --- a/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php +++ b/app/Filament/Server/Resources/FileResource/Pages/EditFiles.php @@ -133,7 +133,7 @@ class EditFiles extends Page try { return $this->getDaemonFileRepository()->getContent($this->path, config('panel.files.max_edit_size')); } catch (FileSizeTooLargeException) { - AlertBanner::make() + AlertBanner::make('file_too_large') ->title('' . basename($this->path) . ' is too large!') ->body('Max is ' . convert_bytes_to_readable(config('panel.files.max_edit_size'))) ->danger() @@ -142,7 +142,7 @@ class EditFiles extends Page $this->redirect(ListFiles::getUrl(['path' => dirname($this->path)])); } catch (FileNotFoundException) { - AlertBanner::make() + AlertBanner::make('file_not_found') ->title('' . basename($this->path) . ' not found!') ->danger() ->closable() @@ -150,7 +150,7 @@ class EditFiles extends Page $this->redirect(ListFiles::getUrl(['path' => dirname($this->path)])); } catch (FileNotEditableException) { - AlertBanner::make() + AlertBanner::make('file_is_directory') ->title('' . basename($this->path) . ' is a directory') ->danger() ->closable() @@ -184,15 +184,6 @@ class EditFiles extends Page ->info() ->closable() ->send(); - - try { - $this->getDaemonFileRepository()->getDirectory('/'); - } catch (ConnectionException) { - AlertBanner::make('node_connection_error') - ->title('Could not connect to the node!') - ->danger() - ->send(); - } } } diff --git a/app/Filament/Server/Resources/FileResource/Pages/ListFiles.php b/app/Filament/Server/Resources/FileResource/Pages/ListFiles.php index d2473b235..6dc448188 100644 --- a/app/Filament/Server/Resources/FileResource/Pages/ListFiles.php +++ b/app/Filament/Server/Resources/FileResource/Pages/ListFiles.php @@ -430,7 +430,7 @@ class ListFiles extends ListRecords ->property('file', join_paths($path, $data['name'])) ->log(); } catch (FileExistsException) { - AlertBanner::make() + AlertBanner::make('file_already_exists') ->title('' . $path . ' already exists!') ->danger() ->closable() @@ -471,7 +471,7 @@ class ListFiles extends ListRecords ->log(); } catch (FileExistsException) { $path = join_paths($this->path, $data['name']); - AlertBanner::make() + AlertBanner::make('folder_already_exists') ->title('' . $path . ' already exists!') ->danger() ->closable() diff --git a/app/Livewire/AlertBanner.php b/app/Livewire/AlertBanner.php index 773046081..813012945 100644 --- a/app/Livewire/AlertBanner.php +++ b/app/Livewire/AlertBanner.php @@ -4,25 +4,32 @@ namespace App\Livewire; use Closure; use Filament\Notifications\Concerns; -use Filament\Support\Concerns\EvaluatesClosures; -use Illuminate\Support\Str; -use Livewire\Wireable; +use Filament\Support\Components\ViewComponent; +use Illuminate\Contracts\Support\Arrayable; -final class AlertBanner implements Wireable +final class AlertBanner extends ViewComponent implements Arrayable { use Concerns\HasBody; use Concerns\HasIcon; use Concerns\HasId; use Concerns\HasStatus; use Concerns\HasTitle; - use EvaluatesClosures; protected bool|Closure $closable = false; - public static function make(?string $id = null): AlertBanner + protected string $view = 'livewire.alerts.alert-banner'; + + protected string $viewIdentifier = 'alert-banner'; + + public function __construct(string $id) { - $static = new self(); - $static->id($id ?? Str::orderedUuid()); + $this->id($id); + } + + public static function make(string $id): AlertBanner + { + $static = new self($id); + $static->configure(); return $static; } @@ -30,7 +37,7 @@ final class AlertBanner implements Wireable /** * @return array{id: string, title: ?string, body: ?string, status: ?string, icon: ?string, closeable: bool} */ - public function toLivewire(): array + public function toArray(): array { return [ 'id' => $this->getId(), @@ -42,15 +49,18 @@ final class AlertBanner implements Wireable ]; } - public static function fromLivewire(mixed $value): AlertBanner + /** + * @param array{id: string, title: ?string, body: ?string, status: ?string, icon: ?string, closeable: bool} $data + */ + public static function fromArray(array $data): AlertBanner { - $static = AlertBanner::make($value['id']); + $static = AlertBanner::make($data['id']); - $static->title($value['title']); - $static->body($value['body']); - $static->status($value['status']); - $static->icon($value['icon']); - $static->closable($value['closeable']); + $static->title($data['title']); + $static->body($data['body']); + $static->status($data['status']); + $static->icon($data['icon']); + $static->closable($data['closeable']); return $static; } @@ -69,7 +79,7 @@ final class AlertBanner implements Wireable public function send(): AlertBanner { - session()->push('alert-banners', $this->toLivewire()); + session()->push('alert-banners', $this->toArray()); return $this; } diff --git a/app/Livewire/AlertBannerContainer.php b/app/Livewire/AlertBannerContainer.php index d5d7bf60c..66dba002d 100644 --- a/app/Livewire/AlertBannerContainer.php +++ b/app/Livewire/AlertBannerContainer.php @@ -2,18 +2,18 @@ namespace App\Livewire; +use Filament\Notifications\Collection; use Illuminate\Contracts\View\View; use Livewire\Attributes\On; use Livewire\Component; class AlertBannerContainer extends Component { - /** @var array */ - public array $alertBanners; + public Collection $alertBanners; public function mount(): void { - $this->alertBanners = []; + $this->alertBanners = new Collection(); $this->pullFromSession(); } @@ -21,15 +21,16 @@ class AlertBannerContainer extends Component public function pullFromSession(): void { foreach (session()->pull('alert-banners', []) as $alertBanner) { - $alertBanner = AlertBanner::fromLivewire($alertBanner); - $this->alertBanners[$alertBanner->getId()] = $alertBanner; + $alertBanner = AlertBanner::fromArray($alertBanner); + $this->alertBanners->put($alertBanner->getId(), $alertBanner); } } public function remove(string $id): void { - $alertBanners = &$this->alertBanners; - unset($alertBanners[$id]); + if ($this->alertBanners->has($id)) { + $this->alertBanners->forget($id); + } } public function render(): View diff --git a/app/Models/File.php b/app/Models/File.php index b8d9cfef2..759f5e15b 100644 --- a/app/Models/File.php +++ b/app/Models/File.php @@ -194,7 +194,7 @@ class File extends Model $message = str('Node connection failed'); } - AlertBanner::make() + AlertBanner::make('files_node_error') ->title('Could not load files!') ->body($message->toString()) ->danger() diff --git a/resources/views/livewire/alerts/alert-banner-container.blade.php b/resources/views/livewire/alerts/alert-banner-container.blade.php index 3d1f34d3f..6ebd6b62b 100644 --- a/resources/views/livewire/alerts/alert-banner-container.blade.php +++ b/resources/views/livewire/alerts/alert-banner-container.blade.php @@ -1,5 +1,5 @@
- @foreach (array_values($alertBanners) as $alertBanner) - @include('livewire.alerts.alert-banner', ['alertBanner' => $alertBanner]) + @foreach ($alertBanners as $alertBanner) + {{ $alertBanner }} @endforeach
diff --git a/resources/views/livewire/alerts/alert-banner.blade.php b/resources/views/livewire/alerts/alert-banner.blade.php index 68bae2706..136a7cf2b 100644 --- a/resources/views/livewire/alerts/alert-banner.blade.php +++ b/resources/views/livewire/alerts/alert-banner.blade.php @@ -1,14 +1,12 @@ -@props(['alertBanner']) - @php - $icon = $alertBanner->getIcon(); - $title = $alertBanner->getTitle(); - $body = $alertBanner->getBody(); + $icon = $getIcon(); + $title = $getTitle(); + $body = $getBody(); @endphp -
+
@if (filled($icon)) - + @endif
@@ -21,7 +19,7 @@ @endif
- @if ($alertBanner->isCloseable()) - + @if ($isCloseable()) + @endif