diff --git a/app/Filament/Server/Pages/Startup.php b/app/Filament/Server/Pages/Startup.php index a6410a56e..c99ed866f 100644 --- a/app/Filament/Server/Pages/Startup.php +++ b/app/Filament/Server/Pages/Startup.php @@ -5,12 +5,11 @@ namespace App\Filament\Server\Pages; use Exception; use App\Facades\Activity; use App\Filament\Components\Actions\PreviewStartupAction; +use App\Filament\Components\Forms\Fields\StartupVariable; use App\Models\Permission; use App\Models\Server; use App\Models\ServerVariable; -use Closure; use Filament\Facades\Filament; -use Filament\Schemas\Components\Component; use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Repeater; use Filament\Forms\Components\Select; @@ -20,7 +19,6 @@ use Filament\Notifications\Notification; use Filament\Schemas\Components\Section; use Filament\Schemas\Schema; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; class Startup extends ServerFormPage @@ -108,50 +106,13 @@ class Startup extends ServerFormPage ->grid() ->disabled(fn (Server $server) => !auth()->user()->can(Permission::ACTION_STARTUP_UPDATE, $server)) ->reorderable(false)->addable(false)->deletable(false) - ->schema(function () { - $text = TextInput::make('variable_value') - ->hidden($this->shouldHideComponent(...)) - ->dehydratedWhenHidden() - ->disabled(fn (ServerVariable $serverVariable) => !$serverVariable->variable->user_editable) - ->required(fn (ServerVariable $serverVariable) => $serverVariable->variable->getRequiredAttribute()) - ->rules([ - fn (ServerVariable $serverVariable): Closure => function (string $attribute, $value, Closure $fail) use ($serverVariable) { - $validator = Validator::make(['validatorkey' => $value], [ - 'validatorkey' => $serverVariable->variable->rules, - ]); - - if ($validator->fails()) { - $message = str($validator->errors()->first())->replace('validatorkey', $serverVariable->variable->name); - - $fail($message); - } - }, - ]); - - $select = Select::make('variable_value') - ->hidden($this->shouldHideComponent(...)) - ->dehydratedWhenHidden() - ->disabled(fn (ServerVariable $serverVariable) => !$serverVariable->variable->user_editable) - ->options($this->getSelectOptionsFromRules(...)) - ->selectablePlaceholder(false); - - $components = [$text, $select]; - - foreach ($components as &$component) { - $component = $component - ->live(onBlur: true) - ->afterStateUpdated(function ($state, ServerVariable $serverVariable) { - $this->update($state, $serverVariable); - }) - ->hintIcon('tabler-code') - ->label(fn (ServerVariable $serverVariable) => $serverVariable->variable->name) - ->hintIconTooltip(fn (ServerVariable $serverVariable) => implode('|', $serverVariable->variable->rules)) - ->prefix(fn (ServerVariable $serverVariable) => '{{' . $serverVariable->variable->env_variable . '}}') - ->helperText(fn (ServerVariable $serverVariable) => empty($serverVariable->variable->description) ? '—' : $serverVariable->variable->description); - } - - return $components; - }) + ->schema([ + StartupVariable::make('variable_value') + ->fromRecord() + ->afterStateUpdated(function ($state, ServerVariable $serverVariable) { + $this->update($state, $serverVariable); + }), + ]) ->columnSpan(6), ]), ]); @@ -167,36 +128,6 @@ class Startup extends ServerFormPage return parent::canAccess() && auth()->user()->can(Permission::ACTION_STARTUP_READ, Filament::getTenant()); } - private function shouldHideComponent(ServerVariable $serverVariable, Component $component): bool - { - $containsRuleIn = Arr::first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:'), false); - - if ($component instanceof Select) { - return !$containsRuleIn; - } - - if ($component instanceof TextInput) { - return $containsRuleIn; - } - - throw new Exception('Component type not supported: ' . $component::class); - } - - /** - * @return string[] - */ - private function getSelectOptionsFromRules(ServerVariable $serverVariable): array - { - $inRule = Arr::first($serverVariable->variable->rules, fn ($value) => str($value)->startsWith('in:')); - - return str($inRule) - ->after('in:') - ->explode(',') - ->each(fn ($value) => str($value)->trim()) - ->mapWithKeys(fn ($value) => [$value => $value]) - ->all(); - } - public function update(?string $state, ServerVariable $serverVariable): null { $original = $serverVariable->variable_value;