webapp/src/components/Views/Feed.tsx

155 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-11-29 11:01:23 +00:00
import { createEffect, createMemo, createSignal, For, onMount, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import '../../styles/Feed.scss'
2022-10-25 21:45:37 +00:00
import stylesBeside from '../../components/Feed/Beside.module.scss'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-09-09 11:53:35 +00:00
import { TopicCard } from '../Topic/Card'
import { ArticleCard } from '../Feed/Card'
2022-09-13 09:59:04 +00:00
import { AuthorCard } from '../Author/Card'
2022-09-09 11:53:35 +00:00
import { t } from '../../utils/intl'
2022-09-13 09:59:04 +00:00
import { FeedSidebar } from '../Feed/Sidebar'
2022-09-09 11:53:35 +00:00
import CommentCard from '../Article/Comment'
2022-11-18 02:23:04 +00:00
import { loadShouts, useArticlesStore } from '../../stores/zine/articles'
2022-09-09 11:53:35 +00:00
import { useReactionsStore } from '../../stores/zine/reactions'
import { useAuthorsStore } from '../../stores/zine/authors'
2022-09-09 12:18:09 +00:00
import { useTopicsStore } from '../../stores/zine/topics'
2022-09-22 09:37:49 +00:00
import { useTopAuthorsStore } from '../../stores/zine/topAuthors'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2022-09-09 11:53:35 +00:00
2022-11-21 00:26:20 +00:00
// const AUTHORSHIP_REACTIONS = [
// ReactionKind.Accept,
// ReactionKind.Reject,
// ReactionKind.Propose,
// ReactionKind.Ask
// ]
2022-09-09 11:53:35 +00:00
export const FEED_PAGE_SIZE = 20
export const FeedView = () => {
2022-09-09 11:53:35 +00:00
// state
2022-11-18 02:23:04 +00:00
const { sortedArticles } = useArticlesStore()
2022-11-16 06:33:58 +00:00
const { sortedReactions: topComments, loadReactionsBy } = useReactionsStore({})
2022-09-28 20:16:44 +00:00
const { sortedAuthors } = useAuthorsStore()
const { topTopics } = useTopicsStore()
const { topAuthors } = useTopAuthorsStore()
2022-11-14 10:02:08 +00:00
const { session } = useSession()
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2022-11-29 11:01:23 +00:00
const collaborativeShouts = createMemo(() =>
sortedArticles().filter((shout) => shout.visibility === 'authors')
)
createEffect(async () => {
if (collaborativeShouts()) {
// load reactions on collaborativeShouts
await loadReactionsBy({ by: { shouts: [...collaborativeShouts()] }, limit: 5 })
}
})
const userslug = createMemo(() => session()?.user?.slug)
createEffect(async () => {
if (userslug()) {
// load recent editing shouts ( visibility = authors )
await loadShouts({ filters: { author: userslug(), visibility: 'authors' }, limit: 15 })
}
})
const loadMore = async () => {
2022-11-18 02:23:04 +00:00
const { hasMore } = await loadShouts({
filters: { visibility: 'community' },
2022-11-15 14:24:50 +00:00
limit: FEED_PAGE_SIZE,
offset: sortedArticles().length
})
setIsLoadMoreButtonVisible(hasMore)
2022-09-13 09:59:04 +00:00
}
2022-11-16 06:33:58 +00:00
onMount(async () => {
// load 5 recent comments overall
2022-11-26 01:46:34 +00:00
await loadReactionsBy({ by: { comment: true }, limit: 5, offset: 0 })
2022-11-16 06:33:58 +00:00
// load recent shouts not only published ( visibility = community )
await loadMore()
})
2022-09-09 11:53:35 +00:00
return (
<>
<div class="container feed">
<div class="row">
<div class="col-md-3 feed-navigation">
2022-09-28 20:16:44 +00:00
<FeedSidebar authors={sortedAuthors()} />
2022-09-09 11:53:35 +00:00
</div>
<div class="col-md-6">
<ul class="feed-filter">
2022-09-30 14:22:33 +00:00
<Show when={!!session()?.user?.slug}>
2022-09-09 11:53:35 +00:00
<li class="selected">
<a href="/feed/my">{t('My feed')}</a>
</li>
</Show>
<li>
<a href="/feed/?by=views">{t('Most read')}</a>
2022-09-09 11:53:35 +00:00
</li>
<li>
<a href="/feed/?by=rating">{t('Top rated')}</a>
2022-09-09 11:53:35 +00:00
</li>
<li>
<a href="/feed/?by=comments">{t('Most commented')}</a>
2022-09-09 11:53:35 +00:00
</li>
</ul>
2022-09-28 20:16:44 +00:00
<Show when={sortedArticles().length > 0}>
<For each={sortedArticles().slice(0, 4)}>
2022-09-09 11:53:35 +00:00
{(article) => <ArticleCard article={article} settings={{ isFeedMode: true }} />}
</For>
2022-10-25 21:45:37 +00:00
<div class={stylesBeside.besideColumnTitle}>
2022-09-09 11:53:35 +00:00
<h4>{t('Popular authors')}</h4>
<a href="/authors">
2022-09-09 11:53:35 +00:00
{t('All authors')}
<Icon name="arrow-right" />
</a>
</div>
2022-10-25 21:45:37 +00:00
<ul class={stylesBeside.besideColumn}>
2022-09-28 20:16:44 +00:00
<For each={topAuthors().slice(0, 5)}>
2022-09-13 09:59:04 +00:00
{(author) => (
2022-09-09 12:18:09 +00:00
<li>
<AuthorCard author={author} compact={true} hasLink={true} />
</li>
)}
</For>
</ul>
2022-09-09 11:53:35 +00:00
2022-09-28 20:16:44 +00:00
<For each={sortedArticles().slice(4)}>
2022-09-09 11:53:35 +00:00
{(article) => <ArticleCard article={article} settings={{ isFeedMode: true }} />}
</For>
</Show>
</div>
<aside class="col-md-3">
<section class="feed-comments">
<h4>{t('Comments')}</h4>
2022-11-16 06:33:58 +00:00
<For each={topComments()}>
2022-09-13 09:59:04 +00:00
{(comment) => <CommentCard comment={comment} compact={true} />}
2022-09-09 11:53:35 +00:00
</For>
</section>
2022-09-28 20:16:44 +00:00
<Show when={topTopics().length > 0}>
2022-09-09 11:53:35 +00:00
<section class="feed-topics">
<h4>{t('Topics')}</h4>
2022-09-28 20:16:44 +00:00
<For each={topTopics().slice(0, 5)}>
2022-09-09 11:53:35 +00:00
{(topic) => <TopicCard topic={topic} subscribeButtonBottom={true} />}
</For>
</section>
</Show>
</aside>
</div>
<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>
</>
)
}