From faf3dc2a48f2db286a13cac860550d3caea4a62d Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Sat, 17 Feb 2024 23:40:22 +0530 Subject: [PATCH] feat(web): Allow managing servers from settings dialog --- CHANGELOG.md | 3 +- src/App.tsx | 2 +- src/components/appBar/appBar.tsx | 33 +- src/components/settings/index.tsx | 631 ++++++++++++++----- src/components/settings/settings.module.scss | 57 +- 5 files changed, 557 insertions(+), 169 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0301c65..8bd7c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ - # v0.0.5-dev > **Note** : This is an unstable release - - feat(web): Add new About page (#258) by @prayag17 \ No newline at end of file + - feat(web): Add new About page (#258) by @prayag17 + - feat(web): Allow managing servers from settings dialog by @prayag17 \ No newline at end of file diff --git a/src/App.tsx b/src/App.tsx index 51b805e..48c15bc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -505,7 +505,7 @@ function AppReady() { - + ); diff --git a/src/components/appBar/appBar.tsx b/src/components/appBar/appBar.tsx index cd2a4fe..1f0b0ca 100644 --- a/src/components/appBar/appBar.tsx +++ b/src/components/appBar/appBar.tsx @@ -35,6 +35,7 @@ import { useApi } from "../../utils/store/api"; import { getTypeIcon } from "../utils/iconsCollection"; +import { Divider } from "@mui/material"; import logo from "../../assets/icon.svg"; import { setSettingsDialogOpen, @@ -384,10 +385,23 @@ export const AppBar = () => { sx={{ mt: 2 }} disableScrollLock > + { + handleLogout(); + handleMenuClose(); + }} + > + +
logout
+
+ Logout +
+ { setSettingsDialogOpen(true); setSettingsTabValue(1); + handleMenuClose(); }} > @@ -399,6 +413,7 @@ export const AppBar = () => { onClick={() => { setSettingsDialogOpen(true); setSettingsTabValue(10); + handleMenuClose(); }} > @@ -406,24 +421,6 @@ export const AppBar = () => { About - - -
logout
-
- Logout -
- { - navigate("/servers/list"); - }} - > - -
- dns -
-
- Select Server -
diff --git a/src/components/settings/index.tsx b/src/components/settings/index.tsx index 5198849..6730488 100644 --- a/src/components/settings/index.tsx +++ b/src/components/settings/index.tsx @@ -1,12 +1,19 @@ import { + Button, Chip, Dialog, + DialogActions, + DialogContent, + DialogTitle, + Fab, FormControlLabel, + IconButton, Link, Skeleton, Switch, Tab, Tabs, + TextField, Typography, styled, withStyles, @@ -21,33 +28,36 @@ import useSettingsStore, { import logo from "../../assets/logoBlack.png"; import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; -import { useQuery } from "@tanstack/react-query"; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useApi } from "../../utils/store/api"; import { useCentralStore } from "../../utils/store/central"; import "./settings.module.scss"; import { LoadingButton } from "@mui/lab"; +import { red } from "@mui/material/colors"; import { relaunch } from "@tauri-apps/api/process"; import { checkUpdate, installUpdate } from "@tauri-apps/api/updater"; import { enqueueSnackbar, useSnackbar } from "notistack"; +import { useNavigate } from "react-router-dom"; +import { + delServer, + getDefaultServer, + setDefaultServer, + setServer, +} from "../../utils/storage/servers"; +import { delUser } from "../../utils/storage/user"; const motionConfig = { - forward: { + initial: { opacity: 0, - transform: "translateY(-20px)", }, visible: { opacity: 1, - transform: "translateY(0px)", - }, - backward: { - opacity: 0, - transform: "translateY(20px)", }, }; const Settings = () => { - const [api] = useApi((state) => [state.api]); + const [api, jellyfin] = useApi((state) => [state.api, state.jellyfin]); const systemInfo = useQuery({ queryKey: ["about", "systemInfo"], queryFn: async () => { @@ -57,6 +67,18 @@ const Settings = () => { enabled: Boolean(api), }); + const navigate = useNavigate(); + + const [applicationVersion, serversOnDiskFn] = useCentralStore((state) => [ + state.clientVersion, + state.allServersOnDisk, + ]); + + const serversOnDisk = useQuery({ + queryKey: ["settings", "serversOnDisk"], + queryFn: serversOnDiskFn, + }); + const updateInfo = useQuery({ queryKey: ["about", "updater"], queryFn: async () => { @@ -65,6 +87,11 @@ const Settings = () => { }, }); + const defaultServer = useQuery({ + queryKey: ["settings", "default-server"], + queryFn: async () => await getDefaultServer(), + }); + const { enqueueSnackbar } = useSnackbar(); const [open, tabValue] = useSettingsStore((state) => [ @@ -72,12 +99,80 @@ const Settings = () => { state.tabValue, ]); - const [applicationVersion] = useCentralStore((state) => [ - state.clientVersion, - ]); + const queryClient = useQueryClient(); const [updating, setUpdating] = useState(false); const [animDirection, setAnimDirection] = useState("forward"); + const [serverState, setServerState] = useState(""); + const [ask, setAsk] = useState(false); + const [action, setAction] = useState(null); + const [addServerDialog, setAddServerDialog] = useState(false); + const [serverIp, setServerIp] = useState(""); + + const handleServerChange = useMutation({ + mutationFn: async () => { + await delUser(); + localStorage.clear(); + await setDefaultServer(serverState); + }, + onSuccess: async () => { + await relaunch(); + }, + onError: (error) => { + console.error(error); + enqueueSnackbar("Error changing the server", { + variant: "error", + }); + }, + }); + + const handleDelete = async (serverId: string) => { + await delServer(serverId); + + if (serverId === defaultServer.data) { + await delUser(); + await serversOnDisk.refetch(); + + if (serversOnDisk.data?.length > 0) { + setDefaultServer(serversOnDisk.data[0].id); + } + } + + setSettingsDialogOpen(false); + await relaunch(); + }; + + const checkServer = useMutation({ + mutationFn: async () => { + const servers = + await jellyfin.discovery.getRecommendedServerCandidates(serverIp); + const bestServer = jellyfin.discovery.findBestServer(servers); + return bestServer; + }, + onSuccess: (bestServer) => { + if (bestServer) { + setServer(bestServer.systemInfo.Id, bestServer); + setAddServerDialog(false); + serversOnDisk.refetch(); + enqueueSnackbar("Client added successfully", { + variant: "success", + }); + } + }, + onError: (err) => { + console.error(err); + enqueueSnackbar(`${err}`, { variant: "error" }); + enqueueSnackbar("Something went wrong", { variant: "error" }); + }, + onSettled: (bestServer) => { + console.log(bestServer); + if (!bestServer) { + enqueueSnackbar("Provided server address is not a Jellyfin server.", { + variant: "error", + }); + } + }, + }); return ( { orientation="vertical" value={tabValue} onChange={(e, newValue) => { + console.log(`NewValue: ${newValue}`); + console.log(`TabValue: ${tabValue}`); if (newValue > tabValue) { - setAnimDirection("backward"); - } else { + console.log("forward"); setAnimDirection("forward"); + } else { + console.log("backward"); + setAnimDirection("backward"); } setSettingsTabValue(newValue); }} @@ -115,6 +214,17 @@ const Settings = () => { height: "48px", }} /> + dns} + iconPosition="start" + value={2} + label="Servers" + className="settings-tab" + sx={{ + minHeight: "48px", + height: "48px", + }} + /> info} iconPosition="start" @@ -124,152 +234,22 @@ const Settings = () => { sx={{ minHeight: "48px", height: "48px", + marginTop: "auto", }} /> - {/* About */} - {tabValue === 10 && ( -
- JellyPlayer -
-
-
- Client Version: - - {updateInfo.data?.shouldUpdate - ? "update" - : "update_disabled"} - - } - label={ - - {applicationVersion} - - } - color={ - updateInfo.data?.shouldUpdate ? "error" : "success" - } - size="medium" - style={{ - width: "fit-content !important", - }} - /> -
-
- Update Available: - - {updateInfo.isFetching - ? "Checking for new updates..." - : updateInfo.data?.shouldUpdate - ? updateInfo.data.manifest?.version - : "No update found."} - -
- { - if (updateInfo.data?.shouldUpdate) { - setUpdating(true); - await installUpdate(); - enqueueSnackbar( - "Update has been installed! You need to relaunch JellyPlayer.", - { - variant: "success", - }, - ); - await relaunch(); - } - }} - > - {updateInfo.isFetching - ? "Checking for Update..." - : updateInfo.data?.shouldUpdate - ? "Update" - : "No Update Found"} - -
- {systemInfo.isSuccess ? ( -
-
- Server: - {systemInfo.data?.ServerName} -
-
- Jellyfin Version: - {systemInfo.data?.Version} -
-
- Operating System: - - {systemInfo.data?.OperatingSystemDisplayName} - -
-
- Server Architecture: - - {systemInfo.data?.SystemArchitecture} - -
-
- ) : ( - - )} -
-
- - Links: - -
- - https://github.com/prayag17/JellyPlayer - - https://jellyfin.org -
-
-
- )} - {/* General */} {tabValue === 1 && (
@@ -515,8 +495,363 @@ const Settings = () => { />
)} + + {/* Server */} + {tabValue === 2 && ( +
+ {serversOnDisk.isSuccess && + serversOnDisk.data.map((server) => { + return ( +
+ + dns + +
+ + {server.systemInfo?.ServerName} + {systemInfo.data?.Id === server.id && ( + + Current + + } + color="info" + sx={{ + ml: 2, + width: "5.4em", + }} + size="medium" + /> + )} + + + {server.address} + + + Version:{" "} + + {server.systemInfo?.Version} + + +
+
+ { + setAsk(true); + setAction("change-server"); + setServerState(server.id); + }} + disabled={handleServerChange.isPending} + > +
start
+
+ { + setAsk(true); + setAction("delete-server"); + setServerState(server.id); + }} + > +
+ delete_forever +
+
+
+
+ ); + })} + + setAddServerDialog(true)} + style={{ + position: "absolute", + bottom: "20px", + right: "20px", + }} + > + + add_circle + + Add server + +
+ )} + + {/* About */} + {tabValue === 10 && ( +
+ JellyPlayer +
+
+
+ Client Version: + + {updateInfo.data?.shouldUpdate + ? "update" + : "update_disabled"} + + } + label={ + + {applicationVersion} + + } + color={ + updateInfo.data?.shouldUpdate ? "error" : "success" + } + size="medium" + style={{ + width: "fit-content !important", + }} + /> +
+
+ Update Available: + + {updateInfo.isFetching + ? "Checking for new updates..." + : updateInfo.data?.shouldUpdate + ? updateInfo.data.manifest?.version + : "No update found."} + +
+ { + if (updateInfo.data?.shouldUpdate) { + setUpdating(true); + await installUpdate(); + enqueueSnackbar( + "Update has been installed! You need to relaunch JellyPlayer.", + { + variant: "success", + }, + ); + await relaunch(); + } + }} + > + {updateInfo.isFetching + ? "Checking for Update..." + : updateInfo.data?.shouldUpdate + ? "Update" + : "No Update Found"} + +
+ {systemInfo.isSuccess ? ( +
+
+ Server: + {systemInfo.data?.ServerName} +
+
+ Jellyfin Version: + {systemInfo.data?.Version} +
+
+ Operating System: + + {systemInfo.data?.OperatingSystemDisplayName} + +
+
+ Server Architecture: + + {systemInfo.data?.SystemArchitecture} + +
+
+ ) : ( + + )} +
+
+ + Links: + +
+ + https://github.com/prayag17/JellyPlayer + + + https://jellyfin.org + +
+
+
+ )}
+ {/* Show dialog before deleting or changing server */} + + Are you sure? + + JellyPlayer will relaunch. + + + + + + + + {/* Add Server */} + + Add Server + + setServerIp(e.target.value)} + /> + + + + + add_circle + + } + variant="contained" + loading={checkServer.isPending} + loadingPosition="start" + onClick={checkServer.mutate} + color="success" + > + Add + + +
); }; diff --git a/src/components/settings/settings.module.scss b/src/components/settings/settings.module.scss index d8547ba..8ace15c 100644 --- a/src/components/settings/settings.module.scss +++ b/src/components/settings/settings.module.scss @@ -4,8 +4,11 @@ height: 60vh; overflow: hidden !important; + .MuiTabs-flexContainerVertical{ + height: 100%; + } &-container{ - margin: 14px 0em 14px 14px; + margin: 14px 0 14px 14px; } &-tab{ display: grid !important; @@ -21,6 +24,7 @@ &-scroll{ overflow-y: auto; mask-image: linear-gradient(to top, transparent, black 15px calc(100% - 15px), transparent); + scrollbar-gutter:stable ; // Magic scrollbar &::-webkit-scrollbar { &-thumb { @@ -75,7 +79,58 @@ width: 100%; padding: 0 !important; margin: 0 0 0.4em 0!important; + } + //serverList + &-server{ + &-container{ + display: flex; + flex-direction: column; + gap: 1em; + align-items: center; + padding-bottom: 4.6em; + } + + display: grid; + grid-template-columns: 3.4em 1fr auto; + justify-items: start; + align-items: center; + gap: 0.8em; + background: rgb(0 0 0 / 0.3); + padding: 1em; + width: 100%; + border-radius: $border-radius-default; + + &-icon { + font-size: 3.4em !important; + font-variation-settings: "FILL" 1, "wght" 500, "GRAD" 10, "opsz" 40; + + } + &-info { + display: flex; + flex-direction: column; + // width: 100%; + &-version { + display: flex; + align-items: center; + gap:0.4em; + } + } + &-buttons { + display: flex; + flex-direction: row; + gap: 0.5em; + // width: 100%; + overflow: hidden; + } + + // Dialog + &-add { + display: flex; + flex-direction: column; + align-items: center; + gap: 1em; + } } }