hide setting if a user is not valid

This commit is contained in:
Nate Kelley 2025-03-28 09:35:17 -06:00
parent c6adb313c3
commit aa6ce9f40e
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
5 changed files with 53 additions and 27 deletions

View File

@ -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 <ClientRedirect to={newUserRoute} />;
}

View File

@ -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(() => <SidebarPrimaryHeader />, []);
const HeaderMemoized = useMemo(
() => <SidebarPrimaryHeader hideActions={!isUserRegistered} />,
[isUserRegistered]
);
const FooterMemoized = useMemo(() => <SidebarUserFooter />, []);
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 (
<div className="flex items-center justify-between">
<BusterLogoWithText />
<div className="flex items-center gap-2">
<Tooltip title="Settings">
<Link href={createBusterRoute({ route: BusterRoutes.SETTINGS_PROFILE })}>
<Button prefix={<Gear />} variant="ghost" />
</Link>
</Tooltip>
<Tooltip title="Start a chat" shortcuts={['C']}>
<Link href={createBusterRoute({ route: BusterRoutes.APP_HOME })}>
<Button
size="tall"
rounding={'large'}
prefix={
<div className="flex items-center justify-center">
<PencilSquareIcon />
</div>
}
/>
</Link>
</Tooltip>
</div>
{!hideActions && (
<div className="flex items-center gap-2">
<Tooltip title="Settings">
<Link href={createBusterRoute({ route: BusterRoutes.SETTINGS_PROFILE })}>
<Button prefix={<Gear />} variant="ghost" />
</Link>
</Tooltip>
<Tooltip title="Start a chat" shortcuts={['C']}>
<Link href={createBusterRoute({ route: BusterRoutes.APP_HOME })}>
<Button
size="tall"
rounding={'large'}
prefix={
<div className="flex items-center justify-center">
<PencilSquareIcon />
</div>
}
/>
</Link>
</Tooltip>
</div>
)}
</div>
);
};

View File

@ -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);

View File

@ -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();

View File

@ -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;
}