-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFilesWrapper.svelte
More file actions
106 lines (93 loc) · 3.22 KB
/
Copy pathFilesWrapper.svelte
File metadata and controls
106 lines (93 loc) · 3.22 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
<script lang="ts">
import { onMount } from 'svelte';
import { downloadAttachmentsSerial } from '@/org.libersoft.messages/scripts/messages.js';
import { FileUploadRecordStatus, FileUploadRecordType, FileUploadRole, type IFileUpload } from '@/org.libersoft.messages/services/Files/types.ts';
import fileUploadStore from '@/org.libersoft.messages/stores/FileUploadStore.ts';
import { assembleFile } from '@/org.libersoft.messages/services/Files/utils.ts';
import Button from '@/core/components/Button/Button.svelte';
let { children } = $props();
let ref: HTMLDivElement;
let attachedUploads = $state<IFileUpload[]>([]);
let attachmentIds = $state<string[]>([]);
let downloadableRecords = $derived.by(() => {
return attachedUploads.filter(upload => {
return (upload.record.type === FileUploadRecordType.P2P && upload.role === FileUploadRole.RECEIVER && upload.record.status === FileUploadRecordStatus.BEGUN) || (upload.record.type === FileUploadRecordType.SERVER && upload.record.status === FileUploadRecordStatus.FINISHED);
});
});
$inspect('attachedUploads', attachedUploads);
$inspect('downloadableRecords', downloadableRecords);
// determine if the bulk download action should be shown
let showBulkDownloadAction = $derived(downloadableRecords.length > 1);
const uploadStore = $derived(fileUploadStore.store);
// get the uploads based on present attachment IDs
$effect(() => {
const uploads = $uploadStore;
console.log('!!! attachmentIds', attachmentIds);
console.log('!!! uploads', uploads);
if (attachmentIds.length === 0) {
return;
}
attachedUploads = uploads.filter(upload => {
return attachmentIds.includes(upload.record.id);
});
});
// determine attachments type based on the first attachment
const uploadRecordType = $derived.by(() => {
return attachedUploads.length > 0 ? attachedUploads[0].record.type : null;
});
function getAttachmentEls() {
return ref?.closest('.attachments-wrap')?.querySelectorAll('.message-attachment');
}
function onAcceptAll() {
downloadAttachmentsSerial(
downloadableRecords.map(upload => upload.record),
download => {
assembleFile(
new Blob(download.chunksReceived, {
type: download.record.fileMimeType,
}),
download.record.fileOriginalName
);
}
);
}
onMount(() => {
children.forEach(child => {
const uploadId = child.props?.id;
if (uploadId) attachmentIds.push(uploadId);
});
});
</script>
<style>
.attachments-wrap {
display: flex;
flex-direction: column;
gap: 10px;
}
.attachments {
display: flex;
flex-direction: column;
gap: 10px;
}
:global(.message.outgoing .message-attachment-accept-btn) {
display: flex;
}
:global(.message.outgoing) .actions {
display: flex;
justify-content: right;
}
</style>
<div class="attachments-wrap" bind:this={ref}>
<div class="attachments">
{#each children as child (child.tagUniqueId)}
{#if child.component}
<child.component {...child.props} />
{/if}
{/each}
</div>
{#if showBulkDownloadAction}
<div class="actions">
<Button img={uploadRecordType === FileUploadRecordType.P2P ? 'img/check.svg' : 'img/download.svg'} text={uploadRecordType === FileUploadRecordType.P2P ? 'Accept all' : 'Download all'} onClick={onAcceptAll} />
</div>
{/if}
</div>