Files
warren/frontend/utils/files.ts
2025-07-30 21:57:05 +02:00

61 lines
1.5 KiB
TypeScript

import { moveFile } from '~/lib/api/warrens';
import type { DirectoryEntry } from '~/shared/types';
export function joinPaths(path: string, ...other: string[]): string {
for (const p of other) {
if (!path.endsWith('/')) {
path += '/';
}
path += trim(p, '/');
}
return path;
}
export function getParentPath(path: string): string {
const sliceEnd = Math.max(1, path.lastIndexOf('/'));
return path.slice(0, sliceEnd);
}
export function onDirectoryEntryDrop(
entry: DirectoryEntry,
isParent: boolean = false
): (e: DragEvent) => void {
return async (e: DragEvent) => {
const warrenStore = useWarrenStore();
if (e.dataTransfer == null || warrenStore.current == null) {
return;
}
if (entry.fileType !== 'directory') {
return;
}
const fileName = e.dataTransfer.getData('application/warren');
if (entry.name === fileName) {
return;
}
const currentPath = joinPaths(warrenStore.current.path, fileName);
let targetPath: string;
if (isParent) {
targetPath = joinPaths(
getParentPath(warrenStore.current.path),
fileName
);
} else {
targetPath = joinPaths(
warrenStore.current.path,
entry.name,
fileName
);
}
await moveFile(warrenStore.current.warrenId, currentPath, targetPath);
};
}