From 6d278e38fb9107cd23488486d506c49b425848e5 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 10:19:23 -0300 Subject: [PATCH 01/11] docs --- packages/panels/docs/06-notifications.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/panels/docs/06-notifications.md b/packages/panels/docs/06-notifications.md index 107dfe10b10..9f4a20da62a 100644 --- a/packages/panels/docs/06-notifications.md +++ b/packages/panels/docs/06-notifications.md @@ -33,6 +33,27 @@ public function panel(Panel $panel): Panel } ``` +## Customizing the "Mark All as Read" Action + +To customize the "Mark All as Read" button, you can use the `databaseNotificationsMarkAllAsReadAction()` method, passing a closure that returns an action. All methods that are available to [customize action trigger buttons](../../actions/trigger-button) can be used: + +```php +use Filament\Actions\Action; +use Filament\Panel; + +public function panel(Panel $panel): Panel +{ + return $panel + // ... + ->databaseNotifications() + ->databaseNotificationsMarkAllAsReadAction(fn (Action $action) => $action + ->button() + ->color('success') + ->label('Read All') + ); +} +``` + ## Setting up websockets in a panel The Panel Builder comes with a level of inbuilt support for real-time broadcast and database notifications. However there are a number of areas you will need to install and configure to wire everything up and get it working. From a554061298453a0bc1af3686462a2066853513e6 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 10:40:12 -0300 Subject: [PATCH 02/11] These changes introduce the databaseNotificationsMarkAllAsReadAction() method, which allows customization of the Mark All as Read action within the database notifications modal. --- .../database/modal/actions.blade.php | 12 +++------ .../components/database/modal/index.blade.php | 2 ++ .../views/database-notifications.blade.php | 2 ++ .../src/Livewire/DatabaseNotifications.php | 8 ++++++ packages/panels/src/Facades/Filament.php | 1 + packages/panels/src/FilamentManager.php | 5 ++++ .../src/Livewire/DatabaseNotifications.php | 6 +++++ .../src/Panel/Concerns/HasNotifications.php | 27 +++++++++++++++++++ 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/packages/notifications/resources/views/components/database/modal/actions.blade.php b/packages/notifications/resources/views/components/database/modal/actions.blade.php index 2f058146aaf..f1e40cc0655 100644 --- a/packages/notifications/resources/views/components/database/modal/actions.blade.php +++ b/packages/notifications/resources/views/components/database/modal/actions.blade.php @@ -1,18 +1,12 @@ @props([ 'notifications', 'unreadNotificationsCount', + 'markAllNotificationsAsReadAction' ])
class('mt-2 flex gap-x-3') }}> - @if ($unreadNotificationsCount) - - {{ __('filament-notifications::database.modal.actions.mark_all_as_read.label') }} - + @if ($unreadNotificationsCount && $markAllNotificationsAsReadAction) + {{ ($markAllNotificationsAsReadAction)->isVisible() ? $markAllNotificationsAsReadAction : '' }} @endif
diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index c03b24e842e..b3e3bfdc51f 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,6 +1,7 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); + $markAllNotificationsAsReadAction = $this->getMarkAllNotificationsAsReadAction(); @endphp
@if ($broadcastChannel = $this->getBroadcastChannel()) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 0290bd8f987..4092c4c2027 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -3,6 +3,7 @@ namespace Filament\Notifications\Livewire; use Carbon\CarbonInterface; +use Filament\Actions\Action; use Filament\Notifications\Notification; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Pagination\Paginator; @@ -28,6 +29,8 @@ class DatabaseNotifications extends Component public static ?string $authGuard = null; + public static ?Action $markAllNotificationsAsReadAction = null; + #[On('databaseNotificationsSent')] public function refresh(): void {} @@ -113,6 +116,11 @@ public function getTrigger(): ?View return view($viewPath); } + public function getMarkAllNotificationsAsReadAction(): Action + { + return static::$markAllNotificationsAsReadAction; + } + public function getUser(): Model | Authenticatable | null { return auth(static::$authGuard)->user(); diff --git a/packages/panels/src/Facades/Filament.php b/packages/panels/src/Facades/Filament.php index a0c3bed23e5..09c6fdc627a 100644 --- a/packages/panels/src/Facades/Filament.php +++ b/packages/panels/src/Facades/Filament.php @@ -105,6 +105,7 @@ * @method static string | null getUrl(Model | null $tenant = null) * @method static string getVerifyEmailUrl(MustVerifyEmail | Model | Authenticatable $user, array $parameters = []) * @method static array getWidgets() + * @method static Action getDatabaseNotificationsMarkAllAsReadAction() * @method static bool hasBreadcrumbs() * @method static bool hasCollapsibleNavigationGroups() * @method static bool hasDarkMode() diff --git a/packages/panels/src/FilamentManager.php b/packages/panels/src/FilamentManager.php index 777e27a0c99..1f586074a9e 100644 --- a/packages/panels/src/FilamentManager.php +++ b/packages/panels/src/FilamentManager.php @@ -589,6 +589,11 @@ public function getWidgets(): array return $this->getCurrentPanel()->getWidgets(); } + public function getDatabaseNotificationsMarkAllAsReadAction(): Action + { + return $this->currentPanel->getDatabaseNotificationsMarkAllAsReadAction(); + } + public function hasBreadcrumbs(): bool { return $this->getCurrentPanel()->hasBreadcrumbs(); diff --git a/packages/panels/src/Livewire/DatabaseNotifications.php b/packages/panels/src/Livewire/DatabaseNotifications.php index 8ce0e9d67b8..dd4bb155bb5 100644 --- a/packages/panels/src/Livewire/DatabaseNotifications.php +++ b/packages/panels/src/Livewire/DatabaseNotifications.php @@ -2,6 +2,7 @@ namespace Filament\Livewire; +use Filament\Actions\Action; use Filament\Facades\Filament; use Filament\Notifications\Livewire\DatabaseNotifications as BaseComponent; use Illuminate\Contracts\Auth\Authenticatable; @@ -24,4 +25,9 @@ public function getTrigger(): View { return view('filament-panels::components.topbar.database-notifications-trigger'); } + + public function getMarkAllNotificationsAsReadAction(): Action + { + return Filament::getDatabaseNotificationsMarkAllAsReadAction(); + } } diff --git a/packages/panels/src/Panel/Concerns/HasNotifications.php b/packages/panels/src/Panel/Concerns/HasNotifications.php index a8686661707..1228f698632 100644 --- a/packages/panels/src/Panel/Concerns/HasNotifications.php +++ b/packages/panels/src/Panel/Concerns/HasNotifications.php @@ -3,6 +3,7 @@ namespace Filament\Panel\Concerns; use Closure; +use Filament\Actions\Action; trait HasNotifications { @@ -10,6 +11,8 @@ trait HasNotifications protected string | Closure | null $databaseNotificationsPolling = '30s'; + protected ?Closure $modifyDatabaseNotificationsMarkAllAsReadUsing = null; + public function databaseNotifications(bool | Closure $condition = true): static { $this->hasDatabaseNotifications = $condition; @@ -24,6 +27,13 @@ public function databaseNotificationsPolling(string | Closure | null $interval): return $this; } + public function databaseNotificationsMarkAllAsReadAction(?Closure $callback): static + { + $this->modifyDatabaseNotificationsMarkAllAsReadUsing = $callback; + + return $this; + } + public function hasDatabaseNotifications(): bool { return (bool) $this->evaluate($this->hasDatabaseNotifications); @@ -33,4 +43,21 @@ public function getDatabaseNotificationsPollingInterval(): ?string { return $this->evaluate($this->databaseNotificationsPolling); } + + public function getDatabaseNotificationsMarkAllAsReadAction(): Action + { + $action = Action::make('markAllNotificationsAsRead') + ->link() + ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) + ->extraAttributes(['tabindex' => '-1']) + ->action('markAllNotificationsAsRead'); + + if ($this->modifyDatabaseNotificationsMarkAllAsReadUsing) { + $action = $this->evaluate($this->modifyDatabaseNotificationsMarkAllAsReadUsing, [ + 'action' => $action, + ]) ?? $action; + } + + return $action; + } } From 364d1c8b5b43efcb98cb77e74ec6ad2afe872e28 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 11:57:02 -0300 Subject: [PATCH 03/11] Mark All as Read action within the database notifications modal. --- .../src/Livewire/DatabaseNotifications.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 4092c4c2027..3d089bb0cc5 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -118,10 +118,18 @@ public function getTrigger(): ?View public function getMarkAllNotificationsAsReadAction(): Action { - return static::$markAllNotificationsAsReadAction; + if ($action = static::$markAllNotificationsAsReadAction) { + return $action; + } + + return Action::make('markAllNotificationsAsRead') + ->link() + ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) + ->extraAttributes(['tabindex' => '-1']) + ->action('markAllNotificationsAsRead'); } - public function getUser(): Model | Authenticatable | null + public function getUser(): Model|Authenticatable|null { return auth(static::$authGuard)->user(); } From ccbf56dc3202cfb52d48d4acd730da6216ccac57 Mon Sep 17 00:00:00 2001 From: leandrocfe Date: Thu, 5 Sep 2024 15:07:50 +0000 Subject: [PATCH 04/11] chore: fix code style --- .../resources/views/components/database/modal/actions.blade.php | 2 +- .../resources/views/components/database/modal/index.blade.php | 2 +- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/notifications/resources/views/components/database/modal/actions.blade.php b/packages/notifications/resources/views/components/database/modal/actions.blade.php index f1e40cc0655..fa5e62347be 100644 --- a/packages/notifications/resources/views/components/database/modal/actions.blade.php +++ b/packages/notifications/resources/views/components/database/modal/actions.blade.php @@ -1,7 +1,7 @@ @props([ 'notifications', 'unreadNotificationsCount', - 'markAllNotificationsAsReadAction' + 'markAllNotificationsAsReadAction', ])
class('mt-2 flex gap-x-3') }}> diff --git a/packages/notifications/resources/views/components/database/modal/index.blade.php b/packages/notifications/resources/views/components/database/modal/index.blade.php index 2664f6bc9c4..f569e2fd099 100644 --- a/packages/notifications/resources/views/components/database/modal/index.blade.php +++ b/packages/notifications/resources/views/components/database/modal/index.blade.php @@ -1,7 +1,7 @@ @props([ 'notifications', 'unreadNotificationsCount', - 'markAllNotificationsAsReadAction' + 'markAllNotificationsAsReadAction', ]) @php diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 3d089bb0cc5..ac5735e12b7 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -129,7 +129,7 @@ public function getMarkAllNotificationsAsReadAction(): Action ->action('markAllNotificationsAsRead'); } - public function getUser(): Model|Authenticatable|null + public function getUser(): Model | Authenticatable | null { return auth(static::$authGuard)->user(); } From e89eae3c27fa437f879c0fa0830e099cc91fa3b3 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Wed, 4 Dec 2024 11:23:20 -0300 Subject: [PATCH 05/11] rename action --- .../resources/views/database-notifications.blade.php | 2 +- .../notifications/src/Livewire/DatabaseNotifications.php | 7 +++++-- packages/panels/src/Livewire/DatabaseNotifications.php | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index b3e3bfdc51f..061c5a8f44b 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,7 +1,7 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); - $markAllNotificationsAsReadAction = $this->getMarkAllNotificationsAsReadAction(); + $markAllNotificationsAsReadAction = $this->markAllNotificationsAsReadAction(); @endphp
Date: Wed, 4 Dec 2024 14:32:07 +0000 Subject: [PATCH 06/11] chore: fix code style --- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index a69fd40a1fa..15025caad11 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -21,8 +21,8 @@ class DatabaseNotifications extends Component implements HasActions { - use WithPagination; use InteractsWithActions; + use WithPagination; public static bool $isPaginated = true; From d71eb2d8e38aad7b213581a10768bc9842a0f20a Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 9 Dec 2024 09:30:33 -0300 Subject: [PATCH 07/11] render the action in the view --- .../resources/views/database-notifications.blade.php | 3 +-- .../notifications/src/Livewire/DatabaseNotifications.php | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index 061c5a8f44b..758658be8e2 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,7 +1,6 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); - $markAllNotificationsAsReadAction = $this->markAllNotificationsAsReadAction(); @endphp
@if ($broadcastChannel = $this->getBroadcastChannel()) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 15025caad11..0bc94385928 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -6,6 +6,8 @@ use Filament\Actions\Action; use Filament\Actions\Concerns\InteractsWithActions; use Filament\Actions\Contracts\HasActions; +use Filament\Forms\Concerns\InteractsWithForms; +use Filament\Forms\Contracts\HasForms; use Filament\Notifications\Notification; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Pagination\Paginator; @@ -19,9 +21,10 @@ use Livewire\Component; use Livewire\WithPagination; -class DatabaseNotifications extends Component implements HasActions +class DatabaseNotifications extends Component implements HasForms, HasActions { use InteractsWithActions; + use InteractsWithForms; use WithPagination; public static bool $isPaginated = true; From 5dca5ede194c9e9059553c1bd9376e386834ce4b Mon Sep 17 00:00:00 2001 From: leandrocfe Date: Mon, 9 Dec 2024 12:41:08 +0000 Subject: [PATCH 08/11] chore: fix code style --- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 0bc94385928..4d0e14c533d 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -21,7 +21,7 @@ use Livewire\Component; use Livewire\WithPagination; -class DatabaseNotifications extends Component implements HasForms, HasActions +class DatabaseNotifications extends Component implements HasActions, HasForms { use InteractsWithActions; use InteractsWithForms; From 6a245f67dfb3f2bc7d37984923468988f389947b Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 2 Feb 2025 19:59:50 +0000 Subject: [PATCH 09/11] Refactor to override DB notifications LW component --- .../database/modal/actions.blade.php | 9 ++++- .../components/database/modal/index.blade.php | 2 + .../views/database-notifications.blade.php | 1 + .../src/Livewire/DatabaseNotifications.php | 17 +++++--- .../src/Livewire/DatabaseNotifications.php | 6 --- .../src/Panel/Concerns/HasComponents.php | 3 +- .../src/Panel/Concerns/HasNotifications.php | 40 +++++++------------ 7 files changed, 37 insertions(+), 41 deletions(-) diff --git a/packages/notifications/resources/views/components/database/modal/actions.blade.php b/packages/notifications/resources/views/components/database/modal/actions.blade.php index fa5e62347be..934abccf779 100644 --- a/packages/notifications/resources/views/components/database/modal/actions.blade.php +++ b/packages/notifications/resources/views/components/database/modal/actions.blade.php @@ -1,12 +1,17 @@ @props([ 'notifications', 'unreadNotificationsCount', + 'clearNotificationsAction', 'markAllNotificationsAsReadAction', ])
class('mt-2 flex gap-x-3') }}> - @if ($unreadNotificationsCount && $markAllNotificationsAsReadAction) - {{ ($markAllNotificationsAsReadAction)->isVisible() ? $markAllNotificationsAsReadAction : '' }} + @if ($unreadNotificationsCount && $markAllNotificationsAsReadAction?->isVisible()) + {{ $markAllNotificationsAsReadAction }} + @endif + + @if ($clearNotificationsAction?->isVisible()) + {{ $clearNotificationsAction }} @endif
diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index 758658be8e2..d38b85616ab 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -18,6 +18,7 @@ class="flex" diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 4d0e14c533d..cb748723f1d 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -35,8 +35,6 @@ class DatabaseNotifications extends Component implements HasActions, HasForms public static ?string $authGuard = null; - public static ?Action $markAllNotificationsAsReadAction = null; - #[On('databaseNotificationsSent')] public function refresh(): void {} @@ -124,10 +122,6 @@ public function getTrigger(): ?View public function markAllNotificationsAsReadAction(): Action { - if ($action = static::$markAllNotificationsAsReadAction) { - return $action; - } - return Action::make('markAllNotificationsAsRead') ->link() ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) @@ -135,6 +129,17 @@ public function markAllNotificationsAsReadAction(): Action ->action('markAllNotificationsAsRead'); } + public function clearNotificationsAction(): Action + { + return Action::make('clearNotifications') + ->link() + ->color('danger') + ->label(__('filament-notifications::database.modal.actions.clear.label')) + ->extraAttributes(['tabindex' => '-1']) + ->action('clearNotifications') + ->close(); + } + public function getUser(): Model | Authenticatable | null { return auth(static::$authGuard)->user(); diff --git a/packages/panels/src/Livewire/DatabaseNotifications.php b/packages/panels/src/Livewire/DatabaseNotifications.php index 7bf84588faf..8ce0e9d67b8 100644 --- a/packages/panels/src/Livewire/DatabaseNotifications.php +++ b/packages/panels/src/Livewire/DatabaseNotifications.php @@ -2,7 +2,6 @@ namespace Filament\Livewire; -use Filament\Actions\Action; use Filament\Facades\Filament; use Filament\Notifications\Livewire\DatabaseNotifications as BaseComponent; use Illuminate\Contracts\Auth\Authenticatable; @@ -25,9 +24,4 @@ public function getTrigger(): View { return view('filament-panels::components.topbar.database-notifications-trigger'); } - - public function markAllNotificationsAsReadAction(): Action - { - return Filament::getDatabaseNotificationsMarkAllAsReadAction(); - } } diff --git a/packages/panels/src/Panel/Concerns/HasComponents.php b/packages/panels/src/Panel/Concerns/HasComponents.php index ab434288735..76b1a254ca1 100644 --- a/packages/panels/src/Panel/Concerns/HasComponents.php +++ b/packages/panels/src/Panel/Concerns/HasComponents.php @@ -5,7 +5,6 @@ use Closure; use Filament\Auth\Pages\EditProfile; use Filament\Clusters\Cluster; -use Filament\Livewire\DatabaseNotifications; use Filament\Livewire\GlobalSearch; use Filament\Livewire\Notifications; use Filament\Livewire\Sidebar; @@ -484,7 +483,7 @@ public function livewireComponents(array $components): static protected function registerLivewireComponents(): void { if (! $this->hasCachedComponents()) { - $this->queueLivewireComponentForRegistration(DatabaseNotifications::class); + $this->queueLivewireComponentForRegistration($this->getDatabaseNotificationsLivewireComponent()); $this->queueLivewireComponentForRegistration(EditProfile::class); $this->queueLivewireComponentForRegistration(GlobalSearch::class); $this->queueLivewireComponentForRegistration(Notifications::class); diff --git a/packages/panels/src/Panel/Concerns/HasNotifications.php b/packages/panels/src/Panel/Concerns/HasNotifications.php index bcb46007d7a..8425e1d5b66 100644 --- a/packages/panels/src/Panel/Concerns/HasNotifications.php +++ b/packages/panels/src/Panel/Concerns/HasNotifications.php @@ -3,7 +3,7 @@ namespace Filament\Panel\Concerns; use Closure; -use Filament\Actions\Action; +use Filament\Livewire\DatabaseNotifications; trait HasNotifications { @@ -11,34 +11,36 @@ trait HasNotifications protected bool | Closure $hasLazyLoadedDatabaseNotifications = true; + protected string | Closure | null $databaseNotificationsLivewireComponent = null; + protected string | Closure | null $databaseNotificationsPolling = '30s'; - protected ?Closure $modifyDatabaseNotificationsMarkAllAsReadUsing = null; - public function databaseNotifications(bool | Closure $condition = true, bool | Closure $isLazy = true): static + public function databaseNotifications(bool | Closure $condition = true, string | Closure | null $livewireComponent = null, bool | Closure $isLazy = true): static { $this->hasDatabaseNotifications = $condition; + $this->databaseNotificationsLivewireComponent($livewireComponent); $this->lazyLoadedDatabaseNotifications($isLazy); return $this; } - public function lazyLoadedDatabaseNotifications(bool | Closure $condition = true): static + public function databaseNotificationsLivewireComponent(string | Closure | null $livewireComponent): static { - $this->hasLazyLoadedDatabaseNotifications = $condition; + $this->databaseNotificationsLivewireComponent = $livewireComponent; return $this; } - public function databaseNotificationsPolling(string | Closure | null $interval): static + public function lazyLoadedDatabaseNotifications(bool | Closure $condition = true): static { - $this->databaseNotificationsPolling = $interval; + $this->hasLazyLoadedDatabaseNotifications = $condition; return $this; } - public function databaseNotificationsMarkAllAsReadAction(?Closure $callback): static + public function databaseNotificationsPolling(string | Closure | null $interval): static { - $this->modifyDatabaseNotificationsMarkAllAsReadUsing = $callback; + $this->databaseNotificationsPolling = $interval; return $this; } @@ -53,25 +55,13 @@ public function hasLazyLoadedDatabaseNotifications(): bool return (bool) $this->evaluate($this->hasLazyLoadedDatabaseNotifications); } - public function getDatabaseNotificationsPollingInterval(): ?string + public function getDatabaseNotificationsLivewireComponent(): string { - return $this->evaluate($this->databaseNotificationsPolling); + return $this->evaluate($this->databaseNotificationsLivewireComponent) ?? DatabaseNotifications::class; } - public function getDatabaseNotificationsMarkAllAsReadAction(): Action + public function getDatabaseNotificationsPollingInterval(): ?string { - $action = Action::make('markAllNotificationsAsRead') - ->link() - ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) - ->extraAttributes(['tabindex' => '-1']) - ->action('markAllNotificationsAsRead'); - - if ($this->modifyDatabaseNotificationsMarkAllAsReadUsing) { - $action = $this->evaluate($this->modifyDatabaseNotificationsMarkAllAsReadUsing, [ - 'action' => $action, - ]) ?? $action; - } - - return $action; + return $this->evaluate($this->databaseNotificationsPolling); } } From e9b5b9a20ae72694718a74e47d93e723f0b288e1 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 2 Feb 2025 20:01:30 +0000 Subject: [PATCH 10/11] clean up --- packages/panels/docs/06-notifications.md | 21 --------------------- packages/panels/src/Facades/Filament.php | 1 - packages/panels/src/FilamentManager.php | 6 ------ 3 files changed, 28 deletions(-) diff --git a/packages/panels/docs/06-notifications.md b/packages/panels/docs/06-notifications.md index 9f4a20da62a..107dfe10b10 100644 --- a/packages/panels/docs/06-notifications.md +++ b/packages/panels/docs/06-notifications.md @@ -33,27 +33,6 @@ public function panel(Panel $panel): Panel } ``` -## Customizing the "Mark All as Read" Action - -To customize the "Mark All as Read" button, you can use the `databaseNotificationsMarkAllAsReadAction()` method, passing a closure that returns an action. All methods that are available to [customize action trigger buttons](../../actions/trigger-button) can be used: - -```php -use Filament\Actions\Action; -use Filament\Panel; - -public function panel(Panel $panel): Panel -{ - return $panel - // ... - ->databaseNotifications() - ->databaseNotificationsMarkAllAsReadAction(fn (Action $action) => $action - ->button() - ->color('success') - ->label('Read All') - ); -} -``` - ## Setting up websockets in a panel The Panel Builder comes with a level of inbuilt support for real-time broadcast and database notifications. However there are a number of areas you will need to install and configure to wire everything up and get it working. diff --git a/packages/panels/src/Facades/Filament.php b/packages/panels/src/Facades/Filament.php index 3dcc96d4ed9..857a043def9 100644 --- a/packages/panels/src/Facades/Filament.php +++ b/packages/panels/src/Facades/Filament.php @@ -112,7 +112,6 @@ * @method static string getVerifyEmailChangeUrl(MustVerifyEmail | Model | Authenticatable $user, string $newEmail, array $parameters = []) * @method static string getBlockEmailChangeVerificationUrl(MustVerifyEmail | Model | Authenticatable $user, string $newEmail, string $verificationSignature, array $parameters = []) * @method static array getWidgets() - * @method static Action getDatabaseNotificationsMarkAllAsReadAction() * @method static bool hasBreadcrumbs() * @method static bool hasCollapsibleNavigationGroups() * @method static bool hasDarkMode() diff --git a/packages/panels/src/FilamentManager.php b/packages/panels/src/FilamentManager.php index 2494eb3b910..a1451a12eac 100644 --- a/packages/panels/src/FilamentManager.php +++ b/packages/panels/src/FilamentManager.php @@ -619,12 +619,6 @@ public function getWidgets(): array { return $this->getCurrentOrDefaultPanel()->getWidgets(); } - - public function getDatabaseNotificationsMarkAllAsReadAction(): Action - { - return $this->currentPanel->getDatabaseNotificationsMarkAllAsReadAction(); - } - public function hasBreadcrumbs(): bool { return $this->getCurrentOrDefaultPanel()->hasBreadcrumbs(); From 57a5b51334a46dbdaa4835a86a34ee22e92845d2 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 2 Feb 2025 20:03:59 +0000 Subject: [PATCH 11/11] improve type safety and cs --- packages/panels/src/FilamentManager.php | 1 + .../panels/src/Panel/Concerns/HasNotifications.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/panels/src/FilamentManager.php b/packages/panels/src/FilamentManager.php index a1451a12eac..0b059a6cd93 100644 --- a/packages/panels/src/FilamentManager.php +++ b/packages/panels/src/FilamentManager.php @@ -619,6 +619,7 @@ public function getWidgets(): array { return $this->getCurrentOrDefaultPanel()->getWidgets(); } + public function hasBreadcrumbs(): bool { return $this->getCurrentOrDefaultPanel()->hasBreadcrumbs(); diff --git a/packages/panels/src/Panel/Concerns/HasNotifications.php b/packages/panels/src/Panel/Concerns/HasNotifications.php index 8425e1d5b66..7b5b6b9560a 100644 --- a/packages/panels/src/Panel/Concerns/HasNotifications.php +++ b/packages/panels/src/Panel/Concerns/HasNotifications.php @@ -4,6 +4,7 @@ use Closure; use Filament\Livewire\DatabaseNotifications; +use Livewire\Component; trait HasNotifications { @@ -15,6 +16,9 @@ trait HasNotifications protected string | Closure | null $databaseNotificationsPolling = '30s'; + /** + * @param class-string | Closure | null $livewireComponent + */ public function databaseNotifications(bool | Closure $condition = true, string | Closure | null $livewireComponent = null, bool | Closure $isLazy = true): static { $this->hasDatabaseNotifications = $condition; @@ -24,9 +28,12 @@ public function databaseNotifications(bool | Closure $condition = true, string | return $this; } - public function databaseNotificationsLivewireComponent(string | Closure | null $livewireComponent): static + /** + * @param class-string | Closure | null $component + */ + public function databaseNotificationsLivewireComponent(string | Closure | null $component): static { - $this->databaseNotificationsLivewireComponent = $livewireComponent; + $this->databaseNotificationsLivewireComponent = $component; return $this; } @@ -55,6 +62,9 @@ public function hasLazyLoadedDatabaseNotifications(): bool return (bool) $this->evaluate($this->hasLazyLoadedDatabaseNotifications); } + /** + * @return class-string + */ public function getDatabaseNotificationsLivewireComponent(): string { return $this->evaluate($this->databaseNotificationsLivewireComponent) ?? DatabaseNotifications::class;