import { clsx } from 'clsx' import { For, Show, createEffect, createMemo, createSignal, onCleanup, onMount } from 'solid-js' import { useUI } from '~/context/ui' import { useLocalize } from '../../../context/localize' import { useSession } from '../../../context/session' import { useTopics } from '../../../context/topics' import type { Topic } from '../../../graphql/schema/core.gen' import { getRandomTopicsFromArray } from '../../../utils/getRandomTopicsFromArray' import { getDescription } from '../../../utils/meta' import { SharePopup, getShareUrl } from '../../Article/SharePopup' import { Icon } from '../../_shared/Icon' import { Newsletter } from '../../_shared/Newsletter' import { AuthModal } from '../AuthModal' import { ConfirmModal } from '../ConfirmModal' import { HeaderAuth } from '../HeaderAuth' import { Modal } from '../Modal' import { SearchModal } from '../SearchModal/SearchModal' import { Snackbar } from '../Snackbar' import { A, useLocation, useNavigate, useSearchParams } from '@solidjs/router' import styles from './Header.module.scss' type Props = { title?: string slug?: string isHeaderFixed?: boolean articleBody?: string cover?: string scrollToComments?: (value: boolean) => void } type HeaderSearchParams = { source?: string } const handleSwitchLanguage = (value: string) => { location.href = `${location.href}${location.href.includes('?') ? '&' : '?'}lng=${value}` } export const Header = (props: Props) => { const { t, lang } = useLocalize() const { modal } = useUI() const navigate = useNavigate() const [searchParams] = useSearchParams() const { requireAuthentication } = useSession() const { sortedTopics } = useTopics() const topics = createMemo(() => sortedTopics()) const [randomTopics, setRandomTopics] = createSignal([]) const [getIsScrollingBottom, setIsScrollingBottom] = createSignal(false) const [getIsScrolled, setIsScrolled] = createSignal(false) const [fixed, setFixed] = createSignal(false) const [isSharePopupVisible, setIsSharePopupVisible] = createSignal(false) const [isProfilePopupVisible, setIsProfilePopupVisible] = createSignal(false) const [isKnowledgeBaseVisible, setIsKnowledgeBaseVisible] = createSignal(false) const [isTopicsVisible, setIsTopicsVisible] = createSignal(false) const [isZineVisible, setIsZineVisible] = createSignal(false) const [isFeedVisible, setIsFeedVisible] = createSignal(false) const { session } = useSession() const toggleFixed = () => setFixed(!fixed()) const tag = (topic: Topic) => /[ะะ-ัั‘]/.test(topic.title || '') && lang() !== 'ru' ? topic.slug : topic.title let windowScrollTop = 0 createEffect(() => { if (topics()?.length) { const rt: Topic[] = getRandomTopicsFromArray(topics()) setRandomTopics(rt) } }) createEffect(() => { const mainContent = document.querySelector('.main-content') if ((window && fixed()) || modal() !== null) { windowScrollTop = window.scrollY if (mainContent) { mainContent.style.marginTop = `-${windowScrollTop}px` } } document.body.classList.toggle('fixed', fixed() || modal() !== null) document.body.classList.toggle(styles.fixed, fixed() && !modal()) if (!(fixed() || modal())) { if (mainContent) { mainContent.style.marginTop = '' } window.scrollTo(0, windowScrollTop) } }) onMount(() => { let scrollTop = window.scrollY const handleScroll = () => { setIsScrollingBottom(window.scrollY > scrollTop) setIsScrolled(window.scrollY > 0) scrollTop = window.scrollY } window.addEventListener('scroll', handleScroll, { passive: true }) onCleanup(() => { window.removeEventListener('scroll', handleScroll) }) }) const scrollToComments = (event: MouseEvent | undefined, value: boolean) => { event?.preventDefault() props.scrollToComments?.(value) } const handleBookmarkButtonClick = (ev: MouseEvent | undefined) => { requireAuthentication(() => { // TODO: implement bookmark clicked ev?.preventDefault() }, 'bookmark') } const handleCreateButtonClick = (ev: MouseEvent | undefined) => { requireAuthentication(() => { ev?.preventDefault() navigate('/create') }, 'create') } const toggleSubnavigation = (isShow: boolean, signal?: (v: boolean) => void) => { clearTimer() setIsKnowledgeBaseVisible(false) setIsTopicsVisible(false) setIsZineVisible(false) setIsFeedVisible(false) if (signal) { signal(isShow) } } let timer: string | number | NodeJS.Timeout const clearTimer = () => { clearTimeout(timer) } const hideSubnavigation = (time = 500) => { timer = setTimeout(() => { toggleSubnavigation(false) }, time) } const loc = useLocation() const handleToggleMenuByLink = (event: MouseEvent, route: string) => { event.preventDefault() // console.debug('[Header] toggle menu link from', loc.pathname) // console.debug('to', route) if (!fixed()) return if (loc.pathname.startsWith(route) || loc.pathname.startsWith(`/${route}`)) { toggleFixed() } navigate(route) } return (
) }