|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Pages\Tools; |
| 4 | + |
| 5 | +use App\Actions\GetOoklaSpeedtestServers; |
| 6 | +use Filament\Actions\Action; |
| 7 | +use Filament\Forms\Components\Textarea; |
| 8 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 9 | +use Filament\Forms\Contracts\HasForms; |
| 10 | +use Filament\Forms\Form; |
| 11 | +use Filament\Notifications\Notification; |
| 12 | +use Filament\Pages\Page; |
| 13 | + |
| 14 | +class ListOoklaServers extends Page implements HasForms |
| 15 | +{ |
| 16 | + use InteractsWithForms; |
| 17 | + |
| 18 | + protected static ?string $navigationIcon = 'heroicon-o-server-stack'; |
| 19 | + |
| 20 | + protected static ?string $navigationGroup = 'Tools'; |
| 21 | + |
| 22 | + protected static ?string $title = 'List Ookla Servers'; |
| 23 | + |
| 24 | + protected static ?string $navigationLabel = 'List Ookla Servers'; |
| 25 | + |
| 26 | + protected static string $view = 'filament.pages.tools.list-ookla-servers'; |
| 27 | + |
| 28 | + protected static ?string $slug = 'tools/list-ookla-servers'; |
| 29 | + |
| 30 | + public static function shouldRegisterNavigation(): bool |
| 31 | + { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + public ?string $servers = null; |
| 36 | + |
| 37 | + public bool $isLoading = true; |
| 38 | + |
| 39 | + public function mount(): void |
| 40 | + { |
| 41 | + $this->fetchServers(); |
| 42 | + } |
| 43 | + |
| 44 | + public function fetchServers(): void |
| 45 | + { |
| 46 | + $this->isLoading = true; |
| 47 | + |
| 48 | + try { |
| 49 | + $servers = GetOoklaSpeedtestServers::fetch(); |
| 50 | + |
| 51 | + $this->servers = json_encode($servers, JSON_PRETTY_PRINT); |
| 52 | + |
| 53 | + $this->form->fill([ |
| 54 | + 'servers' => $this->servers, |
| 55 | + ]); |
| 56 | + } catch (\Exception $e) { |
| 57 | + Notification::make() |
| 58 | + ->title('Error fetching servers') |
| 59 | + ->body($e->getMessage()) |
| 60 | + ->danger() |
| 61 | + ->send(); |
| 62 | + |
| 63 | + $this->servers = ''; |
| 64 | + $this->form->fill([ |
| 65 | + 'servers' => '', |
| 66 | + ]); |
| 67 | + } finally { |
| 68 | + $this->isLoading = false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public function form(Form $form): Form |
| 73 | + { |
| 74 | + return $form |
| 75 | + ->schema([ |
| 76 | + Textarea::make('servers') |
| 77 | + ->label(false) |
| 78 | + ->rows(20) |
| 79 | + ->columnSpanFull() |
| 80 | + ->extraAttributes(['class' => 'font-mono']) |
| 81 | + ->disabled(), |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + protected function getHeaderActions(): array |
| 86 | + { |
| 87 | + return [ |
| 88 | + Action::make('refreshServers') |
| 89 | + ->label('Refresh') |
| 90 | + ->icon('heroicon-o-arrow-path') |
| 91 | + ->action(function () { |
| 92 | + $this->fetchServers(); |
| 93 | + |
| 94 | + Notification::make() |
| 95 | + ->title('Servers refreshed successfully') |
| 96 | + ->success() |
| 97 | + ->send(); |
| 98 | + }), |
| 99 | + |
| 100 | + Action::make('copyServers') |
| 101 | + ->label('Copy to Clipboard') |
| 102 | + ->icon('heroicon-o-clipboard') |
| 103 | + ->disabled(fn () => blank($this->servers)) |
| 104 | + ->requiresConfirmation(false) |
| 105 | + ->action(function () { |
| 106 | + $this->js('navigator.clipboard.writeText('.json_encode($this->servers).')'); |
| 107 | + |
| 108 | + Notification::make() |
| 109 | + ->title('Copied to clipboard') |
| 110 | + ->success() |
| 111 | + ->send(); |
| 112 | + }), |
| 113 | + ]; |
| 114 | + } |
| 115 | +} |
0 commit comments