[ENG-343] Navigate to / when library is switched (#574)

navigate to `/` when library is switched
This commit is contained in:
jake 2023-02-20 06:10:53 +00:00 committed by GitHub
parent 4cc582e08c
commit ac2f7a2cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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(() => {