From c1475e193d165e51179c52c98f097187da188c1f Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Tue, 19 Sep 2023 20:56:45 -0400 Subject: [PATCH] preload server list and bump to 20 results --- app/Filament/Pages/Settings/GeneralPage.php | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Filament/Pages/Settings/GeneralPage.php b/app/Filament/Pages/Settings/GeneralPage.php index 88c81a8e3..c6f72a427 100644 --- a/app/Filament/Pages/Settings/GeneralPage.php +++ b/app/Filament/Pages/Settings/GeneralPage.php @@ -81,11 +81,31 @@ public function form(Form $form): Form Forms\Components\Select::make('speedtest_server') ->label('Speedtest servers') ->helperText('Leave empty to let the system pick the best server.') - ->nullable() - ->multiple() ->maxItems(10) - ->preload() + ->multiple() + ->nullable() + ->preload(false) ->searchable() + ->options(function (): array { + $response = Http::get( + url: 'https://www.speedtest.net/api/js/servers', + query: [ + 'engine' => 'js', + 'https_functional' => true, + 'limit' => 20, + ] + ); + + if ($response->failed()) { + return [ + '' => 'There was an error retrieving Speedtest servers', + ]; + } + + return $response->collect()->mapWithKeys(function (array $item, int $key) { + return [$item['id'] => $item['id'].': '.$item['name'].' ('.$item['sponsor'].')']; + })->toArray(); + }) ->getSearchResultsUsing(fn (string $search): array => $this->getServerSearchOptions($search)) ->getOptionLabelsUsing(fn (array $values): array => $this->getServerLabels($values)) ->columnSpan('full'), @@ -119,7 +139,7 @@ protected function getServerSearchOptions(string $search): array 'engine' => 'js', 'search' => $search, 'https_functional' => true, - 'limit' => 10, + 'limit' => 20, ] );