import { createEffect, For, createMemo, onMount, Show, createSignal } from 'solid-js' import { Title } from '@solidjs/meta' import { clsx } from 'clsx' import { getPagePath } from '@nanostores/router' import MD from './MD' import type { Author, Shout } from '../../graphql/types.gen' import { useSession } from '../../context/session' import { useLocalize } from '../../context/localize' import { useReactions } from '../../context/reactions' import { MediaItem } from '../../pages/types' import { router, useRouter } from '../../stores/router' import { formatDate } from '../../utils' import { getDescription } from '../../utils/meta' import { imageProxy } from '../../utils/imageProxy' import { isDesktop } from '../../utils/media-query' import { AuthorCard } from '../Author/AuthorCard' import { TableOfContents } from '../TableOfContents' import { AudioPlayer } from './AudioPlayer' import { SharePopup } from './SharePopup' import { ShoutRatingControl } from './ShoutRatingControl' import { CommentsTree } from './CommentsTree' import stylesHeader from '../Nav/Header.module.scss' import { AudioHeader } from './AudioHeader' import { Popover } from '../_shared/Popover' import { VideoPlayer } from '../_shared/VideoPlayer' import { Icon } from '../_shared/Icon' import { SolidSwiper } from '../_shared/SolidSwiper' import styles from './Article.module.scss' import { CardTopic } from '../Feed/CardTopic' interface Props { article: Shout scrollToComments?: boolean } export const FullArticle = (props: Props) => { const { t } = useLocalize() const { user, isAuthenticated, actions: { requireAuthentication } } = useSession() const [isReactionsLoaded, setIsReactionsLoaded] = createSignal(false) const formattedDate = createMemo(() => formatDate(new Date(props.article.createdAt))) const mainTopic = createMemo( () => props.article.topics?.find((topic) => topic?.slug === props.article.mainTopic) || props.article.topics[0] ) const canEdit = () => props.article.authors?.some((a) => a.slug === user()?.slug) const handleBookmarkButtonClick = (ev) => { requireAuthentication(() => { // TODO: implement bookmark clicked ev.preventDefault() }, 'bookmark') } const body = createMemo(() => { if (props.article.layout === 'literature') { try { const media = JSON.parse(props.article.media) if (media.length > 0) { return media[0].body } } catch (error) { console.error(error) } } return props.article.body }) const media = createMemo(() => { return JSON.parse(props.article.media || '[]') }) const commentsRef: { current: HTMLDivElement } = { current: null } const scrollToComments = () => { window.scrollTo({ top: commentsRef.current.offsetTop - 96, left: 0, behavior: 'smooth' }) } const { searchParams, changeSearchParam } = useRouter() createEffect(() => { if (props.scrollToComments) { scrollToComments() } }) createEffect(() => { if (searchParams()?.scrollTo === 'comments' && commentsRef.current) { scrollToComments() changeSearchParam('scrollTo', null) } }) createEffect(() => { if (searchParams().commentId && isReactionsLoaded()) { const commentElement = document.querySelector(`[id='comment_${searchParams().commentId}']`) if (commentElement) { commentElement.scrollIntoView({ behavior: 'smooth' }) } } }) const { actions: { loadReactionsBy } } = useReactions() onMount(async () => { await loadReactionsBy({ by: { shout: props.article.slug } }) setIsReactionsLoaded(true) }) return ( <>