Skip to content

Commit

Permalink
chore: select a collection by default
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle committed Mar 7, 2024
1 parent 243afcc commit 073cdaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-eggs-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root-cms': patch
---

chore: select a collection by default
32 changes: 31 additions & 1 deletion packages/root-cms/ui/pages/CollectionPage/CollectionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
IconFolder,
} from '@tabler/icons-preact';

import {useState} from 'preact/hooks';
import {useEffect, useState} from 'preact/hooks';
import {route} from 'preact-router';

import {DocActionsMenu} from '../../components/DocActionsMenu/DocActionsMenu.js';
Expand All @@ -29,6 +29,36 @@ interface CollectionPageProps {
export function CollectionPage(props: CollectionPageProps) {
const [query, setQuery] = useState('');

// If no collection is selected, select one by default.
useEffect(() => {
const projectId = window.__ROOT_CTX.rootConfig.projectId;
const collections = window.__ROOT_CTX.collections;
const localStorageKey = `root-cms::${projectId}::last_collection`;
if (props.collection) {
// Store the current collection in local storage.
window.localStorage.setItem(localStorageKey, props.collection);
} else {
// Route to the last visited collection.
const lastVisited = window.localStorage.getItem(localStorageKey);
if (lastVisited && collections[lastVisited]) {
route(`/cms/content/${lastVisited}`);
return;
}
// Default to a collection called "Pages" if it exists.
if (window.__ROOT_CTX.collections['Pages']) {
route('/cms/content/Pages');
return;
}
const collectionIds = Object.keys(window.__ROOT_CTX.collections) || [];
if (collectionIds.length > 0) {
const firstCollectionId = collectionIds[0];
route(`/cms/content/${firstCollectionId}`);
return;
}
console.warn('no collections');
}
}, [props.collection]);

const collectionIds = Object.keys(window.__ROOT_CTX.collections);
const matchedCollections = collectionIds
.filter((id) => {
Expand Down

0 comments on commit 073cdaa

Please sign in to comment.