From 99ce691e0811db5b89cee5f413286a3cb754be01 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 8 Dec 2024 16:09:34 -0500 Subject: [PATCH] Fix power buttons --- app/Filament/Server/Pages/Console.php | 24 +++++++++++++++---- .../components/server-console.blade.php | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/Filament/Server/Pages/Console.php b/app/Filament/Server/Pages/Console.php index 44946c9e4a..4ae888c9b2 100644 --- a/app/Filament/Server/Pages/Console.php +++ b/app/Filament/Server/Pages/Console.php @@ -2,6 +2,7 @@ namespace App\Filament\Server\Pages; +use App\Enums\ContainerStatus; use App\Filament\Server\Widgets\ServerConsole; use App\Filament\Server\Widgets\ServerCpuChart; use App\Filament\Server\Widgets\ServerMemoryChart; @@ -12,6 +13,7 @@ use Filament\Facades\Filament; use Filament\Pages\Page; use Filament\Support\Enums\ActionSize; +use Livewire\Attributes\On; class Console extends Page { @@ -21,6 +23,8 @@ class Console extends Page protected static string $view = 'filament.server.pages.console'; + public ContainerStatus $status = ContainerStatus::Missing; + public function getWidgetData(): array { return [ @@ -50,6 +54,16 @@ public function getColumns(): int|string|array return 3; } + #[On('powerChanged')] + public function powerChanged(string $state): void + { + $this->status = ContainerStatus::from($state); + + $this->cachedHeaderActions = []; + + $this->cacheHeaderActions(); + } + protected function getHeaderActions(): array { /** @var Server $server */ @@ -60,17 +74,18 @@ protected function getHeaderActions(): array ->color('primary') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'start')) - ->disabled(fn () => $server->isInConflictState()), + ->disabled(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Running, ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting])), Action::make('restart') ->color('gray') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'restart')) - ->disabled(fn () => $server->isInConflictState() || $server->retrieveStatus() == 'offline'), + ->disabled(fn () => $server->isInConflictState() || $this->status !== ContainerStatus::Running), Action::make('stop') ->color('danger') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'stop')) - ->disabled(fn () => $server->isInConflictState() || $server->retrieveStatus() == 'offline'), + ->hidden(fn () => in_array($this->status, [ContainerStatus::Stopping, ContainerStatus::Restarting, ContainerStatus::Starting])) + ->disabled(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Starting, ContainerStatus::Stopping, ContainerStatus::Restarting, ContainerStatus::Exited, ContainerStatus::Offline])), Action::make('kill') ->color('danger') ->requiresConfirmation() @@ -79,7 +94,8 @@ protected function getHeaderActions(): array ->modalSubmitActionLabel('Kill Server') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'kill')) - ->disabled(fn () => $server->isInConflictState() || $server->retrieveStatus() == 'offline'), + ->hidden(fn () => $server->isInConflictState() || in_array($this->status, [ContainerStatus::Running, ContainerStatus::Restarting, ContainerStatus::Offline, ContainerStatus::Removing, ContainerStatus::Dead, ContainerStatus::Exited, ContainerStatus::Created])) + ->disabled(fn () => $server->isInConflictState() || $this->status === ContainerStatus::Offline), ]; } } diff --git a/resources/views/filament/components/server-console.blade.php b/resources/views/filament/components/server-console.blade.php index 80b9b9759a..6d3c99f097 100644 --- a/resources/views/filament/components/server-console.blade.php +++ b/resources/views/filament/components/server-console.blade.php @@ -125,6 +125,8 @@ class="w-full focus:outline-none focus:ring-0 border-none" break; case 'status': handlePowerChangeEvent(args[0]); + + $wire.dispatch('powerChanged', {state: args[0]}) break; case 'transfer status': handleTransferStatus(args[0]);