@7365admin1/layer-common 4.17.1-staging.332 → 4.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -4
- package/components/DocumentForm.vue +112 -407
- package/components/DocumentManagement.vue +12 -213
- package/components/EntryPass/QrTemplatePreview.vue +25 -21
- package/components/EntryPassInformation.vue +24 -119
- package/components/Facility/BookingSetup.vue +0 -63
- package/components/HidIntercomManagement.vue +43 -193
- package/components/HidReaderManagement.vue +0 -26
- package/components/HidReaderUserRoster.vue +2 -32
- package/components/HidUserEnrollment.vue +213 -779
- package/components/InvitationClientForm.vue +1 -19
- package/components/Layout/Header.vue +0 -26
- package/components/VisitorManagement.vue +5 -267
- package/composables/useAccessManagement.ts +0 -5
- package/composables/useDocument.ts +0 -7
- package/composables/useFacility.ts +0 -11
- package/composables/useHidAmico.ts +0 -97
- package/composables/useHidNavigation.ts +1 -25
- package/composables/usePeople.ts +0 -19
- package/composables/useSecurityPermission.ts +7 -8
- package/composables/useWebUsb.ts +11 -14
- package/package.json +1 -1
- package/pages/[org]/[site]/access-mgmt/administrator/index.vue +1 -1
- package/pages/[org]/[site]/access-mgmt/hid-cards/index.vue +1 -1
- package/pages/[org]/[site]/access-mgmt/hid-readers/index.vue +1 -1
- package/types/document.d.ts +0 -14
- package/types/facility.d.ts +0 -4
- package/types/people.d.ts +1 -4
- package/utils/materialSymbols.ts +0 -3
- package/components/HidAccessPermissions.vue +0 -424
- package/components/HidProfileQrCode.vue +0 -323
- package/components/NotificationBell.vue +0 -321
- package/composables/useNotification.ts +0 -78
- package/pages/[org]/[site]/access-mgmt/hid-qr-code/index.vue +0 -23
- package/utils/hid-permission-assignments.ts +0 -190
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
MY HID QR CODE.
|
|
3
|
-
|
|
4
|
-
The signed-in member's own pass for one reader. Every other HID screen acts on
|
|
5
|
-
somebody else - a resident being enrolled, a visitor being issued a pass. This
|
|
6
|
-
one has no subject picker at all, because the server takes the caller from the
|
|
7
|
-
session and there is no user id to send.
|
|
8
|
-
|
|
9
|
-
Issuing a code does NOT grant access. A member's door permission comes from the
|
|
10
|
-
HID permissions screen, and minting an access rule here would let anyone who
|
|
11
|
-
can press Generate admit themselves. So the screen reports what the permissions
|
|
12
|
-
screen says (`assigned`) and warns when a code would be refused at the door,
|
|
13
|
-
rather than handing out something that silently fails.
|
|
14
|
-
-->
|
|
15
|
-
<template>
|
|
16
|
-
<div class="hid-profile-qr">
|
|
17
|
-
<div class="hid-profile-qr__header">
|
|
18
|
-
<h2 class="hid-profile-qr__title">HID QR Code</h2>
|
|
19
|
-
<AppSelect
|
|
20
|
-
v-if="readerOptions.length"
|
|
21
|
-
v-model="selectedReaderId"
|
|
22
|
-
class="reader-select"
|
|
23
|
-
:items="readerOptions"
|
|
24
|
-
@update:model-value="loadProfile"
|
|
25
|
-
/>
|
|
26
|
-
</div>
|
|
27
|
-
|
|
28
|
-
<v-card class="screen-modal hid-profile-qr__card">
|
|
29
|
-
<v-card-text class="screen-modal__body">
|
|
30
|
-
<div v-if="loading" class="hid-profile-qr__state">Loading...</div>
|
|
31
|
-
|
|
32
|
-
<div v-else-if="!readerOptions.length" class="hid-profile-qr__state">
|
|
33
|
-
No HID reader is set up for this site yet.
|
|
34
|
-
</div>
|
|
35
|
-
|
|
36
|
-
<template v-else>
|
|
37
|
-
<p class="hid-profile-qr__reader">
|
|
38
|
-
{{ profile?.readerName || "HID reader" }}
|
|
39
|
-
<span>{{ profile?.portalName || "Portal not configured" }}</span>
|
|
40
|
-
</p>
|
|
41
|
-
|
|
42
|
-
<!-- A code this reader cannot read is worse than no code, so the
|
|
43
|
-
capability is checked before anything is offered. -->
|
|
44
|
-
<v-alert
|
|
45
|
-
v-if="profile && !profile.qrEnabled"
|
|
46
|
-
type="warning"
|
|
47
|
-
variant="tonal"
|
|
48
|
-
density="compact"
|
|
49
|
-
class="mb-4"
|
|
50
|
-
>
|
|
51
|
-
This reader is not set up to read QR codes.
|
|
52
|
-
</v-alert>
|
|
53
|
-
|
|
54
|
-
<!-- The honest part: a code opens the door only for a member the
|
|
55
|
-
permissions screen already admits. `assigned` is null when the
|
|
56
|
-
lookup failed, which is not the same as "no". -->
|
|
57
|
-
<v-alert
|
|
58
|
-
v-else-if="profile && profile.assigned === false"
|
|
59
|
-
type="warning"
|
|
60
|
-
variant="tonal"
|
|
61
|
-
density="compact"
|
|
62
|
-
class="mb-4"
|
|
63
|
-
>
|
|
64
|
-
You are not assigned to this reader, so this code will be refused at
|
|
65
|
-
the door. Ask a property manager to add you on the HID permissions
|
|
66
|
-
screen.
|
|
67
|
-
</v-alert>
|
|
68
|
-
|
|
69
|
-
<div v-if="qrImage" class="hid-profile-qr__code">
|
|
70
|
-
<img :src="qrImage" alt="Your HID QR code" width="220" height="220" />
|
|
71
|
-
<p class="hid-profile-qr__issued">
|
|
72
|
-
Issued {{ formatIssuedAt(profile?.issuedAt) }}
|
|
73
|
-
</p>
|
|
74
|
-
</div>
|
|
75
|
-
<p v-else class="hid-profile-qr__state">
|
|
76
|
-
You have no QR code on this reader yet.
|
|
77
|
-
</p>
|
|
78
|
-
|
|
79
|
-
<div class="hid-profile-qr__actions">
|
|
80
|
-
<AppButton
|
|
81
|
-
:disabled="working || !profile?.qrEnabled"
|
|
82
|
-
@click="generate"
|
|
83
|
-
>
|
|
84
|
-
{{ profile?.issued ? "Regenerate" : "Generate QR Code" }}
|
|
85
|
-
</AppButton>
|
|
86
|
-
<AppButton
|
|
87
|
-
v-if="qrImage"
|
|
88
|
-
variant="ghost"
|
|
89
|
-
icon="mdi-download"
|
|
90
|
-
@click="downloadQr"
|
|
91
|
-
>
|
|
92
|
-
Download
|
|
93
|
-
</AppButton>
|
|
94
|
-
<AppButton
|
|
95
|
-
v-if="profile?.issued"
|
|
96
|
-
variant="ghost"
|
|
97
|
-
:disabled="working"
|
|
98
|
-
@click="remove"
|
|
99
|
-
>
|
|
100
|
-
Remove
|
|
101
|
-
</AppButton>
|
|
102
|
-
</div>
|
|
103
|
-
|
|
104
|
-
<p class="field-hint">
|
|
105
|
-
Regenerating replaces the previous code. The old one stops working
|
|
106
|
-
immediately.
|
|
107
|
-
</p>
|
|
108
|
-
</template>
|
|
109
|
-
</v-card-text>
|
|
110
|
-
</v-card>
|
|
111
|
-
|
|
112
|
-
<v-snackbar v-model="snackbar.show" :color="snackbar.color" timeout="4000">
|
|
113
|
-
{{ snackbar.message }}
|
|
114
|
-
</v-snackbar>
|
|
115
|
-
</div>
|
|
116
|
-
</template>
|
|
117
|
-
|
|
118
|
-
<script setup lang="ts">
|
|
119
|
-
import QRCode from "qrcode";
|
|
120
|
-
import type { HidProfileQr } from "../composables/useHidAmico";
|
|
121
|
-
|
|
122
|
-
const props = defineProps<{ site: string }>();
|
|
123
|
-
|
|
124
|
-
const { getReaders, getProfileQr, issueProfileQr, deleteProfileQr } = useHidAmico();
|
|
125
|
-
|
|
126
|
-
type HidReaderOption = { _id: string; name?: string; portalName?: string };
|
|
127
|
-
|
|
128
|
-
const readers = ref<HidReaderOption[]>([]);
|
|
129
|
-
const selectedReaderId = ref("");
|
|
130
|
-
const profile = ref<HidProfileQr | null>(null);
|
|
131
|
-
const qrImage = ref("");
|
|
132
|
-
const loading = ref(false);
|
|
133
|
-
const working = ref(false);
|
|
134
|
-
const snackbar = reactive({ show: false, color: "success", message: "" });
|
|
135
|
-
|
|
136
|
-
const readerOptions = computed(() => readers.value.map((reader) => ({
|
|
137
|
-
title: `${reader.name || reader._id} — ${reader.portalName || "Portal not configured"}`,
|
|
138
|
-
value: reader._id,
|
|
139
|
-
})));
|
|
140
|
-
|
|
141
|
-
function showToast(message: string, color: "success" | "error" = "success") {
|
|
142
|
-
snackbar.message = message;
|
|
143
|
-
snackbar.color = color;
|
|
144
|
-
snackbar.show = true;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function toRecord(value: unknown): Record<string, any> {
|
|
148
|
-
return value && typeof value === "object" ? (value as Record<string, any>) : {};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function errorMessage(error: unknown) {
|
|
152
|
-
const record = toRecord(error);
|
|
153
|
-
return String(toRecord(record.data).message || record.message || "Something went wrong.");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function formatIssuedAt(value?: string) {
|
|
157
|
-
if (!value) return "";
|
|
158
|
-
const date = new Date(value);
|
|
159
|
-
return Number.isNaN(date.getTime()) ? "" : date.toLocaleString();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* The image is drawn here, never fetched. The value is the credential itself,
|
|
164
|
-
* so keeping it on this page and out of any URL is the point.
|
|
165
|
-
*/
|
|
166
|
-
async function renderQr(value: string) {
|
|
167
|
-
if (!value) {
|
|
168
|
-
qrImage.value = "";
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
qrImage.value = await QRCode.toDataURL(value, { width: 440, margin: 1 });
|
|
173
|
-
} catch (error) {
|
|
174
|
-
console.error("Unable to render the HID QR code:", error);
|
|
175
|
-
qrImage.value = "";
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Saves the rendered code as a PNG.
|
|
181
|
-
*
|
|
182
|
-
* Straight off the data URL already in the page - no request, so the credential
|
|
183
|
-
* never travels again to be downloaded. Same anchor-click approach `VisitorForm`
|
|
184
|
-
* uses for a visitor pass, and the reader name is in the file name because a
|
|
185
|
-
* member can hold one code per reader.
|
|
186
|
-
*/
|
|
187
|
-
function downloadQr() {
|
|
188
|
-
if (!qrImage.value) return;
|
|
189
|
-
const slug = String(profile.value?.readerName || "reader")
|
|
190
|
-
.trim()
|
|
191
|
-
.replace(/[^a-z0-9]+/gi, "-")
|
|
192
|
-
.replace(/^-|-$/g, "")
|
|
193
|
-
.toLowerCase();
|
|
194
|
-
const link = document.createElement("a");
|
|
195
|
-
link.href = qrImage.value;
|
|
196
|
-
link.download = `hid-qr-${slug || "reader"}.png`;
|
|
197
|
-
link.click();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async function loadReaders() {
|
|
201
|
-
if (!props.site) return;
|
|
202
|
-
const response = toRecord(await getReaders({ site: props.site, page: 1, limit: 100 }));
|
|
203
|
-
const data = toRecord(response.data);
|
|
204
|
-
const items = response.items ?? data.items ?? data.readers ?? [];
|
|
205
|
-
readers.value = (Array.isArray(items) ? items : []).filter((reader) => reader?._id);
|
|
206
|
-
if (!selectedReaderId.value && readers.value.length) {
|
|
207
|
-
selectedReaderId.value = String(readers.value[0]._id);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async function loadProfile() {
|
|
212
|
-
if (!selectedReaderId.value) return;
|
|
213
|
-
loading.value = true;
|
|
214
|
-
try {
|
|
215
|
-
const response = await getProfileQr(selectedReaderId.value);
|
|
216
|
-
profile.value = response?.data ?? null;
|
|
217
|
-
await renderQr(profile.value?.qrValue || "");
|
|
218
|
-
} catch (error: unknown) {
|
|
219
|
-
profile.value = null;
|
|
220
|
-
qrImage.value = "";
|
|
221
|
-
showToast(errorMessage(error), "error");
|
|
222
|
-
} finally {
|
|
223
|
-
loading.value = false;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
async function generate() {
|
|
228
|
-
if (!selectedReaderId.value) return;
|
|
229
|
-
working.value = true;
|
|
230
|
-
try {
|
|
231
|
-
const response = await issueProfileQr(selectedReaderId.value);
|
|
232
|
-
profile.value = response?.data ?? null;
|
|
233
|
-
await renderQr(profile.value?.qrValue || "");
|
|
234
|
-
showToast("QR code generated.");
|
|
235
|
-
} catch (error: unknown) {
|
|
236
|
-
showToast(errorMessage(error), "error");
|
|
237
|
-
} finally {
|
|
238
|
-
working.value = false;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
async function remove() {
|
|
243
|
-
if (!selectedReaderId.value) return;
|
|
244
|
-
if (!window.confirm("Remove your QR code from this reader?")) return;
|
|
245
|
-
working.value = true;
|
|
246
|
-
try {
|
|
247
|
-
const response = await deleteProfileQr(selectedReaderId.value);
|
|
248
|
-
profile.value = response?.data ?? null;
|
|
249
|
-
await renderQr("");
|
|
250
|
-
showToast("QR code removed.");
|
|
251
|
-
} catch (error: unknown) {
|
|
252
|
-
showToast(errorMessage(error), "error");
|
|
253
|
-
} finally {
|
|
254
|
-
working.value = false;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
watch(
|
|
259
|
-
() => props.site,
|
|
260
|
-
async () => {
|
|
261
|
-
await loadReaders();
|
|
262
|
-
await loadProfile();
|
|
263
|
-
},
|
|
264
|
-
{ immediate: true },
|
|
265
|
-
);
|
|
266
|
-
</script>
|
|
267
|
-
|
|
268
|
-
<style scoped>
|
|
269
|
-
.hid-profile-qr__header {
|
|
270
|
-
display: flex;
|
|
271
|
-
align-items: center;
|
|
272
|
-
gap: 12px;
|
|
273
|
-
flex-wrap: wrap;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
.hid-profile-qr__title {
|
|
277
|
-
margin: 0;
|
|
278
|
-
font-size: 18px;
|
|
279
|
-
font-weight: 600;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
.hid-profile-qr__card {
|
|
283
|
-
margin-top: 16px;
|
|
284
|
-
max-width: 460px;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
.hid-profile-qr__reader {
|
|
288
|
-
margin: 0 0 14px;
|
|
289
|
-
font-size: 14px;
|
|
290
|
-
font-weight: 600;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
.hid-profile-qr__reader span {
|
|
294
|
-
display: block;
|
|
295
|
-
color: var(--muted);
|
|
296
|
-
font-size: 12px;
|
|
297
|
-
font-weight: 500;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
.hid-profile-qr__code {
|
|
301
|
-
display: grid;
|
|
302
|
-
place-items: center;
|
|
303
|
-
margin-bottom: 14px;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
.hid-profile-qr__issued {
|
|
307
|
-
margin: 8px 0 0;
|
|
308
|
-
color: var(--muted);
|
|
309
|
-
font-size: 12px;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
.hid-profile-qr__state {
|
|
313
|
-
color: var(--muted);
|
|
314
|
-
font-size: 13.5px;
|
|
315
|
-
padding: 8px 0 14px;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
.hid-profile-qr__actions {
|
|
319
|
-
display: flex;
|
|
320
|
-
gap: 8px;
|
|
321
|
-
flex-wrap: wrap;
|
|
322
|
-
}
|
|
323
|
-
</style>
|
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<v-menu
|
|
3
|
-
v-model="open"
|
|
4
|
-
offset="10px"
|
|
5
|
-
:close-on-content-click="false"
|
|
6
|
-
location="bottom end"
|
|
7
|
-
>
|
|
8
|
-
<template #activator="{ props: activator }">
|
|
9
|
-
<v-btn
|
|
10
|
-
v-bind="activator"
|
|
11
|
-
variant="text"
|
|
12
|
-
size="36"
|
|
13
|
-
class="app-topbar-shell__icon-btn ms-2"
|
|
14
|
-
:aria-label="
|
|
15
|
-
unread > 0 ? `Notifications, ${unread} unread` : 'Notifications'
|
|
16
|
-
"
|
|
17
|
-
>
|
|
18
|
-
<v-badge
|
|
19
|
-
:model-value="unread > 0"
|
|
20
|
-
:content="badgeLabel"
|
|
21
|
-
color="error"
|
|
22
|
-
offset-x="-2"
|
|
23
|
-
offset-y="-2"
|
|
24
|
-
>
|
|
25
|
-
<v-icon icon="mdi-bell-outline" size="20" />
|
|
26
|
-
</v-badge>
|
|
27
|
-
</v-btn>
|
|
28
|
-
</template>
|
|
29
|
-
|
|
30
|
-
<v-card :width="440" elevation="0" class="notif-panel">
|
|
31
|
-
<div class="notif-panel__head">Notification</div>
|
|
32
|
-
|
|
33
|
-
<div class="notif-panel__body">
|
|
34
|
-
<div v-if="loading" class="notif-panel__state">
|
|
35
|
-
<v-progress-circular indeterminate size="22" />
|
|
36
|
-
</div>
|
|
37
|
-
|
|
38
|
-
<p v-else-if="error" class="notif-panel__state text-error">
|
|
39
|
-
Notifications could not be loaded.
|
|
40
|
-
</p>
|
|
41
|
-
|
|
42
|
-
<p v-else-if="!items.length" class="notif-panel__state">
|
|
43
|
-
You have no notifications.
|
|
44
|
-
</p>
|
|
45
|
-
|
|
46
|
-
<template v-else>
|
|
47
|
-
<button
|
|
48
|
-
v-for="item in items"
|
|
49
|
-
:key="item._id"
|
|
50
|
-
type="button"
|
|
51
|
-
class="notif-card"
|
|
52
|
-
:class="{ 'notif-card--unread': !item.hasRead }"
|
|
53
|
-
@click="openNotification(item)"
|
|
54
|
-
>
|
|
55
|
-
<div class="notif-card__top">
|
|
56
|
-
<span class="notif-card__title" :class="titleClass(item)">
|
|
57
|
-
{{ item.title }}
|
|
58
|
-
</span>
|
|
59
|
-
<span class="notif-card__age">{{ relativeAge(item.createdAt) }}</span>
|
|
60
|
-
</div>
|
|
61
|
-
|
|
62
|
-
<div class="notif-card__bottom">
|
|
63
|
-
<v-icon size="18" class="notif-card__icon">
|
|
64
|
-
mdi-file-document-outline
|
|
65
|
-
</v-icon>
|
|
66
|
-
<span class="notif-card__body">{{ item.body }}</span>
|
|
67
|
-
<span
|
|
68
|
-
class="notif-card__dot"
|
|
69
|
-
:class="{ 'notif-card__dot--read': item.hasRead }"
|
|
70
|
-
/>
|
|
71
|
-
</div>
|
|
72
|
-
</button>
|
|
73
|
-
</template>
|
|
74
|
-
</div>
|
|
75
|
-
</v-card>
|
|
76
|
-
</v-menu>
|
|
77
|
-
</template>
|
|
78
|
-
|
|
79
|
-
<script setup lang="ts">
|
|
80
|
-
/**
|
|
81
|
-
* The top-bar notification feed for the web.
|
|
82
|
-
*
|
|
83
|
-
* Opt-in from `Layout/Header.vue` rather than always on: the header is shared by
|
|
84
|
-
* every app in the estate, and only the ones whose modules actually raise a
|
|
85
|
-
* notification should grow a bell that is permanently empty.
|
|
86
|
-
*/
|
|
87
|
-
const PAGE_SIZE = 10;
|
|
88
|
-
|
|
89
|
-
const props = defineProps({
|
|
90
|
-
/** Scopes the feed to one site. Empty shows everything addressed to the user. */
|
|
91
|
-
siteId: {
|
|
92
|
-
type: String,
|
|
93
|
-
default: "",
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
const { getAll, getUnreadCount, markAsRead } = useNotification();
|
|
98
|
-
const { currentUser } = useLocalAuth();
|
|
99
|
-
|
|
100
|
-
const open = ref(false);
|
|
101
|
-
const loading = ref(false);
|
|
102
|
-
const error = ref(false);
|
|
103
|
-
const items = ref<Array<Record<string, any>>>([]);
|
|
104
|
-
const unread = ref(0);
|
|
105
|
-
|
|
106
|
-
const userId = computed(() => String(currentUser.value?._id ?? ""));
|
|
107
|
-
|
|
108
|
-
/** Three digits of badge is a smudge; past 99 the exact number changes nothing. */
|
|
109
|
-
const badgeLabel = computed(() => (unread.value > 99 ? "99+" : String(unread.value)));
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Red for a document that has already lapsed, amber for one approaching it.
|
|
113
|
-
*
|
|
114
|
-
* Matched on the title because the stored feed row carries no structured stage:
|
|
115
|
-
* `send()` persists title, body, module, screen and params, and drops the rest
|
|
116
|
-
* of its `data`. The two titles come from one tested function in
|
|
117
|
-
* `document-expiry-reminder.util.ts`, and "expiring" does not contain
|
|
118
|
-
* "expired", so the test is narrower than it looks.
|
|
119
|
-
*/
|
|
120
|
-
function titleClass(item: Record<string, any>) {
|
|
121
|
-
return /expired/i.test(String(item?.title ?? ""))
|
|
122
|
-
? "notif-card__title--urgent"
|
|
123
|
-
: "notif-card__title--warn";
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** "24 mins ago", the form the design prints. */
|
|
127
|
-
function relativeAge(value?: string | Date | null) {
|
|
128
|
-
if (!value) return "";
|
|
129
|
-
|
|
130
|
-
const then = new Date(value).getTime();
|
|
131
|
-
if (Number.isNaN(then)) return "";
|
|
132
|
-
|
|
133
|
-
const seconds = Math.max(0, Math.floor((Date.now() - then) / 1000));
|
|
134
|
-
if (seconds < 60) return "just now";
|
|
135
|
-
|
|
136
|
-
const minutes = Math.floor(seconds / 60);
|
|
137
|
-
if (minutes < 60) return `${minutes} min${minutes === 1 ? "" : "s"} ago`;
|
|
138
|
-
|
|
139
|
-
const hours = Math.floor(minutes / 60);
|
|
140
|
-
if (hours < 24) return `${hours} hour${hours === 1 ? "" : "s"} ago`;
|
|
141
|
-
|
|
142
|
-
const days = Math.floor(hours / 24);
|
|
143
|
-
return `${days} day${days === 1 ? "" : "s"} ago`;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Tolerates the reply being paginated, wrapped once, or a bare array. */
|
|
147
|
-
function readRows(response: any): Array<Record<string, any>> {
|
|
148
|
-
const payload = response?.data ?? response;
|
|
149
|
-
const rows = payload?.items ?? payload?.data ?? payload;
|
|
150
|
-
return Array.isArray(rows) ? rows : [];
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function readCount(response: any): number {
|
|
154
|
-
const payload = response?.data ?? response;
|
|
155
|
-
const value = payload?.unread ?? payload?.count ?? payload;
|
|
156
|
-
return Number.isFinite(Number(value)) ? Number(value) : 0;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async function refreshUnread() {
|
|
160
|
-
if (!userId.value) return;
|
|
161
|
-
|
|
162
|
-
try {
|
|
163
|
-
unread.value = readCount(await getUnreadCount(userId.value));
|
|
164
|
-
} catch {
|
|
165
|
-
// A badge that cannot be counted is not worth an error in the top bar of
|
|
166
|
-
// every screen; the panel itself reports a failure when it is opened.
|
|
167
|
-
unread.value = 0;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async function refreshList() {
|
|
172
|
-
if (!userId.value) return;
|
|
173
|
-
|
|
174
|
-
loading.value = true;
|
|
175
|
-
error.value = false;
|
|
176
|
-
|
|
177
|
-
try {
|
|
178
|
-
items.value = readRows(
|
|
179
|
-
await getAll({
|
|
180
|
-
page: 1,
|
|
181
|
-
limit: PAGE_SIZE,
|
|
182
|
-
siteId: props.siteId,
|
|
183
|
-
user: userId.value,
|
|
184
|
-
}),
|
|
185
|
-
);
|
|
186
|
-
} catch {
|
|
187
|
-
error.value = true;
|
|
188
|
-
items.value = [];
|
|
189
|
-
} finally {
|
|
190
|
-
loading.value = false;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function openNotification(item: Record<string, any>) {
|
|
195
|
-
if (item.hasRead) return;
|
|
196
|
-
|
|
197
|
-
// Marked locally first. The count and the dot are the whole feedback for the
|
|
198
|
-
// click, and waiting on a round trip to move them reads as a dead control.
|
|
199
|
-
item.hasRead = true;
|
|
200
|
-
unread.value = Math.max(0, unread.value - 1);
|
|
201
|
-
|
|
202
|
-
try {
|
|
203
|
-
await markAsRead(String(item._id), userId.value);
|
|
204
|
-
} catch {
|
|
205
|
-
item.hasRead = false;
|
|
206
|
-
await refreshUnread();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Fetched when the panel is opened rather than on a timer: a poll in a shared
|
|
211
|
-
// header would run on every screen of every app that mounts it.
|
|
212
|
-
watch(open, (isOpen) => {
|
|
213
|
-
if (isOpen) {
|
|
214
|
-
refreshList();
|
|
215
|
-
refreshUnread();
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
onMounted(refreshUnread);
|
|
220
|
-
</script>
|
|
221
|
-
|
|
222
|
-
<style scoped>
|
|
223
|
-
.notif-panel {
|
|
224
|
-
border-radius: var(--r-menu);
|
|
225
|
-
background: var(--card);
|
|
226
|
-
overflow: hidden;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
.notif-panel__head {
|
|
230
|
-
padding: 14px 16px;
|
|
231
|
-
font-weight: 600;
|
|
232
|
-
border-bottom: 1px solid var(--border);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
.notif-panel__body {
|
|
236
|
-
max-height: 420px;
|
|
237
|
-
overflow-y: auto;
|
|
238
|
-
padding: 12px;
|
|
239
|
-
display: flex;
|
|
240
|
-
flex-direction: column;
|
|
241
|
-
gap: 10px;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
.notif-panel__state {
|
|
245
|
-
padding: 24px 8px;
|
|
246
|
-
text-align: center;
|
|
247
|
-
color: var(--muted);
|
|
248
|
-
margin: 0;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
.notif-card {
|
|
252
|
-
width: 100%;
|
|
253
|
-
text-align: left;
|
|
254
|
-
padding: 12px 14px;
|
|
255
|
-
border: 1px solid var(--border);
|
|
256
|
-
border-radius: var(--r-inner);
|
|
257
|
-
background: transparent;
|
|
258
|
-
cursor: pointer;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
.notif-card:hover {
|
|
262
|
-
background: var(--hover);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
.notif-card__top {
|
|
266
|
-
display: flex;
|
|
267
|
-
align-items: baseline;
|
|
268
|
-
justify-content: space-between;
|
|
269
|
-
gap: 12px;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
.notif-card__title {
|
|
273
|
-
font-size: var(--fs-cell);
|
|
274
|
-
font-weight: 500;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
.notif-card__title--warn {
|
|
278
|
-
color: var(--warn);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
.notif-card__title--urgent {
|
|
282
|
-
color: var(--err);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
.notif-card__age {
|
|
286
|
-
flex: 0 0 auto;
|
|
287
|
-
font-size: 12px;
|
|
288
|
-
color: var(--muted);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
.notif-card__bottom {
|
|
292
|
-
display: flex;
|
|
293
|
-
align-items: center;
|
|
294
|
-
gap: 8px;
|
|
295
|
-
margin-top: 6px;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
.notif-card__icon {
|
|
299
|
-
flex: 0 0 auto;
|
|
300
|
-
color: var(--text);
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
.notif-card__body {
|
|
304
|
-
flex: 1 1 auto;
|
|
305
|
-
font-size: var(--fs-cell);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/* Filled while unread, hollow grey once read - the design's two states. */
|
|
309
|
-
.notif-card__dot {
|
|
310
|
-
flex: 0 0 auto;
|
|
311
|
-
width: 10px;
|
|
312
|
-
height: 10px;
|
|
313
|
-
border-radius: 50%;
|
|
314
|
-
background: var(--info);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
.notif-card__dot--read {
|
|
318
|
-
background: var(--muted);
|
|
319
|
-
opacity: 0.45;
|
|
320
|
-
}
|
|
321
|
-
</style>
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The web client for the in-app notification feed.
|
|
3
|
-
*
|
|
4
|
-
* The feed itself is not new - it has been written to for a while and read by
|
|
5
|
-
* the MA mobile app. Nothing on the web ever read it, which is why this is the
|
|
6
|
-
* first composable for it.
|
|
7
|
-
*
|
|
8
|
-
* `appSlug` is deliberately never sent. The inbox query applies that filter only
|
|
9
|
-
* when it is given one, so omitting it returns the rows written for the web
|
|
10
|
-
* (which carry no slug) alongside anything else addressed to this user, while a
|
|
11
|
-
* mobile client asking for its own slug still sees only its own.
|
|
12
|
-
*/
|
|
13
|
-
export default function useNotification() {
|
|
14
|
-
function getAll({
|
|
15
|
-
page = 1,
|
|
16
|
-
limit = 10,
|
|
17
|
-
siteId = "",
|
|
18
|
-
hasRead,
|
|
19
|
-
search = "",
|
|
20
|
-
user = "",
|
|
21
|
-
}: {
|
|
22
|
-
page?: number;
|
|
23
|
-
limit?: number;
|
|
24
|
-
siteId?: string;
|
|
25
|
-
hasRead?: boolean;
|
|
26
|
-
search?: string;
|
|
27
|
-
user?: string;
|
|
28
|
-
} = {}) {
|
|
29
|
-
const query: Record<string, any> = { page, limit, search };
|
|
30
|
-
|
|
31
|
-
// Sent only when set. The list schema rejects a malformed id rather than
|
|
32
|
-
// ignoring it, and `hasRead` false is a real filter that `||` would drop.
|
|
33
|
-
if (siteId) query.siteId = siteId;
|
|
34
|
-
if (user) query.user = user;
|
|
35
|
-
if (hasRead !== undefined) query.hasRead = hasRead;
|
|
36
|
-
|
|
37
|
-
return useNuxtApp().$api<Record<string, any>>("/api/notifications", {
|
|
38
|
-
method: "GET",
|
|
39
|
-
query,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getUnreadCount(user: string) {
|
|
44
|
-
return useNuxtApp().$api<Record<string, any>>(
|
|
45
|
-
"/api/notifications/unread-count",
|
|
46
|
-
{
|
|
47
|
-
method: "GET",
|
|
48
|
-
query: { user },
|
|
49
|
-
},
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function markAsRead(id: string, user: string) {
|
|
54
|
-
return useNuxtApp().$api<Record<string, any>>(
|
|
55
|
-
`/api/notifications/${id}/${user}/read`,
|
|
56
|
-
{
|
|
57
|
-
method: "PATCH",
|
|
58
|
-
},
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function markAllAsRead(user: string) {
|
|
63
|
-
return useNuxtApp().$api<Record<string, any>>(
|
|
64
|
-
"/api/notifications/read-all",
|
|
65
|
-
{
|
|
66
|
-
method: "PATCH",
|
|
67
|
-
query: { user },
|
|
68
|
-
},
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
getAll,
|
|
74
|
-
getUnreadCount,
|
|
75
|
-
markAsRead,
|
|
76
|
-
markAllAsRead,
|
|
77
|
-
};
|
|
78
|
-
}
|