mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 03:25:12 +01:00
adjust fileview/tile to pendingevent changes
This commit is contained in:
parent
628a3b65c6
commit
f7a07a9e79
@ -17,21 +17,17 @@ limitations under the License.
|
|||||||
|
|
||||||
import {MessageTile} from "./MessageTile.js";
|
import {MessageTile} from "./MessageTile.js";
|
||||||
import {formatSize} from "../../../../../utils/formatSize.js";
|
import {formatSize} from "../../../../../utils/formatSize.js";
|
||||||
|
import {SendStatus} from "../../../../../matrix/room/sending/PendingEvent.js";
|
||||||
|
|
||||||
export class FileTile extends MessageTile {
|
export class FileTile extends MessageTile {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
super(options);
|
super(options);
|
||||||
this._error = null;
|
this._downloadError = null;
|
||||||
this._downloading = false;
|
this._downloading = false;
|
||||||
if (this._isUploading) {
|
|
||||||
this.track(this._entry.attachments.url.status.subscribe(() => {
|
|
||||||
this.emitChange("label");
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async download() {
|
async download() {
|
||||||
if (this._downloading || this._isUploading) {
|
if (this._downloading || this.isPending) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const content = this._getContent();
|
const content = this._getContent();
|
||||||
@ -43,7 +39,7 @@ export class FileTile extends MessageTile {
|
|||||||
blob = await this._mediaRepository.downloadAttachment(content);
|
blob = await this._mediaRepository.downloadAttachment(content);
|
||||||
this.platform.saveFileAs(blob, filename);
|
this.platform.saveFileAs(blob, filename);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._error = err;
|
this._downloadError = err;
|
||||||
} finally {
|
} finally {
|
||||||
blob?.dispose();
|
blob?.dispose();
|
||||||
this._downloading = false;
|
this._downloading = false;
|
||||||
@ -51,39 +47,40 @@ export class FileTile extends MessageTile {
|
|||||||
this.emitChange("label");
|
this.emitChange("label");
|
||||||
}
|
}
|
||||||
|
|
||||||
get size() {
|
|
||||||
if (this._isUploading) {
|
|
||||||
return this._entry.attachments.url.localPreview.size;
|
|
||||||
} else {
|
|
||||||
return this._getContent().info?.size;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get _isUploading() {
|
|
||||||
return this._entry.attachments?.url && !this._entry.attachments.url.isUploaded;
|
|
||||||
}
|
|
||||||
|
|
||||||
get label() {
|
get label() {
|
||||||
if (this._error) {
|
if (this._downloadError) {
|
||||||
return `Could not decrypt file: ${this._error.message}`;
|
return `Could not download file: ${this._downloadError.message}`;
|
||||||
}
|
|
||||||
if (this._entry.attachments?.url?.error) {
|
|
||||||
return `Failed to upload: ${this._entry.attachments.url.error.message}`;
|
|
||||||
}
|
}
|
||||||
const content = this._getContent();
|
const content = this._getContent();
|
||||||
const filename = content.body;
|
const filename = content.body;
|
||||||
const size = formatSize(this.size);
|
|
||||||
if (this._isUploading) {
|
|
||||||
return this.i18n`Uploading (${this._entry.attachments.url.status.get()}) ${filename} (${size})…`;
|
|
||||||
} else if (this._downloading) {
|
|
||||||
return this.i18n`Downloading ${filename} (${size})…`;
|
|
||||||
} else {
|
|
||||||
return this.i18n`Download ${filename} (${size})`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
get error() {
|
if (this._entry.isPending) {
|
||||||
return null;
|
const {pendingEvent} = this._entry;
|
||||||
|
switch (pendingEvent?.status) {
|
||||||
|
case SendStatus.Waiting:
|
||||||
|
return this.i18n`Waiting to send ${filename}…`;
|
||||||
|
case SendStatus.EncryptingAttachments:
|
||||||
|
case SendStatus.Encrypting:
|
||||||
|
return this.i18n`Encrypting ${filename}…`;
|
||||||
|
case SendStatus.UploadingAttachments:{
|
||||||
|
const percent = Math.round((pendingEvent.attachmentsSentBytes / pendingEvent.attachmentsTotalBytes) * 100);
|
||||||
|
return this.i18n`Uploading ${filename}: ${percent}%`;
|
||||||
|
}
|
||||||
|
case SendStatus.Sending:
|
||||||
|
return this.i18n`Sending ${filename}…`;
|
||||||
|
case SendStatus.Error:
|
||||||
|
return this.i18n`Error: could not send ${filename}: ${pendingEvent.error.message}`;
|
||||||
|
default:
|
||||||
|
return `Unknown send status for ${filename}`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const size = formatSize(this._getContent().info?.size);
|
||||||
|
if (this._downloading) {
|
||||||
|
return this.i18n`Downloading ${filename} (${size})…`;
|
||||||
|
} else {
|
||||||
|
return this.i18n`Download ${filename} (${size})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get shape() {
|
get shape() {
|
||||||
|
@ -19,11 +19,17 @@ import {renderMessage} from "./common.js";
|
|||||||
|
|
||||||
export class FileView extends TemplateView {
|
export class FileView extends TemplateView {
|
||||||
render(t, vm) {
|
render(t, vm) {
|
||||||
return renderMessage(t, vm, [
|
if (vm.isPending) {
|
||||||
t.p([
|
return renderMessage(t, vm, t.p([
|
||||||
|
vm => vm.label,
|
||||||
|
" ",
|
||||||
|
t.button({className: "link", onClick: () => vm.abortSending()}, vm.i18n`Cancel`),
|
||||||
|
]));
|
||||||
|
} else {
|
||||||
|
return renderMessage(t, vm, t.p([
|
||||||
t.button({className: "link", onClick: () => vm.download()}, vm => vm.label),
|
t.button({className: "link", onClick: () => vm.download()}, vm => vm.label),
|
||||||
t.time({className: {hidden: !vm.date}}, vm.date + " " + vm.time)
|
t.time(vm.date + " " + vm.time)
|
||||||
])
|
]));
|
||||||
]);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ export function renderMessage(t, vm, children) {
|
|||||||
pending: vm.isPending,
|
pending: vm.isPending,
|
||||||
unverified: vm.isUnverified,
|
unverified: vm.isUnverified,
|
||||||
continuation: vm => vm.isContinuation,
|
continuation: vm => vm.isContinuation,
|
||||||
messageStatus: vm => vm.shape === "message-status" || vm.shape === "missing-attachment",
|
messageStatus: vm => vm.shape === "message-status" || vm.shape === "missing-attachment" || vm.shape === "file",
|
||||||
};
|
};
|
||||||
|
|
||||||
const profile = t.div({className: "profile"}, [
|
const profile = t.div({className: "profile"}, [
|
||||||
|
Loading…
Reference in New Issue
Block a user