Skip to content

Commit

Permalink
Fix power buttons (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
lancepioch authored Dec 8, 2024
1 parent 5d2b892 commit 839ff96
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app/Filament/Server/Pages/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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 [
Expand Down Expand Up @@ -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 */
Expand All @@ -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()
Expand All @@ -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),
];
}
}
2 changes: 2 additions & 0 deletions resources/views/filament/components/server-console.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 839ff96

Please sign in to comment.