webapp/src/components/Nav/HeaderAuth.tsx

108 lines
3.7 KiB
TypeScript
Raw Normal View History

import styles from './Header.module.scss'
import { clsx } from 'clsx'
import { useRouter } from '../../stores/router'
import { t } from '../../utils/intl'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-11-15 13:55:00 +00:00
import { createSignal, Show } from 'solid-js'
import Notifications from './Notifications'
import { ProfilePopup } from './ProfilePopup'
import Userpic from '../Author/Userpic'
import type { Author } from '../../graphql/types.gen'
import { showModal, useWarningsStore } from '../../stores/ui'
import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
type HeaderAuthProps = {
setIsProfilePopupVisible: (value: boolean) => void
}
export const HeaderAuth = (props: HeaderAuthProps) => {
const { page } = useRouter()
const [visibleWarnings, setVisibleWarnings] = createSignal(false)
const { warnings } = useWarningsStore()
2022-11-14 10:02:08 +00:00
const { session, isAuthenticated } = useSession()
const toggleWarnings = () => setVisibleWarnings(!visibleWarnings())
const handleBellIconClick = (event: Event) => {
event.preventDefault()
if (!isAuthenticated()) {
showModal('auth')
return
}
toggleWarnings()
}
return (
<ShowOnlyOnClient>
<Show when={!session.loading}>
<div class={styles.usernav}>
<div class={clsx(styles.userControl, styles.userControl, 'col')}>
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose)}>
<a href="/create">
<span class={styles.textLabel}>{t('Create post')}</span>
2022-11-27 06:33:05 +00:00
<Icon name="feather" class={styles.icon} />
</a>
</div>
<Show when={isAuthenticated()}>
<div class={styles.userControlItem}>
<a href="#" onClick={handleBellIconClick}>
<div>
<Icon name="bell-white" counter={isAuthenticated() ? warnings().length : 1} />
</div>
</a>
</div>
</Show>
<Show when={visibleWarnings()}>
<div class={clsx(styles.userControlItem, 'notifications')}>
<Notifications />
</div>
</Show>
<Show
when={isAuthenticated()}
fallback={
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose, 'loginbtn')}>
<a href="?modal=auth&mode=login">
<span class={styles.textLabel}>{t('Enter')}</span>
2022-11-26 16:51:08 +00:00
<Icon name="user-default" class={styles.icon} />
</a>
</div>
}
>
<div class={clsx(styles.userControlItem, styles.userControlItemInbox)}>
<a href="/inbox">
{/*FIXME: replace with route*/}
<div classList={{ entered: page().path === '/inbox' }}>
<Icon name="inbox-white" counter={session()?.news?.unread || 0} />
</div>
</a>
</div>
<ProfilePopup
onVisibilityChange={(isVisible) => {
props.setIsProfilePopupVisible(isVisible)
}}
containerCssClass={styles.control}
trigger={
<div class={styles.userControlItem}>
<button class={styles.button}>
<div classList={{ entered: page().path === `/${session().user?.slug}` }}>
<Userpic user={session().user as Author} class={styles.userpic} />
</div>
</button>
</div>
}
/>
</Show>
</div>
</div>
</Show>
</ShowOnlyOnClient>
)
}