Skip to content

Commit ce3f4b4

Browse files
authored
[Feature] Added dashboard buttons to the toolbar (alexjustesen#1842)
added dashboard button to the toolbar
1 parent 91b699e commit ce3f4b4

File tree

7 files changed

+150
-56
lines changed

7 files changed

+150
-56
lines changed

app/Filament/Pages/Dashboard.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,16 @@
22

33
namespace App\Filament\Pages;
44

5-
use App\Actions\GetOoklaSpeedtestServers;
6-
use App\Actions\Ookla\StartSpeedtest;
75
use App\Filament\Widgets\RecentDownloadChartWidget;
86
use App\Filament\Widgets\RecentDownloadLatencyChartWidget;
97
use App\Filament\Widgets\RecentJitterChartWidget;
108
use App\Filament\Widgets\RecentPingChartWidget;
119
use App\Filament\Widgets\RecentUploadChartWidget;
1210
use App\Filament\Widgets\RecentUploadLatencyChartWidget;
1311
use App\Filament\Widgets\StatsOverviewWidget;
14-
use App\Helpers\Ookla;
1512
use Carbon\Carbon;
1613
use Cron\CronExpression;
17-
use Filament\Actions\Action;
18-
use Filament\Forms;
19-
use Filament\Notifications\Notification;
2014
use Filament\Pages\Dashboard as BasePage;
21-
use Filament\Support\Enums\IconPosition;
2215

2316
class Dashboard extends BasePage
2417
{
@@ -41,55 +34,6 @@ public function getSubheading(): ?string
4134
return 'Next speedtest at: '.$nextRunDate;
4235
}
4336

44-
protected function getHeaderActions(): array
45-
{
46-
return [
47-
Action::make('home')
48-
->label('Public Dashboard')
49-
->icon('heroicon-o-chart-bar')
50-
->iconPosition(IconPosition::Before)
51-
->color('gray')
52-
->hidden(fn (): bool => ! config('speedtest.public_dashboard'))
53-
->url(shouldOpenInNewTab: true, url: '/'),
54-
55-
Action::make('speedtest')
56-
->form([
57-
Forms\Components\Select::make('server_id')
58-
->label('Select Server')
59-
->helperText('Leave empty to run the speedtest without specifying a server.')
60-
->options(function (): array {
61-
return array_filter([
62-
'Manual servers' => Ookla::getConfigServers(),
63-
'Closest servers' => GetOoklaSpeedtestServers::run(),
64-
]);
65-
})
66-
->searchable(),
67-
])
68-
->action(function (array $data) {
69-
$serverId = $data['server_id'] ?? null;
70-
71-
StartSpeedtest::run(
72-
scheduled: false,
73-
serverId: $serverId,
74-
);
75-
76-
Notification::make()
77-
->title('Speedtest started')
78-
->success()
79-
->send();
80-
})
81-
->modalHeading('Run Speedtest')
82-
->modalWidth('lg')
83-
->modalSubmitActionLabel('Start')
84-
->button()
85-
->color('primary')
86-
->label('Run Speedtest')
87-
->icon('heroicon-o-rocket-launch')
88-
->iconPosition(IconPosition::Before)
89-
->hidden(! auth()->user()->is_admin),
90-
];
91-
}
92-
9337
protected function getHeaderWidgets(): array
9438
{
9539
return [
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace App\Livewire\Topbar;
4+
5+
use App\Actions\GetOoklaSpeedtestServers;
6+
use App\Actions\Ookla\StartSpeedtest;
7+
use App\Helpers\Ookla;
8+
use Filament\Actions\Action;
9+
use Filament\Actions\Concerns\InteractsWithActions;
10+
use Filament\Actions\Contracts\HasActions;
11+
use Filament\Forms\Components\Select;
12+
use Filament\Forms\Concerns\InteractsWithForms;
13+
use Filament\Forms\Contracts\HasForms;
14+
use Filament\Notifications\Notification;
15+
use Filament\Support\Enums\IconPosition;
16+
use Livewire\Component;
17+
18+
class RunSpeedtestAction extends Component implements HasActions, HasForms
19+
{
20+
use InteractsWithActions, InteractsWithForms;
21+
22+
public function dashboardAction(): Action
23+
{
24+
return Action::make('home')
25+
->label('Dashboard')
26+
->icon('heroicon-o-chart-bar')
27+
->iconPosition(IconPosition::Before)
28+
->color('gray')
29+
->hidden(fn (): bool => ! config('speedtest.public_dashboard'))
30+
->url(shouldOpenInNewTab: true, url: '/')
31+
->extraAttributes([
32+
'id' => 'dashboardAction',
33+
]);
34+
}
35+
36+
public function speedtestAction(): Action
37+
{
38+
return Action::make('speedtest')
39+
->form([
40+
Select::make('server_id')
41+
->label('Select Server')
42+
->helperText('Leave empty to run the speedtest without specifying a server.')
43+
->options(function (): array {
44+
return array_filter([
45+
'Manual servers' => Ookla::getConfigServers(),
46+
'Closest servers' => GetOoklaSpeedtestServers::run(),
47+
]);
48+
})
49+
->searchable(),
50+
])
51+
->action(function (array $data) {
52+
$serverId = $data['server_id'] ?? null;
53+
54+
StartSpeedtest::run(
55+
scheduled: false,
56+
serverId: $serverId,
57+
);
58+
59+
Notification::make()
60+
->title('Speedtest started')
61+
->success()
62+
->send();
63+
})
64+
->modalHeading('Run Speedtest')
65+
->modalWidth('lg')
66+
->modalSubmitActionLabel('Start')
67+
->button()
68+
->color('primary')
69+
->label('Speedtest')
70+
->icon('heroicon-o-rocket-launch')
71+
->iconPosition(IconPosition::Before)
72+
->hidden(! auth()->user()->is_admin)
73+
->extraAttributes([
74+
'id' => 'speedtestAction',
75+
]);
76+
}
77+
78+
public function render()
79+
{
80+
return view('livewire.topbar.run-speedtest-action');
81+
}
82+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Filament\Support\Assets\Css;
6+
use Filament\Support\Facades\FilamentAsset;
7+
use Filament\Support\Facades\FilamentView;
8+
use Filament\View\PanelsRenderHook;
9+
use Illuminate\Support\Facades\Blade;
10+
use Illuminate\Support\ServiceProvider;
11+
12+
class FilamentServiceProvider extends ServiceProvider
13+
{
14+
/**
15+
* Register services.
16+
*/
17+
public function register(): void
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Bootstrap services.
24+
*/
25+
public function boot(): void
26+
{
27+
FilamentAsset::register([
28+
Css::make('panel', __DIR__.'/../../resources/css/panel.css'),
29+
]);
30+
31+
FilamentView::registerRenderHook(
32+
PanelsRenderHook::GLOBAL_SEARCH_BEFORE,
33+
fn (): string => Blade::render("@livewire('topbar.run-speedtest-action')"),
34+
);
35+
}
36+
}

bootstrap/providers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
return [
44
App\Providers\AppServiceProvider::class,
5+
App\Providers\FilamentServiceProvider::class,
56
App\Providers\Filament\AdminPanelProvider::class,
67
];

public/css/app/panel.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.fi-topbar #dashboardAction .fi-btn-label,
2+
.fi-topbar #speedtestAction .fi-btn-label {
3+
display: none;
4+
}
5+
6+
@media (min-width: 768px) {
7+
.fi-topbar #dashboardAction .fi-btn-label,
8+
.fi-topbar #speedtestAction .fi-btn-label {
9+
display: block;
10+
}
11+
}

resources/css/panel.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.fi-topbar #dashboardAction .fi-btn-label,
2+
.fi-topbar #speedtestAction .fi-btn-label {
3+
display: none;
4+
}
5+
6+
@media (min-width: 768px) {
7+
.fi-topbar #dashboardAction .fi-btn-label,
8+
.fi-topbar #speedtestAction .fi-btn-label {
9+
display: block;
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
<div class="flex flex-wrap items-start justify-start gap-3">
3+
{{ $this->dashboard }}
4+
5+
{{ $this->speedtestAction }}
6+
</div>
7+
8+
<x-filament-actions::modals />
9+
</div>

0 commit comments

Comments
 (0)