webapp/src/components/Views/Author.tsx

221 lines
8.0 KiB
TypeScript
Raw Normal View History

import { Show, createMemo, createSignal, Switch, onMount, For, Match, createEffect } from 'solid-js'
2022-09-22 09:37:49 +00:00
import type { Author, Shout } from '../../graphql/types.gen'
import { Row1 } from '../Feed/Row1'
import { Row2 } from '../Feed/Row2'
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-18 02:23:04 +00:00
import { loadShouts, useArticlesStore } from '../../stores/zine/articles'
2022-09-22 09:37:49 +00:00
import { useRouter } from '../../stores/router'
import { restoreScrollPosition, saveScrollPosition } from '../../utils/scroll'
import { splitToPages } from '../../utils/splitToPages'
import { RatingControl } from '../Article/RatingControl'
import styles from './Author.module.scss'
2023-01-23 21:31:47 +00:00
import stylesArticle from '../../styles/Article.module.scss'
import { clsx } from 'clsx'
import Userpic from '../Author/Userpic'
import { Popup } from '../_shared/Popup'
import { AuthorCard } from '../Author/Card'
import { apiClient } from '../../utils/apiClient'
2023-01-20 10:53:35 +00:00
import { Comment } from '../Article/Comment'
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 = {
by: '' | 'viewed' | 'rating' | 'commented' | 'recent' | 'followed' | 'about' | 'popular'
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] })
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])
const subscribers = Array.from({ length: 12 }).fill(author())
2022-10-25 16:25:42 +00:00
const { searchParams, changeSearchParam } = useRouter<AuthorPageSearchParams>()
2022-09-13 09:59:04 +00:00
2023-01-23 22:12:28 +00:00
changeSearchParam('by', 'rating')
const loadMore = async () => {
saveScrollPosition()
2022-11-18 02:23:04 +00:00
const { hasMore } = await loadShouts({
filters: { 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)
)
console.log('!!! authorEntities():', author())
const [commented, setCommented] = createSignal([])
createEffect(async () => {
if (searchParams().by === 'commented') {
try {
const data = await apiClient.getReactionsBy({
by: { comment: true, createdBy: props.authorSlug },
limit: 100,
offset: 0
})
setCommented(data)
} catch (error) {
console.log('!!! error:', error)
}
}
})
2022-09-09 11:53:35 +00:00
return (
2022-11-20 22:10:07 +00:00
<div class="author-page">
2022-09-09 11:53:35 +00:00
<Show when={author()} fallback={<div class="center">{t('Loading')}</div>}>
2022-11-20 22:10:07 +00:00
<div class="wide-container">
<AuthorFull author={author()} />
<div class="row group__controls">
<div class="col-md-8">
<ul class="view-switcher">
<li classList={{ selected: searchParams().by === 'rating' }}>
<button type="button" onClick={() => changeSearchParam('by', 'rating')}>
2023-01-24 22:39:21 +00:00
{t('Publications')}
2022-11-20 22:10:07 +00:00
</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('Comments')}
</button>
</li>
2023-01-23 21:31:47 +00:00
{/*
<li classList={{ selected: searchParams().by === 'popular' }}>
<button type="button" onClick={() => changeSearchParam('by', 'popular')}>
Популярное
</button>
</li>
2023-01-30 13:42:07 +00:00
*/}
<li classList={{ selected: searchParams().by === 'about' }}>
<button type="button" onClick={() => changeSearchParam('by', 'about')}>
О себе
2022-11-20 22:10:07 +00:00
</button>
</li>
</ul>
</div>
<div class={clsx('col-md-4', styles.additionalControls)}>
<Popup
{...props}
trigger={
<div class={styles.subscribers}>
<Userpic user={author()} class={styles.userpic} />
<Userpic user={author()} class={styles.userpic} />
<Userpic user={author()} class={styles.userpic} />
<div class={clsx(styles.userpic, styles.subscribersCounter)}>12</div>
</div>
}
variant="tiny"
>
<ul class={clsx('nodash', styles.subscribersList)}>
<For each={subscribers}>
{(item: Author) => (
<li>
<AuthorCard author={item} hideDescription={true} hideFollow={true} hasLink={true} />
</li>
)}
</For>
</ul>
</Popup>
<div class={styles.ratingContainer}>
Карма
<RatingControl rating={19} class={styles.ratingControl} />
2022-11-20 22:10:07 +00:00
</div>
2022-09-09 11:53:35 +00:00
</div>
</div>
</div>
2023-01-23 21:31:47 +00:00
<Switch
fallback={
<div class="wide-container">
2023-01-26 07:48:38 +00:00
<p>{t('Nothing here yet')}</p>
2023-01-23 21:31:47 +00:00
</div>
}
>
<Match when={searchParams().by === 'about'}>
2023-01-23 21:31:47 +00:00
<div class="wide-container">
<Show when={authorEntities()[author().slug].bio}>
<p>{authorEntities()[author().slug].bio}</p>
</Show>
</div>
</Match>
<Match when={searchParams().by === 'commented'}>
2023-01-23 21:31:47 +00:00
<div class="wide-container">
<ul class={stylesArticle.comments}>
<For each={commented()}>{(comment) => <Comment comment={comment} />}</For>
</ul>
</div>
</Match>
2023-01-23 22:12:28 +00:00
<Match when={searchParams().by === 'rating'}>
<Row1 article={sortedArticles()[0]} />
<Row2 articles={sortedArticles().slice(1, 3)} isEqual={true} />
<Row1 article={sortedArticles()[3]} />
<Row2 articles={sortedArticles().slice(4, 6)} isEqual={true} />
<Row1 article={sortedArticles()[6]} />
<Row2 articles={sortedArticles().slice(7, 9)} isEqual={true} />
<For each={pages()}>
{(page) => (
<>
<Row1 article={page[0]} />
<Row2 articles={page.slice(1, 3)} isEqual={true} />
<Row1 article={page[3]} />
<Row2 articles={page.slice(4, 6)} isEqual={true} />
<Row1 article={page[6]} />
<Row2 articles={page.slice(7, 9)} isEqual={true} />
</>
)}
</For>
<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
</Show>
</div>
)
}