Skip to content

Commit

Permalink
Refactor to override DB notifications LW component
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Feb 2, 2025
1 parent 340cbc0 commit 6a245f6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
@props([
'notifications',
'unreadNotificationsCount',
'clearNotificationsAction',
'markAllNotificationsAsReadAction',
])

<div {{ $attributes->class('mt-2 flex gap-x-3') }}>
@if ($unreadNotificationsCount && $markAllNotificationsAsReadAction)
{{ ($markAllNotificationsAsReadAction)->isVisible() ? $markAllNotificationsAsReadAction : '' }}
@if ($unreadNotificationsCount && $markAllNotificationsAsReadAction?->isVisible())
{{ $markAllNotificationsAsReadAction }}
@endif

@if ($clearNotificationsAction?->isVisible())
{{ $clearNotificationsAction }}
@endif

<x-filament::link
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@props([
'notifications',
'unreadNotificationsCount',
'clearNotificationsAction',
'markAllNotificationsAsReadAction',
])

Expand Down Expand Up @@ -34,6 +35,7 @@
<x-filament-notifications::database.modal.actions
:notifications="$notifications"
:unread-notifications-count="$unreadNotificationsCount"
:clear-notifications-action="$clearNotificationsAction"
:mark-all-notifications-as-read-action="$markAllNotificationsAsReadAction"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class="flex"
<x-filament-notifications::database.modal
:notifications="$notifications"
:unread-notifications-count="$unreadNotificationsCount"
:clear-notifications-action="$this->clearNotificationsAction"
:mark-all-notifications-as-read-action="$this->markAllNotificationsAsReadAction"
/>

Expand Down
17 changes: 11 additions & 6 deletions packages/notifications/src/Livewire/DatabaseNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -124,17 +122,24 @@ 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'))
->extraAttributes(['tabindex' => '-1'])
->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();
Expand Down
6 changes: 0 additions & 6 deletions packages/panels/src/Livewire/DatabaseNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +24,4 @@ public function getTrigger(): View
{
return view('filament-panels::components.topbar.database-notifications-trigger');
}

public function markAllNotificationsAsReadAction(): Action
{
return Filament::getDatabaseNotificationsMarkAllAsReadAction();
}
}
3 changes: 1 addition & 2 deletions packages/panels/src/Panel/Concerns/HasComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 15 additions & 25 deletions packages/panels/src/Panel/Concerns/HasNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,44 @@
namespace Filament\Panel\Concerns;

use Closure;
use Filament\Actions\Action;
use Filament\Livewire\DatabaseNotifications;

trait HasNotifications
{
protected bool | Closure $hasDatabaseNotifications = false;

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;
}
Expand All @@ -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);
}
}

0 comments on commit 6a245f6

Please sign in to comment.