diff --git a/src/assets/icons/avatar.png b/src/assets/icons/avatar.png new file mode 100644 index 0000000..b5605b0 Binary files /dev/null and b/src/assets/icons/avatar.png differ diff --git a/src/components/appBar/backOnly.jsx b/src/components/appBar/backOnly.tsx similarity index 68% rename from src/components/appBar/backOnly.jsx rename to src/components/appBar/backOnly.tsx index bb335cc..a58aa27 100644 --- a/src/components/appBar/backOnly.jsx +++ b/src/components/appBar/backOnly.tsx @@ -2,6 +2,8 @@ import AppBar from "@mui/material/AppBar"; import IconButton from "@mui/material/IconButton"; import Toolbar from "@mui/material/Toolbar"; import { useRouter } from "@tanstack/react-router"; +import React from "react"; +import QuickConnectButton from "../buttons/quickConnectButton"; export const AppBarBackOnly = () => { const { history } = useRouter(); @@ -16,13 +18,23 @@ export const AppBarBackOnly = () => { arrow_back + ); diff --git a/src/components/buttons/quickConnectButton.tsx b/src/components/buttons/quickConnectButton.tsx index 5945f95..698f2a5 100644 --- a/src/components/buttons/quickConnectButton.tsx +++ b/src/components/buttons/quickConnectButton.tsx @@ -1,8 +1,7 @@ -import { saveUser } from "@/utils/storage/user"; +import useInterval from "@/utils/hooks/useInterval"; import { useApiInContext } from "@/utils/store/api"; import { getQuickConnectApi } from "@jellyfin/sdk/lib/utils/api/quick-connect-api"; -import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; -import { LoadingButton } from "@mui/lab"; +import { LoadingButton, type LoadingButtonProps } from "@mui/lab"; import { Button, Checkbox, @@ -11,45 +10,62 @@ import { DialogContent, DialogContentText, FormControlLabel, + Slide, Tooltip, Typography, } from "@mui/material"; -import { useMutation, useQuery } from "@tanstack/react-query"; -import { useNavigate } from "@tanstack/react-router"; -import type { Timeout } from "@tanstack/react-router/dist/esm/utils"; +import type { TransitionProps } from "@mui/material/transitions"; +import { skipToken, useMutation, useQuery } from "@tanstack/react-query"; import { writeText } from "@tauri-apps/plugin-clipboard-manager"; import { useSnackbar } from "notistack"; import React, { useState } from "react"; -const QuickConnectButton = () => { - const api = useApiInContext((s) => s.api); - const createApi = useApiInContext((s) => s.createApi); - const headers = { - "X-Emby-Authorization": `MediaBrowser Client="${api.clientInfo.name}", Device="${api.deviceInfo.name}", DeviceId="${api.deviceInfo.id}", Version="${api.clientInfo.version}"`, - }; +const Transition = React.forwardRef(function Transition( + props: TransitionProps & { + children: React.ReactElement; + }, + ref: React.Ref, +) { + return ( + + ); +}); - const [quickConnectStatusInterval, setQuickConnectStatusInterval] = - useState(null); +const QuickConnectButton = (props: LoadingButtonProps) => { + const api = useApiInContext((s) => s.api); + const headers = { + "X-Emby-Authorization": `MediaBrowser Client="${api?.clientInfo.name}", Device="${api?.deviceInfo.name}", DeviceId="${api?.deviceInfo.id}", Version="${api?.clientInfo.version}"`, + }; const quickConnectEnabled = useQuery({ queryKey: ["quick-connect-button", "check-quick-connect-enabled"], - queryFn: async () => await getQuickConnectApi(api).getQuickConnectEnabled(), + queryFn: api + ? async () => await getQuickConnectApi(api).getQuickConnectEnabled() + : skipToken, + enabled: Boolean(api), }); - const navigate = useNavigate(); - const { enqueueSnackbar } = useSnackbar(); const [quickConnectCode, setQuickConnectCode] = useState(); const [rememberUser, setRememberUser] = useState(true); const checkQuickConnectStatus = useMutation({ mutationKey: ["quick-connect-button", "check-quick-connect-status"], mutationFn: async (secret: string) => - ( - await getQuickConnectApi(api).getQuickConnectState( - { secret }, - { headers }, - ) - ).data, + api + ? ( + await getQuickConnectApi(api).getQuickConnectState( + { secret }, + { headers }, + ) + ).data + : skipToken, }); const initQuickConnect = useMutation({ mutationKey: ["quick-connect-button", "initiate-connection"], @@ -59,57 +75,42 @@ const QuickConnectButton = () => { variant: "error", }); } - return await getQuickConnectApi(api).initiateQuickConnect({ headers }); + if (api) { + return await getQuickConnectApi(api).initiateQuickConnect({ headers }); + } }, onSuccess: (result) => { - setQuickConnectCode(result.data.Code); - const authAwaitInterval = setInterval(async () => { - // Check if user has been authenticated by the server - const quickConnectCheck = await checkQuickConnectStatus.mutateAsync( - result.data.Secret, - ); - if (quickConnectCheck.Authenticated) { - const userAuth = await getUserApi(api).authenticateWithQuickConnect({ - quickConnectDto: { - Secret: quickConnectCheck.Secret, - }, - }); - enqueueSnackbar(`Logged in as ${userAuth.data.User?.Name}!`, { - variant: "success", - }); - sessionStorage.setItem("accessToken", userAuth.data.AccessToken); - createApi(api.basePath, userAuth.data.AccessToken); - - if (rememberUser) { - await saveUser(userAuth.data.User?.Name, userAuth.data.AccessToken); - } - - clearInterval(authAwaitInterval); - - navigate({ to: "/home" }); - } - }, 1000); - setQuickConnectStatusInterval(authAwaitInterval); + setQuickConnectCode(result?.data.Code); }, onError: (error) => { enqueueSnackbar("Error initiating Quick Connect.", { variant: "error" }); console.error(error); }, }); + + useInterval( + () => { + if (quickConnectCode) { + checkQuickConnectStatus.mutate(quickConnectCode); + } + }, + quickConnectCode ? 1000 : null, + ); + return ( <> @@ -119,10 +120,12 @@ const QuickConnectButton = () => { open={Boolean(quickConnectCode)} onClose={() => { setQuickConnectCode(null); - clearInterval(quickConnectStatusInterval); + initQuickConnect.reset(); + checkQuickConnectStatus.reset(); }} fullWidth maxWidth="xs" + TransitionComponent={Transition} > { borderRadius: "10px", cursor: "pointer", }} - onClick={async (e) => { - await writeText(quickConnectCode); + onClick={async () => { + quickConnectCode && (await writeText(quickConnectCode)); enqueueSnackbar("Quick Connect Code copied!", { variant: "info", key: "copiedText", diff --git a/src/components/card/card.tsx b/src/components/card/card.tsx index 9358b36..4cce857 100644 --- a/src/components/card/card.tsx +++ b/src/components/card/card.tsx @@ -115,16 +115,19 @@ const CardComponent = ({ {item.Name { + if (api) { + console.log(`API is set - Route ${window.location.pathname}`); + console.log(api); + } else { + console.log(`API is not set - Route ${window.location.pathname}`); + } + router.invalidate(); // This is a hack to force the router to re-evaluate the routes and re-run the beforeLoad functions + }, [api]); return ( void; + api: Api | undefined; + createApi: (serverAddress: string, accessToken: string | undefined) => void; user: UserDto | null | undefined; jellyfinSDK: Jellyfin; fetchCurrentUser: (api: Api | undefined) => Promise; diff --git a/src/routes/_api.tsx b/src/routes/_api.tsx index 78e3c99..5364609 100644 --- a/src/routes/_api.tsx +++ b/src/routes/_api.tsx @@ -36,7 +36,7 @@ export const Route = createFileRoute("/_api")({ userOnDisk.AccessToken, ); try { - const user = await getUserApi(apiTemp).getCurrentUser(); + await getUserApi(apiTemp).getCurrentUser(); // return { user: user.data }; } catch (error) { console.error(error); @@ -50,7 +50,6 @@ export const Route = createFileRoute("/_api")({ } context.createApi(currentServer?.address, userOnDisk.AccessToken); } else { - context.jellyfinSDK.createApi(currentServer?.address, undefined); context.createApi(currentServer?.address, undefined); // Creates Api // return { api: apiTemp }; @@ -61,7 +60,6 @@ export const Route = createFileRoute("/_api")({ if (context.api.accessToken && location.pathname !== "/login/manual") { try { await getUserApi(context.api).getCurrentUser(); // Verify user is able to authenticate - } catch (error) { console.error(error); throw redirect({ diff --git a/src/routes/_api/login.tsx b/src/routes/_api/login.tsx index d71eed1..66a9cb6 100644 --- a/src/routes/_api/login.tsx +++ b/src/routes/_api/login.tsx @@ -1,5 +1,7 @@ import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; +import { CircularProgress, Typography } from "@mui/material"; import { createFileRoute, redirect } from "@tanstack/react-router"; +import React from "react"; export const Route = createFileRoute("/_api/login")({ beforeLoad: async ({ context, location }) => { @@ -7,13 +9,26 @@ export const Route = createFileRoute("/_api/login")({ const api = context.api; if (!api) { console.info("Awaiting Api generation."); + // throw redirect({ to: "/login/manual" }); + } else { + const publicUsers = (await getUserApi(api).getPublicUsers()).data; + if (publicUsers.length > 0) { + throw redirect({ to: "/login/list" }); + } throw redirect({ to: "/login/manual" }); } - const publicUsers = (await getUserApi(api).getPublicUsers()).data; - if (publicUsers.length > 0) { - throw redirect({ to: "/login/list" }); - } - throw redirect({ to: "/login/manual" }); } }, + // component: () => { + // const data = Route.useRouteContext(); + // console.log(data); + // return ( + //
+ // + // + // Fetching users... + // + //
+ // ); + // }, }); diff --git a/src/routes/_api/login/list.tsx b/src/routes/_api/login/list.tsx index 3652598..81c5983 100644 --- a/src/routes/_api/login/list.tsx +++ b/src/routes/_api/login/list.tsx @@ -1,12 +1,10 @@ import { useQuery } from "@tanstack/react-query"; import React, { useLayoutEffect } from "react"; -import { Grid, Typography } from "@mui/material"; +import { Chip, IconButton, Typography } from "@mui/material"; import Button from "@mui/material/Button"; -import Container from "@mui/material/Container"; import { AppBarBackOnly } from "@/components/appBar/backOnly.jsx"; -import { Card } from "@/components/card/card.js"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; @@ -15,13 +13,42 @@ import "./login.scss"; import QuickConnectButton from "@/components/buttons/quickConnectButton"; import { useApiInContext } from "@/utils/store/api"; -import { setBackdrop } from "@/utils/store/backdrop.js"; -import { createFileRoute, useNavigate } from "@tanstack/react-router"; +import { useBackdropStore } from "@/utils/store/backdrop.js"; +import type { UserDto } from "@jellyfin/sdk/lib/generated-client"; +import { Link, createFileRoute, useNavigate } from "@tanstack/react-router"; + +import avatar from "../../../assets/icons/avatar.png"; export const Route = createFileRoute("/_api/login/list")({ component: LoginPublicUsersList, }); +const UserCard = ({ user }: { user: UserDto }) => { + const api = useApiInContext((s) => s.api); + return ( + +
+ {user.PrimaryImageTag ? ( + {"user"} + ) : ( + user + )} +
+ + {user.Name} + + + ); +} + function LoginPublicUsersList() { const navigate = useNavigate(); @@ -30,18 +57,18 @@ function LoginPublicUsersList() { const handleChangeServer = () => { navigate({ to: "/setup/server/list" }); }; - const handleManualLogin = () => { - navigate({ to: "/login/manual" }); - }; const users = useQuery({ queryKey: ["login", "public-users"], queryFn: async () => { + if (!api) return []; const result = await getUserApi(api).getPublicUsers(); return result.data; }, enabled: Boolean(api), }); + const setBackdrop = useBackdropStore((state) => state.setBackdrop); + useLayoutEffect(() => { setBackdrop("", ""); }, []); @@ -49,66 +76,18 @@ function LoginPublicUsersList() { return ( <> - - +
+ Users - - {users.data.map((item, index) => { - return ( - - - navigate({ - to: "/login/$userId/$userName", - params: { userId: item.Id, userName: item.Name }, - }) - } - overrideIcon="User" - imageType="Primary" - /> - - ); +
+ {users.data.map((item) => { + return ; })} - +
-
+ {/*
-
- +
*/} + + Don't see your user? Try using{" "} + Manual Login or{" "} + Changing Server + + } + /> +
); } if (users.isError) { - return ; + return ; } } diff --git a/src/routes/_api/login/login.scss b/src/routes/_api/login/login.scss index 8154a29..0d2da11 100644 --- a/src/routes/_api/login/login.scss +++ b/src/routes/_api/login/login.scss @@ -1,16 +1,35 @@ .user { - &List { - display: flex; - gap: 1em; - width: min(60vw, 45em); + &-list { + &-container { + display: flex; + flex-flow: row wrap; + gap: 1em; + justify-content: center; + align-items: center; + height: 30em; + overflow-y: auto; + overflow-x: hidden; + scroll-snap-type: y mandatory; + margin: 2em 0; + } } - &Card { - border-radius: $border-radius-default; - display: flex; - flex-flow: column; - flex: 1 1 0px; - overflow: hidden; + &-card { + width: fit-content; + scroll-snap-align: start; + text-decoration: none !important; + color: white !important; + &-image{ + width: 100%; + height: 100%; + object-fit: cover; + &-container{ + aspect-ratio: 1; + width: 8em; + overflow: hidden; + border-radius: 100%; + } + } } &Icon { font-size: 5em; @@ -31,13 +50,21 @@ } .login { - width: 100vw; + // width: 50em; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; + &-container{ + width: min(40em,80vw); + height: 100vh; + margin: auto; + padding-top: 4em; + padding-bottom: 4em; + } + &-form { width: 28em; display: flex; diff --git a/src/styles/global.scss b/src/styles/global.scss index e1badf0..a0a4a8a 100644 --- a/src/styles/global.scss +++ b/src/styles/global.scss @@ -298,4 +298,8 @@ h1 { .opacity-07{ opacity: 0.7; +} + +a { + color: $clr-accent-default; } \ No newline at end of file diff --git a/src/utils/hooks/useInterval.tsx b/src/utils/hooks/useInterval.tsx new file mode 100644 index 0000000..0bf5156 --- /dev/null +++ b/src/utils/hooks/useInterval.tsx @@ -0,0 +1,23 @@ +import { useEffect, useRef } from "react"; + +function useInterval(callback: () => void, delay) { + const savedCallback = useRef(null); + + // Remember the latest callback. + useEffect(() => { + savedCallback.current = callback; + }, [callback]); + + // Set up the interval. + useEffect(() => { + function tick() { + savedCallback.current(); + } + if (delay !== null) { + const id = setInterval(tick, delay); + return () => clearInterval(id); + } + }, [delay]); +} + +export default useInterval; \ No newline at end of file diff --git a/src/utils/store/api.tsx b/src/utils/store/api.tsx index ad8cc62..5e176ca 100644 --- a/src/utils/store/api.tsx +++ b/src/utils/store/api.tsx @@ -11,7 +11,7 @@ import axios from "axios"; import axiosTauriApiAdapter from "axios-tauri-api-adapter"; type ApiStore = { - api: Api; + api: Api | undefined; deviceId: string | null; jellyfin: Jellyfin; createApi: (serverAddress: string, accessToken?: string | undefined) => void;