import { Show, createSignal, createEffect, onMount, onCleanup, For } from 'solid-js' import { getPagePath, redirectPage } from '@nanostores/router' import { clsx } from 'clsx' import { Modal } from '../Modal' import { AuthModal } from '../AuthModal' import { HeaderAuth } from '../HeaderAuth' import { ConfirmModal } from '../ConfirmModal' import { getShareUrl, SharePopup } from '../../Article/SharePopup' import { Snackbar } from '../Snackbar' import { Icon } from '../../_shared/Icon' import type { Topic } from '../../../graphql/types.gen' import { useModalStore } from '../../../stores/ui' import { router, useRouter } from '../../../stores/router' import { getDescription } from '../../../utils/meta' import { useLocalize } from '../../../context/localize' import { useSession } from '../../../context/session' import styles from './Header.module.scss' import { apiClient } from '../../../utils/apiClient' import { RANDOM_TOPICS_COUNT } from '../../Views/Home' import { Link } from './Link' type Props = { title?: string slug?: string isHeaderFixed?: boolean articleBody?: string cover?: string scrollToComments?: (value: boolean) => void } type HeaderSearchParams = { source?: string } export const Header = (props: Props) => { const { t, lang } = useLocalize() const { modal } = useModalStore() const { actions: { requireAuthentication } } = useSession() const { searchParams } = useRouter() 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 toggleFixed = () => setFixed((oldFixed) => !oldFixed) const tag = (topic: Topic) => /[ЁА-яё]/.test(topic.title || '') && lang() !== 'ru' ? topic.slug : topic.title let windowScrollTop = 0 createEffect(() => { const mainContent = document.querySelector('.main-content') as HTMLDivElement if (fixed() || modal() !== null) { windowScrollTop = window.scrollY mainContent.style.marginTop = `-${windowScrollTop}px` } document.body.classList.toggle('fixed', fixed() || modal() !== null) document.body.classList.toggle(styles.fixed, fixed() && !modal()) if (!fixed() && !modal()) { 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, value) => { event.preventDefault() props.scrollToComments(value) } const handleBookmarkButtonClick = (ev) => { requireAuthentication(() => { // TODO: implement bookmark clicked ev.preventDefault() }, 'bookmark') } const handleCreateButtonClick = (ev) => { requireAuthentication(() => { ev.preventDefault() redirectPage(router, 'create') }, 'create') } const toggleSubnavigation = (isShow, signal?) => { clearTimer() setIsKnowledgeBaseVisible(false) setIsTopicsVisible(false) setIsZineVisible(false) setIsFeedVisible(false) if (signal) { signal(isShow) } } let timer const clearTimer = () => { clearTimeout(timer) } const hideSubnavigation = (event, time = 500) => { timer = setTimeout(() => { toggleSubnavigation(false) }, time) } onMount(async () => { const topics = await apiClient.getRandomTopics({ amount: RANDOM_TOPICS_COUNT }) setRandomTopics(topics) }) return (
) }