webapp/src/components/Views/Feed/Feed.tsx

443 lines
15 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2024-02-04 11:25:21 +00:00
import { For, Show, createEffect, createMemo, createSignal, on, onMount } from 'solid-js'
2024-06-24 17:50:27 +00:00
import { Meta } from '@solidjs/meta'
import { A, useLocation, useSearchParams } from '@solidjs/router'
import { useGraphQL } from '~/context/graphql'
import { useUI } from '~/context/ui'
import getUnratedShoutsQuery from '~/graphql/query/core/articles-load-unrated'
import { useAuthors } from '../../../context/authors'
import { useFeed } from '../../../context/feed'
import { useLocalize } from '../../../context/localize'
import { useReactions } from '../../../context/reactions'
2023-12-26 10:05:15 +00:00
import { useSession } from '../../../context/session'
2024-05-06 23:44:25 +00:00
import { useTopics } from '../../../context/topics'
2024-02-05 15:04:23 +00:00
import type { Author, LoadShoutsOptions, Reaction, Shout } from '../../../graphql/schema/core.gen'
import { getImageUrl } from '../../../utils/getImageUrl'
2024-03-01 13:04:28 +00:00
import { byCreated } from '../../../utils/sortby'
import { CommentDate } from '../../Article/CommentDate'
2024-01-08 13:02:52 +00:00
import { getShareUrl } from '../../Article/SharePopup'
import { AuthorBadge } from '../../Author/AuthorBadge'
2023-12-31 05:01:34 +00:00
import { AuthorLink } from '../../Author/AuthorLink'
import { ArticleCard } from '../../Feed/ArticleCard'
2024-06-24 17:50:27 +00:00
import stylesBeside from '../../Feed/Beside.module.scss'
import stylesTopic from '../../Feed/CardTopic.module.scss'
2024-05-10 16:52:13 +00:00
import { Placeholder } from '../../Feed/Placeholder'
import { Sidebar } from '../../Feed/Sidebar'
2024-02-03 08:16:47 +00:00
import { Modal } from '../../Nav/Modal'
2024-02-04 11:25:21 +00:00
import { DropDown } from '../../_shared/DropDown'
import { Icon } from '../../_shared/Icon'
import { InviteMembers } from '../../_shared/InviteMembers'
import { Loading } from '../../_shared/Loading'
import { ShareModal } from '../../_shared/ShareModal'
import styles from './Feed.module.scss'
2022-09-09 11:53:35 +00:00
export const FEED_PAGE_SIZE = 20
const UNRATED_ARTICLES_COUNT = 5
type FeedPeriod = 'week' | 'month' | 'year'
type VisibilityMode = 'all' | 'community' | 'featured'
type PeriodItem = {
value: FeedPeriod
title: string
}
type VisibilityItem = {
value: VisibilityMode
title: string
}
type FeedSearchParams = {
2024-05-01 14:33:37 +00:00
by: 'publish_date' | 'likes' | 'last_comment'
period: FeedPeriod
visibility: VisibilityMode
}
2024-04-24 12:48:19 +00:00
type Props = {
loadShouts: (options: LoadShoutsOptions) => Promise<{
hasMore: boolean
newShouts: Shout[]
}>
}
2023-12-24 13:08:04 +00:00
const getFromDate = (period: FeedPeriod): number => {
const now = new Date()
2023-12-24 13:08:04 +00:00
let d: Date = now
switch (period) {
case 'week': {
2023-12-24 13:08:04 +00:00
d = new Date(now.setDate(now.getDate() - 7))
2023-12-24 13:32:25 +00:00
break
}
case 'month': {
2023-12-24 13:08:04 +00:00
d = new Date(now.setMonth(now.getMonth() - 1))
2023-12-24 13:32:25 +00:00
break
}
case 'year': {
2023-12-24 13:08:04 +00:00
d = new Date(now.setFullYear(now.getFullYear() - 1))
2023-12-24 13:32:25 +00:00
break
}
}
2023-12-24 13:08:04 +00:00
return Math.floor(d.getTime() / 1000)
}
export const FeedView = (props: Props) => {
2023-12-20 16:54:20 +00:00
const { t } = useLocalize()
const monthPeriod: PeriodItem = { value: 'month', title: t('This month') }
const periods: PeriodItem[] = [
{ value: 'week', title: t('This week') },
monthPeriod,
2024-06-26 08:22:05 +00:00
{ value: 'year', title: t('This year') }
]
const visibilities: VisibilityItem[] = [
{ value: 'community', title: t('All') },
2024-06-26 08:22:05 +00:00
{ value: 'featured', title: t('Published') }
]
2024-06-24 17:50:27 +00:00
const { query } = useGraphQL()
const [searchParams, changeSearchParams] = useSearchParams<FeedSearchParams>()
const loc = useLocation()
const { showModal } = useUI()
const [isLoading, setIsLoading] = createSignal(false)
const [isRightColumnLoaded, setIsRightColumnLoaded] = createSignal(false)
2024-06-24 17:50:27 +00:00
const { session } = useSession()
2024-02-04 17:40:15 +00:00
const { loadReactionsBy } = useReactions()
2024-06-24 17:50:27 +00:00
const { sortedFeed } = useFeed()
2024-05-06 23:44:25 +00:00
const { topTopics } = useTopics()
2024-06-24 17:50:27 +00:00
const { topAuthors } = useAuthors()
const [isLoadMoreButtonVisible, setIsLoadMoreButtonVisible] = createSignal(false)
2023-02-17 09:21:02 +00:00
const [topComments, setTopComments] = createSignal<Reaction[]>([])
const [unratedArticles, setUnratedArticles] = createSignal<Shout[]>([])
2024-06-24 17:50:27 +00:00
const [_searchResults, setSearchResults] = createSignal<Shout[]>([])
const currentPeriod = createMemo(() => {
2024-06-24 17:50:27 +00:00
const period = periods.find((p) => p.value === searchParams?.period)
if (!period) {
return monthPeriod
}
return period
})
const currentVisibility = createMemo(() => {
2024-06-24 17:50:27 +00:00
const visibility = visibilities.find((v) => v.value === searchParams?.visibility)
if (!visibility) {
2024-04-24 12:48:19 +00:00
return visibilities[0]
}
return visibility
})
const loadUnratedArticles = async () => {
2024-01-18 12:52:02 +00:00
if (session()) {
2024-06-24 17:50:27 +00:00
const resp = await query(getUnratedShoutsQuery, { limit: UNRATED_ARTICLES_COUNT }).toPromise()
setUnratedArticles(resp?.data?.load_shouts_unrated || [])
2024-01-18 12:52:02 +00:00
}
}
const loadTopComments = async () => {
2024-02-29 17:51:07 +00:00
const comments = await loadReactionsBy({ by: { comment: true }, limit: 50 })
2024-03-01 13:04:28 +00:00
setTopComments(comments.sort(byCreated).reverse())
}
onMount(() => {
loadMore()
// eslint-disable-next-line promise/catch-or-return
2024-01-18 12:52:02 +00:00
Promise.all([loadTopComments()]).finally(() => setIsRightColumnLoaded(true))
2023-12-25 04:01:52 +00:00
})
2024-05-20 23:15:52 +00:00
createEffect(
on(
[() => session(), unratedArticles],
([s, seen]) => {
if (s?.access_token && !(seen?.length > 0)) loadUnratedArticles()
},
2024-06-26 08:22:05 +00:00
{ defer: true }
)
2024-05-20 23:15:52 +00:00
)
2024-06-24 17:50:27 +00:00
// TODO: declare some details
createEffect(
on(
2024-06-24 17:50:27 +00:00
() => searchParams,
(_p) => {
setSearchResults([])
loadMore()
},
2024-06-26 08:22:05 +00:00
{ defer: true }
)
)
const loadFeedShouts = () => {
const options: LoadShoutsOptions = {
2022-11-15 14:24:50 +00:00
limit: FEED_PAGE_SIZE,
2024-06-26 08:22:05 +00:00
offset: sortedFeed()?.length || 0
}
2024-06-24 17:50:27 +00:00
if (searchParams?.by) {
options.order_by = searchParams?.by
}
2024-06-24 17:50:27 +00:00
const visibilityMode = searchParams?.visibility
2024-04-24 12:48:19 +00:00
2024-01-05 18:38:35 +00:00
if (visibilityMode === 'all') {
options.filters = { ...options.filters }
} else if (visibilityMode) {
options.filters = {
...options.filters,
2024-06-26 08:22:05 +00:00
featured: visibilityMode === 'featured'
}
}
2024-06-24 17:50:27 +00:00
if (searchParams?.by && searchParams?.by !== 'publish_date') {
const period = searchParams?.period || 'month'
2023-12-24 13:08:04 +00:00
options.filters = { after: getFromDate(period) }
}
2024-04-24 12:48:19 +00:00
return props.loadShouts(options)
}
const loadMore = async () => {
setIsLoading(true)
const { hasMore, newShouts } = await loadFeedShouts()
setIsLoading(false)
2023-02-28 17:13:14 +00:00
loadReactionsBy({
by: {
2024-06-26 08:22:05 +00:00
shouts: newShouts.map((s) => s.slug)
}
2023-02-28 17:13:14 +00:00
})
setIsLoadMoreButtonVisible(hasMore)
2022-09-13 09:59:04 +00:00
}
const ogImage = getImageUrl('production/image/logo_image.png')
const description = t(
2024-06-26 08:22:05 +00:00
'Independent media project about culture, science, art and society with horizontal editing'
)
const ogTitle = t('Feed')
2024-01-08 13:02:52 +00:00
const [shareData, setShareData] = createSignal<Shout | undefined>()
2024-03-01 13:04:28 +00:00
const handleShare = (shared: Shout | undefined) => {
2024-01-08 13:02:52 +00:00
showModal('share')
setShareData(shared)
}
2022-09-09 11:53:35 +00:00
return (
<div class="wide-container feed">
<Meta name="descprition" content={description} />
<Meta name="keywords" content={t('keywords')} />
<Meta name="og:type" content="article" />
<Meta name="og:title" content={ogTitle} />
<Meta name="og:image" content={ogImage} />
<Meta name="twitter:image" content={ogImage} />
<Meta name="og:description" content={description} />
<Meta name="twitter:card" content="summary_large_image" />
<Meta name="twitter:title" content={ogTitle} />
<Meta name="twitter:description" content={description} />
<div class="row">
<div class={clsx('col-md-5 col-xl-4', styles.feedNavigation)}>
<Sidebar />
</div>
2023-03-10 17:42:48 +00:00
<div class="col-md-12 offset-xl-1">
2024-06-24 17:50:27 +00:00
<Show when={!session() && loc?.pathname !== 'feed'}>
<Placeholder type={loc?.pathname} mode="feed" />
2024-05-18 22:03:06 +00:00
</Show>
2024-06-24 17:50:27 +00:00
<Show when={(session() || loc?.pathname === 'feed') && sortedFeed().length}>
2024-05-10 16:52:13 +00:00
<div class={styles.filtersContainer}>
<ul class={clsx('view-switcher', styles.feedFilter)}>
<li
class={clsx({
'view-switcher__item--selected':
2024-06-26 08:22:05 +00:00
searchParams?.by === 'publish_date' || !searchParams?.by
2024-05-10 16:52:13 +00:00
})}
>
2024-06-24 17:50:27 +00:00
<A href={loc.pathname}>{t('Recent')}</A>
2024-05-10 16:52:13 +00:00
</li>
{/*<li>*/}
{/* <a href="/feed/?by=views">{t('Most read')}</a>*/}
{/*</li>*/}
<li
class={clsx({
2024-06-26 08:22:05 +00:00
'view-switcher__item--selected': searchParams?.by === 'likes'
2024-05-10 16:52:13 +00:00
})}
>
<span class="link" onClick={() => changeSearchParams({ by: 'likes' })}>
{t('Top rated')}
</span>
</li>
<li
class={clsx({
2024-06-26 08:22:05 +00:00
'view-switcher__item--selected': searchParams?.by === 'last_comment'
2024-05-10 16:52:13 +00:00
})}
>
<span class="link" onClick={() => changeSearchParams({ by: 'last_comment' })}>
{t('Most commented')}
</span>
</li>
</ul>
<div class={styles.dropdowns}>
2024-06-24 17:50:27 +00:00
<Show when={searchParams?.by && searchParams?.by !== 'publish_date'}>
2024-05-10 16:52:13 +00:00
<DropDown
popupProps={{ horizontalAnchor: 'right' }}
options={periods}
currentOption={currentPeriod()}
triggerCssClass={styles.periodSwitcher}
onChange={(period: PeriodItem) => changeSearchParams({ period: period.value })}
/>
</Show>
<DropDown
2024-01-06 04:06:58 +00:00
popupProps={{ horizontalAnchor: 'right' }}
2024-05-10 16:52:13 +00:00
options={visibilities}
currentOption={currentVisibility()}
triggerCssClass={styles.periodSwitcher}
2024-05-10 16:52:13 +00:00
onChange={(visibility: VisibilityItem) =>
changeSearchParams({ visibility: visibility.value })
}
/>
2024-05-10 16:52:13 +00:00
</div>
</div>
2023-08-12 14:17:00 +00:00
2024-05-10 16:52:13 +00:00
<Show when={!isLoading()} fallback={<Loading />}>
2024-06-24 17:50:27 +00:00
<Show when={sortedFeed().length > 0}>
<For each={sortedFeed().slice(0, 4)}>
2024-05-10 16:52:13 +00:00
{(article) => (
<ArticleCard
onShare={(shared) => handleShare(shared)}
onInvite={() => showModal('inviteMembers')}
article={article}
settings={{ isFeedMode: true }}
desktopCoverSize="M"
/>
)}
</For>
<div class={styles.asideSection}>
<div class={stylesBeside.besideColumnTitle}>
<h4>{t('Popular authors')}</h4>
<a href="/authors">
{t('All authors')}
<Icon name="arrow-right" class={stylesBeside.icon} />
</a>
</div>
<ul class={stylesBeside.besideColumn}>
<For each={topAuthors().slice(0, 5)}>
{(author) => (
<li>
<AuthorBadge author={author} />
</li>
)}
</For>
</ul>
</div>
2024-06-24 17:50:27 +00:00
<For each={sortedFeed().slice(4)}>
2024-05-10 16:52:13 +00:00
{(article) => (
<ArticleCard article={article} settings={{ isFeedMode: true }} desktopCoverSize="M" />
)}
</For>
</Show>
2024-05-10 16:52:13 +00:00
<Show when={isLoadMoreButtonVisible()}>
<p class="load-more-container">
<button class="button" onClick={loadMore}>
{t('Load more')}
</button>
</p>
</Show>
</Show>
</Show>
</div>
2023-03-10 17:42:48 +00:00
<aside class={clsx('col-md-7 col-xl-6 offset-xl-1', styles.feedAside)}>
<Show when={isRightColumnLoaded()}>
<Show when={topComments().length > 0}>
<section class={styles.asideSection}>
<h4>{t('Comments')}</h4>
<For each={topComments()}>
{(comment) => {
return (
<div class={styles.comment}>
<div class={clsx('text-truncate', styles.commentBody)}>
2024-06-24 17:50:27 +00:00
<A
href={`article/${comment.shout.slug}?commentId=${comment.id}`}
innerHTML={comment.body || ''}
/>
</div>
<div class={styles.commentDetails}>
2023-12-20 16:54:20 +00:00
<AuthorLink author={comment.created_by as Author} size={'XS'} />
<CommentDate comment={comment} isShort={true} isLastInRow={true} />
</div>
<div class={clsx('text-truncate', styles.commentArticleTitle)}>
<a href={`/${comment.shout.slug}`}>{comment.shout.title}</a>
</div>
</div>
)
}}
</For>
</section>
</Show>
<Show when={topTopics().length > 0}>
<section class={styles.asideSection}>
<h4>{t('Hot topics')}</h4>
<For each={topTopics().slice(0, 7)}>
{(topic) => (
<span class={clsx(stylesTopic.shoutTopic, styles.topic)}>
<a href={`/topic/${topic.slug}`}>{topic.title}</a>{' '}
</span>
)}
</For>
</section>
</Show>
<section class={clsx(styles.asideSection, styles.pinnedLinks)}>
<h4>{t('Knowledge base')}</h4>
<ul class="nodash">
<li>
2024-06-24 17:50:27 +00:00
<A href={'/guide'}>Как устроен Дискурс</A>
</li>
<li>
2024-06-24 17:50:27 +00:00
<A href="/how-to-write-a-good-article">Как создать хороший текст</A>
</li>
<li>
2024-06-24 17:50:27 +00:00
<A href="#">Правила конструктивных дискуссий</A>
</li>
<li>
2024-06-24 17:50:27 +00:00
<A href={'/principles'}>Принципы сообщества</A>
</li>
</ul>
</section>
2024-01-13 14:14:35 +00:00
<Show when={unratedArticles()}>
<section class={clsx(styles.asideSection)}>
<h4>{t('Be the first to rate')}</h4>
<For each={unratedArticles()}>
{(article) => (
<ArticleCard article={article} settings={{ noimage: true, nodate: true }} />
)}
</For>
</section>
</Show>
</Show>
</aside>
</div>
2024-01-08 13:02:52 +00:00
<Show when={shareData()}>
<ShareModal
2024-06-24 17:50:27 +00:00
title={shareData()?.title || ''}
description={shareData()?.description || ''}
imageUrl={shareData()?.cover || ''}
shareUrl={getShareUrl({ pathname: `/${shareData()?.slug || ''}` })}
2024-01-08 13:02:52 +00:00
/>
</Show>
2024-02-03 05:19:15 +00:00
<Modal variant="medium" name="inviteCoauthors">
<InviteMembers variant={'coauthors'} title={t('Invite experts')} />
</Modal>
</div>
2022-09-09 11:53:35 +00:00
)
}