Removing stuff, refactoring for simplicity

This commit is contained in:
Leo Vasanko
2023-11-02 21:51:16 +00:00
parent 7e5901a2cf
commit 831b2716f7
11 changed files with 84 additions and 751 deletions

View File

@@ -1,28 +1,22 @@
import type { FileStructure, DocumentStore } from '@/stores/documents'
import type { DocumentStore } from '@/stores/documents'
import { useDocumentStore } from '@/stores/documents'
import { getFileExtension } from '@/utils'
import Client from '@/repositories/Client'
export type FUID = string;
type BaseDocument = {
name: string
key?: number | string
selected?: boolean
key: FUID
};
export type FolderDocument = BaseDocument & {
type: 'folder' | 'folder-file';
size: number;
sizedisp: string;
mtime: number;
modified: string;
type: 'folder' | 'file'
size: number
sizedisp: string
mtime: number
modified: string
};
export type FileDocument = BaseDocument & {
type: 'file';
ext: string;
data: string;
};
export type Document = FolderDocument
export type errorEvent = {
error: {
@@ -32,8 +26,31 @@ export type errorEvent = {
}
};
export type Document = FolderDocument | FileDocument;
// Raw types the backend /api/watch sends us
export type FileEntry = {
id: FUID
size: number
mtime: number
}
export type DirEntry = {
id: FUID
size: number
mtime: number
dir: DirList
}
export type DirList = Record<string, FileEntry | DirEntry>
export type UpdateEntry = {
name: string
deleted?: boolean
id?: FUID
size?: number
mtime?: number
dir?: DirList
}
export const url_document_watch_ws = '/api/watch'
export const url_document_upload_ws = '/api/upload'
@@ -60,18 +77,27 @@ export class DocumentHandler {
}
}
private handleRootMessage({ root }: { root: FileStructure }) {
private handleRootMessage({ root }: { root: DirEntry }) {
if (this.store && this.store.root) {
this.store.user.isLoggedIn = true;
this.store.root = root;
}
}
private handleUpdateMessage(updateData: { update: FileStructure[] }) {
const root = updateData.update[0]
if(root) {
this.store.user.isLoggedIn = true;
this.store.root = root
private handleUpdateMessage(updateData: { update: UpdateEntry[] }) {
let node: DirEntry = this.store.root;
for (const elem of updateData.update) {
if (elem.deleted) {
delete node.dir[elem.name]
break // Deleted elements can't have further children
}
if (elem.name !== undefined) {
// @ts-ignore
node = node.dir[elem.name] ||= {}
}
if (elem.id !== undefined) node.id = elem.id
if (elem.size !== undefined) node.size = elem.size
if (elem.mtime !== undefined) node.mtime = elem.mtime
if (elem.dir !== undefined) node.dir = elem.dir
}
}
private handleError(msg: errorEvent){
@@ -103,13 +129,3 @@ export class DocumentUploadHandler {
console.log('Written message', msg.written)
}
}
export async function fetchFile(path: string): Promise<FileDocument>{
const file = await Client.get(url_document_get + path)
const name = path.substring(1 , path.length)
return {
name,
data: file.data,
type: 'file',
ext: getFileExtension(name)
}
}