From 144225d0cb16781baac7916a914c4e65adfdb417 Mon Sep 17 00:00:00 2001 From: Violet Caulfield Date: Tue, 22 Oct 2024 11:42:14 -0500 Subject: [PATCH] oh maybe i just didn't commit all of this lol --- api/queries.ts | 7 ++++--- api/queries/functions/api.ts | 13 +++++++------ api/queries/functions/storage.ts | 6 +++--- components/Login/helpers/server-address.tsx | 11 ++++------- .../Login/helpers/server-authentication.tsx | 6 +++--- components/Login/provider.tsx | 16 +++++++++++++++- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/api/queries.ts b/api/queries.ts index a90214be..7b339f07 100644 --- a/api/queries.ts +++ b/api/queries.ts @@ -2,12 +2,13 @@ import { useQuery } from "@tanstack/react-query"; import { QueryKeys } from "../enums/query-keys"; import { createApi, createPublicApi } from "./queries/functions/api"; -export const usePublicApi = (serverUrl: string) => useQuery({ - queryKey: [QueryKeys.PublicApi, { serverUrl }], +export const usePublicApi = () => useQuery({ + queryKey: [QueryKeys.PublicApi], queryFn: createPublicApi }); export const useApi = () => useQuery({ queryKey: [QueryKeys.Api], - queryFn: createApi + queryFn: createApi, + gcTime: 1000 }) \ No newline at end of file diff --git a/api/queries/functions/api.ts b/api/queries/functions/api.ts index 2f8f9b8d..b691df6a 100644 --- a/api/queries/functions/api.ts +++ b/api/queries/functions/api.ts @@ -1,5 +1,5 @@ import { Api } from "@jellyfin/sdk"; -import { fetchCredentials } from "./storage"; +import { fetchCredentials, fetchServer } from "./storage"; import { client } from "../../client"; import _ from "lodash"; import { QueryFunctionContext, QueryKey } from "@tanstack/react-query"; @@ -22,12 +22,13 @@ export function createApi(): Promise { }); } -export function createPublicApi({ queryKey }: QueryFunctionContext): Promise { - return new Promise((resolve) => { +export function createPublicApi(): Promise { + return new Promise(async (resolve) => { - ///@ts-ignore - const [_key, { serverUrl } ] = queryKey; + console.log("Fetching server details from storage") + const server = await fetchServer() - resolve(client.createApi(serverUrl)); + console.log(`Found stored server ${server.name}`) + resolve(client.createApi(server.url)); }); } \ No newline at end of file diff --git a/api/queries/functions/storage.ts b/api/queries/functions/storage.ts index 7f77b677..f9244d7f 100644 --- a/api/queries/functions/storage.ts +++ b/api/queries/functions/storage.ts @@ -27,7 +27,7 @@ export const fetchCredentials : () => Promise Promise = () => new Promise(async (resolve) => { +export const fetchServer : () => Promise = () => new Promise(async (resolve, reject) => { console.log("Attempting to fetch server address from storage"); @@ -35,12 +35,12 @@ export const fetchServer : () => Promise = () => new Promise(asyn if (_.isEmpty(serverJson) || _.isNull(serverJson)) { console.warn("No stored server address exists"); - return Promise.reject(new Error("No stored server address exists")); + return reject(new Error("No stored server address exists")); } try { let server : JellifyServer = JSON.parse(serverJson) as JellifyServer; - resolve(server); + return resolve(server); } catch(error: any) { return Promise.reject(new Error(error)); } diff --git a/components/Login/helpers/server-address.tsx b/components/Login/helpers/server-address.tsx index 06842601..997e125b 100644 --- a/components/Login/helpers/server-address.tsx +++ b/components/Login/helpers/server-address.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React from "react"; import _ from "lodash"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { useMutation } from "@tanstack/react-query"; @@ -6,9 +6,8 @@ import { AsyncStorageKeys } from "../../../enums/async-storage-keys"; import { JellifyServer } from "../../../types/JellifyServer"; import { mutateServer, serverMutation } from "../../../api/mutators/functions/storage"; import { useApiClientContext } from "../../jellyfin-api-provider"; -import { useTheme, View, XStack } from "tamagui"; +import { View, XStack } from "tamagui"; import { SwitchWithLabel } from "../../helpers/switch-with-label"; -import { buildApiClient } from "../../../api/client"; import { useAuthenticationContext } from "../provider"; import { Heading } from "../../helpers/text"; import Input from "../../helpers/input"; @@ -16,9 +15,7 @@ import Button from "../../helpers/button"; export default function ServerAddress(): React.JSX.Element { - const { serverAddress, setServerAddress, setChangeServer, useHttps, setUseHttps, refetchServer } = useAuthenticationContext(); - - const { apiClient } = useApiClientContext(); + const { serverAddress, setServerAddress, setChangeServer, useHttps, setUseHttps, refetchPublicApi } = useAuthenticationContext(); const useServerMutation = useMutation({ mutationFn: serverMutation, @@ -40,7 +37,7 @@ export default function ServerAddress(): React.JSX.Element { } await mutateServer(jellifyServer); - await refetchServer(); + await refetchPublicApi(); setChangeServer(false); }, onError: async (error: Error) => { diff --git a/components/Login/helpers/server-authentication.tsx b/components/Login/helpers/server-authentication.tsx index 556db7c2..fd077fb0 100644 --- a/components/Login/helpers/server-authentication.tsx +++ b/components/Login/helpers/server-authentication.tsx @@ -12,14 +12,14 @@ import Input from "../../helpers/input"; import { mutateServer } from "../../../api/mutators/functions/storage"; export default function ServerAuthentication(): React.JSX.Element { - const { username, setUsername, setChangeUsername, setServerAddress, setChangeServer, storedServer } = useAuthenticationContext(); + const { username, setUsername, setChangeUsername, setChangeServer, storedServer, publicApi } = useAuthenticationContext(); const [password, setPassword] = React.useState(''); - const { apiClient, refetchApi } = useApiClientContext(); + const { refetchApi } = useApiClientContext(); const useApiMutation = useMutation({ mutationFn: async (credentials: JellyfinCredentials) => { - return await apiClient!.authenticateUserByName(credentials.username, credentials.password!); + return await publicApi!.authenticateUserByName(credentials.username, credentials.password!); }, onSuccess: async (authResult, credentials) => { diff --git a/components/Login/provider.tsx b/components/Login/provider.tsx index 49d593b5..3c06ae8a 100644 --- a/components/Login/provider.tsx +++ b/components/Login/provider.tsx @@ -5,6 +5,8 @@ import { SharedWebCredentials } from "react-native-keychain"; import { JellifyServer } from "../../types/JellifyServer"; import { QueryObserverResult, RefetchOptions } from "@tanstack/react-query"; import { mutateServer, mutateServerCredentials } from "../../api/mutators/functions/storage"; +import { usePublicApi } from "../../api/queries"; +import { Api } from "@jellyfin/sdk"; interface JellyfinAuthenticationContext { username: string | undefined; @@ -27,6 +29,8 @@ interface JellyfinAuthenticationContext { setLibraryId: React.Dispatch>; triggerAuth: boolean; setTriggerAuth: React.Dispatch>; + publicApi: Api | undefined; + refetchPublicApi: (options?: RefetchOptions | undefined) => Promise>; } const JellyfinAuthenticationContextInitializer = () => { @@ -43,6 +47,8 @@ const JellyfinAuthenticationContextInitializer = () => { const [triggerAuth, setTriggerAuth] = useState(true); + + const { data: publicApi, isPending: publicApiPending, refetch: refetchPublicApi } = usePublicApi(); // Fetch from storage on init to load non-sensitive fields from previous logins const { data: storedServer, isPending: serverPending, refetch: refetchServer } = useServer(); const { data: credentials, isPending: credentialsPending } : { data: SharedWebCredentials | undefined, isPending: boolean } = useCredentials(); @@ -94,7 +100,9 @@ const JellyfinAuthenticationContextInitializer = () => { libraryId, setLibraryId, triggerAuth, - setTriggerAuth + setTriggerAuth, + publicApi, + refetchPublicApi, }; } @@ -120,6 +128,8 @@ const JellyfinAuthenticationContext = setLibraryId: () => {}, triggerAuth: true, setTriggerAuth: () => {}, + publicApi: undefined, + refetchPublicApi: () => new Promise(() => {}) }); export const JellyfinAuthenticationProvider: ({ children }: { @@ -147,6 +157,8 @@ export const JellyfinAuthenticationProvider: ({ children }: { setLibraryId, triggerAuth, setTriggerAuth, + publicApi, + refetchPublicApi } = JellyfinAuthenticationContextInitializer(); return ( @@ -171,6 +183,8 @@ export const JellyfinAuthenticationProvider: ({ children }: { setLibraryId, triggerAuth, setTriggerAuth, + publicApi, + refetchPublicApi }}> { children }