fix disable/enable environment variabel sorting

This commit is contained in:
ayntk-ai 2024-08-12 23:00:08 +02:00
parent e28289ce1e
commit fbde257166
No known key found for this signature in database

View File

@ -34,32 +34,38 @@ public function mount()
$this->showPreview = true;
}
$this->modalId = new Cuid2;
$this->sortMe();
}
public function sortMe()
{
$sortBy = 'key'; // Always sort by key
$this->resource->load(['environment_variables', 'environment_variables_preview']);
$this->resource->environment_variables = $this->sortEnvironmentVariables($this->resource->environment_variables, $sortBy);
$this->resource->environment_variables_preview = $this->sortEnvironmentVariables($this->resource->environment_variables_preview, $sortBy);
$this->getDevView();
}
private function sortEnvironmentVariables($variables, $sortBy)
{
return $variables->sortBy(function ($item) use ($sortBy) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
$this->sortEnvironmentVariables();
}
public function instantSave()
{
$this->resource->settings->save();
$this->sortMe();
$this->sortEnvironmentVariables();
$this->dispatch('success', 'Environment variable settings updated.');
}
public function sortEnvironmentVariables()
{
$this->resource->load(['environment_variables', 'environment_variables_preview']);
$sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id';
$sortFunction = function ($variables) use ($sortBy) {
if ($sortBy === 'key') {
return $variables->sortBy(function ($item) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
} else {
return $variables->sortBy('id')->values();
}
};
$this->resource->environment_variables = $sortFunction($this->resource->environment_variables);
$this->resource->environment_variables_preview = $sortFunction($this->resource->environment_variables_preview);
$this->getDevView();
}
public function getDevView()
{
$this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables);
@ -72,10 +78,10 @@ private function formatEnvironmentVariables($variables)
{
return $variables->map(function ($item) {
if ($item->is_shown_once) {
return "$item->key=(locked secret)";
return "$item->key=(Locked Secret, delete and add again to change)";
}
if ($item->is_multiline) {
return "$item->key=(multiline, edit in normal view)";
return "$item->key=(Multiline environment variable, edit in normal view)";
}
return "$item->key=$item->value";
})->join("\n");
@ -84,7 +90,7 @@ private function formatEnvironmentVariables($variables)
public function switch()
{
$this->view = $this->view === 'normal' ? 'dev' : 'normal';
$this->sortMe();
$this->sortEnvironmentVariables();
}
public function submit($data = null)
@ -96,7 +102,7 @@ public function submit($data = null)
$this->handleSingleSubmit($data);
}
$this->sortMe();
$this->sortEnvironmentVariables();
} catch (\Throwable $e) {
return handleError($e, $this);
}
@ -222,6 +228,7 @@ private function setEnvironmentResourceId($environment)
public function refreshEnvs()
{
$this->resource->refresh();
$this->sortEnvironmentVariables();
$this->getDevView();
}
}