refactor: improvemets
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* Provides reactive access to file manager state and actions
|
||||
*/
|
||||
|
||||
import { computed, ref } from 'vue'
|
||||
import { computed, ref, unref } from 'vue'
|
||||
import type { Ref, ComputedRef } from 'vue'
|
||||
import { useProvidersStore } from '@DocumentsManager/stores/providersStore'
|
||||
import { useServicesStore } from '@DocumentsManager/stores/servicesStore'
|
||||
@@ -17,8 +17,8 @@ import { EntityObject } from '@DocumentsManager/models/entity'
|
||||
const TRANSFER_BASE_URL = '/m/documents_manager'
|
||||
|
||||
export interface UseFileManagerOptions {
|
||||
providerId: string
|
||||
serviceId: string
|
||||
providerId: string | Ref<string> | ComputedRef<string>
|
||||
serviceId: string | Ref<string> | ComputedRef<string>
|
||||
autoFetch?: boolean
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
|
||||
const { providerId, serviceId, autoFetch = false } = options
|
||||
|
||||
const currentProviderId = () => unref(providerId)
|
||||
const currentServiceId = () => unref(serviceId)
|
||||
|
||||
// Current location (folder being viewed)
|
||||
const currentLocation: Ref<string> = ref(ROOT_ID)
|
||||
|
||||
@@ -37,21 +40,21 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
const error = computed(() => nodesStore.error)
|
||||
|
||||
// Provider and service
|
||||
const provider = computed(() => providersStore.provider(providerId))
|
||||
const service = computed(() => servicesStore.service(providerId, serviceId))
|
||||
const provider = computed(() => providersStore.provider(currentProviderId()))
|
||||
const service = computed(() => servicesStore.service(currentProviderId(), currentServiceId()))
|
||||
const rootId = computed(() => ROOT_ID)
|
||||
|
||||
// Current children
|
||||
const currentChildren = computed(() =>
|
||||
nodesStore.getChildren(providerId, serviceId, currentLocation.value)
|
||||
nodesStore.getChildren(currentProviderId(), currentServiceId(), currentLocation.value)
|
||||
)
|
||||
|
||||
const currentCollections: ComputedRef<CollectionObject[]> = computed(() =>
|
||||
nodesStore.getChildCollections(providerId, serviceId, currentLocation.value)
|
||||
nodesStore.getChildCollections(currentProviderId(), currentServiceId(), currentLocation.value)
|
||||
)
|
||||
|
||||
const currentEntities: ComputedRef<EntityObject[]> = computed(() =>
|
||||
nodesStore.getChildEntities(providerId, serviceId, currentLocation.value)
|
||||
nodesStore.getChildEntities(currentProviderId(), currentServiceId(), currentLocation.value)
|
||||
)
|
||||
|
||||
// Breadcrumb path
|
||||
@@ -59,7 +62,7 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
if (currentLocation.value === ROOT_ID) {
|
||||
return []
|
||||
}
|
||||
return nodesStore.getPath(providerId, serviceId, currentLocation.value)
|
||||
return nodesStore.getPath(currentProviderId(), currentServiceId(), currentLocation.value)
|
||||
})
|
||||
|
||||
// Is at root?
|
||||
@@ -76,7 +79,7 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
if (currentLocation.value === ROOT_ID) {
|
||||
return
|
||||
}
|
||||
const currentNode = nodesStore.getNode(providerId, serviceId, currentLocation.value)
|
||||
const currentNode = nodesStore.getNode(currentProviderId(), currentServiceId(), currentLocation.value)
|
||||
if (currentNode) {
|
||||
await navigateTo(currentNode.collection ? String(currentNode.collection) : ROOT_ID)
|
||||
}
|
||||
@@ -94,8 +97,8 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
range?: ListRange
|
||||
) => {
|
||||
await nodesStore.fetchNodes(
|
||||
providerId,
|
||||
serviceId,
|
||||
currentProviderId(),
|
||||
currentServiceId(),
|
||||
currentLocation.value === ROOT_ID ? ROOT_ID : currentLocation.value,
|
||||
filter,
|
||||
sort,
|
||||
@@ -106,8 +109,8 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
// Create a new folder
|
||||
const createFolder = async (label: string): Promise<CollectionObject> => {
|
||||
return await nodesStore.createCollection(
|
||||
providerId,
|
||||
serviceId,
|
||||
currentProviderId(),
|
||||
currentServiceId(),
|
||||
currentLocation.value === ROOT_ID ? ROOT_ID : currentLocation.value,
|
||||
{ label, owner: '' }
|
||||
)
|
||||
@@ -129,8 +132,8 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
}
|
||||
|
||||
return await nodesStore.createEntity(
|
||||
providerId,
|
||||
serviceId,
|
||||
currentProviderId(),
|
||||
currentServiceId(),
|
||||
currentLocation.value === ROOT_ID ? ROOT_ID : currentLocation.value,
|
||||
properties
|
||||
)
|
||||
@@ -138,13 +141,13 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
|
||||
// Rename a node
|
||||
const renameNode = async (nodeId: string, newLabel: string) => {
|
||||
const node = nodesStore.getNode(providerId, serviceId, nodeId)
|
||||
const node = nodesStore.getNode(currentProviderId(), currentServiceId(), nodeId)
|
||||
if (!node) {
|
||||
throw new Error('Node not found')
|
||||
}
|
||||
|
||||
if (node instanceof CollectionObject) {
|
||||
return await nodesStore.updateCollection(providerId, serviceId, nodeId, {
|
||||
return await nodesStore.updateCollection(currentProviderId(), currentServiceId(), nodeId, {
|
||||
label: newLabel,
|
||||
owner: node.properties.owner,
|
||||
})
|
||||
@@ -158,53 +161,53 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
format: node.properties.format,
|
||||
encoding: node.properties.encoding,
|
||||
}
|
||||
return await nodesStore.updateEntity(providerId, serviceId, node.collection, nodeId, properties)
|
||||
return await nodesStore.updateEntity(currentProviderId(), currentServiceId(), node.collection, nodeId, properties)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a node
|
||||
const deleteNode = async (nodeId: string): Promise<boolean> => {
|
||||
const node = nodesStore.getNode(providerId, serviceId, nodeId)
|
||||
const node = nodesStore.getNode(currentProviderId(), currentServiceId(), nodeId)
|
||||
if (!node) {
|
||||
throw new Error('Node not found')
|
||||
}
|
||||
|
||||
if (node instanceof CollectionObject) {
|
||||
return await nodesStore.deleteCollection(providerId, serviceId, nodeId)
|
||||
return await nodesStore.deleteCollection(currentProviderId(), currentServiceId(), nodeId)
|
||||
} else {
|
||||
return await nodesStore.deleteEntity(providerId, serviceId, node.collection, nodeId)
|
||||
return await nodesStore.deleteEntity(currentProviderId(), currentServiceId(), node.collection, nodeId)
|
||||
}
|
||||
}
|
||||
|
||||
// Read file content
|
||||
const readFile = async (entityId: string): Promise<string | null> => {
|
||||
const node = nodesStore.getNode(providerId, serviceId, entityId)
|
||||
const node = nodesStore.getNode(currentProviderId(), currentServiceId(), entityId)
|
||||
if (!node || !(node instanceof EntityObject)) {
|
||||
throw new Error('Entity not found')
|
||||
}
|
||||
return await nodesStore.readEntity(providerId, serviceId, node.collection || ROOT_ID, entityId)
|
||||
return await nodesStore.readEntity(currentProviderId(), currentServiceId(), node.collection || ROOT_ID, entityId)
|
||||
}
|
||||
|
||||
// Write file content
|
||||
const writeFile = async (entityId: string, content: string): Promise<number> => {
|
||||
const node = nodesStore.getNode(providerId, serviceId, entityId)
|
||||
const node = nodesStore.getNode(currentProviderId(), currentServiceId(), entityId)
|
||||
if (!node || !(node instanceof EntityObject)) {
|
||||
throw new Error('Entity not found')
|
||||
}
|
||||
return await nodesStore.writeEntity(providerId, serviceId, node.collection, entityId, content)
|
||||
return await nodesStore.writeEntity(currentProviderId(), currentServiceId(), node.collection, entityId, content)
|
||||
}
|
||||
|
||||
// Get a URL suitable for inline viewing (img src / video src)
|
||||
const getEntityUrl = (entityId: string, collectionId?: string | null): string => {
|
||||
const collection = collectionId ?? currentLocation.value
|
||||
return `${TRANSFER_BASE_URL}/download/entity/${encodeURIComponent(providerId)}/${encodeURIComponent(serviceId)}/${encodeURIComponent(collection)}/${encodeURIComponent(entityId)}`
|
||||
return `${TRANSFER_BASE_URL}/download/entity/${encodeURIComponent(currentProviderId())}/${encodeURIComponent(currentServiceId())}/${encodeURIComponent(collection)}/${encodeURIComponent(entityId)}`
|
||||
}
|
||||
|
||||
// Download a single file
|
||||
const downloadEntity = (entityId: string, collectionId?: string | null): void => {
|
||||
const collection = collectionId ?? currentLocation.value
|
||||
// Use path parameters: /download/entity/{provider}/{service}/{collection}/{identifier}
|
||||
const url = `${TRANSFER_BASE_URL}/download/entity/${encodeURIComponent(providerId)}/${encodeURIComponent(serviceId)}/${encodeURIComponent(collection)}/${encodeURIComponent(entityId)}`
|
||||
const url = `${TRANSFER_BASE_URL}/download/entity/${encodeURIComponent(currentProviderId())}/${encodeURIComponent(currentServiceId())}/${encodeURIComponent(collection)}/${encodeURIComponent(entityId)}`
|
||||
|
||||
// Trigger download by opening URL (browser handles it)
|
||||
window.open(url, '_blank')
|
||||
@@ -212,7 +215,7 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
|
||||
const downloadCollection = (collectionId: string): void => {
|
||||
// Use path parameters: /download/collection/{provider}/{service}/{identifier}
|
||||
const url = `${TRANSFER_BASE_URL}/download/collection/${encodeURIComponent(providerId)}/${encodeURIComponent(serviceId)}/${encodeURIComponent(collectionId)}`
|
||||
const url = `${TRANSFER_BASE_URL}/download/collection/${encodeURIComponent(currentProviderId())}/${encodeURIComponent(currentServiceId())}/${encodeURIComponent(collectionId)}`
|
||||
|
||||
window.open(url, '_blank')
|
||||
}
|
||||
@@ -220,8 +223,8 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
const downloadArchive = (ids: string[], name: string = 'download', collectionId?: string | null): void => {
|
||||
const collection = collectionId ?? currentLocation.value
|
||||
const params = new URLSearchParams({
|
||||
provider: providerId,
|
||||
service: serviceId,
|
||||
provider: currentProviderId(),
|
||||
service: currentServiceId(),
|
||||
})
|
||||
ids.forEach(id => params.append('ids[]', id))
|
||||
if (name) {
|
||||
@@ -239,7 +242,7 @@ export function useFileManager(options: UseFileManagerOptions) {
|
||||
// Initialize - fetch providers, services, and initial nodes if autoFetch
|
||||
const initialize = async () => {
|
||||
await providersStore.list()
|
||||
await servicesStore.list({ [providerId]: true })
|
||||
await servicesStore.list({ [currentProviderId()]: true })
|
||||
if (autoFetch) {
|
||||
await refresh()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user