Start working on new server page #202

This commit is contained in:
Prayag Prajapati committed 2023-10-10 22:15:56 +05:30
1 parent 0e774ebc8c
commit 7e7535edfb
5 files changed
+161 -107

No files matched your search

+5 -11
View File
@@ -98,6 +98,7 @@ import PlaylistTitlePage from "./routes/playlist/index.jsx";
// Initial custom axios client to use tauri's http module
import axios from "axios";
import axiosTauriApiAdapter from "axios-tauri-api-adapter";
import { useApi } from "./utils/store/api.js";
export const axiosClient = axios.create({
adapter: axiosTauriApiAdapter,
@@ -170,12 +171,6 @@ const AnimationWrapper = () => {
function App() {
const navigate = useNavigate();
useEffect(() => {
document
.querySelector(".app-loading")
.setAttribute("style", "display:none");
}, []);
const [serverReachable, setServerReachable] = useState(true);
const [playbackDataLoading] = usePlaybackDataLoadStore((state) => [
@@ -486,6 +481,10 @@ function App() {
path="/"
element={<LogicalRoutes />}
/>
<Route
path="/login"
element={<LoginLogicalRoutes />}
/>
<Route path="/home" element={<Home />} />
<Route
@@ -569,11 +568,6 @@ function App() {
path="/player"
element={<VideoPlayer />}
/>
<Route
path="/login"
element={<LoginLogicalRoutes />}
/>
</Route>
</Routes>
</div>
+3 -3
View File
@@ -5,8 +5,6 @@ import ReactDOM from "react-dom/client";
import { BrowserRouter as Router } from "react-router-dom";
import { invoke } from "@tauri-apps/api/tauri";
import App from "./App";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
@@ -37,5 +35,7 @@ document.addEventListener("DOMContentLoaded", () => {
// This will wait for the window to load, but you could
// run this function on whatever trigger you want
console.info("Initial render complete");
invoke("close_splashscreen");
document
.querySelector(".app-loading")
.setAttribute("style", "display:none");
});
+90 -92
View File
@@ -15,6 +15,7 @@ import Grid from "@mui/material/Grid";
import LoadingButton from "@mui/lab/LoadingButton";
import SvgIcon from "@mui/material/SvgIcon";
import Container from "@mui/material/Container";
import LinearProgress from "@mui/material/LinearProgress";
import { mdiChevronRight } from "@mdi/js";
@@ -30,9 +31,13 @@ import { v4 as uuidv4 } from "uuid";
// SCSS
import "./server.module.scss";
import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { axiosClient } from "../../../App.jsx";
import axios from "axios";
import { useApi } from "../../../utils/store/api.js";
import { RecommendedServerDiscovery } from "@jellyfin/sdk/lib/discovery/recommended-server-discovery.js";
import { MdiInformation } from "../../../components/icons/mdiInformation.jsx";
import { yellow } from "@mui/material/colors";
export const ServerList = () => {
const [renderServerList, setRenderServerList] = useState(false);
@@ -62,7 +67,7 @@ export const ServerList = () => {
// localStorage.setItem("servers", serverList);
};
const ServerList = (props) => {
const ServerLists = (props) => {
const startRender = props.startRender;
if (startRender) {
return serverList.map((item, index) => {
@@ -128,106 +133,79 @@ export const ServerList = () => {
);
};
export const ServerSetup = (props) => {
const sdk = new Jellyfin({
clientInfo: {
name: "JellyPlayer",
version: appVer,
},
deviceInfo: {
name: "JellyPlayer",
id: uuidv4(),
},
});
export const ServerSetup = () => {
/**
* @type {[import("@jellyfin/sdk").Api, function, Jellyfin]}
*/
const [api, createApi, jellyfin] = useApi((state) => [
state.api,
state.createApi,
state.jellyfin,
]);
const [serverIp, setServerIp] = useState("");
const [checkingServer, setServerCheckState] = useState(false);
const { enqueueSnackbar } = useSnackbar();
const navigate = useNavigate();
const [loading, setLoading] = useState(false);
const usersAvailable = async () => {};
const addServer = async () => {
event.emit("create-jellyfin-api", serverIp);
let sysInfo = await getSystemApi(window.api).getPublicSystemInfo();
sysInfo = sysInfo.data;
// Set Server Config to Tauri store
sysInfo.Ip = serverIp;
setServer(sysInfo);
};
const checkServer = async () => {
setServerCheckState(true);
try {
const result = await axiosClient.get(`${serverIp}/System/Ping`);
if (result.data == "Jellyfin Server") {
event.emit("create-jellyfin-api", serverIp);
let sysInfo = await getSystemApi(
window.api,
).getPublicSystemInfo();
sysInfo = sysInfo.data;
sysInfo.Ip = serverIp;
setServer(sysInfo);
const users = await getUserApi(window.api).getPublicUsers();
if (users.data.length >= 1) {
navigate("/login/users");
} else {
navigate("/login/manual");
}
} else {
const checkServer = useMutation({
mutationFn: async () => {
const servers =
await jellyfin.discovery.getRecommendedServerCandidates(
serverIp,
);
const bestServer = jellyfin.discovery.findBestServer(servers);
return bestServer;
},
onSuccess: (bestServer) => {
if (bestServer) {
console.info(bestServer);
enqueueSnackbar("Client added successfully", {
variant: "success",
});
createApi(bestServer.address, null);
navigate("/login");
}
},
onError: (err) => {
console.error(err);
enqueueSnackbar(`${err}`, { variant: "error" });
enqueueSnackbar("Something went wrong", { variant: "error" });
},
onSettled: (bestServer) => {
console.log(bestServer);
if (!bestServer) {
enqueueSnackbar(
"The provided ip address doesn't seem point to a Jellyfin server.",
{ variant: "error" },
`Provided server address is not a Jellyfin server.`,
{
variant: "error",
},
);
}
} catch (error) {
console.log(error);
enqueueSnackbar("Unable to verfiy server address.", {
variant: "error",
});
}
setServerCheckState(false);
return "";
};
const handleAddServer = () => {
// setLoading(true);
// let data;
// fetch(`${serverIp}/System/Ping`, {
// body: JSON.stringify(data),
// })
// .then((res) => res.json())
// .then((data) => {
// if (data == "Jellyfin Server") {
// addServer();
// setLoading(false);
// return true;
// } else {
// enqueueSnackbar(
// "The server address does not seem be a jellyfin server",
// { variant: "error" },
// );
// setLoading(false);
// return false;
// }
// })
// .catch((error) => {
// setLoading(false);
// enqueueSnackbar(
// "Unable to verify whether the give address is a valid Jellyfin server",
// { variant: "error" },
// );
// console.error(error);
// });
};
},
});
return (
<>
<Container maxWidth="sm" className={"centered serverContainer"}>
<LinearProgress
sx={{
position: "fixed",
top: 0,
left: 0,
right: 0,
opacity: checkServer.isLoading ? 1 : 0,
transition: "opacity 350ms",
}}
/>
<Container
maxWidth="sm"
className={"centered serverContainer"}
style={{
opacity: checkServer.isLoading ? "0.5" : "1",
transition: "opacity 350ms",
}}
>
<Grid
container
spacing={2}
@@ -247,7 +225,6 @@ export const ServerSetup = (props) => {
onChange={(event) => {
setServerIp(event.target.value);
}}
helperText="Add your server adddress with https:// or http://. Eg: https://demo.jellyfin.org"
></TextField>
</Grid>
<Grid item xl={5} md={6} sx={{ width: "100%" }}>
@@ -255,18 +232,39 @@ export const ServerSetup = (props) => {
variant="contained"
sx={{ width: "100%" }}
size="large"
loading={checkingServer}
loading={checkServer.isLoading}
endIcon={
<SvgIcon>
<path d={mdiChevronRight}></path>
</SvgIcon>
}
loadingPosition="end"
onClick={() => checkServer()}
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,
}}
>
<MdiInformation
sx={{
color: yellow[700],
}}
/>
<Typography variant="subtitle1">
Example: https://demo.jellyfin.org/stable
</Typography>
</Grid>
</Grid>
</Container>
</>
+62
View File
@@ -0,0 +1,62 @@
/** @format */
import { create } from "zustand";
import { Api, Jellyfin } from "@jellyfin/sdk";
import { version as appVer } from "../../../package.json";
// Initial custom axios client to use tauri's http module
import axios from "axios";
import axiosTauriApiAdapter from "axios-tauri-api-adapter";
import { v4 as uuidv4 } from "uuid";
export const axiosClient = axios.create({
adapter: axiosTauriApiAdapter,
headers: { "Access-Control-Allow-Origin": "*" },
timeout: 60000,
});
const deviceId = localStorage.getItem("deviceId");
if (!deviceId) {
localStorage.setItem("deviceId", uuidv4());
}
// event.on("create-jellyfin-api", (serverAddress) => {
// window.api = ;
// // window.api = jellyfin.createApi(serverAddress);
// });
// event.on("set-api-accessToken", (serverAddress) => {
// window.api = jellyfin.createApi(
// serverAddress,
// sessionStorage.getItem("accessToken"),
// axiosClient,
// );
// // });
const jellyfin = new Jellyfin({
clientInfo: {
name: "JellyPlayer",
version: appVer,
},
deviceInfo: {
name: "JellyPlayer",
id: deviceId,
},
});
/**
* @typedef {import("@jellyfin/sdk").Api} Api
*/
export const useApi = create((set) => ({
/**
* @type {Api}
*/
api: null,
deviceId: localStorage.getItem("deviceId"),
jellyfin: jellyfin,
createApi: (serverAddress, accessToken) =>
set((state) => ({
...state,
api: jellyfin.createApi(serverAddress, accessToken, axiosClient),
})),
}));