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

310 lines
11 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'
import { AuthorFull } from '../../Author/Full'
2023-02-17 09:21:02 +00:00
import { useAuthorsStore } from '../../../stores/zine/authors'
import { loadShouts, useArticlesStore } from '../../../stores/zine/articles'
import { 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 Userpic from '../../Author/Userpic'
import { Popup } from '../../_shared/Popup'
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 { TopicCard } from '../../Topic/Card'
import { Loading } from '../../_shared/Loading'
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
2022-09-09 11:53:35 +00:00
}
2023-04-01 04:20:14 +00:00
export type AuthorPageSearchParams = {
2023-04-22 14:36:38 +00:00
by:
| ''
| 'viewed'
| 'rating'
| 'commented'
| 'recent'
| 'subscriptions'
2023-04-22 14:36:38 +00:00
| 'followers'
| 'about'
| 'popular'
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
function isAuthor(value: Author | Topic): value is Author {
return 'name' in value
}
2022-09-22 09:37:49 +00:00
export const AuthorView = (props: AuthorProps) => {
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 })
const { searchParams, changeSearchParam } = useRouter<AuthorPageSearchParams>()
2022-09-28 20:16:44 +00:00
const { authorEntities } = useAuthorsStore({ authors: [props.author] })
2023-04-20 14:01:15 +00:00
const author = authorEntities()[props.authorSlug]
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2023-02-13 13:48:05 +00:00
const [followers, setFollowers] = createSignal<Author[]>([])
const [subscriptions, setSubscriptions] = createSignal<Array<Author | Topic>>([])
const [isLoaded, setIsLoaded] = createSignal<boolean>()
2023-04-22 14:36:38 +00:00
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
onMount(async () => {
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.log('[getAuthorFollowers]', error)
2023-04-22 14:36:38 +00:00
}
if (!searchParams().by) {
changeSearchParam('by', 'rating')
}
2023-04-22 15:32:21 +00:00
if (sortedArticles().length === PRERENDERED_ARTICLES_COUNT) {
await loadMore()
}
})
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 pages = 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 (searchParams().by === 'subscriptions') {
setIsLoaded(false)
const { authors, topics } = await fetchSubscriptions()
setSubscriptions([...authors, ...topics])
setIsLoaded(true)
}
if (searchParams().by === 'commented') {
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 (
2022-11-20 22:10:07 +00:00
<div class="author-page">
2023-02-17 09:21:02 +00:00
<div class="wide-container">
2023-04-20 14:01:15 +00:00
<AuthorFull author={author} />
2023-02-17 09:21:02 +00:00
<div class="row group__controls">
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={{ selected: searchParams().by === 'rating' }}>
<button type="button" onClick={() => changeSearchParam('by', 'rating')}>
{t('Publications')}
</button>
</li>
2023-04-22 14:36:38 +00:00
<li classList={{ selected: searchParams().by === 'followers' }}>
<button type="button" onClick={() => changeSearchParam('by', 'followers')}>
2023-02-17 09:21:02 +00:00
{t('Followers')}
</button>
</li>
<li classList={{ selected: searchParams().by === 'subscriptions' }}>
<button type="button" onClick={() => changeSearchParam('by', 'subscriptions')}>
{t('Subscriptions')}
2023-04-22 14:36:38 +00:00
</button>
</li>
2023-02-17 09:21:02 +00:00
<li classList={{ selected: searchParams().by === 'commented' }}>
<button type="button" onClick={() => changeSearchParam('by', 'commented')}>
{t('Comments')}
</button>
</li>
<li classList={{ selected: searchParams().by === 'about' }}>
<button type="button" onClick={() => changeSearchParam('by', 'about')}>
2023-04-01 04:20:14 +00:00
{t('About myself')}
2023-02-17 09:21:02 +00:00
</button>
</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
<Popup
trigger={
<div class={styles.subscribers}>
<Switch>
<Match when={followers().length <= 3}>
<For each={followers().slice(0, 3)}>
{(f) => <Userpic user={f} class={styles.userpic} />}
</For>
</Match>
<Match when={followers().length > 3}>
<For each={followers().slice(0, 2)}>
{(f) => <Userpic user={f} class={styles.userpic} />}
</For>
<div class={clsx(styles.userpic, styles.subscribersCounter)}>
{followers().length}
</div>
</Match>
</Switch>
</div>
}
variant="tiny"
>
<ul class={clsx('nodash', styles.subscribersList)}>
<For each={followers()}>
{(item: Author) => (
<li class={styles.subscriber}>
<AuthorCard
author={item}
isNowrap={true}
hideDescription={true}
hideFollow={true}
hasLink={true}
/>
</li>
)}
</For>
2022-11-20 22:10:07 +00:00
</ul>
2023-02-17 09:21:02 +00:00
</Popup>
<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>
2023-02-17 09:21:02 +00:00
<Match when={searchParams().by === 'about'}>
<div class="wide-container">
2023-04-20 14:01:15 +00:00
<p>{author.bio}</p>
2023-02-17 09:21:02 +00:00
</div>
</Match>
<Match when={searchParams().by === 'commented'}>
<div class="wide-container">
<ul class={stylesArticle.comments}>
<For each={commented()}>{(comment) => <Comment comment={comment} />}</For>
</ul>
</div>
</Match>
2023-04-22 14:36:38 +00:00
<Match when={searchParams().by === 'followers'}>
2023-03-05 13:31:36 +00:00
<div class="wide-container">
<div class="row">
<For each={followers()}>
{(follower: Author) => (
<div class="col-md-6 col-lg-4">
<AuthorCard author={follower} hideWriteButton={true} hasLink={true} />
</div>
)}
</For>
</div>
2023-03-05 13:31:36 +00:00
</div>
</Match>
<Match when={searchParams().by === 'subscriptions'}>
2023-05-17 20:27:24 +00:00
<div class={clsx('wide-container', styles.subscriptions)}>
<div class="row position-relative">
<Show
when={isLoaded()}
fallback={
<div class={styles.loadingWrapper}>
<Loading />
2023-04-22 14:36:38 +00:00
</div>
}
>
<For each={subscriptions()}>
{(subscription: Author | Topic) => (
2023-05-17 20:27:24 +00:00
<div class="col-md-20 col-lg-18">
{isAuthor(subscription) ? (
2023-05-17 20:27:24 +00:00
<div class={styles.authorContainer}>
<AuthorCard
author={subscription}
hideWriteButton={true}
hasLink={true}
isTextButton={true}
/>
</div>
) : (
2023-05-17 20:27:24 +00:00
<TopicCard compact isTopicInRow showDescription topic={subscription} />
)}
</div>
)}
</For>
</Show>
2023-04-22 14:36:38 +00:00
</div>
</div>
</Match>
2023-02-17 09:21:02 +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
</div>
)
}