-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathmessagelist.vue
More file actions
846 lines (716 loc) · 33.5 KB
/
messagelist.vue
File metadata and controls
846 lines (716 loc) · 33.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<template>
<div class="messagelist">
<el-dialog v-model="composeDialogVisible" title="Send" destroy-on-close append-to-body align-center width="80%">
<messagecompose @closed="() => composeDialogVisible=false" />
</el-dialog>
<div class="toolbar">
<el-button
v-on:click="composeDialogVisible=true"
:disabled="!isRelayAvailable"
title="Compose">Compose</el-button>
<el-button
v-on:click="showFileSelector"
icon="Upload"
:disabled="!selectedMailbox"
title="Import EML files">Import</el-button>
<el-button-group>
<el-button icon="Delete"
v-on:click="deleteSelected"
:disabled="!selectedmessage"
title="Delete">Delete</el-button>
<el-button v-on:click="relaySelected"
icon="d-arrow-right"
:disabled="!selectedmessage || !isRelayAvailable"
:loading="isRelayInProgress"
title="Relay">Relay...</el-button>
</el-button-group>
<el-button-group>
<el-button icon="refresh"
v-on:click="refresh"
:disabled="loading || !selectedMailbox"
title="Refresh"></el-button>
<el-button v-on:click="markAllMessageRead"
:disabled="loading || !selectedMailbox"
title="Mark all as read">
<font-awesome-icon :icon="['fa-regular','envelope-open']" />
</el-button>
<el-button icon="close" title="Clear" @click="clear"></el-button>
</el-button-group>
<el-select style="flex: 1 0 200px;" v-model="selectedMailbox" class="fill">
<el-option v-for="item in availableMailboxes"
:key="item.name"
:label="item.name"
:value="item.name" />
<template #prefix>
<el-icon><MessageBox /></el-icon>
</template>
</el-select>
<el-select style="flex: 1 0 150px;" v-model="selectedFolder" class="fill" :disabled="!selectedMailbox">
<el-option v-for="folder in availableFolders"
:key="folder"
:label="folder"
:value="folder" />
<template #prefix>
<el-icon><Files /></el-icon>
</template>
</el-select>
<el-input class="fill"
v-model="searchTerm"
clearable
placeholder="Search"
prefix-icon="search"
style="flex: 1 0 150px" />
</div>
<el-alert v-if="error" type="error" title="Error" show-icon>
{{ error.message }}
<el-button v-on:click="refresh">Retry</el-button>
</el-alert>
<el-table :data="messages"
v-loading="loading"
:empty-text="emptyText"
highlight-current-row
@current-change="handleCurrentChange"
@sort-change="sort"
:default-sort="{ prop: 'receivedDate', order: 'descending' }"
class="table"
type="selection"
reserve-selection="true"
row-key="id"
:row-class-name="getRowClass"
:row-attrs="getRowAttrs"
ref="table"
stripe>
<el-table-column property="receivedDate"
label="Received"
width="160"
sortable="custom"
:formatter="formatDate"></el-table-column>
<el-table-column property="from"
label="From"
width="140"
sortable="custom"></el-table-column>
<el-table-column property="to"
label="To"
width="180"
sortable="custom">
<template #default="scope">
<div style="display: flow; gap: 6px;">
<div v-for="recip in scope.row.to" :key="recip">
<strong v-if="(scope.row.deliveredTo??'').includes(recip)">{{recip}}</strong>
<span v-if="!(scope.row.deliveredTo??'').includes(recip)">{{recip}}</span>
</div>
</div>
</template>
</el-table-column>
<el-table-column property="isRelayed"
label=""
width="28">
<template #default="scope">
<el-tooltip effect="light"
content="Message has been relayed"
placement="top-start">
<span> <i v-if="scope.row.isRelayed" class="fas fa-share-square"></i></span>
</el-tooltip>
</template>
</el-table-column>
<el-table-column property="subject" label="Subject" sortable="custom">
<template #default="scope">
{{ scope.row.subject }}
<i class="paperclip"
v-if="scope.row.attachmentCount"
:title="scope.row.attachmentCount + ' attachments'"></i>
</template>
</el-table-column>
<el-table-column property="hasWarnings"
label=""
width="28">
<template #default="scope">
<el-tooltip effect="light"
content="Message has warnings"
placement="top-start">
<el-icon v-if="scope.row.hasWarnings" style="color: orange;">
<Warning />
</el-icon>
</el-tooltip>
</template>
</el-table-column>
</el-table>
<messagelistpager :paged-data="pagedServerMessages"
@on-current-page-change="handlePaginationCurrentChange"
@on-page-size-change="handlePaginationPageSizeChange"></messagelistpager>
</div>
</template>
<script lang="ts">
import { Component, Watch, Prop, Vue, toNative, Emit } from "vue-facing-decorator";
import { ElMessageBox, ElNotification, TableInstance } from "element-plus";
import MessagesController from "../ApiClient/MessagesController";
import MessageSummary from "../ApiClient/MessageSummary";
import HubConnectionManager from "../HubConnectionManager";
import sortedArraySync from "../sortedArraySync";
import { Mutex } from "async-mutex";
import MessageNotificationManager from "../MessageNotificationManager";
import { debounce } from "ts-debounce";
import ServerController from "../ApiClient/ServerController";
import ClientSettingsManager from "../ApiClient/ClientSettingsManager";
import { getGlobalRouter } from "../router";
import PagedResult, { EmptyPagedResult } from "@/ApiClient/PagedResult";
import Messagelistpager from "@/components/messagelistpager.vue";
import Mailbox from "../ApiClient/Mailbox";
import MailboxesController from "../ApiClient/MailboxesController";
import MessageCompose from "@/components/messagecompose.vue";
@Component({
components: {
Messagelistpager,
messagecompose: MessageCompose
},
})
class MessageList extends Vue {
private selectedSortDescending: boolean = true;
private selectedSortColumn: string = "receivedDate";
private static readonly MAILBOX_STORAGE_KEY = "smtp4dev-selected-mailbox";
private isInitialLoad: boolean = true;
private suppressRouteUpdate: boolean = false;
page: number = 1;
pagedServerMessages: PagedResult<MessageSummary> | undefined = EmptyPagedResult<MessageSummary>();
@Prop({ default: null })
connection: HubConnectionManager | null = null;
messages: MessageSummary[] = [];
selectedMailbox: string | null = null;
selectedFolder: string = "INBOX"; // Default to INBOX folder
availableFolders: string[] = [];
isRelayInProgress: boolean = false;
isRelayAvailable: boolean = false;
composeDialogVisible = false;
pendingSelectMessageAfterRefresh: MessageSummary | null = null;
get emptyText() {
if (this.loading) {
return "Loading";
}
if (!this.selectedMailbox) {
return "Select a mailbox to view messages";
}
const folderText = this.selectedFolder ? ` in folder '${this.selectedFolder}'` : '';
return this.searchTerm ?
`No messages matching '${this.searchTerm}' in mailbox '${this.selectedMailbox}'${folderText}`
: `No messages in mailbox '${this.selectedMailbox}'${folderText}`;
}
error: Error | null = null;
selectedmessage: MessageSummary | null = null;
searchTerm: string = "";
loading: boolean = true;
availableMailboxes: Mailbox[] | null = null;
private messageNotificationManager: MessageNotificationManager | null = null;
selectMessage(message: MessageSummary) {
(this.$refs.table as TableInstance).setCurrentRow(message);
this.handleCurrentChange(message);
}
@Emit("selected-message-changed")
handleCurrentChange(message: MessageSummary | null) {
this.selectedmessage = message;
// Update URL with selected message
if (!this.suppressRouteUpdate && !this.isInitialLoad) {
this.updateRouteWithCurrentState();
}
return message;
}
async handlePaginationCurrentChange(page: number) {
this.page = page;
await this.refresh(false);
}
async handlePaginationPageSizeChange(pageSize: number) {
ClientSettingsManager.updateClientSettings({ pageSize });
await this.refresh(false);
}
formatDate(row: number, column: number, cellValue: Date): string {
return cellValue?.toLocaleString();
}
getRowClass(event: { row: MessageSummary }): string {
return event.row.isUnread ? "unread" : "read";
}
getRowAttrs(event: { row: MessageSummary }): Record<string, string> {
return {
'data-message-id': event.row.id
};
}
async relaySelected() {
if (this.selectedmessage == null) {
return;
}
let emails: string[];
try {
let dialogResult = await ElMessageBox.prompt(
"Email address(es) to relay to (separate multiple with ,)",
"Relay Message",
{
confirmButtonText: "OK",
inputValue: this.selectedmessage.to.join(","),
cancelButtonText: "Cancel",
inputPattern: /[^, ]+(, *[^, ]+)*/,
inputErrorMessage: "Invalid email addresses",
}
);
emails = dialogResult.value.split(",").map((e) => e.trim());
} catch {
return;
}
try {
this.isRelayInProgress = true;
await new MessagesController().relayMessage(this.selectedmessage.id, {
overrideRecipientAddresses: emails,
});
ElNotification.success({
title: "Relay Message Success",
message: "Completed OK",
});
} catch (e: any) {
const message = e.response?.data?.detail ?? e.message;
ElNotification.error({ title: "Relay Message Failed", message: message });
} finally {
this.isRelayInProgress = false;
}
}
async deleteSelected() {
if (this.selectedmessage == null) {
return;
}
this.loading = true;
let messageToDelete = this.selectedmessage;
let nextIndex = this.messages.indexOf(messageToDelete) + 1;
if (nextIndex < this.messages.length) {
this.selectMessage(this.messages[nextIndex]);
}
try {
await new MessagesController().delete(messageToDelete.id);
await this.refresh(false);
} catch (e: any) {
ElNotification.error({
title: "Delete Message Failed",
message: e.message,
});
} finally {
this.loading = false;
}
}
showFileSelector() {
// Create a hidden file input element
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.eml';
fileInput.multiple = true;
fileInput.style.display = 'none';
// Handle file selection
fileInput.addEventListener('change', (event: any) => {
const files = event.target.files;
if (files && files.length > 0) {
this.importFiles(Array.from(files));
}
// Clean up the temporary element
document.body.removeChild(fileInput);
});
// Add to DOM and trigger click
document.body.appendChild(fileInput);
fileInput.click();
}
async importFiles(files: File[]) {
if (files.length === 0) {
return;
}
let successCount = 0;
let failCount = 0;
const importedIds: string[] = [];
// Show initial progress notification and store reference to dismiss it later
const progressNotification = ElNotification.info({
title: "Import in Progress",
message: `Importing ${files.length} file(s)...`,
duration: 0 // Don't auto-dismiss, we'll handle it manually
});
try {
for (const file of files) {
try {
// Read file content as text
const emlContent = await this.readFileAsText(file);
// Call the import API for this single file
const messageId = await new MessagesController().import(emlContent, this.selectedMailbox, this.selectedFolder);
successCount++;
importedIds.push(messageId);
} catch (e: any) {
failCount++;
const message = e.response?.data ?? e.message;
ElNotification.error({
title: "Import Failed",
message: `${file.name}: ${message}`,
duration: 5000
});
}
}
// Show final success notification
if (successCount > 0) {
ElNotification.success({
title: "Import Complete",
message: `Successfully imported ${successCount} of ${files.length} files`,
duration: 4000
});
// Refresh the message list
await this.refresh(false);
// Select the first imported message by its specific ID with retry logic
if (importedIds.length > 0) {
const firstImportedId = importedIds[0];
// Retry mechanism to wait for the imported message to appear in the list
let retryCount = 0;
const maxRetries = 10;
const retryDelay = 200; // 200ms between retries
while (retryCount < maxRetries) {
const importedMessage = this.messages.find(m => m.id === firstImportedId);
if (importedMessage) {
this.selectMessage(importedMessage);
break;
}
// Wait before retrying
await new Promise(resolve => setTimeout(resolve, retryDelay));
retryCount++;
}
}
}
} finally {
// Always dismiss the progress notification when import completes
progressNotification.close();
}
}
readFileAsText(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => resolve(e.target?.result as string);
reader.onerror = (e) => reject(new Error('Failed to read file'));
reader.readAsText(file);
});
}
async clear() {
try {
await ElMessageBox.confirm(`Delete all messages in mailbox '${this.selectedMailbox}'?`)
} catch {
return;
}
try {
this.loading = true;
await new MessagesController().deleteAll(this.selectedMailbox!);
await this.refresh(true);
} catch (e: any) {
ElNotification.error({
title: "Delete All Messages Failed",
message: e.message,
});
} finally {
this.loading = false;
}
}
private debouncedRefresh = debounce(() => this.refresh(false), 200);
@Watch("searchTerm")
onSearchTermChanged() {
this.debouncedDoSearch();
// Don't update URL for search - filters not stored in URL
}
debouncedDoSearch = debounce(() => this.refresh(false), 200);
@Watch("selectedMailbox")
async onMailboxChanged() {
this.initialMailboxLoadDone = false;
this.messageNotificationManager = new MessageNotificationManager(this.selectedMailbox,
(message) => {
if (this.selectedFolder && this.selectedFolder != "INBOX") {
this.selectedFolder = "INBOX";
this.pendingSelectMessageAfterRefresh = message;
return;
}
this.selectMessage(message);
this.handleCurrentChange(message);
}
);
// Default to INBOX folder when mailbox changes
this.selectedFolder = "INBOX";
await this.loadFolders();
await this.refresh(true, false);
// Save selected mailbox to localStorage
if (this.selectedMailbox) {
localStorage.setItem(MessageList.MAILBOX_STORAGE_KEY, this.selectedMailbox);
} else {
localStorage.removeItem(MessageList.MAILBOX_STORAGE_KEY);
}
// Update URL route only AFTER initial load and not suppressed
if (!this.isInitialLoad && !this.suppressRouteUpdate) {
this.updateRouteWithCurrentState();
}
}
@Watch("selectedFolder")
async onFolderChanged() {
await this.refresh(false);
// Update URL with folder filter
if (!this.suppressRouteUpdate && !this.isInitialLoad) {
this.updateRouteWithCurrentState();
}
}
async loadFolders() {
if (this.selectedMailbox) {
try {
this.availableFolders = await new MessagesController().getFolders(this.selectedMailbox);
// Ensure INBOX folder exists, if not fall back to first available folder
if (this.availableFolders.length > 0 && !this.availableFolders.includes("INBOX")) {
this.selectedFolder = this.availableFolders[0];
}
} catch (error) {
console.error("Failed to load folders:", error);
this.availableFolders = [];
}
} else {
this.availableFolders = [];
}
}
private lastSort: string | null = null;
private lastSortDescending: boolean = false;
private mutex = new Mutex();
initialMailboxLoadDone = false;
async markAllMessageRead() {
await new MessagesController().markAllMessageRead(this.selectedMailbox!);
}
async refresh(includeNotifications: boolean, silent: boolean = false) {
if (!silent) this.loading = true;
let unlock = await this.mutex.acquire();
try {
this.error = null;
const server = await this.connection!.getServer()
this.isRelayAvailable = !!server.relaySmtpServer;
const mailboxes = await new MailboxesController().getAll();
this.availableMailboxes = mailboxes.sort((a, b) => a.name.localeCompare(b.name));
if (!this.selectedMailbox) {
// Priority order for mailbox selection on initial load:
// 1. URL route parameter (for bookmarks/direct links)
// 2. localStorage (for persistent selection)
// 3. Server default mailbox
// 4. Last mailbox alphabetically
let mailboxToSelect: string | null = null;
if (this.isInitialLoad) {
// Check URL route parameter first
const routeMailbox = this.$route.params.mailbox as string | undefined;
if (routeMailbox) {
const decodedMailbox = decodeURIComponent(routeMailbox);
mailboxToSelect = this.availableMailboxes.find(m => m.name === decodedMailbox)?.name ?? null;
}
// If not in URL, check localStorage
if (!mailboxToSelect) {
const storedMailbox = localStorage.getItem(MessageList.MAILBOX_STORAGE_KEY);
if (storedMailbox) {
mailboxToSelect = this.availableMailboxes.find(m => m.name === storedMailbox)?.name ?? null;
}
}
}
// Fall back to server default or last mailbox
if (!mailboxToSelect) {
mailboxToSelect = this.availableMailboxes.find(m => m.name == server.currentUserDefaultMailboxName)?.name ?? this.availableMailboxes[this.availableMailboxes.length - 1]?.name ?? null;
}
// Suppress route update during initial load to avoid redundant navigation
this.suppressRouteUpdate = true;
this.selectedMailbox = mailboxToSelect;
// On initial load, restore folder from URL path params
if (this.isInitialLoad) {
const params = this.$route.params;
if (params.folder) {
this.selectedFolder = params.folder as string;
}
}
this.suppressRouteUpdate = false;
} else {
//Potentially removed mailbox
this.selectedMailbox = this.availableMailboxes.find(m => m.name == this.selectedMailbox)?.name ?? null;
}
// Get current pageSize from settings
const clientSettings = await ClientSettingsManager.getClientSettings();
const pageSize = clientSettings.pageSize || 25;
// Copy in case they are mutated during the async load below
let sortColumn = this.selectedSortColumn;
let sortDescending = this.selectedSortDescending;
if (!this.selectedMailbox) {
this.pagedServerMessages = { currentPage: 1, firstRowOnPage: 0, lastRowOnPage: 0, pageCount: 1, rowCount: 0, pageSize: pageSize, results: [] };
} else {
this.pagedServerMessages = await new MessagesController().getSummaries(
this.selectedMailbox,
this.searchTerm,
sortColumn,
sortDescending,
this.page,
pageSize,
this.selectedFolder || null
);
}
if (
!this.lastSort ||
this.lastSort != sortColumn ||
this.lastSortDescending != sortDescending ||
this.pagedServerMessages.results.length == 0
) {
this.messages.splice(
0,
this.messages.length,
...this.pagedServerMessages.results
);
} else {
sortedArraySync(
this.pagedServerMessages.results,
this.messages,
(a: MessageSummary, b: MessageSummary) => a.id == b.id,
(sourceItem: MessageSummary, targetItem: MessageSummary) => {
targetItem.isUnread = sourceItem.isUnread;
targetItem.isRelayed = sourceItem.isRelayed;
}
);
}
if (includeNotifications) {
await this.messageNotificationManager?.refresh(!this.initialMailboxLoadDone);
}
if (this.pendingSelectMessageAfterRefresh) {
this.selectMessage(this.pendingSelectMessageAfterRefresh);
this.pendingSelectMessageAfterRefresh = null;
}
// On initial load, select message from URL if specified
if (this.isInitialLoad) {
const messageId = this.$route.params.message as string | undefined;
if (messageId) {
const message = this.messages.find(m => m.id === messageId);
if (message) {
this.suppressRouteUpdate = true;
this.selectMessage(message);
this.suppressRouteUpdate = false;
}
}
}
this.initialMailboxLoadDone = true;
this.lastSort = sortColumn;
this.lastSortDescending = this.selectedSortDescending;
} catch (e: any) {
this.error = e;
} finally {
if (!silent) this.loading = false;
unlock();
}
}
async sort(sortOptions: { prop: string, order: string }) {
let descending: boolean = true;
if (sortOptions.order === "ascending") {
descending = false;
}
if (
this.selectedSortColumn != sortOptions.prop ||
this.selectedSortDescending != descending
) {
this.selectedSortColumn = sortOptions.prop || "receivedDate";
this.selectedSortDescending = descending;
await this.refresh(false);
}
}
// Method to update route with all current state (mailbox, message, filters)
updateRouteWithCurrentState() {
const router = getGlobalRouter();
if (!router) {
console.warn("Router not available");
return;
}
// Only update route if we're currently on a messages route
// This prevents the messagelist from overriding other tabs' routes on initial page load
const currentPath = this.$route.path;
if (!currentPath.startsWith('/messages') && currentPath !== '/') {
return;
}
if (!this.selectedMailbox) {
try {
router.replace({ path: '/messages' });
} catch (e) {
console.error("Error updating route:", e);
}
return;
}
// Build the path
let path = `/messages/mailbox/${encodeURIComponent(this.selectedMailbox)}`;
if (this.selectedFolder) {
path += `/folder/${encodeURIComponent(this.selectedFolder)}`;
}
if (this.selectedmessage) {
path += `/message/${this.selectedmessage.id}`;
}
try {
const result = router.replace({ path });
// Handle the promise if it's returned
if (result && typeof result.catch === 'function') {
result.catch((err: any) => {
// Ignore navigation cancelled errors
if (err && err.name !== 'NavigationDuplicated' && err.type !== 16) {
console.error("Error updating route:", err);
}
});
}
} catch (e) {
console.error("Error updating route:", e);
}
}
async mounted() {
this.loading = true;
await this.refresh(true, false);
this.isInitialLoad = false;
}
@Watch("$route")
onRouteChanged(newRoute: any, oldRoute: any) {
// Handle route changes (e.g., browser back/forward)
const newMailbox = newRoute.params.mailbox as string | undefined;
const oldMailbox = oldRoute.params.mailbox as string | undefined;
const newMessageId = newRoute.params.message as string | undefined;
const oldMessageId = oldRoute.params.message as string | undefined;
const newFolder = newRoute.params.folder as string | undefined;
const oldFolder = oldRoute.params.folder as string | undefined;
this.suppressRouteUpdate = true;
// Handle mailbox change
if (newMailbox !== oldMailbox) {
const decodedMailbox = newMailbox ? decodeURIComponent(newMailbox) : null;
// Only update if the mailbox actually exists and is different from current selection
if (decodedMailbox && this.availableMailboxes) {
const mailbox = this.availableMailboxes.find(m => m.name === decodedMailbox);
if (mailbox && mailbox.name !== this.selectedMailbox) {
this.selectedMailbox = mailbox.name;
}
}
}
// Handle folder change
if (newFolder !== oldFolder) {
this.selectedFolder = newFolder ?? "INBOX";
}
// Handle message selection change
if (newMessageId !== oldMessageId) {
if (newMessageId) {
// Select the message if it exists in current list
const message = this.messages.find(m => m.id === newMessageId);
if (message && message.id !== this.selectedmessage?.id) {
this.selectMessage(message);
}
} else if (this.selectedmessage) {
// Clear selection
this.handleCurrentChange(null);
}
}
// Filters are not stored in URL, so no query parameter handling needed
this.suppressRouteUpdate = false;
}
@Watch("connection")
async onConnectionChanged() {
if (this.connection) {
this.connection.on("messageschanged", async () => {
await this.refresh(true, true);
});
this.connection.onServerChanged(async () => {
await this.refresh(true, true);
});
this.connection.on("mailboxeschanged", async () => {
await this.refresh(false, true);
});
this.connection.addOnConnectedCallback(() => {
this.refresh(true, true);
});
}
}
}
export default toNative(MessageList)
</script>