import { For, Show, createSignal, createMemo, createEffect, onMount, onCleanup } from 'solid-js' import Notifications from './Notifications' import { Icon } from './Icon' import { Modal } from './Modal' import { AuthModal } from './AuthModal' import { t } from '../../utils/intl' import { useModalStore, showModal, useWarningsStore } from '../../stores/ui' import { useAuthStore } from '../../stores/auth' import { handleClientRouteLinkClick, router, Routes, useRouter } from '../../stores/router' import styles from './Header.module.scss' import { getPagePath } from '@nanostores/router' import { getLogger } from '../../utils/logger' import { clsx } from 'clsx' import { SharePopup } from '../Article/SharePopup' import { ProfilePopup } from './ProfilePopup' import Userpic from '../Author/Userpic' import type { Author } from '../../graphql/types.gen' const log = getLogger('header') const resources: { name: string; route: keyof Routes }[] = [ { name: t('zine'), route: 'home' }, { name: t('feed'), route: 'feed' }, { name: t('topics'), route: 'topics' } ] type Props = { title?: string isHeaderFixed?: boolean } export const Header = (props: Props) => { // signals const [getIsScrollingBottom, setIsScrollingBottom] = createSignal(false) const [getIsScrolled, setIsScrolled] = createSignal(false) const [fixed, setFixed] = createSignal(false) const [visibleWarnings, setVisibleWarnings] = createSignal(false) const [isSharePopupVisible, setIsSharePopupVisible] = createSignal(false) const [isProfilePopupVisible, setIsProfilePopupVisible] = createSignal(false) // stores const { warnings } = useWarningsStore() const { session } = useAuthStore() const { modal } = useModalStore() const { page } = useRouter() // methods const toggleWarnings = () => setVisibleWarnings(!visibleWarnings()) const toggleFixed = () => setFixed((oldFixed) => !oldFixed) // effects 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) } }) // derived const authorized = createMemo(() => session()?.user?.slug) const handleBellIconClick = (event: Event) => { event.preventDefault() if (!authorized()) { showModal('auth') return } toggleWarnings() } 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) }) }) return (
) }