fix: default path when no folder collections exist (#452)
This commit is contained in:
parent
f462c33d59
commit
88665faf27
@ -13,6 +13,7 @@ import { discardDraft as discardDraftAction } from '@staticcms/core/actions/entr
|
|||||||
import { currentBackend } from '@staticcms/core/backend';
|
import { currentBackend } from '@staticcms/core/backend';
|
||||||
import { colors, GlobalStyles } from '@staticcms/core/components/UI/styles';
|
import { colors, GlobalStyles } from '@staticcms/core/components/UI/styles';
|
||||||
import { history } from '@staticcms/core/routing/history';
|
import { history } from '@staticcms/core/routing/history';
|
||||||
|
import { getDefaultPath } from '../../lib/util/collection.util';
|
||||||
import CollectionRoute from '../Collection/CollectionRoute';
|
import CollectionRoute from '../Collection/CollectionRoute';
|
||||||
import EditorRoute from '../Editor/EditorRoute';
|
import EditorRoute from '../Editor/EditorRoute';
|
||||||
import MediaLibrary from '../MediaLibrary/MediaLibrary';
|
import MediaLibrary from '../MediaLibrary/MediaLibrary';
|
||||||
@ -24,7 +25,7 @@ import Loader from '../UI/Loader';
|
|||||||
import ScrollTop from '../UI/ScrollTop';
|
import ScrollTop from '../UI/ScrollTop';
|
||||||
import NotFoundPage from './NotFoundPage';
|
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 { RootState } from '@staticcms/core/store';
|
||||||
import type { ComponentType } from 'react';
|
import type { ComponentType } from 'react';
|
||||||
import type { ConnectedProps } from 'react-redux';
|
import type { ConnectedProps } from 'react-redux';
|
||||||
@ -61,19 +62,6 @@ const ErrorCodeBlock = styled('pre')`
|
|||||||
line-height: 1.5;
|
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() {
|
function CollectionSearchRedirect() {
|
||||||
const { name } = useParams();
|
const { name } = useParams();
|
||||||
return <Navigate to={`/collections/${name}`} />;
|
return <Navigate to={`/collections/${name}`} />;
|
||||||
|
@ -1,20 +1,12 @@
|
|||||||
import React, { useMemo } from 'react';
|
import React, { useMemo } from 'react';
|
||||||
import { Navigate, useParams } from 'react-router-dom';
|
import { Navigate, useParams } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { getDefaultPath } from '../../lib/util/collection.util';
|
||||||
import MainView from '../App/MainView';
|
import MainView from '../App/MainView';
|
||||||
import Collection from './Collection';
|
import Collection from './Collection';
|
||||||
|
|
||||||
import type { Collections } from '@staticcms/core/interface';
|
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 {
|
interface CollectionRouteProps {
|
||||||
isSearchResults?: boolean;
|
isSearchResults?: boolean;
|
||||||
isSingleSearchResult?: boolean;
|
isSingleSearchResult?: boolean;
|
||||||
|
@ -2,18 +2,10 @@ import React, { useMemo } from 'react';
|
|||||||
import { Navigate, useParams } from 'react-router-dom';
|
import { Navigate, useParams } from 'react-router-dom';
|
||||||
|
|
||||||
import Editor from './Editor';
|
import Editor from './Editor';
|
||||||
|
import { getDefaultPath } from '../../lib/util/collection.util';
|
||||||
|
|
||||||
import type { Collections } from '@staticcms/core/interface';
|
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 {
|
interface EditorRouteProps {
|
||||||
newRecord?: boolean;
|
newRecord?: boolean;
|
||||||
collections: Collections;
|
collections: Collections;
|
||||||
|
@ -17,6 +17,7 @@ import { selectMediaFolder } from './media.util';
|
|||||||
import type { Backend } from '@staticcms/core/backend';
|
import type { Backend } from '@staticcms/core/backend';
|
||||||
import type {
|
import type {
|
||||||
Collection,
|
Collection,
|
||||||
|
Collections,
|
||||||
Config,
|
Config,
|
||||||
Entry,
|
Entry,
|
||||||
Field,
|
Field,
|
||||||
@ -412,3 +413,20 @@ export function useInferredFields(collection: Collection) {
|
|||||||
return iFields;
|
return iFields;
|
||||||
}, [collection]);
|
}, [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}`;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user