refactor context menu

fixes #1384
This commit is contained in:
notCharles 2025-05-18 19:54:20 -04:00
parent 7034c4d013
commit f0ac41aa13

View File

@ -44,34 +44,54 @@ class ListServers extends ListRecords
$menuOptions = function (Server $server) { $menuOptions = function (Server $server) {
$status = $server->retrieveStatus(); $status = $server->retrieveStatus();
$user = auth()->user();
return [ $actions = [
Action::make('start') 'start' => [
->color('primary') 'permission' => Permission::ACTION_CONTROL_START,
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_START, $server)) 'check' => $status->isStartable(),
->visible(fn () => $status->isStartable()) 'color' => 'primary',
->dispatch('powerAction', ['server' => $server, 'action' => 'start']) 'icon' => 'tabler-player-play-filled',
->icon('tabler-player-play-filled'), ],
Action::make('restart') 'restart' => [
->color('gray') 'permission' => Permission::ACTION_CONTROL_RESTART,
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server)) 'check' => $status->isRestartable(),
->visible(fn () => $status->isRestartable()) 'color' => 'gray',
->dispatch('powerAction', ['server' => $server, 'action' => 'restart']) 'icon' => 'tabler-refresh',
->icon('tabler-refresh'), ],
Action::make('stop') 'stop' => [
->color('danger') 'permission' => Permission::ACTION_CONTROL_STOP,
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) 'check' => $status->isStoppable(),
->visible(fn () => $status->isStoppable()) 'color' => 'danger',
->dispatch('powerAction', ['server' => $server, 'action' => 'stop']) 'icon' => 'tabler-player-stop-filled',
->icon('tabler-player-stop-filled'), ],
Action::make('kill') 'kill' => [
->color('danger') 'permission' => Permission::ACTION_CONTROL_STOP,
->tooltip('This can result in data corruption and/or data loss!') 'check' => $status->isKillable(),
->dispatch('powerAction', ['server' => $server, 'action' => 'kill']) 'color' => 'danger',
->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) 'icon' => 'tabler-alert-square',
->visible(fn () => $status->isKillable()) 'tooltip' => 'This can result in data corruption and/or data loss!',
->icon('tabler-alert-square'), ],
]; ];
$options = [];
foreach ($actions as $name => $details) {
if ($user->can($details['permission'], $server) && $details['check']) {
$action = Action::make($name)
->color($details['color'])
->dispatch('powerAction', ['server' => $server, 'action' => $name])
->icon($details['icon']);
if (isset($details['tooltip'])) {
$action->tooltip($details['tooltip']);
}
$options[] = $action;
}
}
return $options;
}; };
$viewOne = [ $viewOne = [