mirror of
https://github.com/Jellify-Music/App.git
synced 2026-01-07 11:30:30 -06:00
so I do need it this way
This commit is contained in:
@@ -1,8 +1,23 @@
|
||||
import { Jellyfin } from "@jellyfin/sdk/lib/jellyfin";
|
||||
import { fetchServer } from "../../queries/functions/storage";
|
||||
import { JellyfinCredentials } from "../../types/jellyfin-credentials";
|
||||
import * as Keychain from "react-native-keychain"
|
||||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
|
||||
import { client } from "../../queries";
|
||||
|
||||
export const serverMutation = async (serverUrl: string) => {
|
||||
|
||||
console.log("Mutating server URL");
|
||||
|
||||
if (!!!serverUrl)
|
||||
throw Error("Server URL is empty")
|
||||
|
||||
let jellyfin = new Jellyfin(client);
|
||||
let api = jellyfin.createApi(serverUrl);
|
||||
|
||||
console.log(`Created API client for ${api.basePath}`)
|
||||
return await getSystemApi(api).getPublicSystemInfo()
|
||||
}
|
||||
|
||||
export const mutateServerCredentials = async (credentials: JellyfinCredentials) => {
|
||||
return Keychain.setInternetCredentials((await fetchServer()).url, credentials.username, credentials.accessToken!);
|
||||
|
||||
@@ -1,62 +1,12 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { MutationKeys } from "../../enums/mutation-keys";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { AsyncStorageKeys } from "../../enums/async-storage-keys";
|
||||
|
||||
import { Jellyfin } from "@jellyfin/sdk";
|
||||
import { client } from "../queries";
|
||||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"
|
||||
import { JellyfinCredentials } from "../types/jellyfin-credentials";
|
||||
import { mutateServerCredentials } from "./functions/storage";
|
||||
import { JellifyServer } from "../../types/JellifyServer";
|
||||
|
||||
export const clearServer = useMutation({
|
||||
mutationFn: async () => {
|
||||
await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, "");
|
||||
}
|
||||
})
|
||||
|
||||
export const serverUrlMutation = useMutation({
|
||||
mutationFn: async (serverUrl: string) => {
|
||||
|
||||
console.log("Mutating server URL");
|
||||
|
||||
if (!!!serverUrl)
|
||||
throw Error("Server URL is empty")
|
||||
|
||||
let jellyfin = new Jellyfin(client);
|
||||
let api = jellyfin.createApi(serverUrl);
|
||||
|
||||
console.log(`Created API client for ${api.basePath}`)
|
||||
return await getSystemApi(api).getPublicSystemInfo()
|
||||
},
|
||||
onSuccess: (publicSystemInfoResponse, serverUrl, context) => {
|
||||
if (!!!publicSystemInfoResponse.data.Version)
|
||||
throw new Error("Jellyfin instance did not respond");
|
||||
|
||||
console.debug("REMOVE THIS::onSuccess variable", serverUrl);
|
||||
console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`);
|
||||
|
||||
// TODO: Store these along side address
|
||||
// TODO: Rename url to address
|
||||
|
||||
let jellifyServer: JellifyServer = {
|
||||
url: serverUrl,
|
||||
name: publicSystemInfoResponse.data.ServerName!,
|
||||
version: publicSystemInfoResponse.data.Version!,
|
||||
startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted!
|
||||
}
|
||||
return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, JSON.stringify(jellifyServer));
|
||||
},
|
||||
onError: async (error: Error) => {
|
||||
console.error("An error occurred connecting to the Jellyfin instance", error);
|
||||
await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, "");
|
||||
}
|
||||
});
|
||||
|
||||
export const credentials = useMutation({
|
||||
mutationKey: [MutationKeys.Credentials],
|
||||
mutationFn: async (credentials: JellyfinCredentials) => {
|
||||
mutateServerCredentials(credentials)
|
||||
return mutateServerCredentials(credentials)
|
||||
},
|
||||
});
|
||||
@@ -2,6 +2,14 @@ import React, { useState } from "react";
|
||||
import _ from "lodash";
|
||||
import { serverUrlMutation } from "../../../api/mutators/storage";
|
||||
import { Button, SafeAreaView, TextInput, useColorScheme, View } from "react-native";
|
||||
import { Jellyfin } from "@jellyfin/sdk";
|
||||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { client } from "../../../api/queries";
|
||||
import { AsyncStorageKeys } from "../../../enums/async-storage-keys";
|
||||
import { JellifyServer } from "../../../types/JellifyServer";
|
||||
import { serverMutation } from "../../../api/mutators/functions/storage";
|
||||
|
||||
export default function ServerAddress(): React.JSX.Element {
|
||||
|
||||
@@ -9,6 +17,32 @@ export default function ServerAddress(): React.JSX.Element {
|
||||
|
||||
const isDarkMode = useColorScheme() === 'dark';
|
||||
|
||||
const useServerMutation = useMutation({
|
||||
mutationFn: serverMutation,
|
||||
onSuccess: (publicSystemInfoResponse, serverUrl, context) => {
|
||||
if (!!!publicSystemInfoResponse.data.Version)
|
||||
throw new Error("Jellyfin instance did not respond");
|
||||
|
||||
console.debug("REMOVE THIS::onSuccess variable", serverUrl);
|
||||
console.log(`Connected to Jellyfin ${publicSystemInfoResponse.data.Version!}`);
|
||||
|
||||
// TODO: Store these along side address
|
||||
// TODO: Rename url to address
|
||||
|
||||
let jellifyServer: JellifyServer = {
|
||||
url: serverUrl,
|
||||
name: publicSystemInfoResponse.data.ServerName!,
|
||||
version: publicSystemInfoResponse.data.Version!,
|
||||
startUpComplete: publicSystemInfoResponse.data.StartupWizardCompleted!
|
||||
}
|
||||
return AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, JSON.stringify(jellifyServer));
|
||||
},
|
||||
onError: async (error: Error) => {
|
||||
console.error("An error occurred connecting to the Jellyfin instance", error);
|
||||
return await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, "");
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<View>
|
||||
<TextInput
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import React from "react";
|
||||
import { Button, TextInput, useColorScheme, View } from "react-native";
|
||||
import { clearServer } from "../../../api/mutators/storage";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AsyncStorageKeys } from "../../../enums/async-storage-keys";
|
||||
|
||||
|
||||
export default function ServerAuthentication(): React.JSX.Element {
|
||||
@@ -9,6 +12,12 @@ export default function ServerAuthentication(): React.JSX.Element {
|
||||
|
||||
const isDarkMode = useColorScheme() === 'dark';
|
||||
|
||||
const clearServer = useMutation({
|
||||
mutationFn: async () => {
|
||||
return await AsyncStorage.setItem(AsyncStorageKeys.ServerUrl, "");
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<View>
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user