@7365admin1/layer-common 4.5.1-staging.298 → 4.5.1-staging.300

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.
@@ -1,62 +1,120 @@
1
1
  <template>
2
- <v-card class="chat-bubble screen-card mb-2 pa-4" width="400" elevation="0">
3
- <v-row no-gutters align="center">
4
- <v-col cols="auto" class="mr-3">
2
+ <v-card
3
+ class="chat-bubble screen-card pa-3"
4
+ :class="isCurrentUser ? 'chat-bubble--mine' : 'chat-bubble--theirs'"
5
+ elevation="0"
6
+ >
7
+ <v-row no-gutters align="start" class="flex-nowrap">
8
+ <v-col cols="auto" :class="isCurrentUser ? 'order-last ml-3' : 'mr-3'">
5
9
  <v-avatar class="chat-bubble__avatar" size="35">
6
- <v-img v-if="currentUser?.profile" width="15" height="15" />
10
+ <!-- Was `<v-img width="15" height="15" />` with no `src` at all, so a
11
+ sender with a profile picture got a blank 15px box instead of a
12
+ photo - and never fell through to their initials either. -->
13
+ <v-img v-if="avatarUrl" :src="avatarUrl" cover />
7
14
  <span v-else>{{ getNameInitials(senderName) }}</span>
8
15
  </v-avatar>
9
16
  </v-col>
10
17
 
11
- <v-col class="d-flex flex-column">
12
- <span class="chat-bubble__sender">{{ senderName }}</span>
13
- <span class="chat-bubble__sub">{{ role }}</span>
14
- </v-col>
15
- </v-row>
18
+ <v-col class="chat-bubble__body">
19
+ <div class="chat-bubble__sender">{{ senderName }}</div>
20
+ <div v-if="roleLabel" class="chat-bubble__sub">{{ roleLabel }}</div>
21
+
22
+ <div v-if="message" class="chat-bubble__text mt-2">{{ message }}</div>
23
+
24
+ <div v-if="attachments?.length" class="d-flex flex-wrap ga-2 mt-2">
25
+ <v-img
26
+ v-for="file in attachments"
27
+ :key="file.id"
28
+ :src="getFileUrl(file.id)"
29
+ width="110"
30
+ height="90"
31
+ cover
32
+ rounded="lg"
33
+ class="border-thin"
34
+ style="cursor: zoom-in; flex: 0 0 auto"
35
+ @click="emit('preview', getFileUrl(file.id))"
36
+ >
37
+ <template #placeholder>
38
+ <v-row class="fill-height ma-0" align="center" justify="center">
39
+ <v-progress-circular
40
+ indeterminate
41
+ size="20"
42
+ color="grey-lighten-4"
43
+ />
44
+ </v-row>
45
+ </template>
46
+ <template #error>
47
+ <v-row class="fill-height ma-0" align="center" justify="center">
48
+ <v-icon icon="mdi-image-broken" size="24" color="grey" />
49
+ </v-row>
50
+ </template>
51
+ </v-img>
52
+ </div>
16
53
 
17
- <v-row no-gutters class="mt-1">
18
- <v-col>
19
- <span class="chat-bubble__text py-2">{{ message }}</span>
20
-
21
- <v-row no-gutters justify="space-between" align="center" class="mt-1">
22
- <v-col cols="auto">
23
- <span class="chat-bubble__sub">{{ timestamp }}</span>
24
- </v-col>
25
- <v-col v-if="isCurrentUser" cols="auto" class="d-flex align-center">
26
- <v-icon size="16" class="chat-bubble__sub mr-1"
27
- >mdi-eye-outline</v-icon
28
- >
29
- <span class="chat-bubble__sub">{{ seenLabel }}</span>
30
- </v-col>
31
- </v-row>
54
+ <div class="chat-bubble__sub chat-bubble__time mt-2">
55
+ {{ timestamp }}
56
+ </div>
32
57
  </v-col>
33
58
  </v-row>
34
59
  </v-card>
35
60
  </template>
36
61
 
37
62
  <script setup lang="ts">
