Fix powerActions visible while loading (#1708)

This commit is contained in:
MartinOscar 2025-09-18 16:22:23 +02:00 committed by GitHub
parent ce393af7a6
commit 68f8244298
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 66 deletions

View File

@ -112,7 +112,7 @@ class ListServers extends ListRecords
->poll('15s') ->poll('15s')
->columns($usingGrid ? $this->gridColumns() : $this->tableColumns()) ->columns($usingGrid ? $this->gridColumns() : $this->tableColumns())
->recordUrl(!$usingGrid ? (fn (Server $server) => Console::getUrl(panel: 'server', tenant: $server)) : null) ->recordUrl(!$usingGrid ? (fn (Server $server) => Console::getUrl(panel: 'server', tenant: $server)) : null)
->recordActions(!$usingGrid ? ActionGroup::make(static::getPowerActions(view: 'table')) : []) ->recordActions(!$usingGrid ? static::getPowerActionGroup() : [])
->recordActionsAlignment(Alignment::Center->value) ->recordActionsAlignment(Alignment::Center->value)
->contentGrid($usingGrid ? ['default' => 1, 'md' => 2] : null) ->contentGrid($usingGrid ? ['default' => 1, 'md' => 2] : null)
->emptyStateIcon('tabler-brand-docker') ->emptyStateIcon('tabler-brand-docker')
@ -225,47 +225,43 @@ class ListServers extends ListRecords
} }
} }
/** @return Action[]|ActionGroup[] */ public static function getPowerActionGroup(): ActionGroup
public static function getPowerActions(string $view): array
{ {
$actions = [ return ActionGroup::make([
Action::make('start') Action::make('start')
->label(trans('server/console.power_actions.start'))
->color('primary') ->color('primary')
->icon('tabler-player-play-filled') ->icon('tabler-player-play-filled')
->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_START, $server)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_START, $server))
->visible(fn (Server $server) => !$server->isInConflictState() && $server->retrieveStatus()->isStartable()) ->visible(fn (Server $server) => $server->retrieveStatus()->isStartable())
->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'start']), ->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'start']),
Action::make('restart') Action::make('restart')
->label(trans('server/console.power_actions.restart'))
->color('gray') ->color('gray')
->icon('tabler-reload') ->icon('tabler-reload')
->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server))
->visible(fn (Server $server) => !$server->isInConflictState() && $server->retrieveStatus()->isRestartable()) ->visible(fn (Server $server) => $server->retrieveStatus()->isRestartable())
->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'restart']), ->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'restart']),
Action::make('stop') Action::make('stop')
->label(trans('server/console.power_actions.stop'))
->color('danger') ->color('danger')
->icon('tabler-player-stop-filled') ->icon('tabler-player-stop-filled')
->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server))
->visible(fn (Server $server) => !$server->isInConflictState() && $server->retrieveStatus()->isStoppable()) ->visible(fn (Server $server) => $server->retrieveStatus()->isStoppable() && !$server->retrieveStatus()->isKillable())
->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'stop']), ->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'stop']),
Action::make('kill') Action::make('kill')
->label(trans('server/console.power_actions.kill'))
->color('danger') ->color('danger')
->icon('tabler-alert-square') ->icon('tabler-alert-square')
->tooltip('This can result in data corruption and/or data loss!') ->tooltip(trans('server/console.power_actions.kill_tooltip'))
->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server))
->visible(fn (Server $server) => !$server->isInConflictState() && $server->retrieveStatus()->isKillable()) ->visible(fn (Server $server) => $server->retrieveStatus()->isKillable())
->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'kill']), ->dispatch('powerAction', fn (Server $server) => ['server' => $server, 'action' => 'kill']),
]; ])
if ($view === 'table') {
return $actions;
} else {
return [
ActionGroup::make($actions)
->icon('tabler-power') ->icon('tabler-power')
->color('primary') ->color('primary')
->tooltip('Power Actions') ->tooltip(trans('server/dashboard.power_actions'))
->iconSize(IconSize::Large), ->hidden(fn (Server $server) => $server->isInConflictState())
]; ->iconSize(IconSize::Large);
}
} }
} }

View File

