webapp/src/components/Views/Author.tsx

144 lines
4.8 KiB
TypeScript
Raw Normal View History

import { Show, createMemo, createSignal, For, onMount } from 'solid-js'
2022-09-22 09:37:49 +00:00
import type { Author, Shout } from '../../graphql/types.gen'
import { Row2 } from '../Feed/Row2'
import { Row3 } from '../Feed/Row3'
import { AuthorFull } from '../Author/Full'
2022-09-09 11:53:35 +00:00
import { t } from '../../utils/intl'
import { useAuthorsStore } from '../../stores/zine/authors'
2022-11-15 14:24:50 +00:00
import { loadShoutsBy, useArticlesStore } from '../../stores/zine/articles'
2022-09-09 11:53:35 +00:00
2022-10-09 10:56:39 +00:00
import { useTopicsStore } from '../../stores/zine/topics'
2022-09-22 09:37:49 +00:00
import { useRouter } from '../../stores/router'
import { Beside } from '../Feed/Beside'
import { restoreScrollPosition, saveScrollPosition } from '../../utils/scroll'
import { splitToPages } from '../../utils/splitToPages'
2022-09-09 11:53:35 +00:00
2022-09-13 13:38:26 +00:00
// TODO: load reactions on client
2022-09-09 11:53:35 +00:00
type AuthorProps = {
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
// FIXME author topics from server
2022-09-13 13:38:26 +00:00
// topics: Topic[]
2022-09-09 11:53:35 +00:00
}
2022-09-22 09:37:49 +00:00
type AuthorPageSearchParams = {
2022-11-13 12:14:04 +00:00
by: '' | 'viewed' | 'rating' | 'commented' | 'recent' | 'followed'
2022-09-22 09:37:49 +00:00
}
export const PRERENDERED_ARTICLES_COUNT = 12
const LOAD_MORE_PAGE_SIZE = 9 // Row3 + Row3 + Row3
2022-09-22 09:37:49 +00:00
export const AuthorView = (props: AuthorProps) => {
2022-09-28 20:16:44 +00:00
const { sortedArticles } = useArticlesStore({
2022-11-15 14:24:50 +00:00
shouts: props.shouts
2022-09-09 11:53:35 +00:00
})
2022-09-28 20:16:44 +00:00
const { authorEntities } = useAuthorsStore({ authors: [props.author] })
2022-10-09 10:56:39 +00:00
const { topicsByAuthor } = useTopicsStore()
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2022-09-13 09:59:04 +00:00
2022-10-05 15:11:14 +00:00
const author = createMemo(() => authorEntities()[props.authorSlug])
2022-10-25 16:25:42 +00:00
const { searchParams, changeSearchParam } = useRouter<AuthorPageSearchParams>()
2022-09-13 09:59:04 +00:00
const loadMore = async () => {
saveScrollPosition()
2022-11-15 14:24:50 +00:00
const { hasMore } = await loadShoutsBy({
by: { author: author().slug },
limit: LOAD_MORE_PAGE_SIZE,
offset: sortedArticles().length
})
setIsLoadMoreButtonVisible(hasMore)
restoreScrollPosition()
}
onMount(async () => {
if (sortedArticles().length === PRERENDERED_ARTICLES_COUNT) {
loadMore()
}
})
2022-09-09 11:53:35 +00:00
const title = createMemo(() => {
2022-10-25 16:25:42 +00:00
const m = searchParams().by
2022-09-09 11:53:35 +00:00
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')
})
const pages = createMemo<Shout[][]>(() =>
splitToPages(sortedArticles(), PRERENDERED_ARTICLES_COUNT, LOAD_MORE_PAGE_SIZE)
)
2022-09-09 11:53:35 +00:00
return (
<div class="container author-page">
<Show when={author()} fallback={<div class="center">{t('Loading')}</div>}>
<AuthorFull author={author()} />
<div class="row group__controls">
<div class="col-md-8">
<ul class="view-switcher">
2022-11-13 12:14:04 +00:00
<li classList={{ selected: searchParams().by === 'rating' }}>
<button type="button" onClick={() => changeSearchParam('by', 'rating')}>
{t('Popular')}
</button>
</li>
<li classList={{ selected: searchParams().by === 'followed' }}>
<button type="button" onClick={() => changeSearchParam('by', 'followed')}>
{t('Followers')}
</button>
</li>
<li classList={{ selected: searchParams().by === 'commented' }}>
<button type="button" onClick={() => changeSearchParam('by', 'commented')}>
{t('Discussing')}
2022-09-09 11:53:35 +00:00
</button>
</li>
</ul>
</div>
<div class="col-md-4">
<div class="mode-switcher">
{`${t('Show')} `}
<span class="mode-switcher__control">{t('All posts')}</span>
</div>
</div>
</div>
<h3 class="col-12">{title()}</h3>
<div class="row">
<Beside
title={t('Topics which supported by author')}
values={topicsByAuthor()[author().slug].slice(0, 5)}
beside={sortedArticles()[0]}
wrapper={'topic'}
topicShortDescription={true}
isTopicCompact={true}
isTopicInRow={true}
iconButton={true}
/>
<Row3 articles={sortedArticles().slice(1, 4)} />
<Row2 articles={sortedArticles().slice(4, 6)} />
<Row3 articles={sortedArticles().slice(6, 9)} />
<Row3 articles={sortedArticles().slice(9, 12)} />
<For each={pages()}>
{(page) => (
<>
<Row3 articles={page.slice(0, 3)} />
<Row3 articles={page.slice(3, 6)} />
<Row3 articles={page.slice(6, 9)} />
</>
)}
</For>
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={loadMore}>
{t('Load more')}
</button>
</p>
</Show>
2022-09-09 11:53:35 +00:00
</div>
</Show>
</div>
)
}