Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make app-header work with authentication #28

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/(auth)/sign-in/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function Page() {

<form action="/auth/sign-in" method="POST">
<Flex direction="column">
<Button color="gray" highContrast size="3" type="submit">
<Button highContrast size="3" type="submit">
<FaSpotify />
Continue with Spotify
</Button>
Expand Down
98 changes: 59 additions & 39 deletions components/layout/app-header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { ArrowRightIcon, PersonIcon } from '@radix-ui/react-icons';
import { Avatar, Button, DropdownMenu, Flex, Text } from '@radix-ui/themes';
import {
Avatar,
Button,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuRoot,
DropdownMenuSeparator,
DropdownMenuTrigger,
Flex,
Text,
} from '@radix-ui/themes';
import Link from 'next/link';

import { UnstyledButton } from '../ui/unstyled-button';
import { AppContent } from './app-content';
import { AppHeaderLogo } from './app-header-logo';

Expand All @@ -22,52 +34,60 @@ function AppHeaderActionsAuthenticated(
props: Pick<Exclude<Props, { variant: 'guest' }>, 'user'>,
) {
return (
<Flex align="center" gap="2">
<Flex align="end" direction="column">
<Text size="2" weight="medium">
{props.user.username}
</Text>
<DropdownMenuRoot>
<DropdownMenuTrigger>
<Avatar
fallback={<PersonIcon />}
radius="full"
src={props.user.avatarURL}
/>
</DropdownMenuTrigger>

<Text color="gray" size="2">
{props.user.credits === 0 ? 'No' : props.user.credits} credits
remaining
</Text>
</Flex>
<DropdownMenuContent
align="end"
highContrast
style={{
minWidth: 200,
}}
>
<DropdownMenuLabel asChild>
<Flex align="start" direction="column" height="auto">
<Text color="gray" highContrast weight="medium">
{props.user.username}
</Text>

<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Avatar
fallback={<PersonIcon />}
radius="full"
src={props.user.avatarURL}
/>
</DropdownMenu.Trigger>
<Text color="gray">
{props.user.credits === 0 ? 'No' : props.user.credits} credits
remaining
</Text>
</Flex>
</DropdownMenuLabel>

<DropdownMenu.Content>
<DropdownMenu.Item>Buy credits</DropdownMenu.Item>
<DropdownMenu.Item>Settings</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item color="red">Sign out</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
</Flex>
<DropdownMenuSeparator />

<DropdownMenuItem>Your episodes</DropdownMenuItem>
<DropdownMenuItem>Settings</DropdownMenuItem>

<DropdownMenuSeparator />

<form action="/auth/sign-out" method="POST">
<DropdownMenuItem color="red">
<UnstyledButton type="submit">Sign out</UnstyledButton>
</DropdownMenuItem>
</form>
</DropdownMenuContent>
</DropdownMenuRoot>
);
}

function AppHeaderActionsGuest() {
return (
<Flex gap="2">
<Button asChild variant="soft">
<Link href="/sign-in">Sign in</Link>
</Button>

<Button asChild>
<Link href="/sign-up">
Get started
<ArrowRightIcon />
</Link>
</Button>
</Flex>
<Button asChild highContrast>
<Link href="/sign-in">
altaywtf marked this conversation as resolved.
Show resolved Hide resolved
Get started
<ArrowRightIcon />
</Link>
</Button>
);
}

Expand Down
26 changes: 22 additions & 4 deletions components/layout/app-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import type { PropsWithChildren } from 'react';

import { fetchAccount } from '@/lib/services/account';
import { getIsAuthenticated } from '@/lib/services/supabase/auth';
import { Box, Flex } from '@radix-ui/themes';

import { AppContent } from './app-content';
import { AppFooter } from './app-footer';
import { AppHeader } from './app-header';

export function AppLayout({ children }: PropsWithChildren) {
export async function AppLayout({ children }: PropsWithChildren) {
let appHeader = <AppHeader variant="guest" />;
const isAuthenticated = await getIsAuthenticated();

if (isAuthenticated) {
const account = await fetchAccount();

appHeader = (
<AppHeader
user={{
avatarURL: account.avatar_url || '',
credits: account.ai_credit || 0,
username: account.display_name || '',
}}
variant="authenticated"
/>
);
}

return (
<Flex
direction="column"
style={{
minHeight: '100vh',
}}
>
<header className="sticky">
<AppHeader variant="guest" />
</header>
<header className="sticky">{appHeader}</header>

<main>
<AppContent>
Expand Down
2 changes: 1 addition & 1 deletion components/theme-provider/radix-themes.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Theme } from '@radix-ui/themes';

export function RadixThemesProvider({ children }: PropsWithChildren) {
return (
<Theme accentColor="amber" appearance="dark" grayColor="sand">
<Theme accentColor="gray" appearance="dark">
altaywtf marked this conversation as resolved.
Show resolved Hide resolved
{children}
</Theme>
);
Expand Down
21 changes: 21 additions & 0 deletions components/ui/unstyled-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ComponentProps } from 'react';

export function UnstyledButton(props: ComponentProps<'button'>) {
return (
<button
style={{
background: 'none',
border: 'none',
color: 'inherit',
cursor: 'var(--cursor-button)',
font: 'inherit',
margin: 0,
padding: 0,
textAlign: 'left',
width: '100%',
}}
type="button"
{...props}
/>
);
}
Loading