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

View File

@@ -34,32 +34,38 @@ class All extends Component
$this->showPreview = true; $this->showPreview = true;
} }
$this->modalId = new Cuid2; $this->modalId = new Cuid2;
$this->sortMe(); $this->sortEnvironmentVariables();
}
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();
} }
public function instantSave() public function instantSave()
{ {
$this->resource->settings->save(); $this->resource->settings->save();
$this->sortMe(); $this->sortEnvironmentVariables();
$this->dispatch('success', 'Environment variable settings updated.'); $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() public function getDevView()
{ {
$this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables); $this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables);
@@ -72,10 +78,10 @@ class All extends Component
{ {
return $variables->map(function ($item) { return $variables->map(function ($item) {
if ($item->is_shown_once) { 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) { 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"; return "$item->key=$item->value";
})->join("\n"); })->join("\n");
@@ -84,7 +90,7 @@ class All extends Component
public function switch() public function switch()
{ {
$this->view = $this->view === 'normal' ? 'dev' : 'normal'; $this->view = $this->view === 'normal' ? 'dev' : 'normal';
$this->sortMe(); $this->sortEnvironmentVariables();
} }
public function submit($data = null) public function submit($data = null)
@@ -96,7 +102,7 @@ class All extends Component
$this->handleSingleSubmit($data); $this->handleSingleSubmit($data);
} }
$this->sortMe(); $this->sortEnvironmentVariables();
} catch (\Throwable $e) { } catch (\Throwable $e) {
return handleError($e, $this); return handleError($e, $this);
} }
@@ -222,6 +228,7 @@ class All extends Component
public function refreshEnvs() public function refreshEnvs()
{ {
$this->resource->refresh(); $this->resource->refresh();
$this->sortEnvironmentVariables();
$this->getDevView(); $this->getDevView();
} }
} }