Skip to content

Commit 18fc3e1

Browse files
authored
feat(admin): widgets per source (#2)
This pull request introduces a new stats widget for inbound webhooks, improves dashboard visibility, and enhances model factory support. The main focus is on providing a breakdown of inbound webhook sources for admins and enabling easier testing and seeding of webhook data. **Dashboard and Widget Improvements** * Added a new widget, `InboundWebhookStatsBySource`, which displays the percentage and count of inbound webhooks grouped by their source, including icons and colors for clarity. This widget is now shown in the admin list view and registered globally with Filament. **Model Enhancements** * Added a static `newFactory` method to the `InboundWebhook` model, enabling the use of a custom factory for easier testing and data generation.
1 parent 9273b17 commit 18fc3e1

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

src/Filament/Admin/Resources/InboundWebhook/Pages/ListInboundWebhooks.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Basement\Webhooks\Filament\Admin\Resources\InboundWebhook\Pages;
66

77
use Basement\Webhooks\Filament\Admin\Resources\InboundWebhook\InboundWebhookResource;
8+
use Basement\Webhooks\Filament\Admin\Widgets\InboundWebhookStatsByProviderPercentage;
9+
use Basement\Webhooks\Filament\Admin\Widgets\InboundWebhookStatsBySource;
810
use Filament\Actions\BulkActionGroup;
911
use Filament\Actions\DeleteAction;
1012
use Filament\Actions\DeleteBulkAction;
@@ -20,6 +22,13 @@ final class ListInboundWebhooks extends ListRecords
2022
{
2123
protected static string $resource = InboundWebhookResource::class;
2224

25+
protected function getHeaderWidgets(): array
26+
{
27+
return [
28+
InboundWebhookStatsBySource::make(),
29+
];
30+
}
31+
2332
public function table(Table $table): Table
2433
{
2534
return $table
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Basement\Webhooks\Filament\Admin\Widgets;
4+
5+
use Basement\Webhooks\Enums\InboundWebhookSource;
6+
use Basement\Webhooks\Models\InboundWebhook;
7+
use Filament\Widgets\StatsOverviewWidget;
8+
use Filament\Widgets\StatsOverviewWidget\Stat;
9+
10+
class InboundWebhookStatsBySource extends StatsOverviewWidget
11+
{
12+
protected function getStats(): array
13+
{
14+
return $this->getInboundWebhooksStats();
15+
}
16+
17+
private function getInboundWebhooksStats(): array
18+
{
19+
$totalWebhooks = InboundWebhook::count();
20+
$stats = [];
21+
22+
foreach (InboundWebhookSource::cases() as $source) {
23+
$count = InboundWebhook::where('source', $source->value)->count();
24+
$percentage = $totalWebhooks > 0 ? round(($count / $totalWebhooks) * 100, 2) : 0;
25+
$stats[] = Stat::make("{$source->name}","{$percentage}%")
26+
->descriptionIcon($source->getIcon())
27+
->description("{$count} de {$totalWebhooks} webhooks")
28+
->color($source->getColor());
29+
}
30+
return $stats;
31+
}
32+
}

src/FilamentWebhookPlugin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Basement\Webhooks;
66

77
use Basement\Webhooks\Filament\Admin\Resources\InboundWebhook\InboundWebhookResource;
8+
use Basement\Webhooks\Filament\Admin\Widgets\InboundWebhookStatsByProviderPercentage;
9+
use Basement\Webhooks\Filament\Admin\Widgets\InboundWebhookStatsBySource;
810
use Filament\Contracts\Plugin;
911
use Filament\Panel;
1012

@@ -25,6 +27,9 @@ public function register(Panel $panel): void
2527
$panel->resources([
2628
InboundWebhookResource::class,
2729
]);
30+
$panel->widgets([
31+
InboundWebhookStatsBySource::make(),
32+
]);
2833
}
2934

3035
public function boot(Panel $panel): void {}

src/Models/InboundWebhook.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace Basement\Webhooks\Models;
66

7+
use Basement\Webhooks\Database\Factories\InboundWebhookFactory;
78
use Illuminate\Database\Eloquent\Concerns\HasUuids;
89
use Illuminate\Database\Eloquent\Factories\HasFactory;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\SoftDeletes;
1112

13+
1214
final class InboundWebhook extends Model
1315
{
1416
use HasFactory;
@@ -30,4 +32,9 @@ protected function casts(): array
3032
'source' => config('filament-webhooks.providers_enum'),
3133
];
3234
}
35+
36+
protected static function newFactory(): InboundWebhookFactory
37+
{
38+
return InboundWebhookFactory::new();
39+
}
3340
}

0 commit comments

Comments
 (0)