From 09bcee155c60b271694f503c1dbed2e4b1cab2d8 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Fri, 27 Oct 2023 17:07:32 +0530 Subject: [PATCH] Add change server option #202 --- src/App.jsx | 60 ++++--- src/components/appBar/appBar.jsx | 1 + .../homeSection/latestMediaSection.jsx | 6 +- src/components/menu/sidemenu.jsx | 1 + src/routes/login/index.jsx | 12 +- src/routes/setup/serverList/index.jsx | 169 +++++++++++++++++- .../setup/serverList/serverList.module.scss | 53 ++++++ src/utils/storage/servers.js | 2 + 8 files changed, 270 insertions(+), 34 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 73a36f3..fa87d5f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -19,8 +19,6 @@ import { relaunch } from "@tauri-apps/api/process"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; -import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; - // Theming import CssBaseline from "@mui/material/CssBaseline"; import { theme } from "./theme"; @@ -97,6 +95,7 @@ import PlaylistTitlePage from "./routes/playlist/index.jsx"; import { createApi, useApi } from "./utils/store/api.js"; import { useQuery } from "@tanstack/react-query"; import { CircularProgress } from "@mui/material"; +import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; const anim = { initial: { @@ -141,26 +140,28 @@ const LogicalRoute = () => { ); return res; }; - if ( - !defaultServer.isPending && - !server.isPending && - !userSaved.isPending - ) { - if (defaultServer.data) { - if (userSaved.data) { - authenticateUser(); - navigate("/home"); - // return ; + useEffect(() => { + if ( + !defaultServer.isPending && + !server.isPending && + !userSaved.isPending + ) { + if (defaultServer.data) { + if (userSaved.data) { + authenticateUser(); + navigate("/home"); + // return ; + } else { + createApi(server.data.address); + navigate("/login/index"); + // return ; + } } else { - createApi(server.data.address); - navigate("/login/index"); - // return ; + navigate("/setup/server"); + // return ; } - } else { - navigate("/setup/server"); - // return ; } - } + }, []); return (
{ enabled: Boolean(api), }); - if (usersList.isSuccess) { - if (usersList.data.length > 0) { - navigate("/login/users"); - // ; - } else { - navigate("/login/manual"); - // ; + useEffect(() => { + if (usersList.isSuccess) { + if (usersList.data.length > 0) { + navigate("/login/users"); + // ; + } else { + navigate("/login/manual"); + // ; + } } - } + }, []); return (
{ window.scrollTo(0, 0); - console.log(location); }, [location.key]); const [audioPlayerVisible] = useAudioPlayback((state) => [state.display]); @@ -543,7 +545,7 @@ function App() {
- + ); diff --git a/src/components/appBar/appBar.jsx b/src/components/appBar/appBar.jsx index d72b7c7..91a8b98 100644 --- a/src/components/appBar/appBar.jsx +++ b/src/components/appBar/appBar.jsx @@ -90,6 +90,7 @@ export const AppBar = () => { if ( location.pathname.includes("login") || location.pathname.includes("setup") || + location.pathname.includes("server") || location.pathname.includes("player") || location.pathname === "/" ) { diff --git a/src/components/layouts/homeSection/latestMediaSection.jsx b/src/components/layouts/homeSection/latestMediaSection.jsx index 33cfe53..3fd6637 100644 --- a/src/components/layouts/homeSection/latestMediaSection.jsx +++ b/src/components/layouts/homeSection/latestMediaSection.jsx @@ -1,5 +1,5 @@ /** @format */ - +import React from "react"; import { useQuery } from "@tanstack/react-query"; import { CardScroller } from "../../cardScroller/cardScroller"; import { Card } from "../../card/card"; @@ -47,7 +47,7 @@ export const LatestMediaSection = ({ latestMediaLib }) => { displayCards={8} title={"Latest " + latestMediaLib[1]} > - {data.data.map((item, index) => { + {data.data.map((item) => { return ( { ? `S${item.ParentIndexNumber}:E${item.IndexNumber} - ${item.Name}` : item.Type == BaseItemKind.Series ? `${item.ProductionYear} - ${ - !!item.EndDate + item.EndDate ? new Date( item.EndDate, ).toLocaleString( diff --git a/src/components/menu/sidemenu.jsx b/src/components/menu/sidemenu.jsx index 5910e13..6e2d6cf 100644 --- a/src/components/menu/sidemenu.jsx +++ b/src/components/menu/sidemenu.jsx @@ -94,6 +94,7 @@ export const SideMenu = () => { if ( location.pathname.includes("login") || location.pathname.includes("setup") || + location.pathname.includes("server") || location.pathname.includes("player") || location.pathname === "/" ) { diff --git a/src/routes/login/index.jsx b/src/routes/login/index.jsx index f4c30cd..a193cbd 100644 --- a/src/routes/login/index.jsx +++ b/src/routes/login/index.jsx @@ -514,10 +514,20 @@ export const UserLoginManual = () => { loading={loading} loadingPosition="end" size="large" - sx={{ width: "100%" }} + sx={{ width: "100%", mb: 1 }} > Login + diff --git a/src/routes/setup/serverList/index.jsx b/src/routes/setup/serverList/index.jsx index ddcb20e..652d4cb 100644 --- a/src/routes/setup/serverList/index.jsx +++ b/src/routes/setup/serverList/index.jsx @@ -1,6 +1,173 @@ /** @format */ +import React, { useState } from "react"; + +import Typography from "@mui/material/Typography"; +import Paper from "@mui/material/Paper"; +import IconButton from "@mui/material/IconButton"; +import Chip from "@mui/material/Chip"; +import Divider from "@mui/material/Divider"; +import { green, red } from "@mui/material/colors"; + +import "./serverList.module.scss"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import { + getAllServer, + getDefaultServer, + setDefaultServer, +} from "../../../utils/storage/servers"; +import { AppBarBackOnly } from "../../../components/appBar/backOnly"; +import { useNavigate } from "react-router-dom"; +import { useSnackbar } from "notistack"; const ServerList = () => { - return <>; + const navigate = useNavigate(); + const [serverState, setServerState] = useState(null); + const { enqueueSnackbar } = useSnackbar(); + const servers = useQuery({ + queryKey: ["servers-list"], + queryFn: async () => await getAllServer(), + }); + const defaultServer = useQuery({ + queryKey: ["default-server"], + queryFn: async () => await getDefaultServer(), + }); + const handleServerChange = useMutation({ + mutationFn: () => { + setDefaultServer(serverState); + }, + onSuccess: () => { + defaultServer.refetch(); + enqueueSnackbar("Successfully changed server!", { + variant: "success", + }); + navigate("/"); + }, + onError: () => { + enqueueSnackbar("Error changing the server", { + variant: "error", + }); + }, + }); + return ( +
+ +
+ + Servers + + navigate("/setup/server")} + > + add + +
+ + {servers.isSuccess && + servers.data.map((server, index) => ( +
+ + dns + +
+ + {server[1].systemInfo.ServerName} + {server[0] == + defaultServer.data && ( + + Current + + } + color="info" + sx={{ + ml: 2, + width: "5.4em", + }} + size="medium" + /> + )} + + + {server[1].address} + + + Version:{" "} + {server[1].systemInfo.Version} + +
+
+ { + setServerState(server[0]); + handleServerChange.mutate(); + }} + disabled={ + handleServerChange.isPending + } + > + + start + + + + + delete_forever + + +
+
+ ))} +
+
+ ); }; export default ServerList; diff --git a/src/routes/setup/serverList/serverList.module.scss b/src/routes/setup/serverList/serverList.module.scss index e69de29..f2198b5 100644 --- a/src/routes/setup/serverList/serverList.module.scss +++ b/src/routes/setup/serverList/serverList.module.scss @@ -0,0 +1,53 @@ +/** @format */ + +.server-list { + position: absolute; + width: 100vw; + height: 100vh; + overflow: hidden; + display: flex; + align-items: center; + justify-content: center; + flex-direction: column; + &-container { + display: flex; + flex-direction: column; + width: 40vw; + padding: 1.2em; + border-radius: 20px !important; + } + &-item { + display: grid; + grid-template-columns: 3.4em 1fr auto; + justify-items: start; + align-items: center; + gap: 0.8em; + &-icon { + font-size: 3.4em !important; + } + &-info { + display: flex; + flex-direction: column; + // width: 100%; + } + &-buttons { + display: flex; + flex-direction: row; + gap: 0.5em; + width: 100%; + overflow: hidden; + } + border-bottom: 1.2px solid rgb(255 255 255 / 0.1); + + $spacing: 0.75em; + + padding-bottom: $spacing; + margin-bottom: $spacing; + + &:last-child { + border: none; + margin: 0; + padding: 0; + } + } +} diff --git a/src/utils/storage/servers.js b/src/utils/storage/servers.js index b13539a..0d2c96f 100644 --- a/src/utils/storage/servers.js +++ b/src/utils/storage/servers.js @@ -37,6 +37,8 @@ export const getServer = async (serverId) => { */ export const getAllServer = async () => { let server = await servers.entries(); + // remove default server field from this result + server = server.filter((item) => item[0] != "defaultServer"); return server; };