/
NotificationManager.php
44 lines (34 loc) · 1.07 KB
/
NotificationManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Filament;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\Livewire;
use Livewire\Response;
class NotificationManager
{
protected array $notifications = [];
public function notify(string $status, string $message): void
{
session()->push('filament.notifications', [
'id' => uniqid(),
'status' => $status,
'message' => (string) Str::of($message)->markdown()->sanitizeHtml(),
]);
$this->notifications = session()->get('filament.notifications');
}
public function handleLivewireResponses(Component $component, Response $response): Response
{
if (! Livewire::isLivewireRequest()) {
return $response;
}
if ($component->redirectTo !== null) {
return $response;
}
$notifications = $this->notifications;
session()->forget('filament.notifications');
if (count($notifications) > 0) {
$component->dispatchBrowserEvent('notify', $notifications);
}
return $response;
}
}