From ac2f7a2cd21dcdb68c17272953a40d8a749f4f43 Mon Sep 17 00:00:00 2001 From: jake <77554505+brxken128@users.noreply.github.com> Date: Mon, 20 Feb 2023 06:10:53 +0000 Subject: [PATCH] [ENG-343] Navigate to `/` when library is switched (#574) navigate to `/` when library is switched --- packages/client/src/hooks/useCurrentLibrary.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/client/src/hooks/useCurrentLibrary.tsx b/packages/client/src/hooks/useCurrentLibrary.tsx index 065afa57d..907993bb0 100644 --- a/packages/client/src/hooks/useCurrentLibrary.tsx +++ b/packages/client/src/hooks/useCurrentLibrary.tsx @@ -1,4 +1,5 @@ import { PropsWithChildren, createContext, useCallback, useContext, useMemo } from 'react'; +import { useNavigate } from 'react-router'; import { subscribe, useSnapshot } from 'valtio'; import { useBridgeQuery } from '../rspc'; import { valtioPersist } from '../stores'; @@ -34,6 +35,8 @@ export function onLibraryChange(func: (newLibraryId: string | null) => void) { // this is a hook to get the current library loaded into the UI. It takes care of a bunch of invariants under the hood. export const useCurrentLibrary = () => { + const navigate = useNavigate(); + const currentLibraryUuid = useSnapshot(currentLibraryUuidStore).id; const ctx = useContext(CringeContext); if (ctx === undefined) @@ -66,9 +69,13 @@ export const useCurrentLibrary = () => { } }); - const switchLibrary = useCallback((libraryUuid: string) => { - currentLibraryUuidStore.id = libraryUuid; - }, []); + const switchLibrary = useCallback( + (libraryUuid: string) => { + currentLibraryUuidStore.id = libraryUuid; + navigate('/'); + }, + [navigate] + ); // memorize library to avoid re-running find function const library = useMemo(() => {