From e049ce9ad6493ab5e7ccee5846250cc012a519e4 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Tue, 3 Dec 2024 18:13:36 +0530 Subject: [PATCH] refactore: cleanup __root.tsx --- .gitignore | 4 +- package.json | 2 +- src-tauri/tauri.conf.json | 6 +- src/components/backdrop/index.tsx | 46 ++++ src/components/nProgress/index.tsx | 55 ++-- .../{emptyNotice.jsx => emptyNotice.tsx} | 2 +- src/components/routerLoading/index.tsx | 24 ++ src/components/settings/index.tsx | 4 +- src/components/updater/index.tsx | 135 ++++++++++ src/routes/__root.tsx | 252 ++---------------- 10 files changed, 274 insertions(+), 256 deletions(-) create mode 100644 src/components/backdrop/index.tsx rename src/components/notices/emptyNotice/{emptyNotice.jsx => emptyNotice.tsx} (92%) create mode 100644 src/components/routerLoading/index.tsx create mode 100644 src/components/updater/index.tsx diff --git a/.gitignore b/.gitignore index 9591cb6..bebdd8b 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,6 @@ target/ **/*.rs.bk # MSVC Windows builds of rustc generate these, which store debugging information -*.pdb \ No newline at end of file +*.pdb + +generateBanner \ No newline at end of file diff --git a/package.json b/package.json index 4fd8022..e3b0314 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "blink", "private": true, - "version": "0.0.7-dev", + "version": "0.0.6-dev", "type": "module", "scripts": { "dev": "vite --force && tsr watch", diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 93ac6fa..76f471c 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -58,7 +58,11 @@ "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEZEMTg0MjEzRDYzRjJDN0EKUldSNkxEL1dFMElZL1dxbHA0V2lyVDFEdGpOZXkxMG9XdHVLVnRpVVV5c1Z0OFNRUE45dkpxbFAK", "endpoints": [ "https://rawcdn.githack.com/prayag17/Blink/refs/heads/main/latest.json" - ] + ], + "windows": { + "installMode": "passive" + } + } }, "app": { diff --git a/src/components/backdrop/index.tsx b/src/components/backdrop/index.tsx new file mode 100644 index 0000000..ce6af9c --- /dev/null +++ b/src/components/backdrop/index.tsx @@ -0,0 +1,46 @@ +import { useBackdropStore } from "@/utils/store/backdrop"; +import { AnimatePresence, motion } from "framer-motion"; +import React, { useLayoutEffect, useState } from "react"; + +export default function Backdrop() { + const [backdropLoading, setBackdropLoading] = useState(true); + + const [backdropId, backdropUrl] = useBackdropStore((state) => [ + state.backdropId, + state.backdropUrl, + ]); + + // Reset loading status for backdrop + useLayoutEffect(() => { + setBackdropLoading(true); + }, [backdropId]); + + return ( +
+ + setBackdropLoading(false)} + loading="lazy" + /> + +
+ ); +} \ No newline at end of file diff --git a/src/components/nProgress/index.tsx b/src/components/nProgress/index.tsx index 1777aca..fb47213 100644 --- a/src/components/nProgress/index.tsx +++ b/src/components/nProgress/index.tsx @@ -1,27 +1,40 @@ import { LinearProgress } from "@mui/material"; import { useNProgress } from "@tanem/react-nprogress"; -import React from "react"; +import { useIsFetching, useIsMutating } from "@tanstack/react-query"; +import { useRouterState } from "@tanstack/react-router"; +import React, { useMemo } from "react"; +import { createPortal } from "react-dom"; + +export default function NProgress() { + const isQueryFetching = useIsFetching(); + const isMutating = useIsMutating(); + const routeIsLoading = useRouterState().isLoading; + + const isAnimating = useMemo(() => { + if (isQueryFetching || isMutating || routeIsLoading) { + return true; + } + return false; + }, [isQueryFetching, isMutating, routeIsLoading]); -const NProgress: React.FC<{ isAnimating: boolean | number }> = ({ - isAnimating, -}) => { const { animationDuration, isFinished, progress } = useNProgress({ isAnimating, }); - if (isAnimating) { - return ( -
+ return createPortal( +
+ {isAnimating && ( = ({ value={progress * 100} variant="determinate" /> -
- ); - } + )} +
, + document.body, + ); }; -export default NProgress; diff --git a/src/components/notices/emptyNotice/emptyNotice.jsx b/src/components/notices/emptyNotice/emptyNotice.tsx similarity index 92% rename from src/components/notices/emptyNotice/emptyNotice.jsx rename to src/components/notices/emptyNotice/emptyNotice.tsx index 5befac6..c8cd7ab 100644 --- a/src/components/notices/emptyNotice/emptyNotice.jsx +++ b/src/components/notices/emptyNotice/emptyNotice.tsx @@ -4,7 +4,7 @@ import Typography from "@mui/material/Typography"; import { yellow } from "@mui/material/colors"; import React from "react"; -export const EmptyNotice = ({ extraMsg }) => { +export const EmptyNotice = ({ extraMsg }: { extraMsg?: string }) => { return ( + + + ); +} \ No newline at end of file diff --git a/src/components/settings/index.tsx b/src/components/settings/index.tsx index fc33384..67e3aaa 100644 --- a/src/components/settings/index.tsx +++ b/src/components/settings/index.tsx @@ -422,10 +422,10 @@ const Settings = () => { icon={ {updateInfo.data?.available - ? "new_release" + ? "release_alert" : "new_releases"} } diff --git a/src/components/updater/index.tsx b/src/components/updater/index.tsx new file mode 100644 index 0000000..2b826d8 --- /dev/null +++ b/src/components/updater/index.tsx @@ -0,0 +1,135 @@ +import { LoadingButton } from "@mui/lab"; +import { + Button, + Chip, + Dialog, + DialogActions, + DialogContent, + DialogContentText, + DialogTitle, + Link, +} from "@mui/material"; +import { relaunch } from "@tauri-apps/plugin-process"; +import { type Update, check } from "@tauri-apps/plugin-updater"; +import { useSnackbar } from "notistack"; +import React, { useLayoutEffect, useState } from "react"; + +export default function Updater() { + const [updateDialog, setUpdateDialog] = useState(false); + const [updateDialogButton, setUpdateDialogButton] = useState(false); + const [updateInfo, setUpdateInfo] = useState(undefined); + + useLayoutEffect(() => { + async function checkForUpdates() { + try { + const update = await check(); + + if (update?.available) { + setUpdateInfo(update); + setUpdateDialog(true); + + console.info(`Update found : ${update.version}, ${update.date}`); + } + } catch (error) { + console.error(error); + } + } + checkForUpdates(); + }, []); + const { enqueueSnackbar } = useSnackbar(); + + if (!updateInfo) return null; + return ( + + + Update Available! + + + update} + label={`v${updateInfo.currentVersion}`} + variant="outlined" + /> + arrow_right_alt + update} + label={`v${updateInfo.version}`} + variant="outlined" + /> + + + Open changelog + + + + + + {/* */} + + { + setUpdateDialogButton(true); + await updateInfo.downloadAndInstall(); + enqueueSnackbar( + "Update has been installed! You need to relaunch JellyPlayer.", + { + variant: "success", + }, + ); + await relaunch(); + }} + > + Update + + + + ); +} \ No newline at end of file diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx index 7e47250..6d96b10 100644 --- a/src/routes/__root.tsx +++ b/src/routes/__root.tsx @@ -3,37 +3,14 @@ import NProgress from "@/components/nProgress"; import AudioPlayer from "@/components/playback/audioPlayer"; import Settings from "@/components/settings"; import { EasterEgg } from "@/components/utils/easterEgg"; -import { useBackdropStore } from "@/utils/store/backdrop"; -import { LoadingButton } from "@mui/lab"; -import { - Button, - Chip, - CircularProgress, - CssBaseline, - Dialog, - DialogActions, - DialogContent, - DialogContentText, - DialogTitle, - IconButton, - SvgIcon, - ThemeProvider, -} from "@mui/material"; -import { useIsFetching, useIsMutating } from "@tanstack/react-query"; +import { CssBaseline, ThemeProvider } from "@mui/material"; import { Outlet, ScrollRestoration, createRootRouteWithContext, - useRouterState, } from "@tanstack/react-router"; -import { TanStackRouterDevtools } from "@tanstack/router-devtools"; -import { relaunch } from "@tauri-apps/plugin-process"; -import { type Update, check } from "@tauri-apps/plugin-updater"; -import { AnimatePresence, motion } from "framer-motion"; -import { SnackbarProvider, useSnackbar } from "notistack"; -import React, { Suspense, useEffect, useLayoutEffect, useState } from "react"; -import Markdown from "react-markdown"; -import remarkGfm from "remark-gfm"; +import { SnackbarProvider } from "notistack"; +import React from "react"; import "../styles/global.scss"; import { theme } from "@/theme"; @@ -43,7 +20,10 @@ import "@fontsource-variable/jetbrains-mono"; import "@fontsource-variable/noto-sans"; import "material-symbols/rounded.scss"; +import Backdrop from "@/components/backdrop"; import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice"; +import RouterLoading from "@/components/routerLoading"; +import Updater from "@/components/updater"; import type { Api, Jellyfin } from "@jellyfin/sdk"; import type { UserDto } from "@jellyfin/sdk/lib/generated-client"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; @@ -60,213 +40,27 @@ type ApiContext = { export const Route = createRootRouteWithContext()({ component: () => { - const isQueryFetching = useIsFetching(); - const isMutating = useIsMutating(); - const routeIsLoading = useRouterState().isLoading; - const [updateDialog, setUpdateDialog] = useState(false); - const [updateDialogButton, setUpdateDialogButton] = useState(false); - const [updateInfo, setUpdateInfo] = useState(undefined); - useLayoutEffect(() => { - async function checkForUpdates() { - try { - const update = await check(); - - if (update?.available) { - setUpdateInfo(update); - setUpdateDialog(true); - - console.log(`Update found : ${update.version}, ${update.date}`); - } - } catch (error) { - console.error(error); - } - } - checkForUpdates(); - }, []); - const { enqueueSnackbar } = useSnackbar(); - const [backdropUrl, backdropId] = useBackdropStore((state) => [ - state.backdropUrl, - state.backdropId, - ]); - - const [backdropLoading, setBackdropLoading] = useState(true); - // Reset BackdropImageLoading for every new image - useEffect(() => { - setBackdropLoading(true); - }, [backdropId]); return ( - Loading...}> - - - - - - - + + + + + + + - {routeIsLoading && ( - - - - )} + - - {updateInfo !== undefined && ( - <> - - Update Available! - - update - - } - label={`v${updateInfo.version}`} - color="success" - /> - - - - - {updateInfo.body} - - - - - - - - Update - - - - - - { - setUpdateDialogButton(true); - await updateInfo.downloadAndInstall(); - enqueueSnackbar( - "Update has been installed! You need to relaunch JellyPlayer.", - { - variant: "success", - }, - ); - await relaunch(); - }} - > - Update - - - - )} - -
- - setBackdropLoading(false)} - loading="lazy" - /> - -
- - - - - {/* */} -
-
-
+ + + + + + + {/* */} + +
); },