Fix alert banners (#1492) (#2177)

This commit is contained in:
Lance Pioch
2026-02-15 15:34:09 -05:00
committed by GitHub
parent 810f237547
commit 85d5f2ec3f
4 changed files with 39 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ use Filament\Notifications\Concerns\HasStatus;
use Filament\Notifications\Concerns\HasTitle;
use Filament\Support\Components\ViewComponent;
use Illuminate\Contracts\Support\Arrayable;
use Livewire\Livewire;
final class AlertBanner extends ViewComponent implements Arrayable
{
@@ -83,7 +84,13 @@ final class AlertBanner extends ViewComponent implements Arrayable
public function send(): AlertBanner
{
session()->push('alert-banners', $this->toArray());
$data = $this->toArray();
if (Livewire::isLivewireRequest()) {
$data['from_livewire'] = true;
}
session()->push('alert-banners', $data);
return $this;
}

View File

@@ -0,0 +1,15 @@
<?php
namespace App\Livewire;
use Filament\Notifications\Collection;
class AlertBannerCollection extends Collection
{
public static function fromLivewire($value): static
{
return (new static($value))->transform(
fn (array $alertBanner): AlertBanner => AlertBanner::fromArray($alertBanner),
);
}
}

View File

@@ -2,25 +2,35 @@
namespace App\Livewire;
use Filament\Notifications\Collection;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Component;
class AlertBannerContainer extends Component
{
public Collection $alertBanners;
public AlertBannerCollection $alertBanners;
public function mount(): void
{
$this->alertBanners = new Collection();
$this->pullFromSession();
$this->alertBanners = new AlertBannerCollection();
foreach (session()->pull('alert-banners', []) as $alertBanner) {
// Alerts created during Livewire requests should have been consumed by the event handler on the same page.
if (!empty($alertBanner['from_livewire'])) {
// If they weren't, then discard them instead of showing on the wrong page.
continue;
}
$alertBanner = AlertBanner::fromArray($alertBanner);
$this->alertBanners->put($alertBanner->getId(), $alertBanner);
}
}
#[On('alertBannerSent')]
public function pullFromSession(): void
{
foreach (session()->pull('alert-banners', []) as $alertBanner) {
unset($alertBanner['from_livewire']);
$alertBanner = AlertBanner::fromArray($alertBanner);
$this->alertBanners->put($alertBanner->getId(), $alertBanner);
}

View File

@@ -34,7 +34,7 @@ use Illuminate\Support\ServiceProvider;
use Livewire\Component;
use Livewire\Livewire;
use function Livewire\on;
use function Livewire\before;
use function Livewire\store;
class FilamentServiceProvider extends ServiceProvider
@@ -74,7 +74,7 @@ class FilamentServiceProvider extends ServiceProvider
fn () => Blade::render("@vite(['resources/js/app.js'])"),
);
on('dehydrate', function (Component $component) {
before('dehydrate', function (Component $component) {
if (!Livewire::isLivewireRequest()) {
return;
}