Skip to content

Commit

Permalink
fix: remount LibraryLayout on collectionId change
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Jan 17, 2025
1 parent 244cfc6 commit 693da9c
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/library-authoring/LibraryLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { useCallback } from 'react';
import {
Route,
Routes,
useMatch,
useParams,
type PathMatch,
} from 'react-router-dom';

import { ROUTES } from './routes';
import { BASE_ROUTE, ROUTES } from './routes';
import LibraryAuthoringPage from './LibraryAuthoringPage';
import { LibraryProvider } from './common/context/LibraryContext';
import { SidebarProvider } from './common/context/SidebarContext';
Expand All @@ -22,8 +24,15 @@ const LibraryLayout = () => {
throw new Error('Error: route is missing libraryId.');
}

// The top-level route is `${BASE_ROUTE}/*`, so match will always be non-null.
const match = useMatch(`${BASE_ROUTE}${ROUTES.COLLECTION}`) as PathMatch<'libraryId' | 'collectionId'> | null;
const collectionId = match?.params.collectionId;

const context = useCallback((childPage) => (
<LibraryProvider
/** We need to pass the collectionId as key to the LibraryProvider to force a re-render
* when we navigate to a collection page. */
key={collectionId}
libraryId={libraryId}
/** The component picker modal to use. We need to pass it as a reference instead of
* directly importing it to avoid the import cycle:
Expand All @@ -39,7 +48,7 @@ const LibraryLayout = () => {
</>
</SidebarProvider>
</LibraryProvider>
), []);
), [collectionId]);

return (
<Routes>
Expand Down

0 comments on commit 693da9c

Please sign in to comment.