Initialize VUE project with WS connection, main pages, and various small features

This commit is contained in:
2023-10-25 18:43:44 -05:00
parent 444f0226e6
commit 9cd6f83bec
38 changed files with 5624 additions and 1224 deletions

View File

@@ -0,0 +1,87 @@
import type { FileStructure, DocumentStore } from '@/stores/documents'
import { useDocumentStore } from '@/stores/documents'
import { getFileExtension } from '@/utils'
import Client from '@/repositories/Client'
type BaseDocument = {
name: string;
key?: number;
};
export type FolderDocument = BaseDocument & {
size: number;
modified: string;
type: 'folder';
};
export type FileDocument = BaseDocument & {
type: 'file';
ext: string;
data: string;
};
export type Document = FolderDocument | FileDocument;
export const url_document_watch_ws = import.meta.env.VITE_URL_DOCUMENT_WATCH_WS
export const url_document_upload_ws = import.meta.env.VITE_URL_DOCUMENT_UPLOAD_WS
export const url_document_get = import.meta.env.VITE_URL_DOCUMENT_GET
export class DocumentHandler {
constructor( private store: DocumentStore = useDocumentStore() ) {
this.handleWebSocketMessage = this.handleWebSocketMessage.bind(this);
}
handleWebSocketMessage(event: MessageEvent) {
const msg = JSON.parse(event.data);
switch (true) {
case !!msg.root:
this.handleRootMessage(msg);
break;
case !!msg.update:
this.handleUpdateMessage(msg);
break;
default:
}
}
private handleRootMessage({ root }: { root: FileStructure }) {
if (this.store && this.store.root) this.store.root = root;
}
private handleUpdateMessage(updateData: { update: FileStructure[] }) {
const root = updateData.update[0]
if(root) this.store.root = root
}
}
export class DocumentUploadHandler {
constructor( private store: DocumentStore = useDocumentStore() ) {
this.handleWebSocketMessage = this.handleWebSocketMessage.bind(this);
}
handleWebSocketMessage(event: MessageEvent) {
const msg = JSON.parse(event.data);
switch (true) {
case !!msg.written:
this.handleWrittenMessage(msg);
break;
default:
}
}
private handleWrittenMessage(msg : { written : number}) {
// if (this.store && this.store.root) this.store.root = root;
console.log('Written message', msg.written)
}
}
export async function fetchFile(path: string): Promise<FileDocument>{
const file = await Client.get(path)
const name = path.substring(1 , path.length)
return {
name,
data: file.data,
type: 'file',
ext: getFileExtension(name)
}
}