forked from grokability/snipe-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifications.php
More file actions
242 lines (216 loc) · 6.76 KB
/
Notifications.php
File metadata and controls
242 lines (216 loc) · 6.76 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace App\Livewire;
use Livewire\Component;
use Livewire\Attributes\On;
/**
* Live (in-page) notifications component.
*
* Supports:
* - Dispatching events with basic (type, message)
* - Extended payload (type, title, message, description, icon, html, confetti, auto, tag)
* - Replacing an existing alert by tag (so you can "update progress" style messages)
* - Dismissing by id or by tag
* - Optional legacy string-only usage
*
* JS Examples (Livewire v3):
*
* Livewire.dispatch('showNotification', {
* type: 'success',
* title: 'Asset Saved',
* message: 'MacBook Pro added.',
* description: 'Tag: MBP-4418<br>Assigned to Jane Doe',
* icon: 'fas fa-laptop',
* html: true,
* tag: 'asset-create'
* });
*
* Update same tagged notification later:
* Livewire.dispatch('showNotification', {
* type: 'info',
* message: 'Processing (70%)',
* tag: 'bulk-import'
* });
*
* Dismiss by tag:
* Livewire.dispatch('dismissNotificationByTag', 'bulk-import');
*
* Dismiss by id (you usually call this from a close button in Blade):
* Livewire.dispatch('dismissNotification', someId);
*/
class Notifications extends Component
{
/**
* Each alert structure:
* [
* 'id' => string,
* 'type' => 'success'|'danger'|'warning'|'info',
* 'tag' => string|null,
* 'title' => string|null,
* 'message' => string,
* 'description' => string|null,
* 'icon' => string|null,
* 'html' => bool,
* 'created_at' => int (timestamp)
* ]
*
* @var array<int, array<string,mixed>>
*/
public array $liveAlerts=[];
/**
* Main notification listener.
* We bind both 'showNotification' (your current event) and 'notify' (optional alias).
*/
#[On('showNotification')]
#[On('notify')]
public function notify(
$type=null,
$message=null,
$title=null,
$description=null,
$icon=null,
$html=null,
$tag=null,
$payload=null // wrapper form: { payload: { ... } }
): void {
// Wrapper form: { payload: { ...full data... } }
if (is_array($payload)) {
$this->ingestArray($payload);
return;
}
// Legacy simple usage: Livewire.dispatch('showNotification', 'Quick saved!')
if (is_string($type) && $message === null && $title === null) {
$this->pushAlert([
'type' => 'success',
'message' => $type,
'tag' => $tag,
]);
return;
}
// Must have a type + message at minimum
if (!$type || !$message) {
return;
}
$this->pushAlert([
'type' => $type,
'message' => $message,
'title' => $title,
'description' => $description,
'icon' => $icon,
'html' => (bool) $html,
'tag' => $tag,
]);
}
/**
* Ingest an associative array payload (supports multiple key name variants).
*/
protected function ingestArray(array $arr): void
{
$this->pushAlert([
'type' => $arr['type'] ?? $arr['level'] ?? 'info',
'message' => $arr['message'] ?? $arr['msg'] ?? null,
'title' => $arr['title'] ?? $arr['heading'] ?? null,
'description' => $arr['description'] ?? $arr['desc'] ?? null,
'icon' => $arr['icon'] ?? null,
'html' => (bool) ($arr['html'] ?? false),
'tag' => $arr['tag'] ?? null,
]);
}
/**
* Normalize a semantic type into a Bootstrap alert class fragment.
*/
protected function normalizeType(string $type): string
{
return match (strtolower($type)) {
'error', 'danger', 'fail', 'failed' => 'danger',
'ok', 'status' => 'success',
default => strtolower($type),
};
}
/**
* Provide default icon classes if none supplied.
*/
protected function defaultIcon(string $type): string
{
return match ($type) {
'success' => 'fas fa-check faa-pulse animated',
'danger' => 'fas fa-exclamation-triangle faa-pulse animated',
'warning' => 'fas fa-exclamation-triangle faa-pulse animated',
default => 'fas fa-info-circle faa-pulse animated',
};
}
/**
* Insert or replace an alert (if tag provided & already exists).
*/
protected function pushAlert(array $data): void
{
if (empty($data['message'])) {
return;
}
$alert = $this->buildAlert($data);
if ($alert['tag'] !== null && $this->replaceTaggedAlert($alert)) {
return;
}
$this->addAlert($alert);
}
protected function buildAlert(array $data): array
{
$type = $this->normalizeType($data['type'] ?? 'info');
return [
'id' => uniqid('al_', true),
'type' => $type,
'tag' => $data['tag'] ?? null,
'title' => $data['title'] ?? null,
'message' => $data['message'],
'description' => $data['description'] ?? null,
'icon' => $data['icon'] ?? $this->defaultIcon($type),
'html' => $data['html'] ?? false,
'created_at' => time(),
];
}
protected function replaceTaggedAlert(array $alert): bool
{
foreach ($this->liveAlerts as $index => $liveAlert) {
if ($liveAlert['tag'] === $alert['tag']) {
$this->liveAlerts[$index] = $alert;
return true;
}
}
return false;
}
protected function addAlert(array $alert): void
{
$this->liveAlerts[] = $alert;
}
/**
* Dismiss by alert unique ID.
*/
#[On('dismissNotification')]
public function dismiss(string $id): void
{
$this->liveAlerts = array_values(
array_filter($this->liveAlerts, fn ($a) => $a['id'] !== $id)
);
}
/**
* Dismiss all alerts sharing a tag.
*/
#[On('dismissNotificationByTag')]
public function dismissByTag(string $tag): void
{
$this->liveAlerts = array_values(
array_filter($this->liveAlerts, fn ($a) => $a['tag'] !== $tag)
);
}
/**
* Dismiss everything (add a button if you want).
*/
#[On('dismissAllNotifications')]
public function dismissAll(): void
{
$this->liveAlerts = [];
}
public function render()
{
return view('livewire.notifications');
}
}