@ -158,45 +158,52 @@ class Console extends Page
/** @return array<Action|ActionGroup> */ /** @return array<Action|ActionGroup> */
protected function getDefaultHeaderActions(): array protected function getDefaultHeaderActions(): array
{ {
/** @var Server $server */
$server = Filament::getTenant();
return [ return [
ActionGroup::make([
Action::make('start') Action::make('start')
->label(trans('server/console.power_actions.start')) ->label(trans('server/console.power_actions.start'))
->color('primary') ->color('primary')
->size(Size::ExtraLarge) ->icon('tabler-player-play-filled')
->action(fn () => $this->dispatch('setServerState', state: 'start', uuid: $server->uuid)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_START, $server))
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_START, $server)) ->disabled(fn (Server $server) => $server->isInConflictState() || !$this->status->isStartable())
->disabled(fn () => $server->isInConflictState() || !$this->status->isStartable()) ->action(fn (Server $server) => $this->dispatch('setServerState', uuid: $server->uuid, state: 'start'))
->icon('tabler-player-play-filled'), ->size(Size::ExtraLarge),
Action::make('restart') Action::make('restart')
->label(trans('server/console.power_actions.restart')) ->label(trans('server/console.power_actions.restart'))
->color('gray') ->color('gray')
->size(Size::ExtraLarge) ->icon('tabler-reload')
->action(fn () => $this->dispatch('setServerState', state: 'restart', uuid: $server->uuid)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server))
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server)) ->disabled(fn (Server $server) => $server->isInConflictState() || !$this->status->isRestartable())
->disabled(fn () => $server->isInConflictState() || !$this->status->isRestartable()) ->action(fn (Server $server) => $this->dispatch('setServerState', uuid: $server->uuid, state: 'restart'))
->icon('tabler-reload'), ->size(Size::ExtraLarge),
Action::make('stop') Action::make('stop')
->label(trans('server/console.power_actions.stop')) ->label(trans('server/console.power_actions.stop'))
->color('danger') ->color('danger')
->size(Size::ExtraLarge) ->icon('tabler-player-stop-filled')
->action(fn () => $this->dispatch('setServerState', state: 'stop', uuid: $server->uuid)) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server))
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->visible(fn () => !$this->status->isKillable())
->hidden(fn () => $this->status->isStartingOrStopping() || $this->status->isKillable()) ->disabled(fn (Server $server) => $server->isInConflictState() || !$this->status->isStoppable())
->disabled(fn () => $server->isInConflictState() || !$this->status->isStoppable()) ->action(fn (Server $server) => $this->dispatch('setServerState', uuid: $server->uuid, state: 'stop'))
->icon('tabler-player-stop-filled'), ->size(Size::ExtraLarge),
Action::make('kill') Action::make('kill')
->label(trans('server/console.power_actions.kill')) ->label(trans('server/console.power_actions.kill'))
->color('danger') ->color('danger')
->icon('tabler-alert-square')
->tooltip(trans('server/console.power_actions.kill_tooltip')) ->tooltip(trans('server/console.power_actions.kill_tooltip'))
->requiresConfirmation() ->requiresConfirmation()
->size(Size::ExtraLarge) ->authorize(fn (Server $server) => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server))
->action(fn () => $this->dispatch('setServerState', state: 'kill', uuid: $server->uuid)) ->visible(fn () => $this->status->isKillable())
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->disabled(fn (Server $server) => $server->isInConflictState() || !$this->status->isKillable())
->hidden(fn () => $server->isInConflictState() || !$this->status->isKillable()) ->action(fn (Server $server) => $this->dispatch('setServerState', uuid: $server->uuid, state: 'kill'))
->icon('tabler-alert-square'), ->size(Size::ExtraLarge),
])
->record(function () {
/** @var Server $server */
$server = Filament::getTenant();
return $server;
})
->buttonGroup(),
]; ];
} }

View File

@ -1,3 +1,7 @@
@php
$actiongroup = \App\Filament\App\Resources\Servers\Pages\ListServers::getPowerActionGroup()->record($server);
@endphp
<div wire:poll.15s <div wire:poll.15s
class="relative cursor-pointer" class="relative cursor-pointer"
x-on:click="window.location.href = '{{ \App\Filament\Server\Pages\Console::getUrl(panel: 'server', tenant: $server) }}'"> x-on:click="window.location.href = '{{ \App\Filament\Server\Pages\Console::getUrl(panel: 'server', tenant: $server) }}'">
@ -20,13 +24,13 @@
({{ $server->formatResource(\App\Enums\ServerResourceType::Uptime) }}) ({{ $server->formatResource(\App\Enums\ServerResourceType::Uptime) }})
</span> </span>
</h2> </h2>
<div class="end-0" x-on:click.stop> @if ($actiongroup->isVisible())
<div class="flex-1 dark:bg-gray-800 dark:text-white rounded-b-lg overflow-hidden p-1"> <div class="end-0">
@foreach (\App\Filament\App\Resources\Servers\Pages\ListServers::getPowerActions(view: 'grid') as $action) <div class="flex-1 dark:bg-gray-800 dark:text-white rounded-b-lg overflow-hidden p-1" x-on:click.stop>
{{ $action->record($server) }} {{ $actiongroup }}
@endforeach
</div> </div>
</div> </div>
@endif
</div> </div>
<div class="flex justify-between text-center items-center gap-4"> <div class="flex justify-between text-center items-center gap-4">