Implement a better routing and api creation method
This commit is contained in:
5 files changed
+146
-107
No files matched your search
+44
-104
@@ -12,6 +12,7 @@ import {
|
|||||||
useNavigate,
|
useNavigate,
|
||||||
useLocation,
|
useLocation,
|
||||||
Outlet,
|
Outlet,
|
||||||
|
Navigate,
|
||||||
} from "react-router-dom";
|
} from "react-router-dom";
|
||||||
import { AnimatePresence, motion } from "framer-motion";
|
import { AnimatePresence, motion } from "framer-motion";
|
||||||
|
|
||||||
@@ -98,6 +99,7 @@ import { CircularProgress } from "@mui/material";
|
|||||||
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
||||||
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
|
import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api";
|
||||||
import { useServerStore } from "./utils/store/server";
|
import { useServerStore } from "./utils/store/server";
|
||||||
|
import { useCentralStore } from "./utils/store/central.js";
|
||||||
|
|
||||||
const anim = {
|
const anim = {
|
||||||
initial: {
|
initial: {
|
||||||
@@ -114,76 +116,21 @@ const anim = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// const init = async () => {
|
||||||
|
// const defaultServer = await getDefaultServer()
|
||||||
|
// if (defaultServer) {
|
||||||
|
// const server = await getServer(defaultServer)
|
||||||
|
// const savedUser = await getUser();
|
||||||
|
// createApi(server.address)
|
||||||
|
// if (savedUser) {
|
||||||
|
// let auth = await api.authenticateUserByName(savedUser.Name, savedUser.Password);
|
||||||
|
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
const LogicalRoute = () => {
|
const LogicalRoute = () => {
|
||||||
const [api] = useApi((state) => [state.api]);
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const { enqueueSnackbar } = useSnackbar();
|
|
||||||
const defaultServer = useQuery({
|
|
||||||
queryKey: ["routing", "defaultServer"],
|
|
||||||
queryFn: async () => {
|
|
||||||
return await getDefaultServer();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const server = useQuery({
|
|
||||||
queryKey: ["routing", "server"],
|
|
||||||
queryFn: async () => {
|
|
||||||
const result = await getServer(defaultServer.data);
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
enabled: !defaultServer.isFetching,
|
|
||||||
});
|
|
||||||
const pingServer = useQuery({
|
|
||||||
queryKey: ["routing", "pingServer"],
|
|
||||||
queryFn: async () => {
|
|
||||||
const result = await getSystemApi(api).getPingSystem();
|
|
||||||
return result.data;
|
|
||||||
},
|
|
||||||
enabled: Boolean(api) && server.isSuccess,
|
|
||||||
});
|
|
||||||
const userSaved = useQuery({
|
|
||||||
queryKey: ["routing", "savedUser"],
|
|
||||||
queryFn: async () => await getUser(),
|
|
||||||
});
|
|
||||||
const authenticateUser = async () => {
|
|
||||||
const res = await api.authenticateUserByName(
|
|
||||||
userSaved.data.Name,
|
|
||||||
userSaved.data.Password,
|
|
||||||
);
|
|
||||||
return res;
|
|
||||||
};
|
|
||||||
useEffect(() => {
|
|
||||||
if (
|
|
||||||
!defaultServer.isFetching &&
|
|
||||||
!server.isFetching &&
|
|
||||||
!userSaved.isFetching &&
|
|
||||||
!pingServer.isFetching
|
|
||||||
) {
|
|
||||||
if (defaultServer.data) {
|
|
||||||
if (pingServer.data == "Jellyfin Server") {
|
|
||||||
if (userSaved.data) {
|
|
||||||
authenticateUser();
|
|
||||||
navigate("/home");
|
|
||||||
// return <Navigate replace to={} />;
|
|
||||||
} else {
|
|
||||||
console.log(server.data.address);
|
|
||||||
createApi(server.data.address);
|
|
||||||
navigate("/login/index");
|
|
||||||
// return <Navigate replace to={`/login/index`} />;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
navigate("/error");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
navigate("/setup/server");
|
|
||||||
// return <Navigate replace to={`/setup/server`} />;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [
|
|
||||||
defaultServer.isFetching,
|
|
||||||
server.isFetching,
|
|
||||||
userSaved.isFetching,
|
|
||||||
pingServer.isFetching,
|
|
||||||
]);
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
@@ -263,15 +210,6 @@ function App() {
|
|||||||
|
|
||||||
const { enqueueSnackbar } = useSnackbar();
|
const { enqueueSnackbar } = useSnackbar();
|
||||||
|
|
||||||
const serverSaved = async () => {
|
|
||||||
const defaultServer = await getDefaultServer();
|
|
||||||
if (defaultServer) {
|
|
||||||
return defaultServer;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleRelaunch = async (event, reason) => {
|
const handleRelaunch = async (event, reason) => {
|
||||||
if (reason && reason == "backdropClick") {
|
if (reason && reason == "backdropClick") {
|
||||||
return;
|
return;
|
||||||
@@ -285,23 +223,6 @@ function App() {
|
|||||||
await relaunch();
|
await relaunch();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create api if not created
|
|
||||||
useEffect(() => {
|
|
||||||
if (!api) {
|
|
||||||
serverSaved()
|
|
||||||
.then(async (server) => {
|
|
||||||
if (server) {
|
|
||||||
const savedServer = await getServer(server);
|
|
||||||
createApi(savedServer.address);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
enqueueSnackbar(String(error), { variant: "error" });
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [api]);
|
|
||||||
|
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -337,8 +258,12 @@ function App() {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const [initialRoute] = useCentralStore((state) => [state.initialRoute]);
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
if (!initialRoute) {
|
||||||
|
return <>Loading...</>;
|
||||||
|
} else if (initialRoute) {
|
||||||
return (
|
return (
|
||||||
<SnackbarProvider maxSnack={5}>
|
<SnackbarProvider maxSnack={5}>
|
||||||
<ThemeProvider theme={theme}>
|
<ThemeProvider theme={theme}>
|
||||||
@@ -425,7 +350,9 @@ function App() {
|
|||||||
</AnimatePresence>
|
</AnimatePresence>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className={audioPlayerVisible ? "audio-playing" : ""}
|
className={
|
||||||
|
audioPlayerVisible ? "audio-playing" : ""
|
||||||
|
}
|
||||||
style={{
|
style={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
width: "100vw",
|
width: "100vw",
|
||||||
@@ -444,7 +371,9 @@ function App() {
|
|||||||
<Route
|
<Route
|
||||||
path="/"
|
path="/"
|
||||||
exact
|
exact
|
||||||
element={<LogicalRoute />}
|
element={
|
||||||
|
<Navigate to={initialRoute} />
|
||||||
|
}
|
||||||
// element={<></>}
|
// element={<></>}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
@@ -458,12 +387,15 @@ function App() {
|
|||||||
maxWidth="md"
|
maxWidth="md"
|
||||||
>
|
>
|
||||||
<DialogTitle id="alert-dialog-text">
|
<DialogTitle id="alert-dialog-text">
|
||||||
Unable to reach server
|
Unable to reach
|
||||||
|
server
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogContentText id="alert-dialog-desc">
|
<DialogContentText id="alert-dialog-desc">
|
||||||
Unable to connect to
|
Unable to
|
||||||
the jellyfin server.
|
connect to the
|
||||||
|
jellyfin
|
||||||
|
server.
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
@@ -482,7 +414,8 @@ function App() {
|
|||||||
handleRelaunch
|
handleRelaunch
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
Restart JellyPlayer
|
Restart
|
||||||
|
JellyPlayer
|
||||||
</Button>
|
</Button>
|
||||||
</DialogActions>
|
</DialogActions>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
@@ -494,7 +427,10 @@ function App() {
|
|||||||
element={<LoginRoute />}
|
element={<LoginRoute />}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Route path="/home" element={<Home />} />
|
<Route
|
||||||
|
path="/home"
|
||||||
|
element={<Home />}
|
||||||
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/setup/server"
|
path="/setup/server"
|
||||||
element={<ServerSetup />}
|
element={<ServerSetup />}
|
||||||
@@ -571,7 +507,10 @@ function App() {
|
|||||||
path="/settings"
|
path="/settings"
|
||||||
element={<Settings />}
|
element={<Settings />}
|
||||||
/>
|
/>
|
||||||
<Route path="/about" element={<About />} />
|
<Route
|
||||||
|
path="/about"
|
||||||
|
element={<About />}
|
||||||
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/player"
|
path="/player"
|
||||||
element={<VideoPlayer />}
|
element={<VideoPlayer />}
|
||||||
@@ -584,5 +523,6 @@ function App() {
|
|||||||
</SnackbarProvider>
|
</SnackbarProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
@@ -50,9 +50,15 @@ export const AppBar = () => {
|
|||||||
let usr = await getUserApi(api).getCurrentUser();
|
let usr = await getUserApi(api).getCurrentUser();
|
||||||
return usr.data;
|
return usr.data;
|
||||||
},
|
},
|
||||||
enabled: display,
|
enabled: display && Boolean(api),
|
||||||
networkMode: "always",
|
networkMode: "always",
|
||||||
});
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
if (user.isError) {
|
||||||
|
console.log(api);
|
||||||
|
console.error(user.error);
|
||||||
|
}
|
||||||
|
}, [user]);
|
||||||
|
|
||||||
const trigger = useScrollTrigger({
|
const trigger = useScrollTrigger({
|
||||||
disableHysteresis: true,
|
disableHysteresis: true,
|
||||||
@@ -83,7 +89,7 @@ export const AppBar = () => {
|
|||||||
event.emit("create-jellyfin-api", api.basePath);
|
event.emit("create-jellyfin-api", api.basePath);
|
||||||
queryClient.clear();
|
queryClient.clear();
|
||||||
setAnchorEl(null);
|
setAnchorEl(null);
|
||||||
navigate("/login");
|
navigate("/login/index");
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ import { BrowserRouter as Router } from "react-router-dom";
|
|||||||
import App from "./App";
|
import App from "./App";
|
||||||
|
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
|
import { getDefaultServer, getServer } from "./utils/storage/servers";
|
||||||
|
import { getUser } from "./utils/storage/user";
|
||||||
|
import { createApi, useApi } from "./utils/store/api";
|
||||||
|
import { setInitialRoute, useCentralStore } from "./utils/store/central";
|
||||||
|
|
||||||
const queryClient = new QueryClient({
|
const queryClient = new QueryClient({
|
||||||
defaultOptions: {
|
defaultOptions: {
|
||||||
@@ -21,6 +25,38 @@ const queryClient = new QueryClient({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const init = async () => {
|
||||||
|
const defaultServerOnDisk = await getDefaultServer();
|
||||||
|
if (defaultServerOnDisk) {
|
||||||
|
const defaultServerInfo = await getServer(defaultServerOnDisk);
|
||||||
|
createApi(defaultServerInfo.address);
|
||||||
|
const userOnDisk = await getUser();
|
||||||
|
if (userOnDisk) {
|
||||||
|
try {
|
||||||
|
const auth = await useApi
|
||||||
|
.getState()
|
||||||
|
.api.authenticateUserByName(
|
||||||
|
userOnDisk.Name,
|
||||||
|
userOnDisk.Password,
|
||||||
|
);
|
||||||
|
createApi(defaultServerInfo.address, auth.AccessToken);
|
||||||
|
setInitialRoute("/home");
|
||||||
|
// window.startingRoute("/home");
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
setInitialRoute("/error");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setInitialRoute("/login/index");
|
||||||
|
// window.startingRoute = "/login/index";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setInitialRoute("/setup/server");
|
||||||
|
// window.startingRoute = "/setup/server";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
init();
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById("root")).render(
|
ReactDOM.createRoot(document.getElementById("root")).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/** @format */
|
/** @format */
|
||||||
|
|
||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { EventEmitter as event } from "../../eventEmitter.js";
|
import { EventEmitter as event } from "../../eventEmitter.js";
|
||||||
|
|
||||||
@@ -34,6 +34,7 @@ import { AppBarBackOnly } from "../../components/appBar/backOnly.jsx";
|
|||||||
|
|
||||||
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
|
||||||
import { getBrandingApi } from "@jellyfin/sdk/lib/utils/api/branding-api";
|
import { getBrandingApi } from "@jellyfin/sdk/lib/utils/api/branding-api";
|
||||||
|
import { getQuickConnectApi } from "@jellyfin/sdk/lib/utils/api/quick-connect-api";
|
||||||
|
|
||||||
import "./login.module.scss";
|
import "./login.module.scss";
|
||||||
import { ErrorNotice } from "../../components/notices/errorNotice/errorNotice.jsx";
|
import { ErrorNotice } from "../../components/notices/errorNotice/errorNotice.jsx";
|
||||||
@@ -221,6 +222,8 @@ export const LoginWithImage = () => {
|
|||||||
export const UserLogin = () => {
|
export const UserLogin = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const { enqueueSnackbar } = useSnackbar();
|
||||||
|
|
||||||
const [api] = useApi((state) => [state.api]);
|
const [api] = useApi((state) => [state.api]);
|
||||||
|
|
||||||
const handleChangeServer = () => {
|
const handleChangeServer = () => {
|
||||||
@@ -239,6 +242,24 @@ export const UserLogin = () => {
|
|||||||
gcTime: 0,
|
gcTime: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const handleInitiateQuickConnect = useMutation({
|
||||||
|
mutationKey: ["quick-connect"],
|
||||||
|
mutationFn: async () => {
|
||||||
|
console.log(api);
|
||||||
|
const result = await getQuickConnectApi(api).initiate();
|
||||||
|
return result.data;
|
||||||
|
},
|
||||||
|
onSuccess: (data) => {
|
||||||
|
console.info(data);
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
console.error(error);
|
||||||
|
enqueueSnackbar("Unable to use Quick Connect", {
|
||||||
|
variant: "error",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (users.isPending) {
|
if (users.isPending) {
|
||||||
return <LinearProgress />;
|
return <LinearProgress />;
|
||||||
}
|
}
|
||||||
@@ -314,6 +335,14 @@ export const UserLogin = () => {
|
|||||||
>
|
>
|
||||||
Change Server
|
Change Server
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
className="userEventButton"
|
||||||
|
onClick={handleInitiateQuickConnect.mutate}
|
||||||
|
disabled
|
||||||
|
>
|
||||||
|
Quick Connect
|
||||||
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
className="userEventButton"
|
className="userEventButton"
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/** @format */
|
||||||
|
|
||||||
|
import { create } from "zustand";
|
||||||
|
import { getAllServer, getDefaultServer, getServer } from "../storage/servers";
|
||||||
|
import { getUser } from "../storage/user";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Central Store controls the base state required by the app
|
||||||
|
*/
|
||||||
|
export const useCentralStore = create(() => ({
|
||||||
|
defaultServerOnDisk: async () => await getDefaultServer(),
|
||||||
|
defaultServerInfoOnDisk: async () => {
|
||||||
|
let a = await getDefaultServer();
|
||||||
|
return await getServer(a);
|
||||||
|
},
|
||||||
|
allServersOnDisk: async () => getAllServer(),
|
||||||
|
userOnDisk: async () => getUser(),
|
||||||
|
/** This is the initial route that app goes to just after app startups */
|
||||||
|
initialRoute: null,
|
||||||
|
}));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets app inital route
|
||||||
|
* @param {string} route
|
||||||
|
*/
|
||||||
|
export const setInitialRoute = (route) => {
|
||||||
|
useCentralStore.setState((state) => ({ ...state, initialRoute: route }));
|
||||||
|
};
|
||||||
Reference in new issue
Block a user