2022-10-28 01:09:59 -04:00
|
|
|
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 { HistoryRouter } from './components/HistoryRouter';
|
|
|
|
import ServerConnections from './components/ServerConnections';
|
|
|
|
import { ApiContext } from './hooks/useApi';
|
|
|
|
import { UserContext } from './hooks/useUser';
|
|
|
|
import AppRoutes from './routes/index';
|
|
|
|
import events from './utils/events';
|
|
|
|
import { toApi } from './utils/sdk';
|
|
|
|
|
2022-10-28 12:53:31 -04:00
|
|
|
const App = ({ history, connections }: { history: History, connections: typeof ServerConnections }) => {
|
2022-10-28 01:09:59 -04:00
|
|
|
const [ api, setApi ] = useState<Api | undefined>(toApi(connections.currentApiClient()));
|
|
|
|
const [ user, setUser ] = useState<UserDto | undefined>();
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<ApiContext.Provider value={api}>
|
|
|
|
<UserContext.Provider value={user}>
|
|
|
|
<HistoryRouter history={history}>
|
|
|
|
<AppRoutes />
|
|
|
|
</HistoryRouter>
|
|
|
|
</UserContext.Provider>
|
|
|
|
</ApiContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|