From 88665faf275d89c12cf52c76f486927ea42f0009 Mon Sep 17 00:00:00 2001 From: Daniel Lautzenheiser Date: Sat, 28 Jan 2023 17:37:47 -0500 Subject: [PATCH] fix: default path when no folder collections exist (#452) --- packages/core/src/components/App/App.tsx | 16 ++-------------- .../components/Collection/CollectionRoute.tsx | 10 +--------- .../core/src/components/Editor/EditorRoute.tsx | 10 +--------- packages/core/src/lib/util/collection.util.ts | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 32 deletions(-) diff --git a/packages/core/src/components/App/App.tsx b/packages/core/src/components/App/App.tsx index 0987b982..2b55c80f 100644 --- a/packages/core/src/components/App/App.tsx +++ b/packages/core/src/components/App/App.tsx @@ -13,6 +13,7 @@ import { discardDraft as discardDraftAction } from '@staticcms/core/actions/entr import { currentBackend } from '@staticcms/core/backend'; import { colors, GlobalStyles } from '@staticcms/core/components/UI/styles'; import { history } from '@staticcms/core/routing/history'; +import { getDefaultPath } from '../../lib/util/collection.util'; import CollectionRoute from '../Collection/CollectionRoute'; import EditorRoute from '../Editor/EditorRoute'; import MediaLibrary from '../MediaLibrary/MediaLibrary'; @@ -24,7 +25,7 @@ import Loader from '../UI/Loader'; import ScrollTop from '../UI/ScrollTop'; import NotFoundPage from './NotFoundPage'; -import type { Collections, Credentials, TranslatedProps } from '@staticcms/core/interface'; +import type { Credentials, TranslatedProps } from '@staticcms/core/interface'; import type { RootState } from '@staticcms/core/store'; import type { ComponentType } from 'react'; import type { ConnectedProps } from 'react-redux'; @@ -61,19 +62,6 @@ const ErrorCodeBlock = styled('pre')` line-height: 1.5; `; -function getDefaultPath(collections: Collections) { - const options = Object.values(collections).filter( - collection => - collection.hide !== true && (!('files' in collection) || (collection.files?.length ?? 0) > 1), - ); - - if (options.length > 0) { - return `/collections/${options[0].name}`; - } else { - throw new Error('Could not find a non hidden collection'); - } -} - function CollectionSearchRedirect() { const { name } = useParams(); return ; diff --git a/packages/core/src/components/Collection/CollectionRoute.tsx b/packages/core/src/components/Collection/CollectionRoute.tsx index 5a214a72..8ec42c79 100644 --- a/packages/core/src/components/Collection/CollectionRoute.tsx +++ b/packages/core/src/components/Collection/CollectionRoute.tsx @@ -1,20 +1,12 @@ import React, { useMemo } from 'react'; import { Navigate, useParams } from 'react-router-dom'; +import { getDefaultPath } from '../../lib/util/collection.util'; import MainView from '../App/MainView'; import Collection from './Collection'; import type { Collections } from '@staticcms/core/interface'; -function getDefaultPath(collections: Collections) { - const first = Object.values(collections).filter(collection => collection.hide !== true)[0]; - if (first) { - return `/collections/${first.name}`; - } else { - throw new Error('Could not find a non hidden collection'); - } -} - interface CollectionRouteProps { isSearchResults?: boolean; isSingleSearchResult?: boolean; diff --git a/packages/core/src/components/Editor/EditorRoute.tsx b/packages/core/src/components/Editor/EditorRoute.tsx index 417aee91..9afccb40 100644 --- a/packages/core/src/components/Editor/EditorRoute.tsx +++ b/packages/core/src/components/Editor/EditorRoute.tsx @@ -2,18 +2,10 @@ import React, { useMemo } from 'react'; import { Navigate, useParams } from 'react-router-dom'; import Editor from './Editor'; +import { getDefaultPath } from '../../lib/util/collection.util'; import type { Collections } from '@staticcms/core/interface'; -function getDefaultPath(collections: Collections) { - const first = Object.values(collections).filter(collection => collection.hide !== true)[0]; - if (first) { - return `/collections/${first.name}`; - } else { - throw new Error('Could not find a non hidden collection'); - } -} - interface EditorRouteProps { newRecord?: boolean; collections: Collections; diff --git a/packages/core/src/lib/util/collection.util.ts b/packages/core/src/lib/util/collection.util.ts index ff4b0b48..36004ab4 100644 --- a/packages/core/src/lib/util/collection.util.ts +++ b/packages/core/src/lib/util/collection.util.ts @@ -17,6 +17,7 @@ import { selectMediaFolder } from './media.util'; import type { Backend } from '@staticcms/core/backend'; import type { Collection, + Collections, Config, Entry, Field, @@ -412,3 +413,20 @@ export function useInferredFields(collection: Collection) { return iFields; }, [collection]); } + +export function getDefaultPath(collections: Collections) { + if (Object.keys(collections).length === 0) { + throw new Error('No collections found'); + } + + let options = Object.values(collections).filter( + collection => + collection.hide !== true && (!('files' in collection) || (collection.files?.length ?? 0) > 1), + ); + + if (options.length === 0) { + options = Object.values(collections); + } + + return `/collections/${options[0].name}`; +}