From 95010a5cce9c2262fd468eb82e758d954f78865f Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Thu, 4 May 2023 14:36:33 -0400 Subject: [PATCH] fix: draft folder/file handling (#777) --- .../core/dev-test/backends/github/config.yml | 2 + packages/core/src/actions/entries.ts | 26 +- packages/core/src/actions/mediaLibrary.ts | 2 +- .../common/FolderCreationDialog.tsx | 12 +- .../media-library/common/MediaLibraryCard.tsx | 2 +- .../hooks/__tests__/useMediaFiles.spec.tsx | 427 ++++++++++++++++++ packages/core/src/lib/hooks/useMediaAsset.ts | 3 +- packages/core/src/lib/hooks/useMediaFiles.ts | 82 ++-- packages/core/src/lib/util/getBlobSHA.ts | 6 +- packages/core/test/data/fields.mock.ts | 6 + 10 files changed, 505 insertions(+), 63 deletions(-) create mode 100644 packages/core/src/lib/hooks/__tests__/useMediaFiles.spec.tsx diff --git a/packages/core/dev-test/backends/github/config.yml b/packages/core/dev-test/backends/github/config.yml index b9687de6..86bc4cd5 100644 --- a/packages/core/dev-test/backends/github/config.yml +++ b/packages/core/dev-test/backends/github/config.yml @@ -58,6 +58,8 @@ collections: name: image widget: image required: false + media_library: + folder_support: true - label: Body name: body widget: markdown diff --git a/packages/core/src/actions/entries.ts b/packages/core/src/actions/entries.ts index abfbe645..2445dbd4 100644 --- a/packages/core/src/actions/entries.ts +++ b/packages/core/src/actions/entries.ts @@ -556,18 +556,20 @@ export function retrieveLocalBackup(collection: Collection, slug: string) { // load assets from backup const mediaFiles = entry.mediaFiles || []; const assetProxies: AssetProxy[] = await Promise.all( - mediaFiles.map(file => { - if (file.file || file.url) { - return createAssetProxy({ - path: file.path, - file: file.file, - url: file.url, - field: file.field, - }); - } else { - return getAsset(collection, entry, file.path, file.field)(dispatch, getState); - } - }), + mediaFiles + .filter(file => !file.isDirectory) + .map(file => { + if (file.file || file.url) { + return createAssetProxy({ + path: file.path, + file: file.file, + url: file.url, + field: file.field, + }); + } else { + return getAsset(collection, entry, file.path, file.field)(dispatch, getState); + } + }), ); dispatch(addAssets(assetProxies)); diff --git a/packages/core/src/actions/mediaLibrary.ts b/packages/core/src/actions/mediaLibrary.ts index 40999698..9a0a7bf6 100644 --- a/packages/core/src/actions/mediaLibrary.ts +++ b/packages/core/src/actions/mediaLibrary.ts @@ -270,7 +270,7 @@ export function persistMedia( let mediaFile: ImplementationMediaFile; if (editingDraft) { - const id = await getBlobSHA(file); + const id = `${assetProxy.path}/${await getBlobSHA(file)}`; mediaFile = createMediaFileFromAsset({ id, file, diff --git a/packages/core/src/components/media-library/common/FolderCreationDialog.tsx b/packages/core/src/components/media-library/common/FolderCreationDialog.tsx index cb2dd7a4..f74ccf62 100644 --- a/packages/core/src/components/media-library/common/FolderCreationDialog.tsx +++ b/packages/core/src/components/media-library/common/FolderCreationDialog.tsx @@ -33,12 +33,18 @@ const FolderCreationDialog: FC> = ({ } onCreate(folderName); + setFolderName(''); }, [folderName, onCreate]); + const handleClose = useCallback(() => { + onClose(); + setFolderName(''); + }, [onClose]); + return ( + @@ -96,7 +102,7 @@ const FolderCreationDialog: FC> = ({ space-x-2 " > -