fix: default path when no folder collections exist (#452)

This commit is contained in:
Daniel Lautzenheiser 2023-01-28 17:37:47 -05:00 committed by GitHub
parent f462c33d59
commit 88665faf27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 32 deletions

View File

@ -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 <Navigate to={`/collections/${name}`} />;

View File

@ -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;

View File

@ -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;

View File

@ -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}`;
}