From b6bffe2a5a50cd43e5950d7192c12eb21553b28b Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Wed, 21 Sep 2022 18:40:05 -0400 Subject: [PATCH] Redirect to first entry if files collection only has one file --- .../src/components/App/App.js | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/netlify-cms-core/src/components/App/App.js b/packages/netlify-cms-core/src/components/App/App.js index 8e33b0db..2892fe98 100644 --- a/packages/netlify-cms-core/src/components/App/App.js +++ b/packages/netlify-cms-core/src/components/App/App.js @@ -72,6 +72,42 @@ function getDefaultPath(collections) { } } +/** + * Returns default collection name if only one collection + * + * @param {Collection} collection + * @returns {string} + */ +function getDefaultCollectionPath(collection) { + if (collection.has('files') && collection.get('files').size === 1) { + return `/collections/${collection.get('name')}/entries/${collection.get('files').first().get('name')}`; + } + + return null; +} + +function RouteInCollectionDefault({ collections, render, ...props }) { + const defaultPath = getDefaultPath(collections); + return ( + { + const collectionExists = collections.get(routeProps.match.params.name); + if (!collectionExists) { + return ; + } + + const defaultCollectionPath = getDefaultCollectionPath(collections); + if (defaultCollectionPath !== null) { + return ; + } + + return render(routeProps); + }} + /> + ); +} + function RouteInCollection({ collections, render, ...props }) { const defaultPath = getDefaultPath(collections); return ( @@ -224,7 +260,7 @@ class App extends React.Component { to={defaultPath} /> {hasWorkflow ? : null} -