import { A, redirect, useSearchParams } from '@solidjs/router' import { clsx } from 'clsx' import { Show, createEffect, createSignal, onCleanup, onMount } from 'solid-js' import { useLocalize } from '~/context/localize' import { useSession } from '~/context/session' import { useUI } from '~/context/ui' import { SharePopup, getShareUrl } from '../Article/SharePopup' import { AuthModal } from '../AuthModal' import { SearchModal } from '../SearchModal/SearchModal' import { Snackbar } from '../Snackbar/Snackbar' import { RandomTopics } from '../TopicsNav/TopicsNav' import { ConfirmModal } from '../_shared/ConfirmModal' import { Icon } from '../_shared/Icon' import { Modal } from '../_shared/Modal' import { Newsletter } from '../_shared/Newsletter' import styles from './Header.module.scss' import { HeaderAuth } from './HeaderAuth' import { Link } from './HeaderLink' type Props = { title?: string slug?: string isHeaderFixed?: boolean desc?: string cover?: string scrollToComments?: (value: boolean) => void } type HeaderSearchParams = { source?: string } const handleSwitchLanguage = (event: { target: { value: string } }) => { location.href = `${location.href}${location.href.includes('?') ? '&' : '?'}lng=${event.target.value}` } export const Header = (props: Props) => { const { t, lang } = useLocalize() const { modal } = useUI() const { requireAuthentication } = useSession() const [searchParams] = useSearchParams() 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()) let windowScrollTop = 0 createEffect(() => { const mainContent = document.querySelector('.main-content') if (fixed() || modal() !== null) { windowScrollTop = window?.scrollY || 0 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())) { window?.scrollTo(0, windowScrollTop) if (mainContent) mainContent.style.marginTop = '' } }) 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 & { currentTarget: HTMLDivElement; target: Element }, value: boolean ) => { event.preventDefault() props.scrollToComments?.(value) } const handleBookmarkButtonClick = (ev: { preventDefault: () => void }) => { requireAuthentication(() => { // TODO: implement bookmark clicked ev.preventDefault() }, 'bookmark') } const handleCreateButtonClick = (ev?: { preventDefault: () => void }) => { requireAuthentication(() => { ev?.preventDefault() redirect('/edit/new') }, 'create') } const toggleSubnavigation = (isShow: boolean, signal?: (b: 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 = (_ev?: MouseEvent, time = 500) => { timer = setTimeout(() => { toggleSubnavigation(false) }, time) } return (
) }