chore: interface and type
This commit is contained in:
parent
8f716e6f44
commit
4bdf1f620b
2 changed files with 46 additions and 150 deletions
|
|
@ -1,109 +0,0 @@
|
||||||
import { defineStore } from 'pinia'
|
|
||||||
import { io } from 'socket.io-client'
|
|
||||||
import { useTreeDataStore } from '@/stores/tree-data'
|
|
||||||
import { storeToRefs } from 'pinia'
|
|
||||||
|
|
||||||
const {
|
|
||||||
data,
|
|
||||||
listDataFolder,
|
|
||||||
currentFolder,
|
|
||||||
currentPath,
|
|
||||||
currentFile,
|
|
||||||
listDataFile,
|
|
||||||
} = storeToRefs(useTreeDataStore())
|
|
||||||
const {
|
|
||||||
updateEditFolder,
|
|
||||||
updateDeleteFolder,
|
|
||||||
updateCreateFolder,
|
|
||||||
updateDeleteFile,
|
|
||||||
updateNewFile,
|
|
||||||
} = useTreeDataStore()
|
|
||||||
|
|
||||||
export const useSocketStore = defineStore('socket', () => {
|
|
||||||
const socket = io('http://localhost:25570')
|
|
||||||
|
|
||||||
socket.on('connect', () => {
|
|
||||||
console.log('SocketIO Connected')
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('CreateFolder', (dataSocket) => {
|
|
||||||
const { pathname } = dataSocket
|
|
||||||
|
|
||||||
const pathArray: string[] = pathname.split('/').filter(Boolean)
|
|
||||||
const currentPathResult = pathArray.slice(0, -1).join('/') + '/'
|
|
||||||
|
|
||||||
if (currentPath.value == currentPathResult) {
|
|
||||||
data.value = updateCreateFolder(
|
|
||||||
data.value,
|
|
||||||
pathArray.length,
|
|
||||||
currentPathResult,
|
|
||||||
pathname,
|
|
||||||
{
|
|
||||||
pathname: pathname,
|
|
||||||
name: pathArray[pathArray.length - 1],
|
|
||||||
status: true,
|
|
||||||
folder: [],
|
|
||||||
file: [],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
|
||||||
currentFolder.value.findIndex(
|
|
||||||
(v) => v.name === pathArray[pathArray.length - 1],
|
|
||||||
) === -1
|
|
||||||
) {
|
|
||||||
currentFolder.value.push({
|
|
||||||
pathname: pathname,
|
|
||||||
name: pathArray[pathArray.length - 1],
|
|
||||||
status: true,
|
|
||||||
folder: [],
|
|
||||||
file: [],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
currentFolder.value.sort((a, b) => {
|
|
||||||
return a.name.localeCompare(b.name)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('EditFolder', (dataSocket) => {
|
|
||||||
const { from, to } = dataSocket
|
|
||||||
data.value = updateEditFolder(data.value, from, to)
|
|
||||||
currentFolder.value = updateEditFolder(currentFolder.value, from, to)
|
|
||||||
listDataFolder.value = updateEditFolder(listDataFolder.value, from, to)
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('DeleteFolder', (dataSocket) => {
|
|
||||||
const { pathname } = dataSocket
|
|
||||||
data.value = updateDeleteFolder(data.value, pathname)
|
|
||||||
currentFolder.value = updateDeleteFolder(currentFolder.value, pathname)
|
|
||||||
listDataFolder.value = updateDeleteFolder(listDataFolder.value, pathname)
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('FileDelete', (dataSocket) => {
|
|
||||||
const { pathname } = dataSocket
|
|
||||||
|
|
||||||
currentFile.value = updateDeleteFile(currentFile.value, pathname)
|
|
||||||
listDataFile.value = updateDeleteFile(listDataFile.value, pathname)
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('FileUpdate', (dataSocket) => {
|
|
||||||
const metadata = dataSocket
|
|
||||||
|
|
||||||
const pathArray: string[] = metadata.pathname.split('/').filter(Boolean)
|
|
||||||
const currentPathResult = pathArray.slice(0, -1).join('/') + '/'
|
|
||||||
|
|
||||||
if (currentPath.value == currentPathResult) {
|
|
||||||
listDataFile.value = currentFile.value = updateNewFile(
|
|
||||||
currentFile.value,
|
|
||||||
metadata.pathname,
|
|
||||||
metadata,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
socket.on('disconnect', () => {
|
|
||||||
console.log('SocketIO Disconnected')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
@ -7,6 +7,38 @@ import api from '@/services/HttpService'
|
||||||
|
|
||||||
import { useLoader } from './loader'
|
import { useLoader } from './loader'
|
||||||
|
|
||||||
|
type Path = string
|
||||||
|
|
||||||
|
export interface StorageFolder {
|
||||||
|
pathname: string
|
||||||
|
name: string
|
||||||
|
createdAt: string
|
||||||
|
createdBy: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StorageFile {
|
||||||
|
pathname: string
|
||||||
|
path: string
|
||||||
|
fileName: string
|
||||||
|
fileSize: string
|
||||||
|
fileType: string
|
||||||
|
title: string
|
||||||
|
description: string
|
||||||
|
category: string[]
|
||||||
|
keyword: string[]
|
||||||
|
updatedAt: string
|
||||||
|
updatedBy: string
|
||||||
|
createdAt: string
|
||||||
|
createdBy: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Structure extends StorageFolder {
|
||||||
|
folder: Structure[]
|
||||||
|
file: StorageFile[]
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tree = Structure[]
|
||||||
|
|
||||||
function constructUrl(path: string | string[], append = true) {
|
function constructUrl(path: string | string[], append = true) {
|
||||||
const arr = Array.isArray(path) ? path : path.split('/').filter(Boolean)
|
const arr = Array.isArray(path) ? path : path.split('/').filter(Boolean)
|
||||||
const url =
|
const url =
|
||||||
|
|
@ -33,60 +65,29 @@ function constructUrl(path: string | string[], append = true) {
|
||||||
const useStorage = defineStore('storageStore', () => {
|
const useStorage = defineStore('storageStore', () => {
|
||||||
const loader = useLoader()
|
const loader = useLoader()
|
||||||
const init = ref<boolean>(false)
|
const init = ref<boolean>(false)
|
||||||
const folder = ref<
|
const folder = ref<Record<Path, StorageFolder[]>>({})
|
||||||
Record<
|
const file = ref<Record<Path, StorageFile[]>>({})
|
||||||
string, // path that contains folders
|
|
||||||
{
|
|
||||||
pathname: string
|
|
||||||
name: string
|
|
||||||
}[]
|
|
||||||
>
|
|
||||||
>({})
|
|
||||||
const file = ref<
|
|
||||||
Record<
|
|
||||||
string, // path that contains files
|
|
||||||
{
|
|
||||||
pathname: string
|
|
||||||
path: string
|
|
||||||
fileName: string
|
|
||||||
fileSize: string
|
|
||||||
fileType: string
|
|
||||||
title: string
|
|
||||||
description: string
|
|
||||||
category: string[]
|
|
||||||
keyword: string[]
|
|
||||||
updatedAt: string
|
|
||||||
updatedBy: string
|
|
||||||
createdAt: string
|
|
||||||
createdBy: string
|
|
||||||
}[]
|
|
||||||
>
|
|
||||||
>({})
|
|
||||||
const tree = computed(() => {
|
const tree = computed(() => {
|
||||||
type Structure = {
|
let structure: Tree = []
|
||||||
pathname: string
|
|
||||||
name: string
|
|
||||||
folder: Structure
|
|
||||||
file: (typeof file.value)[string]
|
|
||||||
}[]
|
|
||||||
|
|
||||||
let structure: Structure = []
|
|
||||||
|
|
||||||
// parse list of folder and list of file into tree
|
// parse list of folder and list of file into tree
|
||||||
Object.entries(folder.value).forEach(([key, value]) => {
|
Object.entries(folder.value).forEach(([key, value]) => {
|
||||||
const arr = key.split('/').filter(Boolean)
|
const arr = key.split('/').filter(Boolean)
|
||||||
|
|
||||||
// init outer tree
|
// Once run then it is init
|
||||||
|
if (!init.value) init.value = true
|
||||||
|
|
||||||
if (arr.length === 0) {
|
if (arr.length === 0) {
|
||||||
if (!init.value) init.value = true
|
|
||||||
structure = value.map((v) => ({
|
structure = value.map((v) => ({
|
||||||
pathname: v.pathname,
|
pathname: v.pathname,
|
||||||
name: v.name,
|
name: v.name,
|
||||||
|
createdAt: v.createdAt,
|
||||||
|
createdBy: v.createdBy,
|
||||||
folder: [],
|
folder: [],
|
||||||
file: [],
|
file: [],
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
let current: Structure[number] | undefined
|
let current: Structure | undefined
|
||||||
|
|
||||||
// traverse into tree
|
// traverse into tree
|
||||||
arr.forEach((v, i) => {
|
arr.forEach((v, i) => {
|
||||||
|
|
@ -101,6 +102,8 @@ const useStorage = defineStore('storageStore', () => {
|
||||||
current.folder = value.map((v) => ({
|
current.folder = value.map((v) => ({
|
||||||
pathname: v.pathname,
|
pathname: v.pathname,
|
||||||
name: v.name,
|
name: v.name,
|
||||||
|
createdAt: v.createdAt,
|
||||||
|
createdBy: v.createdBy,
|
||||||
folder: [],
|
folder: [],
|
||||||
file: [],
|
file: [],
|
||||||
}))
|
}))
|
||||||
|
|
@ -116,7 +119,7 @@ const useStorage = defineStore('storageStore', () => {
|
||||||
dept: number
|
dept: number
|
||||||
}>({
|
}>({
|
||||||
path: '',
|
path: '',
|
||||||
dept: 1,
|
dept: 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!init.value) goto(sessionStorage.getItem('path') || '')
|
if (!init.value) goto(sessionStorage.getItem('path') || '')
|
||||||
|
|
@ -186,6 +189,8 @@ const useStorage = defineStore('storageStore', () => {
|
||||||
folder.value[path].push({
|
folder.value[path].push({
|
||||||
pathname: data.pathname,
|
pathname: data.pathname,
|
||||||
name: arr[arr.length - 1],
|
name: arr[arr.length - 1],
|
||||||
|
createdAt: 'n/a',
|
||||||
|
createdBy: 'n/a',
|
||||||
})
|
})
|
||||||
folder.value[path].sort((a, b) => a.pathname.localeCompare(b.pathname))
|
folder.value[path].sort((a, b) => a.pathname.localeCompare(b.pathname))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue