webapp/src/components/Nav/Header/Header.tsx

544 lines
19 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Topic } from '../../../graphql/schema/core.gen'
import { getPagePath, redirectPage } from '@nanostores/router'
2022-09-28 21:08:14 +00:00
import { clsx } from 'clsx'
2024-02-04 11:25:21 +00:00
import { For, Show, createEffect, createSignal, onCleanup, onMount } from 'solid-js'
import { useLocalize } from '../../../context/localize'
import { useSession } from '../../../context/session'
2024-02-04 11:25:21 +00:00
import { ROUTES, router, useRouter } from '../../../stores/router'
import { useModalStore } from '../../../stores/ui'
import { getDescription } from '../../../utils/meta'
2024-02-04 11:25:21 +00:00
import { SharePopup, getShareUrl } from '../../Article/SharePopup'
import { Icon } from '../../_shared/Icon'
import { Subscribe } from '../../_shared/Subscribe'
2023-08-26 09:41:21 +00:00
import { AuthModal } from '../AuthModal'
import { ConfirmModal } from '../ConfirmModal'
import { HeaderAuth } from '../HeaderAuth'
import { Modal } from '../Modal'
2023-11-30 08:50:29 +00:00
import { SearchModal } from '../SearchModal/SearchModal'
2023-08-26 09:41:21 +00:00
import { Snackbar } from '../Snackbar'
2023-11-30 08:50:29 +00:00
import { Link } from './Link'
2023-11-30 08:50:29 +00:00
2024-03-18 11:07:28 +00:00
import { useTopics } from '../../../context/topics'
import { getRandomTopicsFromArray } from '../../../utils/getRandomTopicsFromArray'
2023-11-30 08:07:31 +00:00
import styles from './Header.module.scss'
2022-09-22 09:37:49 +00:00
type Props = {
title?: string
2023-04-17 10:31:20 +00:00
slug?: string
2022-09-23 21:29:32 +00:00
isHeaderFixed?: boolean
articleBody?: string
cover?: string
2023-04-17 10:31:20 +00:00
scrollToComments?: (value: boolean) => void
2022-09-22 09:37:49 +00:00
}
type HeaderSearchParams = {
source?: string
}
const handleSwitchLanguage = (event) => {
location.href = `${location.href}${location.href.includes('?') ? '&' : '?'}lng=${event.target.value}`
}
2022-09-22 09:37:49 +00:00
export const Header = (props: Props) => {
const { t, lang } = useLocalize()
const { modal } = useModalStore()
const { page } = useRouter()
2024-02-04 17:40:15 +00:00
const { requireAuthentication } = useSession()
const { searchParams } = useRouter<HeaderSearchParams>()
2024-03-18 11:07:28 +00:00
const { topics } = useTopics()
const [randomTopics, setRandomTopics] = createSignal([])
2022-09-15 18:18:07 +00:00
const [getIsScrollingBottom, setIsScrollingBottom] = createSignal(false)
2022-09-15 19:41:05 +00:00
const [getIsScrolled, setIsScrolled] = createSignal(false)
2022-09-09 11:53:35 +00:00
const [fixed, setFixed] = createSignal(false)
2022-10-25 15:36:32 +00:00
const [isSharePopupVisible, setIsSharePopupVisible] = createSignal(false)
const [isProfilePopupVisible, setIsProfilePopupVisible] = createSignal(false)
2023-08-17 20:28:24 +00:00
const [isKnowledgeBaseVisible, setIsKnowledgeBaseVisible] = createSignal(false)
const [isTopicsVisible, setIsTopicsVisible] = createSignal(false)
const [isZineVisible, setIsZineVisible] = createSignal(false)
const [isFeedVisible, setIsFeedVisible] = createSignal(false)
2024-05-06 14:26:03 +00:00
const { session } = useSession()
2024-03-18 11:07:28 +00:00
2024-02-04 17:40:15 +00:00
const toggleFixed = () => setFixed(!fixed())
const tag = (topic: Topic) =>
/[ЁА-яё]/.test(topic.title || '') && lang() !== 'ru' ? topic.slug : topic.title
let windowScrollTop = 0
2024-03-18 11:07:28 +00:00
createEffect(() => {
2024-05-06 10:53:35 +00:00
if (topics()?.length) {
setRandomTopics(getRandomTopicsFromArray(topics()))
}
2024-03-18 11:07:28 +00:00
})
2022-09-09 11:53:35 +00:00
createEffect(() => {
const mainContent = document.querySelector<HTMLDivElement>('.main-content')
if (fixed() || modal() !== null) {
windowScrollTop = window.scrollY
mainContent.style.marginTop = `-${windowScrollTop}px`
}
2022-10-25 16:25:42 +00:00
document.body.classList.toggle('fixed', fixed() || modal() !== null)
2022-10-25 21:17:35 +00:00
document.body.classList.toggle(styles.fixed, fixed() && !modal())
2024-02-04 17:40:15 +00:00
if (!(fixed() || modal())) {
mainContent.style.marginTop = ''
window.scrollTo(0, windowScrollTop)
}
2022-10-25 15:36:32 +00:00
})
2022-09-09 11:53:35 +00:00
2022-09-14 21:07:47 +00:00
onMount(() => {
2022-09-15 18:18:07 +00:00
let scrollTop = window.scrollY
2022-09-14 21:07:47 +00:00
2022-09-15 18:18:07 +00:00
const handleScroll = () => {
setIsScrollingBottom(window.scrollY > scrollTop)
2022-09-15 19:41:05 +00:00
setIsScrolled(window.scrollY > 0)
2022-09-15 18:18:07 +00:00
scrollTop = window.scrollY
}
2022-09-14 21:07:47 +00:00
2022-09-15 18:18:07 +00:00
window.addEventListener('scroll', handleScroll, { passive: true })
onCleanup(() => {
window.removeEventListener('scroll', handleScroll)
})
})
2022-09-14 21:07:47 +00:00
2023-04-20 14:01:15 +00:00
const scrollToComments = (event, value) => {
event.preventDefault()
2023-04-17 10:31:20 +00:00
props.scrollToComments(value)
}
2023-08-13 12:27:30 +00:00
const handleBookmarkButtonClick = (ev) => {
requireAuthentication(() => {
// TODO: implement bookmark clicked
ev.preventDefault()
}, 'bookmark')
}
const handleCreateButtonClick = (ev) => {
requireAuthentication(() => {
ev.preventDefault()
redirectPage(router, 'create')
}, 'create')
}
2023-08-29 06:33:30 +00:00
const toggleSubnavigation = (isShow, signal?) => {
2023-08-28 22:15:31 +00:00
clearTimer()
2023-08-17 20:28:24 +00:00
setIsKnowledgeBaseVisible(false)
setIsTopicsVisible(false)
setIsZineVisible(false)
setIsFeedVisible(false)
2023-08-28 22:15:31 +00:00
if (signal) {
signal(isShow)
}
}
2024-02-04 09:03:15 +00:00
let timer: string | number | NodeJS.Timeout
2023-08-28 22:15:31 +00:00
const clearTimer = () => {
clearTimeout(timer)
}
2024-02-05 15:49:08 +00:00
const hideSubnavigation = (_event, time = 500) => {
2023-08-28 22:15:31 +00:00
timer = setTimeout(() => {
toggleSubnavigation(false)
2023-09-04 20:45:02 +00:00
}, time)
2023-08-17 20:28:24 +00:00
}
const handleToggleMenuByLink = (event: MouseEvent, route: keyof typeof ROUTES) => {
if (!fixed()) {
return
}
event.preventDefault()
if (page().route === route) {
toggleFixed()
}
}
2022-09-09 11:53:35 +00:00
return (
2022-09-15 18:18:07 +00:00
<header
2022-09-28 21:08:14 +00:00
class={styles.mainHeader}
2022-09-15 18:18:07 +00:00
classList={{
2022-09-28 21:08:14 +00:00
[styles.headerFixed]: props.isHeaderFixed,
2022-09-29 20:10:00 +00:00
[styles.headerScrolledTop]: !getIsScrollingBottom() && getIsScrolled(),
[styles.headerScrolledBottom]:
(getIsScrollingBottom() && getIsScrolled() && !isProfilePopupVisible()) || isSharePopupVisible(),
[styles.headerWithTitle]: Boolean(props.title),
2022-09-15 18:18:07 +00:00
}}
>
<Modal
variant={searchParams().source ? 'narrow' : 'wide'}
name="auth"
allowClose={searchParams().source !== 'authguard'}
noPadding={true}
>
2022-09-09 11:53:35 +00:00
<AuthModal />
</Modal>
2022-10-17 20:53:04 +00:00
<Modal variant="narrow" name="confirm">
<ConfirmModal />
</Modal>
2023-11-02 19:21:51 +00:00
<Modal variant="wide" name="search">
<SearchModal />
</Modal>
2022-10-17 20:53:04 +00:00
<div class={clsx(styles.mainHeaderInner, 'wide-container')}>
<nav class={clsx('row', styles.headerInner, { [styles.fixed]: fixed() })}>
2023-10-16 20:11:08 +00:00
<div class={clsx(styles.burgerContainer, 'col-auto')}>
<div class={clsx(styles.burger, { [styles.fixed]: fixed() })} onClick={toggleFixed}>
2023-10-16 20:11:08 +00:00
<div />
</div>
</div>
2023-06-28 20:18:43 +00:00
<div class={clsx('col-md-5 col-xl-4 col-auto', styles.mainLogo)}>
<a href={getPagePath(router, 'home')}>
2022-09-09 11:53:35 +00:00
<img src="/logo.svg" alt={t('Discours')} />
</a>
</div>
2023-09-04 20:45:02 +00:00
<div class={clsx('col col-md-13 col-lg-12 offset-xl-1', styles.mainNavigationWrapper)}>
2022-10-03 21:44:21 +00:00
<Show when={props.title}>
<div class={styles.articleHeader}>{props.title}</div>
</Show>
<div class={clsx(styles.mainNavigation, { [styles.fixed]: fixed() })}>
<ul class="view-switcher">
2023-09-29 06:46:15 +00:00
<Link
onMouseOver={() => toggleSubnavigation(true, setIsZineVisible)}
onMouseOut={() => hideSubnavigation}
routeName="home"
active={isZineVisible()}
2023-10-16 09:54:14 +00:00
body={t('journal')}
onClick={(event) => handleToggleMenuByLink(event, 'home')}
2023-09-29 06:46:15 +00:00
/>
2023-09-25 06:13:47 +00:00
<Link
onMouseOver={() => toggleSubnavigation(true, setIsFeedVisible)}
onMouseOut={() => hideSubnavigation}
routeName="feed"
active={isFeedVisible()}
body={t('feed')}
onClick={(event) => handleToggleMenuByLink(event, 'feed')}
2023-09-25 06:13:47 +00:00
/>
<Link
onMouseOver={() => toggleSubnavigation(true, setIsTopicsVisible)}
onMouseOut={() => hideSubnavigation}
routeName="topics"
2023-09-26 13:24:59 +00:00
active={isTopicsVisible()}
2023-09-25 06:13:47 +00:00
body={t('topics')}
onClick={(event) => handleToggleMenuByLink(event, 'topics')}
2023-09-25 06:13:47 +00:00
/>
<Link
onMouseOver={(event) => hideSubnavigation(event, 0)}
onMouseOut={(event) => hideSubnavigation(event, 0)}
routeName="authors"
body={t('authors')}
onClick={(event) => handleToggleMenuByLink(event, 'authors')}
2023-09-25 06:13:47 +00:00
/>
<Link
onMouseOver={() => toggleSubnavigation(true, setIsKnowledgeBaseVisible)}
onMouseOut={() => hideSubnavigation}
2023-10-17 22:53:29 +00:00
routeName="guide"
2023-09-25 06:13:47 +00:00
body={t('Knowledge base')}
active={isKnowledgeBaseVisible()}
onClick={(event) => handleToggleMenuByLink(event, 'guide')}
2023-09-25 06:13:47 +00:00
/>
</ul>
<div class={styles.mainNavigationMobile}>
2023-10-23 22:35:02 +00:00
<h4>{t('Participating')}</h4>
<ul class="view-switcher">
<li>
<a href="/create">{t('Create post')}</a>
</li>
<li>
2023-10-23 22:35:02 +00:00
<a href="/connect">{t('Suggest an idea')}</a>
</li>
<li>
2023-10-23 22:35:02 +00:00
<a href="/about/help">{t('Support the project')}</a>
</li>
</ul>
2024-02-04 09:03:15 +00:00
<h4>{t('Subscribe us')}</h4>
<ul class="view-switcher">
<li class={styles.mainNavigationSocial}>
<a href="https://www.instagram.com/discoursio/">
<Icon name="user-link-instagram" class={styles.icon} />
2023-10-23 22:35:02 +00:00
Instagram
</a>
</li>
<li class={styles.mainNavigationSocial}>
<a href="https://facebook.com/discoursio">
<Icon name="user-link-facebook" class={styles.icon} />
2023-10-23 22:35:02 +00:00
Facebook
</a>
</li>
<li class={styles.mainNavigationSocial}>
<a href="https://twitter.com/discours_io">
<Icon name="user-link-twitter" class={styles.icon} />
2023-10-23 22:35:02 +00:00
Twitter
</a>
</li>
<li class={styles.mainNavigationSocial}>
<a href="https://t.me/discoursio">
<Icon name="user-link-telegram" class={styles.icon} />
2023-10-23 22:35:02 +00:00
Telegram
</a>
</li>
<li class={styles.mainNavigationSocial}>
<a href="https://dzen.ru/discoursio">
<Icon name="user-link-dzen" class={styles.icon} />
2023-10-23 22:35:02 +00:00
Dzen
</a>
</li>
<li class={styles.mainNavigationSocial}>
<a href="https://vk.com/discoursio">
<Icon name="user-link-vk" class={styles.icon} />
2023-10-23 22:35:02 +00:00
VK
</a>
</li>
</ul>
<h4>{t('Newsletter')}</h4>
2023-10-21 12:45:07 +00:00
<Subscribe variant={'mobileSubscription'} />
2023-10-23 22:35:02 +00:00
<h4>{t('Language')}</h4>
<select
class={styles.languageSelectorMobile}
onChange={handleSwitchLanguage}
value={lang()}
>
2023-10-23 22:35:02 +00:00
<option value="ru">🇷🇺 Русский</option>
<option value="en">🇬🇧 English</option>
</select>
<div class={styles.mainNavigationAdditionalLinks}>
<a href="/about/dogma">{t('Dogma')}</a>
<a href="/about/discussion-rules" innerHTML={t('Discussion rules')} />
<a href="/about/principles">{t('Principles')}</a>
</div>
<p
class={styles.mobileDescription}
innerHTML={t(
'Independant magazine with an open horizontal cooperation about culture, science and society',
)}
/>
<div class={styles.mobileCopyright}>
{t('Discours')} &copy; 2015&ndash;{new Date().getFullYear()}{' '}
</div>
</div>
</div>
2022-09-15 19:41:05 +00:00
</div>
<HeaderAuth setIsProfilePopupVisible={setIsProfilePopupVisible} />
<Show when={props.title}>
2024-04-30 13:26:31 +00:00
<div
class={clsx(styles.articleControls, 'col-auto', {
2024-05-06 14:26:03 +00:00
[styles.articleControlsAuthorized]: session()?.user?.id,
2024-04-30 13:26:31 +00:00
})}
>
<SharePopup
title={props.title}
imageUrl={props.cover}
2023-01-31 13:58:28 +00:00
shareUrl={getShareUrl()}
description={getDescription(props.articleBody)}
onVisibilityChange={(isVisible) => {
setIsSharePopupVisible(isVisible)
}}
containerCssClass={styles.control}
2023-07-09 18:34:59 +00:00
trigger={
<>
<Icon name="share-outline" class={styles.icon} />
<Icon name="share-outline-hover" class={clsx(styles.icon, styles.iconHover)} />
</>
}
/>
2023-04-20 14:01:15 +00:00
<div onClick={(event) => scrollToComments(event, true)} class={styles.control}>
2023-07-09 18:34:59 +00:00
<Icon name="comment" class={styles.icon} />
<Icon name="comment-hover" class={clsx(styles.icon, styles.iconHover)} />
2023-04-17 10:31:20 +00:00
</div>
2024-02-04 09:03:15 +00:00
<button class={styles.control} onClick={handleCreateButtonClick}>
2022-12-04 15:10:27 +00:00
<Icon name="pencil-outline" class={styles.icon} />
2023-07-09 18:34:59 +00:00
<Icon name="pencil-outline-hover" class={clsx(styles.icon, styles.iconHover)} />
2024-02-04 09:03:15 +00:00
</button>
<button class={styles.control} onClick={handleBookmarkButtonClick}>
2022-12-04 15:10:27 +00:00
<Icon name="bookmark" class={styles.icon} />
2023-07-09 18:34:59 +00:00
<Icon name="bookmark-hover" class={clsx(styles.icon, styles.iconHover)} />
2024-02-04 09:03:15 +00:00
</button>
2022-09-09 11:53:35 +00:00
</div>
</Show>
2023-08-17 20:28:24 +00:00
2023-08-28 22:15:31 +00:00
<div
class={clsx(styles.subnavigation, 'col')}
classList={{ hidden: !isKnowledgeBaseVisible() }}
onMouseOver={clearTimer}
onMouseOut={hideSubnavigation}
>
2023-08-17 20:28:24 +00:00
<ul class="nodash">
<li>
2023-10-16 09:54:14 +00:00
<a href="/about/manifest">{t('Manifesto')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-16 09:54:14 +00:00
<a href="/about/dogma">{t('Dogma')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-16 09:54:14 +00:00
<a href="/about/principles">{t('Community Principles')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-16 09:54:14 +00:00
<a href="/about/guide">{t('Platform Guide')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
<a href="/about/manifest#participation">{t('Support us')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
<a href="/about/help">{t('How to help')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li class={styles.rightItem}>
<a href="/connect">
{t('Suggest an idea')}
<Icon name="arrow-right-black" class={clsx(styles.icon, styles.rightItemIcon)} />
</a>
</li>
</ul>
</div>
2023-08-28 22:15:31 +00:00
<div
class={clsx(styles.subnavigation, 'col')}
classList={{ hidden: !isZineVisible() }}
onMouseOver={clearTimer}
onMouseOut={hideSubnavigation}
>
2023-08-17 20:28:24 +00:00
<ul class="nodash">
2023-09-18 20:28:39 +00:00
<li class="item">
2023-10-10 15:38:02 +00:00
<a href="/expo">{t('Art')}</a>
2023-09-18 20:28:39 +00:00
</li>
<li class="item">
2023-10-30 11:20:33 +00:00
<a href="/podcasts">{t('Podcasts')}</a>
2023-09-18 20:28:39 +00:00
</li>
<li class="item">
2024-02-04 09:03:15 +00:00
<a href="/about/projects">{t('Special Projects')}</a>
2023-09-18 20:28:39 +00:00
</li>
2023-08-17 20:28:24 +00:00
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/interview">#{t('Interview')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/reportage">#{t('Reports')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/empiric">#{t('Experience')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/society">#{t('Society')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/culture">#{t('Culture')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/theory">#{t('Theory')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li>
2023-10-30 11:20:33 +00:00
<a href="/topic/poetry">#{t('Poetry')}</a>
2023-08-17 20:28:24 +00:00
</li>
<li class={styles.rightItem}>
<a href="/topics">
{t('All topics')}
<Icon name="arrow-right-black" class={clsx(styles.icon, styles.rightItemIcon)} />
</a>
</li>
</ul>
</div>
2023-08-28 22:15:31 +00:00
<div
class={clsx(styles.subnavigation, 'col')}
classList={{ hidden: !isTopicsVisible() }}
onMouseOver={clearTimer}
onMouseOut={hideSubnavigation}
>
2023-08-17 20:28:24 +00:00
<ul class="nodash">
<Show when={randomTopics().length > 0}>
<For each={randomTopics()}>
{(topic) => (
<li class="item">
<a href={`/topic/${topic.slug}`}>
<span>#{tag(topic)}</span>
</a>
</li>
)}
</For>
<li class={styles.rightItem}>
<a href="/topics">
{t('All topics')}
<Icon name="arrow-right-black" class={clsx(styles.icon, styles.rightItemIcon)} />
</a>
</li>
</Show>
2023-08-17 20:28:24 +00:00
</ul>
</div>
<div
class={clsx(styles.subnavigation, styles.subnavigationFeed, 'col')}
classList={{ hidden: !isFeedVisible() }}
2023-08-28 22:15:31 +00:00
onMouseOver={clearTimer}
onMouseOut={hideSubnavigation}
2023-08-17 20:28:24 +00:00
>
<ul class="nodash">
<li>
<a href={getPagePath(router, 'feed')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="feed-all" class={styles.icon} />
2023-10-30 11:20:33 +00:00
{t('All')}
2023-08-17 20:28:24 +00:00
</span>
</a>
</li>
<li>
<a href={getPagePath(router, 'feedMy')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="feed-my" class={styles.icon} />
{t('My feed')}
</span>
</a>
</li>
<li>
<a href={getPagePath(router, 'feedCollaborations')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="feed-collaborate" class={styles.icon} />
2023-10-30 11:20:33 +00:00
{t('Participation')}
2023-08-17 20:28:24 +00:00
</span>
</a>
</li>
<li>
<a href={getPagePath(router, 'feedDiscussions')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="feed-discussion" class={styles.icon} />
{t('Discussions')}
</span>
</a>
</li>
<li>
<a href={getPagePath(router, 'feedBookmarks')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="bookmark" class={styles.icon} />
{t('Bookmarks')}
</span>
</a>
</li>
<li>
<a href={getPagePath(router, 'feedNotifications')}>
2023-08-17 20:28:24 +00:00
<span class={styles.subnavigationItemName}>
<Icon name="feed-notifications" class={styles.icon} />
{t('Notifications')}
</span>
</a>
</li>
</ul>
</div>
2022-09-09 11:53:35 +00:00
</nav>
<Snackbar />
2022-09-09 11:53:35 +00:00
</div>
</header>
)
}