From 54d0b0766653892179b04365985e9b83632df6d2 Mon Sep 17 00:00:00 2001 From: Prayag Prajapati Date: Mon, 7 Jul 2025 17:38:29 +0530 Subject: [PATCH] refactor: cleanup appBar hide logic --- src/components/appBar/appBar.tsx | 45 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/components/appBar/appBar.tsx b/src/components/appBar/appBar.tsx index 81a8b4c..ee3e3a9 100644 --- a/src/components/appBar/appBar.tsx +++ b/src/components/appBar/appBar.tsx @@ -12,7 +12,6 @@ import { useLocation, useNavigate, useRouter } from "@tanstack/react-router"; import React, { type MouseEventHandler, useCallback, - useEffect, useMemo, useState, } from "react"; @@ -35,17 +34,28 @@ import { getTypeIcon } from "../utils/iconsCollection"; const MemoizeBackButton = React.memo(BackButton); +const HIDDEN_PATHS = [ + "/login", + "/setup", + "/server", + "/player", + "/error", + "/settings", +]; + export const AppBar = () => { const api = useApiInContext((s) => s.api); const createApi = useApiInContext((s) => s.createApi); const navigate = useNavigate(); - - const [display, setDisplay] = useState(false); - const location = useLocation(); const router = useRouter(); + const display = !HIDDEN_PATHS.some( + (path) => location.pathname.startsWith(path) || location.pathname === "/", + ); + + const [user, resetCurrentUser] = useCentralStore((s) => [ s.currentUser, s.resetCurrentUser, @@ -83,7 +93,7 @@ export const AppBar = () => { }, []); const queryClient = useQueryClient(); - const handleLogout = async () => { + const handleLogout = useCallback(async () => { console.log("Logging out user..."); await api?.logout(); createApi(api?.basePath ?? "", undefined); @@ -91,27 +101,12 @@ export const AppBar = () => { resetCurrentUser(); delUser(); sessionStorage.removeItem("accessToken"); - await router.invalidate(); queryClient.clear(); + await router.invalidate(); setAnchorEl(null); - navigate({ to: "/login" }); - }; + navigate({ to: "/login", replace: true }); + }, [api]); - useEffect(() => { - if ( - location.pathname.includes("login") || - location.pathname.includes("setup") || - location.pathname.includes("server") || - location.pathname === "/player" || - location.pathname.includes("error") || - location.pathname === "/" || - location.pathname.includes("settings") - ) { - setDisplay(false); - } else { - setDisplay(true); - } - }, [location]); const [showDrawer, setShowDrawer] = useState(false); @@ -153,7 +148,7 @@ export const AppBar = () => { const menuButtonSx = useMemo(() => ({ p: 0 }), []); if (!display) { - return <>; + return null; } if (display) { return ( @@ -271,7 +266,7 @@ export const AppBar = () => {