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

257 lines
9.7 KiB
TypeScript
Raw Normal View History

import { Show, createMemo, createSignal, Switch, onMount, For, Match, createEffect } from 'solid-js'
import type { Author, Shout, Topic } from '../../../graphql/types.gen'
import { Row1 } from '../../Feed/Row1'
import { Row2 } from '../../Feed/Row2'
2023-08-27 21:21:40 +00:00
import { Row3 } from '../../Feed/Row3'
import { useAuthorsStore } from '../../../stores/zine/authors'
import { loadShouts, useArticlesStore } from '../../../stores/zine/articles'
import { router, useRouter } from '../../../stores/router'
import { restoreScrollPosition, saveScrollPosition } from '../../../utils/scroll'
import { splitToPages } from '../../../utils/splitToPages'
import styles from './Author.module.scss'
import stylesArticle from '../../Article/Article.module.scss'
import { clsx } from 'clsx'
import { AuthorCard } from '../../Author/AuthorCard'
import { apiClient } from '../../../utils/apiClient'
import { Comment } from '../../Article/Comment'
import { useLocalize } from '../../../context/localize'
import { AuthorRatingControl } from '../../Author/AuthorRatingControl'
import { hideModal } from '../../../stores/ui'
import { getPagePath } from '@nanostores/router'
import { useSession } from '../../../context/session'
2022-09-09 11:53:35 +00:00
type Props = {
2022-11-15 14:24:50 +00:00
shouts: Shout[]
2022-09-09 11:53:35 +00:00
author: Author
2022-10-05 15:11:14 +00:00
authorSlug: string
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
export const AuthorView = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2023-04-20 14:01:15 +00:00
const { sortedArticles } = useArticlesStore({ shouts: props.shouts })
2022-09-28 20:16:44 +00:00
const { authorEntities } = useAuthorsStore({ authors: [props.author] })
const { page } = useRouter()
const { user } = useSession()
const author = createMemo(() => authorEntities()[props.authorSlug])
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2023-09-06 22:58:54 +00:00
const [isBioExpanded, setIsBioExpanded] = createSignal(false)
2023-02-13 13:48:05 +00:00
const [followers, setFollowers] = createSignal<Author[]>([])
const [following, setFollowing] = createSignal<Array<Author | Topic>>([])
2023-09-06 22:58:54 +00:00
const [showExpandBioControl, setShowExpandBioControl] = createSignal(false)
2023-04-22 14:36:38 +00:00
2023-09-15 09:30:50 +00:00
const bioContainerRef: { current: HTMLDivElement } = { current: null }
const bioWrapperRef: { current: HTMLDivElement } = { current: null }
const fetchSubscriptions = async (): Promise<{ authors: Author[]; topics: Topic[] }> => {
2023-04-22 14:36:38 +00:00
try {
const [getAuthors, getTopics] = await Promise.all([
apiClient.getAuthorFollowingUsers({ slug: props.authorSlug }),
apiClient.getAuthorFollowingTopics({ slug: props.authorSlug })
])
const authors = getAuthors
const topics = getTopics
return { authors, topics }
2023-04-22 14:36:38 +00:00
} catch (error) {
console.error('[fetchSubscriptions] :', error)
throw error
2023-04-22 14:36:38 +00:00
}
}
2023-04-22 14:36:38 +00:00
2023-09-06 22:58:54 +00:00
const checkBioHeight = () => {
2023-09-15 09:30:50 +00:00
if (bioContainerRef.current) {
setShowExpandBioControl(bioContainerRef.current.offsetHeight > bioWrapperRef.current.offsetHeight)
2023-09-06 22:58:54 +00:00
}
}
onMount(async () => {
hideModal()
2023-04-22 14:36:38 +00:00
try {
const userSubscribers = await apiClient.getAuthorFollowers({ slug: props.authorSlug })
setFollowers(userSubscribers)
2023-04-22 14:36:38 +00:00
} catch (error) {
console.error('[getAuthorFollowers]', error)
2023-04-22 14:36:38 +00:00
}
2023-09-06 22:58:54 +00:00
checkBioHeight()
2023-04-22 15:32:21 +00:00
if (sortedArticles().length === PRERENDERED_ARTICLES_COUNT) {
await loadMore()
}
const { authors, topics } = await fetchSubscriptions()
setFollowing([...authors, ...topics])
})
2023-01-23 22:12:28 +00:00
const loadMore = async () => {
saveScrollPosition()
2022-11-18 02:23:04 +00:00
const { hasMore } = await loadShouts({
2023-04-20 14:01:15 +00:00
filters: { author: props.authorSlug },
limit: LOAD_MORE_PAGE_SIZE,
offset: sortedArticles().length
})
setIsLoadMoreButtonVisible(hasMore)
restoreScrollPosition()
}
2023-02-17 09:21:02 +00:00
// TODO: use title
// const title = createMemo(() => {
// const m = searchParams().by
// if (m === 'viewed') return t('Top viewed')
// if (m === 'rating') return t('Top rated')
// if (m === 'commented') return t('Top discussed')
// return t('Top recent')
// })
2022-09-09 11:53:35 +00:00
const shouts = createMemo<Shout[][]>(() =>
splitToPages(sortedArticles(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
)
const [commented, setCommented] = createSignal([])
2023-04-20 14:01:15 +00:00
createEffect(async () => {
if (page().route === 'authorComments') {
try {
const data = await apiClient.getReactionsBy({
2023-02-17 09:21:02 +00:00
by: { comment: true, createdBy: props.authorSlug }
})
setCommented(data)
} catch (error) {
2023-03-05 13:31:36 +00:00
console.error('[getReactionsBy comment]', error)
}
}
})
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">
<Show when={author()}>
2023-09-28 21:55:15 +00:00
<div class={styles.authorHeader}>
<AuthorCard
author={author()}
isAuthorPage={true}
followers={followers()}
following={following()}
isCurrentUser={author().slug === user()?.slug}
2023-09-28 21:55:15 +00:00
/>
</div>
</Show>
2023-08-27 21:21:40 +00:00
<div class={clsx(styles.groupControls, 'row')}>
2023-03-10 17:42:48 +00:00
<div class="col-md-16">
2023-02-17 09:21:02 +00:00
<ul class="view-switcher">
<li classList={{ 'view-switcher__item--selected': page().route === 'author' }}>
2023-10-09 21:47:48 +00:00
<a href={getPagePath(router, 'author', { slug: props.authorSlug })}>{t('Publications')}</a>
<span class="view-switcher__counter">{author().stat?.shouts}</span>
2023-02-17 09:21:02 +00:00
</li>
<li classList={{ 'view-switcher__item--selected': page().route === 'authorComments' }}>
<a href={getPagePath(router, 'authorComments', { slug: props.authorSlug })}>
2023-10-09 21:47:48 +00:00
{t('Comments')}
</a>
<span class="view-switcher__counter">{author().stat?.commented}</span>
2023-02-17 09:21:02 +00:00
</li>
<li classList={{ 'view-switcher__item--selected': page().route === 'authorAbout' }}>
<a
onClick={() => checkBioHeight()}
href={getPagePath(router, 'authorAbout', { slug: props.authorSlug })}
2023-09-06 22:58:54 +00:00
>
2023-09-27 22:21:27 +00:00
{t('Profile')}
</a>
2023-02-17 09:21:02 +00:00
</li>
</ul>
</div>
2023-03-10 17:42:48 +00:00
<div class={clsx('col-md-8', styles.additionalControls)}>
2023-02-17 09:21:02 +00:00
<div class={styles.ratingContainer}>
{t('Karma')}
<AuthorRatingControl author={props.author} class={styles.ratingControl} />
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
2023-02-17 09:21:02 +00:00
</div>
2022-09-09 11:53:35 +00:00
<Switch>
<Match when={page().route === 'authorAbout'}>
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
ref={(el) => (bioWrapperRef.current = el)}
2023-09-06 22:58:54 +00:00
class={styles.longBio}
classList={{ [styles.longBioExpanded]: isBioExpanded() }}
>
2023-09-15 09:30:50 +00:00
<div ref={(el) => (bioContainerRef.current = 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>
<Match when={page().route === 'authorComments'}>
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}>
<For each={commented()}>
{(comment) => <Comment comment={comment} class={styles.comment} showArticleLink />}
</For>
</ul>
</div>
</div>
2023-02-17 09:21:02 +00:00
</div>
</Match>
<Match when={page().route === 'author'}>
2023-08-27 21:21:40 +00:00
<Show when={sortedArticles().length === 1}>
<Row1 article={sortedArticles()[0]} noauthor={true} nodate={true} />
2023-08-27 21:21:40 +00:00
</Show>
<Show when={sortedArticles().length === 2}>
<Row2 articles={sortedArticles()} isEqual={true} noauthor={true} nodate={true} />
2023-08-27 21:21:40 +00:00
</Show>
2023-02-17 09:21:02 +00:00
2023-08-27 21:21:40 +00:00
<Show when={sortedArticles().length === 3}>
<Row3 articles={sortedArticles()} noauthor={true} nodate={true} />
2023-08-27 21:21:40 +00:00
</Show>
<Show when={sortedArticles().length > 3}>
<Row1 article={sortedArticles()[0]} noauthor={true} nodate={true} />
<Row2 articles={sortedArticles().slice(1, 3)} isEqual={true} noauthor={true} />
<Row1 article={sortedArticles()[3]} noauthor={true} nodate={true} />
<Row2 articles={sortedArticles().slice(4, 6)} isEqual={true} noauthor={true} />
<Row1 article={sortedArticles()[6]} noauthor={true} nodate={true} />
<Row2 articles={sortedArticles().slice(7, 9)} isEqual={true} noauthor={true} />
2023-08-27 21:21:40 +00:00
<For each={shouts()}>
{(shout) => (
2023-08-27 21:21:40 +00:00
<>
<Row1 article={shout[0]} noauthor={true} nodate={true} />
<Row2 articles={shout.slice(1, 3)} isEqual={true} noauthor={true} />
<Row1 article={shout[3]} noauthor={true} nodate={true} />
<Row2 articles={shout.slice(4, 6)} isEqual={true} noauthor={true} />
<Row1 article={shout[6]} noauthor={true} nodate={true} />
<Row2 articles={shout.slice(7, 9)} isEqual={true} noauthor={true} />
2023-08-27 21:21:40 +00:00
</>
)}
</For>
</Show>
2023-02-17 09:21:02 +00:00
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={loadMore}>
{t('Load more')}
</button>
</p>
</Show>
</Match>
</Switch>
2022-09-09 11:53:35 +00:00
</div>
)
}