oh maybe i just didn't commit all of this lol

This commit is contained in:
Violet Caulfield
2024-10-22 11:42:14 -05:00
parent 1cde4099f5
commit 144225d0cb
6 changed files with 36 additions and 23 deletions

View File

@@ -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
})

View File

@@ -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<Api> {
});
}
export function createPublicApi({ queryKey }: QueryFunctionContext): Promise<Api> {
return new Promise((resolve) => {
export function createPublicApi(): Promise<Api> {
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));
});
}

View File

@@ -27,7 +27,7 @@ export const fetchCredentials : () => Promise<Keychain.SharedWebCredentials | un
resolve(keychain as Keychain.SharedWebCredentials)
});
export const fetchServer : () => Promise<JellifyServer> = () => new Promise(async (resolve) => {
export const fetchServer : () => Promise<JellifyServer> = () => new Promise(async (resolve, reject) => {
console.log("Attempting to fetch server address from storage");
@@ -35,12 +35,12 @@ export const fetchServer : () => Promise<JellifyServer> = () => 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));
}

View File

@@ -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) => {

View File

@@ -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<string | undefined>('');
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) => {

View File

@@ -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<React.SetStateAction<string | undefined>>;
triggerAuth: boolean;
setTriggerAuth: React.Dispatch<React.SetStateAction<boolean>>;
publicApi: Api | undefined;
refetchPublicApi: (options?: RefetchOptions | undefined) => Promise<QueryObserverResult<Api, Error>>;
}
const JellyfinAuthenticationContextInitializer = () => {
@@ -43,6 +47,8 @@ const JellyfinAuthenticationContextInitializer = () => {
const [triggerAuth, setTriggerAuth] = useState<boolean>(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 }
</JellyfinAuthenticationContext.Provider>