import { A, useLocation } from '@solidjs/router' import { clsx } from 'clsx' import { For, Match, Show, Switch, createEffect, createMemo, createSignal, on } from 'solid-js' import { Loading } from '~/components/_shared/Loading' import { useAuthors } from '~/context/authors' import { useFollowing } from '~/context/following' import { useGraphQL } from '~/context/graphql' import { useLocalize } from '~/context/localize' import { useSession } from '~/context/session' import getAuthorFollowersQuery from '~/graphql/query/core/author-followers' import getAuthorFollowsQuery from '~/graphql/query/core/author-follows' import type { Author, Reaction, Shout, Topic } from '~/graphql/schema/core.gen' import { byCreated } from '~/lib/sort' import { paginate } from '~/utils/paginate' import stylesArticle from '../../Article/Article.module.scss' import { Comment } from '../../Article/Comment' import { AuthorCard } from '../../Author/AuthorCard' import { AuthorShoutsRating } from '../../Author/AuthorShoutsRating' import { Placeholder } from '../../Feed/Placeholder' import { Row1 } from '../../Feed/Row1' import { Row2 } from '../../Feed/Row2' import { Row3 } from '../../Feed/Row3' import styles from './Author.module.scss' type AuthorViewProps = { authorSlug: string selectedTab: string shouts?: Shout[] author?: Author } export const PRERENDERED_ARTICLES_COUNT = 12 const LOAD_MORE_PAGE_SIZE = 9 export const AuthorView = (props: AuthorViewProps) => { // contexts const { t } = useLocalize() const loc = useLocation() const { session } = useSession() const { query } = useGraphQL() const { loadAuthor, authorsEntities } = useAuthors() const { followers: myFollowers, follows: myFollows } = useFollowing() // signals const [isBioExpanded, setIsBioExpanded] = createSignal(false) const [author, setAuthor] = createSignal() const [followers, setFollowers] = createSignal([] as Author[]) const [following, changeFollowing] = createSignal>([] as Array) // flat AuthorFollowsResult const [showExpandBioControl, setShowExpandBioControl] = createSignal(false) const [commented, setCommented] = createSignal([]) // derivatives const me = createMemo(() => session()?.user?.app_data?.profile as Author) const pages = createMemo(() => paginate((props.shouts || []).slice(1), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE) ) // 1 // проверяет не собственный ли это профиль, иначе - загружает const [isFetching, setIsFetching] = createSignal(false) createEffect( on( [() => session()?.user?.app_data?.profile, () => props.authorSlug || ''], async ([me, slug]) => { console.debug('[AuthorView] checking if my profile') const my = slug && me?.slug === slug if (my) { console.debug('[Author] my profile precached') if (me) { setAuthor(me) if (myFollowers()) setFollowers((myFollowers() || []) as Author[]) changeFollowing([...(myFollows?.topics || []), ...(myFollows?.authors || [])]) } } else if (slug && !isFetching()) { setIsFetching(true) await loadAuthor({ slug }) setIsFetching(false) // Сброс состояния загрузки после завершения } }, { defer: true } ) ) // 2 // догружает подписки автора createEffect( on( () => authorsEntities()[props.author?.slug || props.authorSlug || ''], async (found) => { if (!found) return console.debug('[AuthorView] ') console.info(`[Author] profile for @${found.slug} fetched`) const followsResp = await query(getAuthorFollowsQuery, { slug: found.slug }).toPromise() const follows = followsResp?.data?.get_author_followers || {} changeFollowing([...(follows?.authors || []), ...(follows?.topics || [])]) console.info(`[Author] follows for @${found.slug} fetched`) const followersResp = await query(getAuthorFollowersQuery, { slug: found.slug }).toPromise() setFollowers(followersResp?.data?.get_author_followers || []) console.info(`[Author] followers for @${found.slug} fetched`) setIsFetching(false) setTimeout(() => setAuthor(found), 1) }, { defer: true } ) ) // event handlers let bioContainerRef: HTMLDivElement let bioWrapperRef: HTMLDivElement const checkBioHeight = (bio = bioWrapperRef) => { if (!bio) return const showExpand = bioContainerRef.offsetHeight > bio.offsetHeight setShowExpandBioControl(showExpand) console.debug('[AuthorView] mounted, show expand bio container:', showExpand) } const handleDeleteComment = (id: number) => { setCommented((prev) => (prev || []).filter((comment) => comment.id !== id)) } // on load createEffect(on(() => bioContainerRef, checkBioHeight)) createEffect( on( () => props.selectedTab, (tab) => tab && console.log('[views.Author] profile tab switched') ) ) const AuthorFeed = () => ( 0 && props.shouts[0]}> 3}> {(page) => ( <> )} ) return (
}> <>
{t('All posts rating')}
(bioWrapperRef = el)} class={styles.longBio} classList={{ [styles.longBioExpanded]: isBioExpanded() }} >
(bioContainerRef = el)} innerHTML={author()?.about || ''} />
    {(comment) => ( handleDeleteComment(id)} /> )}
) }