38
- defineProps<{
63
+ import { formatSenderRole } from "../../utils/chat-sender.ts";
64
+
65
+ /**
66
+ * One message bubble, drawn the same way for both people in the conversation
67
+ * and matching the mobile apps' bubble field for field: avatar, sender, role,
68
+ * message, photos, time.
69
+ *
70
+ * It used to be a fixed `width="400"` card with no left/right distinction, an
71
+ * avatar that could not render a photo, and a "Seen" line that only the current
72
+ * user's own messages carried.
73
+ */
74
+ const props = defineProps<{
39
75
  isCurrentUser: boolean;
40
76
  message: string;
41
77
  timestamp: string;
42
- seenLabel?: string;
43
78
  senderName: string;
44
79
  role?: string;
80
+ attachments?: TAttachment[];
81
+ avatarId?: string;
45
82
  }>();
46
83
 
47
- const { currentUser } = useLocalAuth();
84
+ const emit = defineEmits<{ (event: "preview", src: string): void }>();
85
+
48
86
  const { getNameInitials } = useUtils();
87
+ const { getFileUrl } = useFile();
88
+
89
+ const roleLabel = computed(() => formatSenderRole(props.role));
90
+ const avatarUrl = computed(() =>
91
+ props.avatarId ? getFileUrl(props.avatarId) : ""
92
+ );
49
93
  </script>
50
94
 
51
95
  <style scoped>
52
96
  .chat-bubble {
53
97
  font-family: var(--font-sans);
54
98
  color: var(--text);
99
+ /* Was a fixed 400px, which is both too wide on a narrow panel and unable to
100
+ grow on a wide one. The thread caps the column instead. */
101
+ max-width: 100%;
102
+ }
103
+
104
+ /* Alignment alone is a weak signal on a panel this narrow, so the two sides
105
+ also differ in fill - the same distinction the mobile bubbles make. */
106
+ .chat-bubble--mine {
107
+ background: var(--hover);
108
+ }
109
+
110
+ .chat-bubble--theirs {
111
+ background: var(--card);
112
+ }
113
+
114
+ .chat-bubble__body {
115
+ min-width: 0;
55
116
  }
56
117
 
57
- /* Was `color="surface-variant"` with the initials at `text-h5`, i.e. a 24px
58
- pair of letters in a 35px circle. The design's avatar is the tone wash with
59
- the accent ink, and the initials fit inside it. */
60
118
  .chat-bubble__avatar {
61
119
  background: var(--hover);
62
120
  color: var(--text2);
@@ -71,6 +129,8 @@ const { getNameInitials } = useUtils();
71
129
 
72
130
  .chat-bubble__text {
73
131
  font-size: var(--fs-cell);
132
+ white-space: pre-wrap;
133
+ overflow-wrap: anywhere;
74
134
  }
75
135
 
76
136
  /* The role, the timestamp and the seen label were `text-grey`, `text-caption`
@@ -79,4 +139,8 @@ const { getNameInitials } = useUtils();
79
139
  font-size: var(--fs-chip);
80
140
  color: var(--muted);
81
141
  }
142
+
143
+ .chat-bubble__time {
144
+ text-align: right;
145
+ }
82
146
  </style>
@@ -11,57 +11,127 @@
11
11
  </div>
12
12
 
13
13
  <v-sheet
14
+ ref="threadEl"
14
15
  class="chat-panel__thread overflow-y-auto flex-grow-1 pa-3"
15
16
  :style="{ height: 'calc(100vh - 300px)', overflowX: 'hidden' }"
16
17
  >
17
- <v-row
18
- v-for="(msg, i) in messages"
19
- :key="i"
20
- no-gutters
21
- :justify="msg.isCurrentUser ? 'end' : 'start'"
22
- class="mb-2"
18
+ <div v-if="isCommentsLoaded" class="d-flex justify-center py-6">
19
+ <v-progress-circular indeterminate size="28" color="primary" />
20
+ </div>
21
+
22
+ <div
23
+ v-else-if="!days.length"
24
+ class="text-center text-medium-emphasis py-6"
23
25
  >
24
- <v-col cols="auto">
25
- <ChatBubbles
26
- :isCurrentUser="msg.isCurrentUser"
27
- :message="msg.message"
28
- :timestamp="msg.timestamp"
29
- :seenLabel="msg.seenLabel"
30
- :senderName="msg.senderName"
31
- :role="msg.role"
32
- />
33
- </v-col>
34
- </v-row>
26
+ No messages yet
27
+ </div>
28
+
29
+ <template v-for="group in days" :key="group.day">
30
+ <div class="d-flex justify-center my-3">
31
+ <span class="chat-panel__day px-3 py-1">{{ group.day }}</span>
32
+ </div>
33
+
34
+ <v-row
35
+ v-for="msg in group.messages"
36
+ :key="msg._id"
37
+ no-gutters
38
+ :justify="isMine(msg) ? 'end' : 'start'"
39
+ class="mb-2"
40
+ >
41
+ <v-col cols="11" sm="10">
42
+ <div
43
+ class="d-flex"
44
+ :class="isMine(msg) ? 'justify-end' : 'justify-start'"
45
+ >
46
+ <ChatBubbles
47
+ :isCurrentUser="isMine(msg)"
48
+ :message="msg.comment"
49
+ :timestamp="formatTime(msg.createdAt)"
50
+ :senderName="isMine(msg) ? 'You' : msg.createdByName || 'User'"
51
+ :role="msg.createdByType"
52
+ :attachments="msg.attachments"
53
+ :avatarId="msg.createdByProfile"
54
+ @preview="openPreview"
55
+ />
56
+ </div>
57
+ </v-col>
58
+ </v-row>
59
+ </template>
35
60
  </v-sheet>
36
61
 
37
62
  <v-card class="screen-card mx-4 mb-4 mt-2 pa-3" elevation="0">
38
63
  <v-row no-gutters align="end">
39
64
  <v-col cols="10">
40
65
  <v-textarea
66
+ v-model="draft"
41
67
  class="filter-field"
42
68
  label="Message"
43
69
  auto-grow
44
70
  hide-details
45
71
  rows="1"
46
72
  variant="outlined"
73
+ :disabled="sending"
74
+ @keydown.enter.exact.prevent="send"
47
75
  />
48
76
  </v-col>
49
77
  <v-col cols="2" class="pl-2">
50
- <v-btn class="screen-btn-primary" variant="flat" block>Send</v-btn>
78
+ <v-btn
79
+ class="screen-btn-primary"
80
+ variant="flat"
81
+ block
82
+ :loading="sending"
83
+ :disabled="!canSend"
84
+ @click="send"
85
+ >
86
+ Send
87
+ </v-btn>
51
88
  </v-col>
52
89
  </v-row>
53
90
 
91
+ <div v-if="photos.length" class="d-flex flex-wrap ga-2 mt-3">
92
+ <div
93
+ v-for="(photo, index) in photos"
94
+ :key="photo.id"
95
+ class="chat-panel__pending"
96
+ >
97
+ <v-img
98
+ :src="getFileUrl(photo.id)"
99
+ width="64"
100
+ height="64"
101
+ cover
102
+ rounded="lg"
103
+ />
104
+ <v-btn
105
+ icon="mdi-close"
106
+ size="x-small"
107
+ variant="flat"
108
+ class="chat-panel__pending-remove"
109
+ @click="photos.splice(index, 1)"
110
+ />
111
+ </div>
112
+ </div>
113
+
54
114
  <v-divider class="my-2" />
55
115
 
56
116
  <v-row justify="start" no-gutters align="center">
57
117
  <v-col cols="auto">
58
- <v-icon size="20" class="chat-panel__tool mr-3">mdi-paperclip</v-icon>
59
- </v-col>
60
-
61
- <v-col cols="auto">
62
- <v-icon size="20" class="chat-panel__tool mr-3"
63
- >mdi-emoticon-happy-outline</v-icon
118
+ <v-btn
119
+ icon
120
+ density="compact"
121
+ variant="text"
122
+ :loading="uploading"
123
+ @click="fileInput?.click()"
64
124
  >
125
+ <v-icon size="20" class="chat-panel__tool">mdi-paperclip</v-icon>
126
+ </v-btn>
127
+ <input
128
+ ref="fileInput"
129
+ type="file"
130
+ accept="image/*"
131
+ multiple
132
+ hidden
133
+ @change="onFilesPicked"
134
+ />
65
135
  </v-col>
66
136
 
67
137
  <v-col cols="auto">
@@ -94,57 +164,185 @@
94
164
  </v-col>
95
165
  </v-row>
96
166
  </v-card>
167
+
168
+ <v-dialog v-model="showPreview" max-width="900">
169
+ <v-card class="screen-modal">
170
+ <v-card-actions class="pa-2 justify-end">
171
+ <v-btn
172
+ icon="mdi-close"
173
+ variant="text"
174
+ class="screen-modal__close"
175
+ @click="showPreview = false"
176
+ />
177
+ </v-card-actions>
178
+ <v-card-text class="screen-modal__body pa-2 pt-0">
179
+ <v-img :src="previewSrc" contain max-height="80vh" />
180
+ </v-card-text>
181
+ </v-card>
182
+ </v-dialog>
183
+
184
+ <Snackbar v-model="showMessage" :text="message" :color="messageColor" />
97
185
  </v-sheet>
98
186
  </template>
99
187
 
100
188
  <script setup lang="ts">
101
- defineProps({
189
+ import { groupChatByDay } from "../../utils/chat-sender.ts";
190
+
191
+ /**
192
+ * The comment thread under a feedback or a work order.
193
+ *
194
+ * This panel used to be a mockup: five hardcoded messages from "Tan Wei" dated
195
+ * 06/28/2024, a textarea with no model, and a Send button wired to nothing. So
196
+ * on the web the chat showed the same fake conversation on every record, and
197
+ * nothing anyone typed was ever sent - while the mobile apps had been reading
198
+ * and writing the real thread through `/api/comments/v1` all along.
199
+ */
200
+ const props = defineProps({
102
201
  type: {
103
202
  type: String,
104
203
  default: "feedback",
105
204
  },
205
+ /** The feedback or work order this thread hangs off. */
206
+ recordId: {
207
+ type: String,
208
+ default: "",
209
+ },
106
210
  });
107
211
 
108
- const messages = [
109
- {
110
- isCurrentUser: true,
111
- message: "Hello we would like to transfer this task...",
112
- timestamp: "06/28/2024 08:40",
113
- seenLabel: "Seen",
114
- senderName: "You",
115
- role: "Site Personnel",
116
- },
117
- {
118
- isCurrentUser: false,
119
- message: "Sure. Let me take care of that.",
120
- timestamp: "06/28/2024 08:42",
121
- senderName: "Tan Wei",
122
- role: "Manager",
123
- },
124
- {
125
- isCurrentUser: true,
126
- message: "Thanks!",
127
- timestamp: "06/28/2024 08:43",
128
- seenLabel: "Seen",
129
- senderName: "You",
130
- role: "Site Personnel",
131
- },
132
- {
133
- isCurrentUser: false,
134
- message: "No worries!",
135
- timestamp: "06/28/2024 08:44",
136
- senderName: "Tan Wei",
137
- role: "Manager",
138
- },
139
- {
140
- isCurrentUser: true,
141
- message: "Let me know if you need anything else.",
142
- timestamp: "06/28/2024 08:45",
143
- seenLabel: "Seen",
144
- senderName: "You",
145
- role: "Site Personnel",
146
- },
147
- ];
212
+ // `useComment` already existed with everything this panel needs - the fetch,
213
+ // the post, and the read receipts - and had not a single caller, because the
214
+ // panel it was written for was never wired up.
215
+ const { comments, isCommentsLoaded, setComments, addComment } = useComment();
216
+ const { currentUser } = useLocalAuth();
217
+ const { getFileUrl, addFile } = useFile();
218
+
219
+ const sending = ref(false);
220
+ const uploading = ref(false);
221
+ const draft = ref("");
222
+ const photos = ref<{ id: string; name?: string }[]>([]);
223
+ const fileInput = ref<HTMLInputElement | null>(null);
224
+ const threadEl = ref<any>(null);
225
+
226
+ const showPreview = ref(false);
227
+ const previewSrc = ref("");
228
+ const showMessage = ref(false);
229
+ const message = ref("");
230
+ const messageColor = ref("error");
231
+
232
+ const notify = (text: string, color = "error") => {
233
+ message.value = text;
234
+ messageColor.value = color;
235
+ showMessage.value = true;
236
+ };
237
+
238
+ const formatDay = (value: string | Date) =>
239
+ new Date(value).toLocaleDateString("en-US", {
240
+ year: "numeric",
241
+ month: "short",
242
+ day: "numeric",
243
+ });
244
+
245
+ const formatTime = (value?: string | Date) =>
246
+ value
247
+ ? new Date(value).toLocaleTimeString("en-US", {
248
+ hour: "2-digit",
249
+ minute: "2-digit",
250
+ })
251
+ : "";
252
+
253
+ // `setComments` hands the thread back oldest first, which is already reading
254
+ // order for a top-to-bottom panel.
255
+ const days = computed(() => groupChatByDay(comments.value, formatDay));
256
+
257
+ // `createdBy` is the author's user id; the API returns it unpopulated.
258
+ const isMine = (msg: TCommentChat) =>
259
+ !!currentUser.value?._id &&
260
+ String(msg.createdBy) === String(currentUser.value._id);
261
+
262
+ const canSend = computed(
263
+ () => !sending.value && (!!draft.value.trim() || photos.value.length > 0)
264
+ );
265
+
266
+ const threadKind = computed(() =>
267
+ props.type === "workOrder" ? "workOrder" : "feedback"
268
+ );
269
+
270
+ const scrollToLatest = () => {
271
+ nextTick(() => {
272
+ const el = threadEl.value?.$el ?? threadEl.value;
273
+ if (el) el.scrollTop = el.scrollHeight;
274
+ });
275
+ };
276
+
277
+ const load = async () => {
278
+ if (!props.recordId) {
279
+ comments.value = [];
280
+ return;
281
+ }
282
+ try {
283
+ await setComments(props.recordId, threadKind.value);
284
+ scrollToLatest();
285
+ } catch (error: any) {
286
+ notify(error?.data?.message || "The chat could not be loaded.");
287
+ }
288
+ };
289
+
290
+ watch(() => props.recordId, load, { immediate: true });
291
+
292
+ const onFilesPicked = async (event: Event) => {
293
+ const input = event.target as HTMLInputElement;
294
+ const files = Array.from(input.files ?? []);
295
+ input.value = "";
296
+ if (!files.length) return;
297
+
298
+ uploading.value = true;
299
+ try {
300
+ for (const file of files) {
301
+ const uploaded = await addFile(file);
302
+ const id = uploaded?.id;
303
+ // No id means nothing to reference; saying so beats attaching a broken
304
+ // image to the message.
305
+ if (!id) throw new Error("The photo upload did not return a file id.");
306
+ photos.value.push({ id, name: file.name });
307
+ }
308
+ } catch (error: any) {
309
+ notify(error?.message || "The photo could not be uploaded.");
310
+ } finally {
311
+ uploading.value = false;
312
+ }
313
+ };
314
+
315
+ const send = async () => {
316
+ if (!canSend.value) return;
317
+ if (!props.recordId) return notify("This record has no id to post against.");
318
+ if (!currentUser.value?._id) return notify("Please sign in again to reply.");
319
+
320
+ sending.value = true;
321
+ try {
322
+ await addComment({
323
+ comment: draft.value.trim(),
324
+ createdBy: String(currentUser.value._id),
325
+ workOrder: threadKind.value === "workOrder" ? props.recordId : "",
326
+ feedback: threadKind.value === "feedback" ? props.recordId : "",
327
+ attachments: photos.value as TAttachment[],
328
+ seenByNames: "",
329
+ } as unknown as TCommentChat);
330
+ // Nothing is cleared until the server has taken it, so a failed send does
331
+ // not silently swallow what was typed.
332
+ draft.value = "";
333
+ photos.value = [];
334
+ await load();
335
+ } catch (error: any) {
336
+ notify(error?.data?.message || "The message could not be sent.");
337
+ } finally {
338
+ sending.value = false;
339
+ }
340
+ };
341
+
342
+ const openPreview = (src: string) => {
343
+ previewSrc.value = src;
344
+ showPreview.value = true;
345
+ };
148
346
 
149
347
  const quickChatOptions = [
150
348
  "This is all noted, thank you!",
@@ -154,9 +352,8 @@ const quickChatOptions = [
154
352
  "We are here to help.",
155
353
  ];
156
354
 
157
- // Placeholder: choosing a quick-chat option is not wired to anything yet.
158
355
  const selectQuickChat = (msg: string) => {
159
- void msg;
356
+ draft.value = draft.value ? `${draft.value} ${msg}` : msg;
160
357
  };
161
358
  </script>
162
359
 
@@ -177,6 +374,23 @@ const selectQuickChat = (msg: string) => {
177
374
  background: var(--bg);
178
375
  }
179
376
 
377
+ .chat-panel__day {
378
+ background: var(--hover);
379
+ color: var(--muted);
380
+ border-radius: 10px;
381
+ font-size: var(--fs-chip);
382
+ }
383
+
384
+ .chat-panel__pending {
385
+ position: relative;
386
+ }
387
+
388
+ .chat-panel__pending-remove {
389
+ position: absolute;
390
+ top: -6px;
391
+ right: -6px;
392
+ }
393
+
180
394
  /* Was a bare icon and one `color="gray"` - not a token, and not the same tone
181
395
  as the icon beside it. */
182
396
  .chat-panel__tool {
@@ -4,7 +4,7 @@
4
4
 
5
5
  <v-col cols="12" xl="7" lg="7" md="7" class="fill-height">
6
6
  <div class="panel-container border-e">
7
- <ChatMessage :type="'feedback'" />
7
+ <ChatMessage :type="'feedback'" :record-id="String(feedback?._id || '')" />
8
8
  </div>
9
9
  </v-col>
10
10