Skip to content

Commit

Permalink
fix: update file msg element after zipping
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanenkoStud committed Nov 22, 2023
1 parent 67afae9 commit ff6b770
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
18 changes: 18 additions & 0 deletions packages/kite-chat/src/kite-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import type {
FileMsg,
ContentMsg,
FailedMsg,
ZippedMsg,
} from './kite-types';

import {MsgType} from './kite-types';

import {
KiteChatElement,
KiteMsgElement,
KiteFileElement,
KiteMsg as KiteChatMsg,
randomStringId,
isPlaintextMsg,
Expand Down Expand Up @@ -248,6 +250,9 @@ export class KiteChat {
case MsgType.FAILED:
this.onFailedMessage(payload);
break;
case MsgType.ZIPPED:
this.onZippedMessage(payload);
break;
case MsgType.ONLINE:
case MsgType.OFFLINE:
this.log(payload);
Expand Down Expand Up @@ -285,6 +290,19 @@ export class KiteChat {
console.error(e.code, e.reason);
}

protected onZippedMessage(e: ZippedMsg) {
console.debug('onZippedMessage', e);
const fileElement = document.querySelector(
`${KiteMsgElement.TAG}[messageId="${e.messageId}"] > ${KiteFileElement.TAG}`
) as KiteFileElement | undefined;
if (fileElement) {
fileElement.file = e.file;
}
this.update(e.messageId, {
file: e.file,
} as ContentMsg);
}

protected onFailedMessage(e: FailedMsg) {
console.debug('onFailedMessage', e);
const msgElement = document.querySelector(
Expand Down
12 changes: 10 additions & 2 deletions packages/kite-chat/src/kite-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export enum MsgType {
PING = 'PING',
PONG = 'PONG',
FAILED = "FAILED",
ACTIVE_TAB = "ACT_TAB"
ACTIVE_TAB = "ACT_TAB",
ZIPPED = "ZIP"
}

export type JoinChannel = {
Expand Down Expand Up @@ -135,6 +136,12 @@ export type ActiveTab = {
type: MsgType.ACTIVE_TAB;
};

export type ZippedMsg = {
type: MsgType.ZIPPED;
messageId: string;
file: File;
};

export type ContentMsg = PlaintextMsg | FileMsg;

export enum FileVerification {
Expand Down Expand Up @@ -171,4 +178,5 @@ export type KiteMsg =
| Ping
| Pong
| FailedMsg
| ActiveTab;
| ActiveTab
| ZippedMsg;
17 changes: 13 additions & 4 deletions packages/kite-chat/src/kite-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function onPlaintextMessage(payload: PlaintextMsg, tabPort: KiteMessagePort) {
function onFileMessage(payload: FileMsg, tabPort: KiteMessagePort) {
messageHistory.push(payload);

const uploadFile = async (file: File) => {
const uploadFile = (file: File) => {
const upload: UploadRequest = {
type: MsgType.UPLOAD,
messageId: payload.messageId,
Expand All @@ -270,7 +270,7 @@ function onFileMessage(payload: FileMsg, tabPort: KiteMessagePort) {
queue(upload);
};

const failedFile = async (reason: FileVerification, errorMessage?: string) => {
const failedFile = (reason: FileVerification, errorMessage?: string) => {
tabPort.postMessage({
type: MsgType.FAILED,
reason: reason,
Expand All @@ -279,6 +279,14 @@ function onFileMessage(payload: FileMsg, tabPort: KiteMessagePort) {
});
}

const zippedFile = (file: File) => {
tabPort.postMessage({
type: MsgType.ZIPPED,
messageId: payload.messageId,
file: file,
});
}

const maxSize = (type: string) => formatSize(SUPPORTED_FILE_FORMATS[type as keyof typeof SUPPORTED_FILE_FORMATS]);

const result = verifyFile(payload.file);
Expand All @@ -293,8 +301,9 @@ function onFileMessage(payload: FileMsg, tabPort: KiteMessagePort) {
break;
}
zipFile(payload.file, ZIP_FILE_FORMAT)
.then(zippedFile => {
uploadFile(zippedFile);
.then(file => {
zippedFile(file);
uploadFile(file);
})
.catch(error => {
console.error('Error zipping file:', error);
Expand Down

0 comments on commit ff6b770

Please sign in to comment.