mirror of
https://github.com/streamyfin/streamyfin.git
synced 2026-02-21 19:42:27 +00:00
Add server switching functionality to settings
Co-authored-by: retardgerman <78982850+retardgerman@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import { MediaToggles } from "@/components/settings/MediaToggles";
|
||||
import { OtherSettings } from "@/components/settings/OtherSettings";
|
||||
import { PluginSettings } from "@/components/settings/PluginSettings";
|
||||
import { QuickConnect } from "@/components/settings/QuickConnect";
|
||||
import { ServerSwitcher } from "@/components/settings/ServerSwitcher";
|
||||
import { StorageSettings } from "@/components/settings/StorageSettings";
|
||||
import { SubtitleToggles } from "@/components/settings/SubtitleToggles";
|
||||
import { UserInfo } from "@/components/settings/UserInfo";
|
||||
@@ -64,6 +65,8 @@ export default function settings() {
|
||||
<View className='p-4 flex flex-col gap-y-4'>
|
||||
<UserInfo />
|
||||
|
||||
<ServerSwitcher className='mb-4' />
|
||||
|
||||
<QuickConnect className='mb-4' />
|
||||
|
||||
<MediaProvider>
|
||||
|
||||
57
components/settings/ServerSwitcher.tsx
Normal file
57
components/settings/ServerSwitcher.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
import type React from "react";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { View, type ViewProps } from "react-native";
|
||||
import { useMMKVString } from "react-native-mmkv";
|
||||
import { useJellyfin } from "@/providers/JellyfinProvider";
|
||||
import { ListGroup } from "../list/ListGroup";
|
||||
import { ListItem } from "../list/ListItem";
|
||||
|
||||
interface Server {
|
||||
address: string;
|
||||
}
|
||||
|
||||
interface Props extends ViewProps {}
|
||||
|
||||
export const ServerSwitcher: React.FC<Props> = ({ ...props }) => {
|
||||
const [_previousServers] = useMMKVString("previousServers");
|
||||
const { switchServer } = useJellyfin();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const previousServers = useMemo(() => {
|
||||
return JSON.parse(_previousServers || "[]") as Server[];
|
||||
}, [_previousServers]);
|
||||
|
||||
const handleServerSwitch = async (server: Server) => {
|
||||
try {
|
||||
await switchServer(server);
|
||||
} catch (error) {
|
||||
console.error("Failed to switch server:", error);
|
||||
}
|
||||
};
|
||||
|
||||
if (!previousServers.length) {
|
||||
return (
|
||||
<View {...props}>
|
||||
<ListGroup title={t("server.quick_switch")}>
|
||||
<ListItem title={t("server.no_previous_servers")} disabled />
|
||||
</ListGroup>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View {...props}>
|
||||
<ListGroup title={t("server.quick_switch")}>
|
||||
{previousServers.map((server) => (
|
||||
<ListItem
|
||||
key={server.address}
|
||||
onPress={() => handleServerSwitch(server)}
|
||||
title={server.address}
|
||||
showArrow
|
||||
/>
|
||||
))}
|
||||
</ListGroup>
|
||||
</View>
|
||||
);
|
||||
};
|
||||
@@ -40,6 +40,7 @@ interface JellyfinContextValue {
|
||||
discoverServers: (url: string) => Promise<Server[]>;
|
||||
setServer: (server: Server) => Promise<void>;
|
||||
removeServer: () => void;
|
||||
switchServer: (server: Server) => Promise<void>;
|
||||
login: (username: string, password: string) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
initiateQuickConnect: () => Promise<string | undefined>;
|
||||
@@ -297,6 +298,18 @@ export const JellyfinProvider: React.FC<{ children: ReactNode }> = ({
|
||||
},
|
||||
});
|
||||
|
||||
const switchServerMutation = useMutation({
|
||||
mutationFn: async (server: Server) => {
|
||||
// First logout the current user
|
||||
await logoutMutation.mutateAsync();
|
||||
// Then set the new server
|
||||
await setServerMutation.mutateAsync(server);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Failed to switch server:", error);
|
||||
},
|
||||
});
|
||||
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [initialLoaded, setInitialLoaded] = useState(false);
|
||||
|
||||
@@ -340,6 +353,7 @@ export const JellyfinProvider: React.FC<{ children: ReactNode }> = ({
|
||||
discoverServers,
|
||||
setServer: (server) => setServerMutation.mutateAsync(server),
|
||||
removeServer: () => removeServerMutation.mutateAsync(),
|
||||
switchServer: (server) => switchServerMutation.mutateAsync(server),
|
||||
login: (username, password) =>
|
||||
loginMutation.mutateAsync({ username, password }),
|
||||
logout: () => logoutMutation.mutateAsync(),
|
||||
|
||||
@@ -32,7 +32,10 @@
|
||||
"clear_button": "Clear",
|
||||
"search_for_local_servers": "Search for local servers",
|
||||
"searching": "Searching...",
|
||||
"servers": "Servers"
|
||||
"servers": "Servers",
|
||||
"quick_switch": "Quick Switch Servers",
|
||||
"switch_server": "Switch Server",
|
||||
"no_previous_servers": "No previous servers available"
|
||||
},
|
||||
"home": {
|
||||
"no_internet": "No Internet",
|
||||
|
||||
Reference in New Issue
Block a user