mirror of
https://github.com/Jellify-Music/App.git
synced 2026-01-28 05:58:43 -06:00
using a cleaner top level provider
This commit is contained in:
@@ -50,13 +50,13 @@ export default function ServerAddress(): React.JSX.Element {
|
||||
startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted!
|
||||
}
|
||||
|
||||
setServer(server);
|
||||
Client.setPublicApiClient(server);
|
||||
setServer(server);
|
||||
},
|
||||
onError: async (error: Error) => {
|
||||
console.error("An error occurred connecting to the Jellyfin instance", error);
|
||||
setServer(undefined);
|
||||
Client.signOut();
|
||||
setServer(undefined);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ export default function ServerAuthentication(): React.JSX.Element {
|
||||
accessToken: (authResult.data.AccessToken as string)
|
||||
}
|
||||
|
||||
setUser(user)
|
||||
return Client.setUser(user)
|
||||
Client.setUser(user);
|
||||
return setUser(user);
|
||||
},
|
||||
onError: async (error: Error) => {
|
||||
console.error("An error occurred connecting to the Jellyfin instance", error);
|
||||
@@ -57,8 +57,8 @@ export default function ServerAuthentication(): React.JSX.Element {
|
||||
{ `Sign in to ${server!.name ?? "Jellyfin"}`}
|
||||
</H1>
|
||||
<Button onPress={() => {
|
||||
setServer(undefined);
|
||||
Client.switchServer()
|
||||
setServer(undefined);
|
||||
}}>
|
||||
Switch Server
|
||||
</Button>
|
||||
|
||||
@@ -7,10 +7,12 @@ import _ from "lodash";
|
||||
import { useMusicLibraries, usePlaylistLibrary } from "@/api/queries/libraries";
|
||||
import { SafeAreaView } from "react-native-safe-area-context";
|
||||
import Client from "@/api/client";
|
||||
import { useJellifyContext } from "@/components/provider";
|
||||
|
||||
export default function ServerLibrary(): React.JSX.Element {
|
||||
|
||||
const { setLibrary, setUser } = useAuthenticationContext();
|
||||
const { setLoggedIn } = useJellifyContext()
|
||||
const { setUser } = useAuthenticationContext();
|
||||
|
||||
const [libraryId, setLibraryId] = useState<string | undefined>(undefined);
|
||||
|
||||
@@ -54,13 +56,14 @@ export default function ServerLibrary(): React.JSX.Element {
|
||||
playlistLibraryId: playlistLibrary!.Id!,
|
||||
playlistLibraryPrimaryImageId: playlistLibrary!.ImageTags!.Primary,
|
||||
});
|
||||
setLoggedIn(true);
|
||||
}}>
|
||||
Let's Go!
|
||||
</Button>
|
||||
|
||||
<Button onPress={() => {
|
||||
setUser(undefined);
|
||||
Client.switchUser();
|
||||
setUser(undefined);
|
||||
}}>
|
||||
Switch User
|
||||
</Button>
|
||||
|
||||
@@ -10,12 +10,15 @@ import { PlayerProvider } from "../player/provider";
|
||||
import { useColorScheme } from "react-native";
|
||||
import { PortalProvider } from "tamagui";
|
||||
import Client from "@/api/client";
|
||||
import { JellifyProvider, useJellifyContext } from "./provider";
|
||||
|
||||
export default function Jellify(): React.JSX.Element {
|
||||
|
||||
return (
|
||||
<PortalProvider shouldAddRootHost>
|
||||
<App />
|
||||
<JellifyProvider>
|
||||
<App />
|
||||
</JellifyProvider>
|
||||
</PortalProvider>
|
||||
);
|
||||
}
|
||||
@@ -23,6 +26,8 @@ export default function Jellify(): React.JSX.Element {
|
||||
function App(): React.JSX.Element {
|
||||
|
||||
const isDarkMode = useColorScheme() === "dark";
|
||||
|
||||
const { loggedIn } = useJellifyContext();
|
||||
|
||||
useEffect(() => {
|
||||
console.debug("Client instance changed")
|
||||
@@ -33,7 +38,7 @@ function App(): React.JSX.Element {
|
||||
return (
|
||||
<NavigationContainer theme={isDarkMode ? JellifyDarkTheme : JellifyLightTheme}>
|
||||
<SafeAreaProvider>
|
||||
{ Client.user && Client.server ? (
|
||||
{ loggedIn ? (
|
||||
<PlayerProvider>
|
||||
<Navigation />
|
||||
</PlayerProvider>
|
||||
|
||||
49
components/provider.tsx
Normal file
49
components/provider.tsx
Normal file
@@ -0,0 +1,49 @@
|
||||
import Client from "@/api/client";
|
||||
import { isUndefined } from "lodash";
|
||||
import { createContext, ReactNode, useContext, useState } from "react";
|
||||
|
||||
interface JellifyContext {
|
||||
loggedIn: boolean;
|
||||
setLoggedIn: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
}
|
||||
|
||||
const JellifyContextInitializer = () => {
|
||||
|
||||
const [loggedIn, setLoggedIn] = useState<boolean>(
|
||||
!isUndefined(Client.api) &&
|
||||
!isUndefined(Client.user) &&
|
||||
!isUndefined(Client.server)
|
||||
);
|
||||
|
||||
return {
|
||||
loggedIn,
|
||||
setLoggedIn,
|
||||
}
|
||||
}
|
||||
|
||||
const JellifyContext = createContext<JellifyContext>({
|
||||
loggedIn: false,
|
||||
setLoggedIn: () => {}
|
||||
});
|
||||
|
||||
export const JellifyProvider: ({ children }: {
|
||||
children: ReactNode
|
||||
}) => React.JSX.Element = ({ children }: { children: ReactNode }) => {
|
||||
const {
|
||||
loggedIn,
|
||||
setLoggedIn
|
||||
} = JellifyContextInitializer();
|
||||
|
||||
return (
|
||||
<JellifyContext.Provider
|
||||
value={{
|
||||
loggedIn,
|
||||
setLoggedIn
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</JellifyContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useJellifyContext = () => useContext(JellifyContext);
|
||||
Reference in New Issue
Block a user