Skip to content

Commit

Permalink
fix: prevent LibraryLayout remount
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Jan 16, 2025
1 parent 7aaa8f7 commit 5e043df
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions src/library-authoring/LibraryLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { useCallback } from 'react';
import {
Route,
Routes,
useParams,
useLocation,
} from 'react-router-dom';

import { ROUTES } from './routes';
import LibraryAuthoringPage from './LibraryAuthoringPage';
import { LibraryProvider } from './common/context/LibraryContext';
import { SidebarProvider } from './common/context/SidebarContext';
import { CreateCollectionModal } from './create-collection';
import LibraryCollectionPage from './collections/LibraryCollectionPage';
import { ComponentPicker } from './component-picker';
import { ComponentEditorModal } from './components/ComponentEditorModal';

Expand All @@ -23,12 +17,8 @@ const LibraryLayout = () => {
throw new Error('Error: route is missing libraryId.');
}

const location = useLocation();
const context = useCallback((childPage) => (
return (
<LibraryProvider
/** We need to pass the pathname as key to the LibraryProvider to force a
* re-render when we navigate to a new path or page. */
key={location.pathname}
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 @@ -37,38 +27,11 @@ const LibraryLayout = () => {
componentPicker={ComponentPicker}
>
<SidebarProvider>
<>
{childPage}
<CreateCollectionModal />
<ComponentEditorModal />
</>
<LibraryAuthoringPage />
<CreateCollectionModal />
<ComponentEditorModal />
</SidebarProvider>
</LibraryProvider>
), [location.pathname]);

return (
<Routes>
<Route
path={ROUTES.COMPONENTS}
element={context(<LibraryAuthoringPage />)}
/>
<Route
path={ROUTES.COLLECTIONS}
element={context(<LibraryAuthoringPage />)}
/>
<Route
path={ROUTES.COMPONENT}
element={context(<LibraryAuthoringPage />)}
/>
<Route
path={ROUTES.COLLECTION}
element={context(<LibraryCollectionPage />)}
/>
<Route
path={ROUTES.HOME}
element={context(<LibraryAuthoringPage />)}
/>
</Routes>
);
};

Expand Down

0 comments on commit 5e043df

Please sign in to comment.