From aa6ce9f40eedbe00893a1700dd57cb7613c4af6f Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Fri, 28 Mar 2025 09:35:17 -0600 Subject: [PATCH] hide setting if a user is not valid --- web/src/app/app/layout.tsx | 6 ++- .../features/sidebars/SidebarPrimary.tsx | 54 +++++++++++-------- .../Users/BusterUserConfigProvider.tsx | 5 +- web/src/middleware.ts | 7 ++- web/src/middleware/pathnameMiddleware.ts | 8 +++ 5 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 web/src/middleware/pathnameMiddleware.ts diff --git a/web/src/app/app/layout.tsx b/web/src/app/app/layout.tsx index c1d0b3050..322259af0 100644 --- a/web/src/app/app/layout.tsx +++ b/web/src/app/app/layout.tsx @@ -7,12 +7,15 @@ import { ClientRedirect } from '../../components/ui/layouts/ClientRedirect'; import { prefetchGetMyUserInfo } from '@/api/buster_rest'; import { getSupabaseUserContext } from '@/lib/supabase'; import { AppProviders } from '@/context/AppProviders'; +import { headers } from 'next/headers'; export default async function Layout({ children }: Readonly<{ children: React.ReactNode; }>) { + const headersList = headers(); + const pathname = headersList.get('x-pathname'); const supabaseContext = await getSupabaseUserContext(); const { accessToken } = supabaseContext; const { initialData: userInfo, queryClient } = await prefetchGetMyUserInfo({ @@ -23,7 +26,8 @@ export default async function Layout({ if ( (!userInfo?.organizations?.[0]?.id || !userInfo?.user?.name) && - !supabaseContext.user?.is_anonymous + !supabaseContext.user?.is_anonymous && + pathname !== newUserRoute ) { return ; } diff --git a/web/src/components/features/sidebars/SidebarPrimary.tsx b/web/src/components/features/sidebars/SidebarPrimary.tsx index c0696f022..704bb461d 100644 --- a/web/src/components/features/sidebars/SidebarPrimary.tsx +++ b/web/src/components/features/sidebars/SidebarPrimary.tsx @@ -120,6 +120,7 @@ const tryGroup = ( export const SidebarPrimary = React.memo(() => { const isAdmin = useUserConfigContextSelector((x) => x.isAdmin); + const isUserRegistered = useUserConfigContextSelector((x) => x.isUserRegistered); const { data: favorites } = useGetUserFavorites(); const currentRoute = useAppLayoutContextSelector((x) => x.currentRoute); const onToggleInviteModal = useInviteModalStore((s) => s.onToggleInviteModal); @@ -132,6 +133,8 @@ export const SidebarPrimary = React.memo(() => { }); const sidebarItems: SidebarProps['content'] = useMemo(() => { + if (!isUserRegistered) return []; + const items = [topItems]; if (isAdmin) { @@ -147,11 +150,14 @@ export const SidebarPrimary = React.memo(() => { items.push(tryGroup(onToggleInviteModal, () => setOpenSupportModal(true))); return items; - }, [isAdmin, favorites, currentRoute, onFavoritesReorder]); + }, [isAdmin, isUserRegistered, favorites, currentRoute, onFavoritesReorder]); const onCloseSupportModal = useMemoizedFn(() => setOpenSupportModal(false)); - const HeaderMemoized = useMemo(() => , []); + const HeaderMemoized = useMemo( + () => , + [isUserRegistered] + ); const FooterMemoized = useMemo(() => , []); return ( @@ -170,7 +176,7 @@ export const SidebarPrimary = React.memo(() => { SidebarPrimary.displayName = 'SidebarPrimary'; -const SidebarPrimaryHeader: React.FC = () => { +const SidebarPrimaryHeader: React.FC<{ hideActions?: boolean }> = ({ hideActions = false }) => { const onChangePage = useAppLayoutContextSelector((s) => s.onChangePage); useHotkeys('C', () => { onChangePage(BusterRoutes.APP_HOME); @@ -179,26 +185,28 @@ const SidebarPrimaryHeader: React.FC = () => { return (
-
- - -
- } - /> - - -
+ {!hideActions && ( +
+ + +
+ } + /> + + + + )} ); }; diff --git a/web/src/context/Users/BusterUserConfigProvider.tsx b/web/src/context/Users/BusterUserConfigProvider.tsx index 9e20446d7..9d7e56605 100644 --- a/web/src/context/Users/BusterUserConfigProvider.tsx +++ b/web/src/context/Users/BusterUserConfigProvider.tsx @@ -15,7 +15,10 @@ export const useUserConfigProvider = () => { const userOrganizations = userResponse?.organizations?.[0]; const userRole = userOrganizations?.role; const isUserRegistered = - !!userResponse && !!userResponse?.organizations?.[0]?.id && !!userResponse?.user?.name; + !!userResponse && + !!userResponse?.organizations?.[0]?.id && + !!userResponse?.user?.name && + !isAnonymousUser; const isAdmin = checkIfUserIsAdmin(userResponse); diff --git a/web/src/middleware.ts b/web/src/middleware.ts index 3ea1b4142..9c9e02e5c 100644 --- a/web/src/middleware.ts +++ b/web/src/middleware.ts @@ -1,10 +1,13 @@ import { NextResponse, type NextRequest } from 'next/server'; import { updateSession } from '@/middleware/supabaseMiddleware'; import { isPublicPage, BusterRoutes, createBusterRoute } from './routes'; +import { pathnameMiddleware } from './middleware/pathnameMiddleware'; export async function middleware(request: NextRequest) { try { - const [supabaseResponse, user] = await updateSession(request); + let [response, user] = await updateSession(request); + + response = await pathnameMiddleware(request, response); if ((!user || !user.id) && !isPublicPage(request)) { return NextResponse.redirect( @@ -12,7 +15,7 @@ export async function middleware(request: NextRequest) { ); } - return supabaseResponse; + return response; } catch (error) { console.error('Error in middleware:', error); return NextResponse.next(); diff --git a/web/src/middleware/pathnameMiddleware.ts b/web/src/middleware/pathnameMiddleware.ts new file mode 100644 index 000000000..62b4fe98f --- /dev/null +++ b/web/src/middleware/pathnameMiddleware.ts @@ -0,0 +1,8 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export async function pathnameMiddleware(request: NextRequest, resonse: NextResponse) { + const pathname = request.nextUrl.pathname; + const response = NextResponse.next(); + response.headers.set('x-pathname', pathname); + return response; +}