webapp/src/components/Views/Author/Author.tsx

265 lines
11 KiB
TypeScript
Raw Normal View History

2024-07-13 09:36:23 +00:00
import { A, useLocation } from '@solidjs/router'
import { clsx } from 'clsx'
2024-07-18 09:22:28 +00:00
import { For, Match, Show, Switch, createEffect, createMemo, createSignal, on } from 'solid-js'
import { Loading } from '~/components/_shared/Loading'
2024-06-24 17:50:27 +00:00
import { useAuthors } from '~/context/authors'
import { useFollowing } from '~/context/following'
2024-06-24 17:50:27 +00:00
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'
2024-07-13 11:47:31 +00:00
import { byCreated } from '~/lib/sort'
2024-07-13 11:42:53 +00:00
import { paginate } from '~/utils/paginate'
import stylesArticle from '../../Article/Article.module.scss'
import { Comment } from '../../Article/Comment'
import { AuthorCard } from '../../Author/AuthorCard'
2023-12-27 23:35:43 +00:00
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'
2024-02-04 11:25:21 +00:00
import styles from './Author.module.scss'
2022-09-09 11:53:35 +00:00
2024-07-13 09:36:23 +00:00
type AuthorViewProps = {
2022-10-05 15:11:14 +00:00
authorSlug: string
2024-07-13 07:01:41 +00:00
selectedTab: string
2024-04-15 18:01:00 +00:00
shouts?: Shout[]
author?: Author
2022-09-22 09:37:49 +00:00
}
export const PRERENDERED_ARTICLES_COUNT = 12
2023-02-17 09:21:02 +00:00
const LOAD_MORE_PAGE_SIZE = 9
2024-07-13 09:36:23 +00:00
export const AuthorView = (props: AuthorViewProps) => {
2024-07-13 07:01:41 +00:00
// contexts
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2024-07-13 07:01:41 +00:00
const loc = useLocation()
2024-06-24 17:50:27 +00:00
const { session } = useSession()
2024-07-13 07:01:41 +00:00
const { query } = useGraphQL()
const { loadAuthor, authorsEntities } = useAuthors()
const { followers: myFollowers, follows: myFollows } = useFollowing()
// signals
2023-09-06 22:58:54 +00:00
const [isBioExpanded, setIsBioExpanded] = createSignal(false)
2024-06-24 17:50:27 +00:00
const [author, setAuthor] = createSignal<Author>()
const [followers, setFollowers] = createSignal<Author[]>([] as Author[])
const [following, changeFollowing] = createSignal<Array<Author | Topic>>([] as Array<Author | Topic>) // flat AuthorFollowsResult
2023-09-06 22:58:54 +00:00
const [showExpandBioControl, setShowExpandBioControl] = createSignal(false)
2024-07-09 09:13:13 +00:00
const [commented, setCommented] = createSignal<Reaction[]>([])
2023-12-25 06:52:04 +00:00
2024-07-13 07:01:41 +00:00
// derivatives
const me = createMemo<Author>(() => session()?.user?.app_data?.profile as Author)
const pages = createMemo<Shout[][]>(() =>
2024-07-18 09:22:28 +00:00
paginate((props.shouts || []).slice(1), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
2024-07-13 07:01:41 +00:00
)
2024-06-24 17:50:27 +00:00
// 1 // проверяет не собственный ли это профиль, иначе - загружает
2024-05-20 23:15:52 +00:00
const [isFetching, setIsFetching] = createSignal(false)
createEffect(
2024-07-13 07:02:05 +00:00
on(
[() => session()?.user?.app_data?.profile, () => props.authorSlug || ''],
async ([me, slug]) => {
2024-07-26 15:49:15 +00:00
console.debug('check if my profile')
2024-07-13 07:02:05 +00:00
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) // Сброс состояния загрузки после завершения
2024-06-24 17:50:27 +00:00
}
2024-07-13 07:02:05 +00:00
},
{ defer: true }
)
2024-06-24 17:50:27 +00:00
)
2024-07-09 17:41:14 +00:00
// 2 // догружает подписки автора
2024-06-24 17:50:27 +00:00
createEffect(
on(
2024-07-13 07:01:41 +00:00
() => authorsEntities()[props.author?.slug || props.authorSlug || ''],
async (found) => {
2024-06-24 17:50:27 +00:00
if (!found) return
2024-07-26 15:49:15 +00:00
setAuthor(found)
2024-07-13 07:01:41 +00:00
console.info(`[Author] profile for @${found.slug} fetched`)
const followsResp = await query(getAuthorFollowsQuery, { slug: found.slug }).toPromise()
2024-06-24 17:50:27 +00:00
const follows = followsResp?.data?.get_author_followers || {}
changeFollowing([...(follows?.authors || []), ...(follows?.topics || [])])
2024-07-13 07:01:41 +00:00
console.info(`[Author] follows for @${found.slug} fetched`)
const followersResp = await query(getAuthorFollowersQuery, { slug: found.slug }).toPromise()
2024-06-24 17:50:27 +00:00
setFollowers(followersResp?.data?.get_author_followers || [])
2024-07-13 07:01:41 +00:00
console.info(`[Author] followers for @${found.slug} fetched`)
2024-06-24 17:50:27 +00:00
setIsFetching(false)
},
2024-06-26 08:22:05 +00:00
{ defer: true }
)
2024-05-20 23:15:52 +00:00
)
2024-07-13 07:01:41 +00:00
// event handlers
2024-06-24 17:50:27 +00:00
let bioContainerRef: HTMLDivElement
let bioWrapperRef: HTMLDivElement
2024-07-18 09:22:28 +00:00
const checkBioHeight = (bio = bioWrapperRef) => {
if (!bio) return
const showExpand = bioContainerRef.offsetHeight > bio.offsetHeight
setShowExpandBioControl(showExpand)
console.debug('[AuthorView] mounted, show expand bio container:', showExpand)
2024-05-20 23:15:52 +00:00
}
const handleDeleteComment = (id: number) => {
2024-06-24 17:50:27 +00:00
setCommented((prev) => (prev || []).filter((comment) => comment.id !== id))
}
2024-07-13 07:01:41 +00:00
// on load
2024-07-18 09:22:28 +00:00
createEffect(on(() => bioContainerRef, checkBioHeight))
2024-07-18 10:22:58 +00:00
createEffect(
on(
() => props.selectedTab,
(tab) => tab && console.log('[views.Author] profile tab switched')
)
)
2024-07-09 17:41:14 +00:00
2022-09-09 11:53:35 +00:00
return (
2023-08-27 21:21:40 +00:00
<div class={styles.authorPage}>
2023-02-17 09:21:02 +00:00
<div class="wide-container">
2024-03-15 16:42:55 +00:00
<Show when={author()} fallback={<Loading />}>
<>
<div class={styles.authorHeader}>
2024-06-24 17:50:27 +00:00
<AuthorCard
author={author() as Author}
followers={followers() || []}
flatFollows={following() || []}
/>
2022-09-09 11:53:35 +00:00
</div>
<div class={clsx(styles.groupControls, 'row')}>
<div class="col-md-16">
<ul class="view-switcher">
2024-07-13 09:36:23 +00:00
<li classList={{ 'view-switcher__item--selected': !props.selectedTab }}>
2024-07-15 23:11:01 +00:00
<A href={`/@${props.authorSlug}`}>{t('Publications')}</A>
2024-06-24 17:50:27 +00:00
<Show when={author()?.stat}>
<span class="view-switcher__counter">{author()?.stat?.shouts || 0}</span>
2023-12-24 17:29:16 +00:00
</Show>
</li>
2024-07-13 09:36:23 +00:00
<li classList={{ 'view-switcher__item--selected': props.selectedTab === 'comment' }}>
2024-07-15 23:11:01 +00:00
<A href={`/@${props.authorSlug}/comments`}>{t('Comments')}</A>
2024-06-24 17:50:27 +00:00
<Show when={author()?.stat}>
<span class="view-switcher__counter">{author()?.stat?.comments || 0}</span>
2023-12-24 17:29:16 +00:00
</Show>
</li>
2024-07-13 09:36:23 +00:00
<li classList={{ 'view-switcher__item--selected': props.selectedTab === 'about' }}>
2024-07-15 23:11:01 +00:00
<A onClick={() => checkBioHeight()} href={`/@${props.authorSlug}/about`}>
2024-07-03 17:38:43 +00:00
{t('About the author')}
2024-06-24 17:50:27 +00:00
</A>
</li>
</ul>
</div>
<div class={clsx('col-md-8', styles.additionalControls)}>
2024-03-15 16:42:55 +00:00
<Show when={author()?.stat?.rating || author()?.stat?.rating === 0}>
2023-12-27 23:14:33 +00:00
<div class={styles.ratingContainer}>
2023-12-28 00:30:09 +00:00
{t('All posts rating')}
2024-06-24 17:50:27 +00:00
<AuthorShoutsRating author={author() as Author} class={styles.ratingControl} />
2023-12-27 23:14:33 +00:00
</div>
</Show>
</div>
</div>
</>
</Show>
2023-02-17 09:21:02 +00:00
</div>
2022-09-09 11:53:35 +00:00
<Switch>
2024-07-13 09:36:23 +00:00
<Match when={props.selectedTab === 'about'}>
2023-02-17 09:21:02 +00:00
<div class="wide-container">
2023-09-06 22:58:54 +00:00
<div class="row">
<div class="col-md-20 col-lg-18">
<div
2024-06-24 17:50:27 +00:00
ref={(el) => (bioWrapperRef = el)}
2023-09-06 22:58:54 +00:00
class={styles.longBio}
classList={{ [styles.longBioExpanded]: isBioExpanded() }}
>
2024-06-24 17:50:27 +00:00
<div ref={(el) => (bioContainerRef = el)} innerHTML={author()?.about || ''} />
2023-09-06 22:58:54 +00:00
</div>
<Show when={showExpandBioControl()}>
<button
class={clsx('button button--subscribe-topic', styles.longBioExpandedControl)}
onClick={() => setIsBioExpanded(!isBioExpanded())}
>
{t('Show more')}
</button>
</Show>
</div>
</div>
2023-02-17 09:21:02 +00:00
</div>
</Match>
2024-07-13 09:36:23 +00:00
<Match when={props.selectedTab === 'comments'}>
<Show when={me()?.slug === props.authorSlug && !me().stat?.comments}>
2024-05-18 22:03:06 +00:00
<div class="wide-container">
2024-06-24 17:50:27 +00:00
<Placeholder type={loc?.pathname} mode="profile" />
2024-05-18 22:03:06 +00:00
</div>
</Show>
2024-05-11 17:27:57 +00:00
2023-02-17 09:21:02 +00:00
<div class="wide-container">
<div class="row">
<div class="col-md-20 col-lg-18">
<ul class={stylesArticle.comments}>
2024-02-29 15:05:05 +00:00
<For each={commented()?.sort(byCreated).reverse()}>
{(comment) => (
<Comment
comment={comment}
class={styles.comment}
showArticleLink={true}
onDelete={(id) => handleDeleteComment(id)}
/>
)}
</For>
</ul>
</div>
</div>
2023-02-17 09:21:02 +00:00
</div>
</Match>
2024-07-13 09:36:23 +00:00
<Match when={!props.selectedTab}>
<Show when={me()?.slug === props.authorSlug && !me().stat?.shouts}>
2024-05-11 17:27:57 +00:00
<div class="wide-container">
2024-06-24 17:50:27 +00:00
<Placeholder type={loc?.pathname} mode="profile" />
2024-05-11 17:27:57 +00:00
</div>
2023-08-27 21:21:40 +00:00
</Show>
2024-07-26 15:49:15 +00:00
<Show when={Array.isArray(props.shouts) && props.shouts.length > 0 && props.shouts[0]}>
<Row1 article={props.shouts?.[0] as Shout} noauthor={true} nodate={true} />
<Show when={props.shouts && props.shouts.length > 1}>
<Switch>
<Match when={props.shouts && props.shouts.length === 2}>
<Row2 articles={props.shouts as Shout[]} isEqual={true} noauthor={true} nodate={true} />
</Match>
<Match when={props.shouts && props.shouts.length === 3}>
<Row3 articles={props.shouts as Shout[]} noauthor={true} nodate={true} />
</Match>
<Match when={props.shouts && props.shouts.length > 3}>
<For each={pages()}>
{(page) => (
<>
<Row1 article={page[0]} noauthor={true} nodate={true} />
<Row2 articles={page.slice(1, 3)} isEqual={true} noauthor={true} />
<Row1 article={page[3]} noauthor={true} nodate={true} />
<Row2 articles={page.slice(4, 6)} isEqual={true} noauthor={true} />
<Row1 article={page[6]} noauthor={true} nodate={true} />
<Row2 articles={page.slice(7, 9)} isEqual={true} noauthor={true} />
</>
)}
</For>
</Match>
</Switch>
</Show>
</Show>
2023-02-17 09:21:02 +00:00
</Match>
</Switch>
2022-09-09 11:53:35 +00:00
</div>
)
}