vito/app/Web/Pages/Scripts/Index.php
2025-03-13 20:53:14 +01:00

79 lines
2.5 KiB
PHP

<?php
namespace App\Web\Pages\Scripts;
use App\Actions\Script\CreateScript;
use App\Models\Script;
use App\Models\User;
use App\Web\Components\Page;
use App\Web\Fields\CodeEditorField;
use Filament\Actions\Action;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\MaxWidth;
class Index extends Page
{
protected static ?string $slug = 'scripts';
protected static ?string $navigationIcon = 'heroicon-o-bolt';
protected static ?int $navigationSort = 2;
protected static ?string $title = 'Scripts';
public static function getNavigationItemActiveRoutePattern(): string
{
return static::getRouteName().'*';
}
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', Script::class) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\ScriptsList::class],
];
}
protected function getHeaderActions(): array
{
/** @var User $user */
$user = auth()->user();
return [
Action::make('read-the-docs')
->label('Read the Docs')
->icon('heroicon-o-document-text')
->color('gray')
->url('https://vitodeploy.com/scripts')
->openUrlInNewTab(),
Action::make('create')
->label('Create a Script')
->icon('heroicon-o-plus')
->authorize('create', Script::class)
->modalWidth(MaxWidth::ThreeExtraLarge)
->form([
TextInput::make('name')
->rules(CreateScript::rules()['name']),
CodeEditorField::make('content')
->rules(CreateScript::rules()['content'])
->helperText('You can use variables like ${VARIABLE_NAME} in the script. The variables will be asked when executing the script'),
Checkbox::make('global')
->label('Is Global (Accessible in all projects)'),
])
->modalSubmitActionLabel('Create')
->action(function (array $data) use ($user): void {
run_action($this, function () use ($data, $user): void {
app(CreateScript::class)->create($user, $data);
$this->dispatch('$refresh');
});
}),
];
}
}