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') }) })