refactor: improve server add process
This commit is contained in:
8 files changed
+279
-138
No files matched your search
@@ -204,12 +204,12 @@ function UserLoginManual() {
|
||||
</div>
|
||||
{server.isSuccess && server.data?.LoginDisclaimer && (
|
||||
<Paper
|
||||
elevation={5}
|
||||
elevation={0}
|
||||
style={{
|
||||
marginTop: "1em",
|
||||
padding: "1em",
|
||||
borderRadius: "15px",
|
||||
width: "32em",
|
||||
width: "28em",
|
||||
}}
|
||||
>
|
||||
<div
|
||||
|
||||
@@ -1036,7 +1036,13 @@ function VideoPlayer() {
|
||||
{playing ? "pause" : "play_arrow"}
|
||||
</span>
|
||||
</IconButton>
|
||||
<IconButton onClick={handleNextChapter}>
|
||||
<IconButton
|
||||
disabled={
|
||||
(item?.Chapters?.[item.Chapters?.length - 1]
|
||||
?.StartPositionTicks ?? 0) <= progress
|
||||
}
|
||||
onClick={handleNextChapter}
|
||||
>
|
||||
<span className="material-symbols-rounded fill">
|
||||
last_page
|
||||
</span>
|
||||
@@ -1114,7 +1120,10 @@ function VideoPlayer() {
|
||||
</span>
|
||||
</IconButton>
|
||||
<QueueButton />
|
||||
<IconButton onClick={handleShowChapterList}>
|
||||
<IconButton
|
||||
disabled={item?.Chapters?.length === 0}
|
||||
onClick={handleShowChapterList}
|
||||
>
|
||||
<span className="material-symbols-rounded">list</span>
|
||||
</IconButton>
|
||||
<Menu
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { type FormEvent, useState } from "react";
|
||||
|
||||
import { setDefaultServer, setServer } from "@/utils/storage/servers";
|
||||
|
||||
import LoadingButton from "@mui/lab/LoadingButton";
|
||||
import Container from "@mui/material/Container";
|
||||
import Grid from "@mui/material/Grid";
|
||||
import LinearProgress from "@mui/material/LinearProgress";
|
||||
import TextField from "@mui/material/TextField";
|
||||
import Typography from "@mui/material/Typography";
|
||||
|
||||
@@ -28,17 +26,18 @@ function ServerSetup() {
|
||||
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
|
||||
const navigate = useNavigate({ from: "/setup/server/add" });
|
||||
const navigate = useNavigate();
|
||||
|
||||
const checkServer = useMutation({
|
||||
mutationFn: async () => {
|
||||
mutationFn: async (e: FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
const servers =
|
||||
await jellyfin.discovery.getRecommendedServerCandidates(serverIp);
|
||||
const bestServer = jellyfin.discovery.findBestServer(servers);
|
||||
return bestServer;
|
||||
},
|
||||
onSuccess: (bestServer) => {
|
||||
if (bestServer) {
|
||||
if (bestServer?.systemInfo?.Id) {
|
||||
createApi(bestServer.address, undefined);
|
||||
|
||||
setDefaultServer(bestServer.systemInfo?.Id);
|
||||
@@ -48,12 +47,12 @@ function ServerSetup() {
|
||||
variant: "success",
|
||||
});
|
||||
|
||||
navigate({ to: "/login" });
|
||||
navigate({ to: "/login", replace: true });
|
||||
}
|
||||
},
|
||||
onError: (err) => {
|
||||
console.error(err);
|
||||
enqueueSnackbar(`${err}`, { variant: "error" });
|
||||
enqueueSnackbar(`${JSON.stringify(err)}`, { variant: "error" });
|
||||
enqueueSnackbar("Something went wrong", { variant: "error" });
|
||||
},
|
||||
onSettled: (bestServer) => {
|
||||
@@ -67,87 +66,69 @@ function ServerSetup() {
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<LinearProgress
|
||||
<div
|
||||
className={"centered serverContainer flex flex-column flex-center"}
|
||||
style={{
|
||||
opacity: checkServer.isPending ? "0.5" : "1",
|
||||
transition: "opacity 350ms",
|
||||
width: "40vw",
|
||||
}}
|
||||
>
|
||||
<div style={{ marginBottom: "1em" }}>
|
||||
<Typography variant="h3">Add Server</Typography>
|
||||
</div>
|
||||
<form
|
||||
onSubmit={(e) => checkServer.mutate(e)}
|
||||
className="flex flex-column"
|
||||
style={{ gap: "1em", width: "100%" }}
|
||||
>
|
||||
<TextField
|
||||
className="textbox"
|
||||
label="Server Address:"
|
||||
variant="outlined"
|
||||
// inputRef={serverIp}
|
||||
onChange={(event) => {
|
||||
setServerIp(event.target.value);
|
||||
}}
|
||||
/>
|
||||
<LoadingButton
|
||||
variant="contained"
|
||||
sx={{ width: "100%" }}
|
||||
size="large"
|
||||
loading={checkServer.isPending}
|
||||
endIcon={
|
||||
<span className="material-symbols-rounded">chevron_right</span>
|
||||
}
|
||||
loadingPosition="end"
|
||||
type="submit"
|
||||
>
|
||||
Add Server
|
||||
</LoadingButton>
|
||||
</form>
|
||||
<Grid
|
||||
item
|
||||
xl={5}
|
||||
md={6}
|
||||
sx={{
|
||||
position: "fixed",
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
opacity: checkServer.isPending ? 1 : 0,
|
||||
transition: "opacity 350ms",
|
||||
}}
|
||||
/>
|
||||
<Container
|
||||
maxWidth="sm"
|
||||
className={"centered serverContainer"}
|
||||
style={{
|
||||
opacity: checkServer.isPending ? "0.5" : "1",
|
||||
transition: "opacity 350ms",
|
||||
width: "100%",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 1,
|
||||
opacity: 0.6,
|
||||
}}
|
||||
>
|
||||
<Grid
|
||||
container
|
||||
spacing={2}
|
||||
direction="column"
|
||||
justifyContent="center"
|
||||
alignItems="center"
|
||||
<span
|
||||
className="material-symbols-rounded"
|
||||
style={{
|
||||
color: yellow[700],
|
||||
}}
|
||||
>
|
||||
<Grid item xl={5} md={6} sx={{ marginBottom: "1em" }}>
|
||||
<Typography variant="h3">Add Server</Typography>
|
||||
</Grid>
|
||||
<Grid item xl={5} md={6} sx={{ width: "100%" }}>
|
||||
<TextField
|
||||
className="textbox"
|
||||
label="Server Address:"
|
||||
variant="outlined"
|
||||
// inputRef={serverIp}
|
||||
onChange={(event) => {
|
||||
setServerIp(event.target.value);
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xl={5} md={6} sx={{ width: "100%" }}>
|
||||
<LoadingButton
|
||||
variant="contained"
|
||||
sx={{ width: "100%" }}
|
||||
size="large"
|
||||
loading={checkServer.isPending}
|
||||
endIcon={
|
||||
<span className="material-symbols-rounded">chevron_right</span>
|
||||
}
|
||||
loadingPosition="end"
|
||||
onClick={checkServer.mutate}
|
||||
>
|
||||
Add Server
|
||||
</LoadingButton>
|
||||
</Grid>
|
||||
<Grid
|
||||
item
|
||||
xl={5}
|
||||
md={6}
|
||||
sx={{
|
||||
width: "100%",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 1,
|
||||
opacity: 0.6,
|
||||
}}
|
||||
>
|
||||
<span
|
||||
className="material-symbols-rounded"
|
||||
style={{
|
||||
color: yellow[700],
|
||||
}}
|
||||
>
|
||||
info
|
||||
</span>
|
||||
<Typography variant="subtitle1">
|
||||
Example: https://demo.jellyfin.org/stable
|
||||
</Typography>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Container>
|
||||
</>
|
||||
info
|
||||
</span>
|
||||
<Typography variant="subtitle1">
|
||||
Example: https://demo.jellyfin.org/stable
|
||||
</Typography>
|
||||
</Grid>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -8,17 +8,23 @@ import { red } from "@mui/material/colors";
|
||||
|
||||
import AppBarBackOnly from "@/components/appBar/backOnly";
|
||||
import {
|
||||
type ServerInfo,
|
||||
delServer,
|
||||
getAllServers,
|
||||
getDefaultServer,
|
||||
setDefaultServer,
|
||||
} from "@/utils/storage/servers";
|
||||
import { delUser } from "@/utils/storage/user";
|
||||
import { setBackdrop } from "@/utils/store/backdrop";
|
||||
import { useBackdropStore } from "@/utils/store/backdrop";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { createFileRoute, useNavigate } from "@tanstack/react-router";
|
||||
import {
|
||||
createFileRoute,
|
||||
useNavigate,
|
||||
useRouter,
|
||||
} from "@tanstack/react-router";
|
||||
import { useSnackbar } from "notistack";
|
||||
import "./serverList.scss";
|
||||
import AddServerDialog from "@/components/addServerDialog";
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
|
||||
export const Route = createFileRoute("/setup/server/list")({
|
||||
@@ -27,8 +33,8 @@ export const Route = createFileRoute("/setup/server/list")({
|
||||
|
||||
function ServerList() {
|
||||
const navigate = useNavigate();
|
||||
const [serverState, setServerState] = useState(null);
|
||||
const { enqueueSnackbar } = useSnackbar();
|
||||
const router = useRouter();
|
||||
|
||||
const createApi = useApiInContext((s) => s.createApi);
|
||||
|
||||
@@ -45,12 +51,12 @@ function ServerList() {
|
||||
});
|
||||
|
||||
const handleServerChange = useMutation({
|
||||
mutationFn: async () => {
|
||||
mutationFn: async (server: ServerInfo) => {
|
||||
await delUser();
|
||||
await setDefaultServer(serverState.id);
|
||||
await setDefaultServer(server.id);
|
||||
queryClient.clear();
|
||||
await defaultServer.refetch();
|
||||
createApi(serverState.address, undefined);
|
||||
createApi(server.address, undefined);
|
||||
queryClient.removeQueries();
|
||||
},
|
||||
onSuccess: async () => {
|
||||
@@ -64,19 +70,21 @@ function ServerList() {
|
||||
},
|
||||
});
|
||||
|
||||
const handleDelete = async (server) => {
|
||||
const handleDelete = async (server: ServerInfo) => {
|
||||
await delServer(server.id);
|
||||
|
||||
if (server.id === defaultServer.data) {
|
||||
await delUser();
|
||||
await servers.refetch();
|
||||
|
||||
if (servers.length > 1) {
|
||||
setDefaultServer(servers.data?.[0].id);
|
||||
createApi(servers.data?.[0].address, undefined);
|
||||
if ((servers.data?.length ?? 0) > 1) {
|
||||
if (servers.data?.[0].id) {
|
||||
setDefaultServer(servers.data?.[0].id);
|
||||
createApi(servers.data?.[0].address, undefined);
|
||||
}
|
||||
} else {
|
||||
await setDefaultServer(null);
|
||||
// TODO: reset api in context here
|
||||
await router.invalidate();
|
||||
}
|
||||
queryClient.removeQueries();
|
||||
navigate({ to: "/" });
|
||||
@@ -87,10 +95,14 @@ function ServerList() {
|
||||
await defaultServer.refetch();
|
||||
};
|
||||
|
||||
const setBackdrop = useBackdropStore((state) => state.setBackdrop);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
setBackdrop("", "");
|
||||
}, []);
|
||||
|
||||
const [addServerDialog, setAddServerDialog] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="server-list">
|
||||
<AppBarBackOnly />
|
||||
@@ -110,15 +122,20 @@ function ServerList() {
|
||||
style={{
|
||||
fontSize: "1.64em",
|
||||
}}
|
||||
onClick={() => navigate({ to: "/setup/server/add" })}
|
||||
onClick={() => setAddServerDialog(true)}
|
||||
>
|
||||
<div className="material-symbols-rounded">add</div>
|
||||
</IconButton>
|
||||
<AddServerDialog
|
||||
open={addServerDialog}
|
||||
sideEffect={() => servers.refetch()}
|
||||
setAddServerDialog={setAddServerDialog}
|
||||
/>
|
||||
</div>
|
||||
<Paper className="server-list-container">
|
||||
{servers.isSuccess &&
|
||||
servers.data.map((server, index) => (
|
||||
<div key={index} className="server-list-item">
|
||||
servers.data.map((server) => (
|
||||
<div key={server.id} className="server-list-item">
|
||||
<div className="material-symbols-rounded server-list-item-icon">
|
||||
dns
|
||||
</div>
|
||||
@@ -131,15 +148,11 @@ function ServerList() {
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
{server.systemInfo.ServerName}
|
||||
{server.systemInfo?.ServerName}
|
||||
{server.id === defaultServer.data && (
|
||||
<Chip
|
||||
label={
|
||||
<Typography
|
||||
variant="caption"
|
||||
fontWeight={600}
|
||||
fontFamily="JetBrains Mono Variable"
|
||||
>
|
||||
<Typography variant="caption" fontWeight={700}>
|
||||
Current
|
||||
</Typography>
|
||||
}
|
||||
@@ -148,7 +161,7 @@ function ServerList() {
|
||||
ml: 2,
|
||||
width: "5.4em",
|
||||
}}
|
||||
size="medium"
|
||||
size="small"
|
||||
/>
|
||||
)}
|
||||
</Typography>
|
||||
@@ -168,7 +181,7 @@ function ServerList() {
|
||||
}}
|
||||
fontWeight={300}
|
||||
>
|
||||
Version: {server.systemInfo.Version}
|
||||
Version: {server.systemInfo?.Version}
|
||||
</Typography>
|
||||
</div>
|
||||
<div className="server-list-item-buttons">
|
||||
@@ -177,8 +190,7 @@ function ServerList() {
|
||||
fontSize: "1.64em",
|
||||
}}
|
||||
onClick={() => {
|
||||
setServerState(server);
|
||||
handleServerChange.mutate();
|
||||
handleServerChange.mutate(server);
|
||||
}}
|
||||
disabled={handleServerChange.isPending}
|
||||
>
|
||||
|
||||
Reference in new issue
Block a user