diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 86fa281..0eebce2 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -112,3 +112,9 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TAURI_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }} TAURI_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }} + + - name: Upload build Artifacts + uses: actions/upload-artifact@v4 + with: + name: JellyPlayer-${{ matrix.platform }}-dev${{ github.sha }} + path: src-tauri/target/release/bundle/* diff --git a/index.html b/index.html index 2879328..80b072e 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,12 @@ align-items: center; justify-content: center; flex-direction: column; + transition: opacity 0.25s ease-in-out; + } + + .app-loading.hidden { + opacity: 0; + pointer-events: none; } .app-loading-logo { diff --git a/src/App.tsx b/src/App.tsx index c1267d3..10256dc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -95,16 +95,27 @@ import AudioPlayer from "./components/playback/audioPlayer/index.jsx"; // Utils import { useAudioPlayback } from "./utils/store/audioPlayback.js"; import { useBackdropStore } from "./utils/store/backdrop.js"; -import { useCentralStore } from "./utils/store/central.js"; +import { setInitialRoute, useCentralStore } from "./utils/store/central.js"; import { usePlaybackDataLoadStore } from "./utils/store/playback"; // 3rd Party +import { Api } from "@jellyfin/sdk"; +import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; import { CircularProgress } from "@mui/material"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; import { useSnackbar } from "notistack"; import { ErrorBoundary } from "react-error-boundary"; import { EasterEgg } from "./components/utils/easterEgg.jsx"; import { handleRelaunch } from "./utils/misc/relaunch"; +import { + delServer, + getAllServers, + getDefaultServer, + getServer, + setDefaultServer, +} from "./utils/storage/servers"; +import { UserStore, delUser, getUser } from "./utils/storage/user"; +import { createApi, useApi } from "./utils/store/api"; // const anim = { // initial: { @@ -138,7 +149,104 @@ import { handleRelaunch } from "./utils/misc/relaunch"; // ); // }; +const handleAuthError = async () => { + await delUser(); + setInitialRoute("/login/index"); +}; + +const authenticateUser = async (address: string, user: UserStore["user"]) => { + try { + createApi(address, user.AccessToken); + setInitialRoute("/home"); + } catch (error) { + console.error(error); + setInitialRoute("/error"); + } +}; + +const init = async () => { + if (window.location.pathname !== "/") { + window.location.href = "/"; + return; + } + + const defaultServerOnDisk = await getDefaultServer(); + + if (defaultServerOnDisk) { + const defaultServerInfo = await getServer(defaultServerOnDisk); + if (!defaultServerInfo) { + await setDefaultServer(null); + await delServer(defaultServerOnDisk); + + const servers = await getAllServers(); + + setInitialRoute(servers.length > 0 ? "/servers/list" : "/setup/server"); + + return; + } + + const userOnDisk = await getUser(); + createApi(defaultServerInfo.address, ""); + + if (userOnDisk) { + try { + let authApi = useApi.getState().api; + + if (!authApi) { + handleAuthError(); + return; + } + + await authenticateUser(defaultServerInfo.address, userOnDisk); + + authApi = useApi.getState().api!; + + const user = await getUserApi(authApi).getCurrentUser(); + + if (!user) { + await delUser(); + handleAuthError(); + } + } catch (error) { + console.error(error); + handleAuthError(); + } + } else { + setInitialRoute("/login/index"); + } + } else { + setInitialRoute("/setup/server"); + } +}; + function App() { + const [appReady, setAppReady] = useState(false); + + useEffect(() => { + init().then(() => { + setAppReady(true); + }); + }, []); + + if (!appReady) { + return ( +
+ +
+ ); + } + + return ; +} + +function AppReady() { const [audioPlayerVisible] = useAudioPlayback((state) => [state.display]); const { enqueueSnackbar } = useSnackbar(); @@ -161,7 +269,6 @@ function App() { undefined, ); - // biome-ignore lint/correctness/useExhaustiveDependencies: this dependency is required useEffect(() => { window.scrollTo(0, 0); }, [location.key]); diff --git a/src/main.tsx b/src/main.tsx index aa87bbc..86124b1 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,26 +1,16 @@ -import { Api } from "@jellyfin/sdk"; -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import { enqueueSnackbar } from "notistack"; import React from "react"; import ReactDOM from "react-dom/client"; import { BrowserRouter as Router } from "react-router-dom"; + +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; + import App from "./App"; -import { - delServer, - getAllServers, - getDefaultServer, - getServer, - setDefaultServer, -} from "./utils/storage/servers"; -import { UserStore, delUser, getUser } from "./utils/storage/user"; -import { createApi, useApi } from "./utils/store/api"; -import { setInitialRoute } from "./utils/store/central"; const queryClient = new QueryClient({ defaultOptions: { queries: { networkMode: "always", - gcTime: 120000, + staleTime: 2 * 60 * 1000, // 2 minutes }, mutations: { networkMode: "always", @@ -28,92 +18,14 @@ const queryClient = new QueryClient({ }, }); -const handleAuthError = async () => { - await delUser(); - setInitialRoute("/login/index"); -}; +ReactDOM.createRoot(document.getElementById("root")!).render( + + + + + + + , +); -const authenticateUser = async ( - address: string, - api: Api, - user: UserStore["user"], -) => { - try { - const auth = await api.authenticateUserByName(user.Name, user.Password); - - if (auth.status !== 200 || !auth.data.AccessToken) { - enqueueSnackbar("Incorrect Username or Password!", { - variant: "error", - }); - - handleAuthError(); - } else { - createApi(address, auth.data.AccessToken); - setInitialRoute("/home"); - } - } catch (error) { - console.error(error); - setInitialRoute("/error"); - } -}; - -const init = async () => { - if (window.location.pathname !== "/") { - window.location.href = "/"; - return; - } - - const defaultServerOnDisk = await getDefaultServer(); - console.log(defaultServerOnDisk) - - if (defaultServerOnDisk) { - const defaultServerInfo = await getServer(defaultServerOnDisk); - if (!defaultServerInfo) { - await setDefaultServer(null); - await delServer(defaultServerOnDisk); - - const servers = await getAllServers(); - - setInitialRoute(servers.length > 0 ? "/servers/list" : "/setup/server"); - - return; - } - - const userOnDisk = await getUser(); - createApi(defaultServerInfo.address, ""); - - if (userOnDisk) { - try { - const authApi = useApi.getState().api; - - if (!authApi) { - handleAuthError(); - return; - } - - await authenticateUser(defaultServerInfo.address, authApi, userOnDisk); - } catch (error) { - console.error(error); - handleAuthError(); - } - } else { - setInitialRoute("/login/index"); - } - } else { - setInitialRoute("/setup/server"); - } -}; - -init().then(() => { - ReactDOM.createRoot(document.getElementById("root")!).render( - - - - - - - , - ); - - document.querySelector(".app-loading")!.setAttribute("style", "display:none"); -}); +document.querySelector(".app-loading")!.classList.toggle("hidden"); diff --git a/src/routes/library/index.jsx b/src/routes/library/index.jsx index c1618dc..e16ecee 100644 --- a/src/routes/library/index.jsx +++ b/src/routes/library/index.jsx @@ -96,7 +96,7 @@ const LibraryView = () => { }, enabled: !!user.data, networkMode: "always", - gcTime: 0, + staleTime: 0, }); const [isPlayed, setIsPlayed] = useState(false); diff --git a/src/routes/login/index.jsx b/src/routes/login/index.jsx index e020407..bd922af 100644 --- a/src/routes/login/index.jsx +++ b/src/routes/login/index.jsx @@ -122,7 +122,7 @@ export const LoginWithImage = () => { const user = await authUser(); sessionStorage.setItem("accessToken", user.data.AccessToken); if (rememberMe === true) { - saveUser(userName, password.password); + saveUser(userName, user.data.AccessToken); } event.emit("set-api-accessToken", api.basePath); setLoading(false); @@ -256,7 +256,7 @@ export const UserLogin = () => { const users = await getUserApi(api).getPublicUsers(); return users.data; }, - gcTime: 0, + staleTime: 0, }); const handleInitiateQuickConnect = useMutation({ @@ -435,7 +435,7 @@ export const UserLoginManual = () => { const user = await authUser(); sessionStorage.setItem("accessToken", user.data.AccessToken); if (rememberMe === true) { - saveUser(userName, password.password); + saveUser(userName, user.data.AccessToken); } event.emit("set-api-accessToken", api.basePath); // setAccessToken(user.data.AccessToken) diff --git a/src/utils/storage/user.ts b/src/utils/storage/user.ts index f5a484f..ba5a667 100644 --- a/src/utils/storage/user.ts +++ b/src/utils/storage/user.ts @@ -3,7 +3,7 @@ import { Store } from "tauri-plugin-store-api"; export interface UserStore { user: { Name: string; - Password: string; + AccessToken: string; }; } @@ -12,10 +12,10 @@ const user = new Store(".user.dat"); /** * Set User details to .user.dat */ -const saveUser = async (userName: string, userPassword?: string) => { +const saveUser = async (userName: string, accessToken: string) => { user.set("user", { Name: userName, - Password: userPassword, + AccessToken: accessToken, }); await user.save();