This commit adds a fallback mechanism to handle `null` user values in the site header account section. Previously, if `user.data` was `null`, the code could fail. Now, it checks for valid `user.data` before proceeding. For the layout section, it now includes a separate condition for 'light' theme, providing a clear distinction between 'dark'
85 lines
2.0 KiB
TypeScript
85 lines
2.0 KiB
TypeScript
'use client';
|
|
|
|
import Link from 'next/link';
|
|
|
|
import type { User } from '@supabase/supabase-js';
|
|
|
|
import { ChevronRight } from 'lucide-react';
|
|
|
|
import { PersonalAccountDropdown } from '@kit/accounts/personal-account-dropdown';
|
|
import { useSignOut } from '@kit/supabase/hooks/use-sign-out';
|
|
import { useUser } from '@kit/supabase/hooks/use-user';
|
|
import { Button } from '@kit/ui/button';
|
|
import { ModeToggle } from '@kit/ui/mode-toggle';
|
|
import { Trans } from '@kit/ui/trans';
|
|
|
|
import featuresFlagConfig from '~/config/feature-flags.config';
|
|
import pathsConfig from '~/config/paths.config';
|
|
|
|
const paths = {
|
|
home: pathsConfig.app.home,
|
|
};
|
|
|
|
const features = {
|
|
enableThemeToggle: featuresFlagConfig.enableThemeToggle,
|
|
};
|
|
|
|
export function SiteHeaderAccountSection({
|
|
user,
|
|
}: React.PropsWithChildren<{
|
|
user: User | null;
|
|
}>) {
|
|
if (!user) {
|
|
return <AuthButtons />;
|
|
}
|
|
|
|
return <SuspendedPersonalAccountDropdown user={user} />;
|
|
}
|
|
|
|
function SuspendedPersonalAccountDropdown(props: { user: User | null }) {
|
|
const signOut = useSignOut();
|
|
const user = useUser(props.user);
|
|
const userData = user.data ?? props.user ?? null;
|
|
|
|
if (userData) {
|
|
return (
|
|
<PersonalAccountDropdown
|
|
paths={paths}
|
|
features={features}
|
|
user={userData}
|
|
signOutRequested={() => signOut.mutateAsync()}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return <AuthButtons />;
|
|
}
|
|
|
|
function AuthButtons() {
|
|
return (
|
|
<div className={'flex space-x-2'}>
|
|
<div className={'hidden space-x-0.5 md:flex'}>
|
|
<ModeToggle />
|
|
|
|
<Link href={pathsConfig.auth.signIn}>
|
|
<Button variant={'link'}>
|
|
<Trans i18nKey={'auth:signIn'} />
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
|
|
<Link href={pathsConfig.auth.signUp}>
|
|
<Button
|
|
variant={'ghost'}
|
|
className={
|
|
'border border-primary hover:bg-primary hover:text-primary-foreground'
|
|
}
|
|
>
|
|
<Trans i18nKey={'auth:getStarted'} />
|
|
<ChevronRight className={'h-4'} />
|
|
</Button>
|
|
</Link>
|
|
</div>
|
|
);
|
|
}
|