webapp/src/components/Nav/HeaderAuth.tsx

209 lines
7.0 KiB
TypeScript
Raw Normal View History

import styles from './Header.module.scss'
import { clsx } from 'clsx'
2023-03-23 17:15:50 +00:00
import { router, useRouter } from '../../stores/router'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
import { createEffect, createMemo, createSignal, onCleanup, onMount, Show } from 'solid-js'
import Notifications from './Notifications'
import { ProfilePopup } from './ProfilePopup'
import Userpic from '../Author/Userpic'
import { showModal, useWarningsStore } from '../../stores/ui'
import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import { getPagePath } from '@nanostores/router'
import { Button } from '../_shared/Button'
2023-05-03 16:13:48 +00:00
import { useEditorContext } from '../../context/editor'
import { Popover } from '../_shared/Popover'
type HeaderAuthProps = {
setIsProfilePopupVisible: (value: boolean) => void
}
type IconedButton = {
value: string
icon: string
action: () => void
}
const MD_WIDTH_BREAKPOINT = 992
export const HeaderAuth = (props: HeaderAuthProps) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
const { page } = useRouter()
const [visibleWarnings, setVisibleWarnings] = createSignal(false)
const { warnings } = useWarningsStore()
2022-12-06 16:03:55 +00:00
const { session, isSessionLoaded, isAuthenticated } = useSession()
2023-05-03 16:13:48 +00:00
const {
2023-05-05 20:05:50 +00:00
actions: { toggleEditorPanel, saveShout, publishShout }
2023-05-03 16:13:48 +00:00
} = useEditorContext()
const toggleWarnings = () => setVisibleWarnings(!visibleWarnings())
const handleBellIconClick = (event: Event) => {
event.preventDefault()
if (!isAuthenticated()) {
showModal('auth')
return
}
toggleWarnings()
}
2023-05-03 16:13:48 +00:00
const isEditorPage = createMemo(
() => page().route === 'create' || page().route === 'edit' || page().route === 'editSettings'
)
2023-04-20 13:58:56 +00:00
const showNotifications = createMemo(() => isAuthenticated() && !isEditorPage())
const showSaveButton = createMemo(() => isAuthenticated() && isEditorPage())
const showCreatePostButton = createMemo(() => isAuthenticated() && !isEditorPage())
2023-05-05 20:05:50 +00:00
const showAuthenticatedControls = createMemo(() => isAuthenticated())
2023-04-20 13:58:56 +00:00
2023-05-01 18:32:32 +00:00
const handleBurgerButtonClick = () => {
2023-05-03 16:13:48 +00:00
toggleEditorPanel()
2023-05-01 18:32:32 +00:00
}
2023-05-08 17:21:06 +00:00
const handleSaveButtonClick = () => {
saveShout()
}
const handlePublishButtonClick = () => {
publishShout()
2023-05-05 20:05:50 +00:00
}
const [width, setWidth] = createSignal(0)
const handleResize = () => setWidth(window.innerWidth)
onMount(() => {
handleResize()
window.addEventListener('resize', handleResize)
onCleanup(() => window.removeEventListener('resize', handleResize))
})
const renderIconedButton = (iconedButtonProps: IconedButton) => {
return (
<Show
when={width() < MD_WIDTH_BREAKPOINT}
fallback={
<Button
value={<span class={styles.textLabel}>{iconedButtonProps.value}</span>}
variant={'outline'}
onClick={handleSaveButtonClick}
/>
}
>
<Popover content={iconedButtonProps.value}>
{(ref) => (
<Button
ref={ref}
variant={'outline'}
onClick={handleSaveButtonClick}
value={<Icon name={iconedButtonProps.icon} class={styles.icon} />}
/>
)}
</Popover>
</Show>
)
}
return (
<ShowOnlyOnClient>
2023-02-17 09:21:02 +00:00
<Show when={isSessionLoaded()} keyed={true}>
2023-03-10 17:42:48 +00:00
<div class={clsx(styles.usernav, 'col')}>
<div class={clsx(styles.userControl, styles.userControl, 'col')}>
2023-04-20 13:58:56 +00:00
<Show when={showCreatePostButton()}>
2023-03-23 17:15:50 +00:00
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose)}>
<a href={getPagePath(router, 'create')}>
<span class={styles.textLabel}>{t('Create post')}</span>
<Icon name="pencil" class={styles.icon} />
</a>
</div>
</Show>
2023-04-20 13:58:56 +00:00
<Show when={showNotifications()}>
<div class={styles.userControlItem}>
<a href="#" onClick={handleBellIconClick}>
<div>
<Icon name="bell-white" counter={isAuthenticated() ? warnings().length : 1} />
</div>
</a>
</div>
</Show>
2023-04-20 13:58:56 +00:00
<Show when={showSaveButton()}>
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose)}>
{renderIconedButton({
value: t('Save'),
icon: 'save',
action: handleSaveButtonClick
})}
</div>
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose)}>
{renderIconedButton({
value: t('Publish'),
icon: 'publish',
action: handlePublishButtonClick
})}
</div>
2023-04-06 21:40:34 +00:00
<div class={clsx(styles.userControlItem, styles.userControlItemVerbose)}>
<Popover content={t('Settings')}>
{(ref) => (
<Button
ref={ref}
value={<Icon name="burger" />}
variant={'outline'}
onClick={handleBurgerButtonClick}
/>
)}
</Popover>
2023-04-06 21:40:34 +00:00
</div>
</Show>
<Show when={visibleWarnings()}>
<div class={clsx(styles.userControlItem, 'notifications')}>
<Notifications />
</div>
</Show>
<Show
2023-05-04 12:16:39 +00:00
when={showAuthenticatedControls()}
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}` }}>
2023-04-20 13:58:56 +00:00
<Userpic user={session().user} class={styles.userpic} />
</div>
</button>
</div>
}
/>
</Show>
</div>
</div>
</Show>
</ShowOnlyOnClient>
)
}