LibreChat/client/src/routes/Root.tsx
Danny Avila 0bae503a0a
refactor: Speed up Config fetching and Setup Config Groundwork 👷🚧 (#1297)
* refactor: move endpoint services to own directory

* refactor: make endpointconfig handling more concise, separate logic, and cache result for subsequent serving

* refactor: ModelController gets same treatment as EndpointController, draft OverrideController

* wip: flesh out override controller more to return real value

* refactor: client/api changes in anticipation of override
2023-12-06 19:36:57 -05:00

71 lines
2.7 KiB
TypeScript

/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect, useState } from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { Outlet, useLocation } from 'react-router-dom';
import { useGetModelsQuery, useGetSearchEnabledQuery } from 'librechat-data-provider';
import type { ContextType } from '~/common';
import { Nav, MobileNav } from '~/components/Nav';
import { useAuthContext, useServerStream, useConversation } from '~/hooks';
import store from '~/store';
export default function Root() {
const location = useLocation();
const { newConversation } = useConversation();
const { isAuthenticated } = useAuthContext();
const [navVisible, setNavVisible] = useState(() => {
const savedNavVisible = localStorage.getItem('navVisible');
return savedNavVisible !== null ? JSON.parse(savedNavVisible) : false;
});
const submission = useRecoilValue(store.submission);
useServerStream(submission ?? null);
const modelsQueryEnabled = useRecoilValue(store.modelsQueryEnabled);
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
const setModelsConfig = useSetRecoilState(store.modelsConfig);
const searchEnabledQuery = useGetSearchEnabledQuery({ enabled: isAuthenticated });
const modelsQuery = useGetModelsQuery({ enabled: isAuthenticated && modelsQueryEnabled });
useEffect(() => {
localStorage.setItem('navVisible', JSON.stringify(navVisible));
}, [navVisible]);
useEffect(() => {
if (modelsQuery.data && location.state?.from?.pathname.includes('/chat')) {
setModelsConfig(modelsQuery.data);
// Note: passing modelsQuery.data prevents navigation
newConversation({}, undefined, modelsQuery.data);
} else if (modelsQuery.data) {
setModelsConfig(modelsQuery.data);
} else if (modelsQuery.isError) {
console.error('Failed to get models', modelsQuery.error);
}
}, [modelsQuery.data, modelsQuery.isError]);
useEffect(() => {
if (searchEnabledQuery.data) {
setIsSearchEnabled(searchEnabledQuery.data);
} else if (searchEnabledQuery.isError) {
console.error('Failed to get search enabled', searchEnabledQuery.error);
}
}, [searchEnabledQuery.data, searchEnabledQuery.isError]);
if (!isAuthenticated) {
return null;
}
return (
<>
<div className="flex h-screen">
<Nav navVisible={navVisible} setNavVisible={setNavVisible} />
<div className="relative z-0 flex h-full w-full overflow-hidden">
<div className="relative flex h-full max-w-full flex-1 flex-col overflow-hidden">
<MobileNav setNavVisible={setNavVisible} />
<Outlet context={{ navVisible, setNavVisible } satisfies ContextType} />
</div>
</div>
</div>
</>
);
}