diff --git a/src/App.tsx b/src/App.tsx index d88e709c6e..9f5552554e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,58 +1,18 @@ -import { Api } from '@jellyfin/sdk'; -import { UserDto } from '@jellyfin/sdk/lib/generated-client/models/user-dto'; import { History } from '@remix-run/router'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { HistoryRouter } from './components/HistoryRouter'; import ServerConnections from './components/ServerConnections'; -import { ApiContext } from './hooks/useApi'; -import { UserContext } from './hooks/useUser'; +import { ApiProvider } from './hooks/useApi'; import AppRoutes from './routes/index'; -import events from './utils/events'; -import { toApi } from './utils/sdk'; const App = ({ history, connections }: { history: History, connections: typeof ServerConnections }) => { - const [ api, setApi ] = useState(toApi(connections.currentApiClient())); - const [ user, setUser ] = useState(); - - useEffect(() => { - connections.currentApiClient() - .getCurrentUser() - .then(newUser => setUser(newUser)) - .catch(err => { - console.warn('[App] Could not get current user', err); - }); - - const udpateApiUser = (_e: any, newUser: UserDto) => { - setUser(newUser); - - if (newUser.ServerId) { - setApi(toApi(connections.getApiClient(newUser.ServerId))); - } - }; - - const resetApiUser = () => { - setApi(undefined); - setUser(undefined); - }; - - events.on(connections, 'localusersignedin', udpateApiUser); - events.on(connections, 'localusersignedout', resetApiUser); - - return () => { - events.off(connections, 'localusersignedin', udpateApiUser); - events.off(connections, 'localusersignedout', resetApiUser); - }; - }, [ connections ]); - return ( - - - - - - - + + + + + ); }; diff --git a/src/components/search/SearchSuggestions.tsx b/src/components/search/SearchSuggestions.tsx index 887fa2481e..d16cdca8e6 100644 --- a/src/components/search/SearchSuggestions.tsx +++ b/src/components/search/SearchSuggestions.tsx @@ -7,7 +7,6 @@ import React, { FunctionComponent, useEffect, useState } from 'react'; import { appRouter } from '../appRouter'; import { useApi } from '../../hooks/useApi'; -import { useUser } from '../../hooks/useUser'; import globalize from '../../scripts/globalize'; import '../../elements/emby-button/emby-button'; @@ -30,8 +29,7 @@ type SearchSuggestionsProps = { const SearchSuggestions: FunctionComponent = ({ parentId }: SearchSuggestionsProps) => { const [ suggestions, setSuggestions ] = useState([]); - const api = useApi(); - const user = useUser(); + const { api, user } = useApi(); useEffect(() => { if (api && user?.Id) { @@ -49,7 +47,7 @@ const SearchSuggestions: FunctionComponent = ({ parentId }) .then(result => setSuggestions(result.data.Items || [])); } - }, [api, parentId, user?.Id]); + }, [ api, parentId, user ]); return (
(undefined); -export const useApi = () => useContext(ApiContext); diff --git a/src/hooks/useApi.tsx b/src/hooks/useApi.tsx new file mode 100644 index 0000000000..7952a9030d --- /dev/null +++ b/src/hooks/useApi.tsx @@ -0,0 +1,71 @@ +import type { Api } from '@jellyfin/sdk'; +import type { UserDto } from '@jellyfin/sdk/lib/generated-client'; +import { ApiClient } from 'jellyfin-apiclient'; +import React, { createContext, FC, useContext, useEffect, useState } from 'react'; + +import type ServerConnections from '../components/ServerConnections'; +import events from '../utils/events'; +import { toApi } from '../utils/jellyfin-apiclient/compat'; + +interface ApiProviderProps { + connections: typeof ServerConnections +} + +interface JellyfinApiContext { + __legacyApiClient__?: ApiClient + api?: Api + user?: UserDto +} + +export const ApiContext = createContext({}); +export const useApi = () => useContext(ApiContext); + +export const ApiProvider: FC = ({ connections, children }) => { + const [ legacyApiClient, setLegacyApiClient ] = useState(); + const [ api, setApi ] = useState(); + const [ user, setUser ] = useState(); + + useEffect(() => { + connections.currentApiClient() + .getCurrentUser() + .then(newUser => udpateApiUser(null, newUser)) + .catch(err => { + console.warn('[ApiProvider] Could not get current user', err); + }); + + const udpateApiUser = (_e: any, newUser: UserDto) => { + setUser(newUser); + + if (newUser.ServerId) { + setLegacyApiClient(connections.getApiClient(newUser.ServerId)); + } + }; + + const resetApiUser = () => { + setLegacyApiClient(undefined); + setUser(undefined); + }; + + events.on(connections, 'localusersignedin', udpateApiUser); + events.on(connections, 'localusersignedout', resetApiUser); + + return () => { + events.off(connections, 'localusersignedin', udpateApiUser); + events.off(connections, 'localusersignedout', resetApiUser); + }; + }, [ connections, setLegacyApiClient, setUser ]); + + useEffect(() => { + setApi(legacyApiClient ? toApi(legacyApiClient) : undefined); + }, [ legacyApiClient, setApi ]); + + return ( + + {children} + + ); +}; diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts deleted file mode 100644 index ad681f71d2..0000000000 --- a/src/hooks/useUser.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { UserDto } from '@jellyfin/sdk/lib/generated-client/models/user-dto'; -import { createContext, useContext } from 'react'; - -export const UserContext = createContext(undefined); -export const useUser = () => useContext(UserContext); diff --git a/src/utils/sdk.ts b/src/utils/jellyfin-apiclient/compat.ts similarity index 100% rename from src/utils/sdk.ts rename to src/utils/jellyfin-apiclient/compat.ts