webapp/src/components/Feed/Card.tsx

243 lines
8.4 KiB
TypeScript
Raw Normal View History

2023-02-07 13:20:07 +00:00
import { createMemo, For, Show } from 'solid-js'
2022-09-22 09:37:49 +00:00
import type { Shout } from '../../graphql/types.gen'
2022-09-09 11:53:35 +00:00
import { capitalize } from '../../utils'
import { translit } from '../../utils/ru2en'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-10-25 21:45:37 +00:00
import styles from './Card.module.scss'
2022-10-19 14:26:49 +00:00
import { clsx } from 'clsx'
import { CardTopic } from './CardTopic'
import { ShoutRatingControl } from '../Article/ShoutRatingControl'
2023-01-31 13:58:28 +00:00
import { getShareUrl, SharePopup } from '../Article/SharePopup'
2023-01-25 22:13:01 +00:00
import stylesHeader from '../Nav/Header.module.scss'
import { getDescription } from '../../utils/meta'
2023-02-06 21:35:08 +00:00
import { FeedArticlePopup } from './FeedArticlePopup'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
2023-04-01 04:20:14 +00:00
import { openPage } from '@nanostores/router'
import { router, useRouter } from '../../stores/router'
2022-09-09 11:53:35 +00:00
2023-04-01 04:20:14 +00:00
type ArticleSearchParams = {
scrollTo: 'comments'
}
2022-09-09 11:53:35 +00:00
interface ArticleCardProps {
settings?: {
noicon?: boolean
noimage?: boolean
nosubtitle?: boolean
noauthor?: boolean
nodate?: boolean
isGroup?: boolean
photoBottom?: boolean
additionalClass?: string
isFeedMode?: boolean
2022-10-19 14:26:49 +00:00
isFloorImportant?: boolean
isWithCover?: boolean
isBigTitle?: boolean
isVertical?: boolean
isShort?: boolean
withBorder?: boolean
isCompact?: boolean
isSingle?: boolean
2022-11-16 21:08:04 +00:00
isBeside?: boolean
2022-09-09 11:53:35 +00:00
}
article: Shout
}
2022-09-22 09:37:49 +00:00
const getTitleAndSubtitle = (article: Shout): { title: string; subtitle: string } => {
let title = article.title
let subtitle = article.subtitle
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
if (!subtitle) {
let tt = article.title?.split('. ') || []
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
if (tt?.length === 1) {
tt = article.title?.split(/{!|\?|:|;}\s/) || []
}
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
if (tt && tt.length > 1) {
const sep = article.title?.replace(tt[0], '').split(' ', 1)[0]
2022-11-21 13:02:04 +00:00
title = tt[0] + (sep === '.' || sep === ':' ? '' : sep)
2022-09-22 09:37:49 +00:00
subtitle = capitalize(article.title?.replace(tt[0] + sep, ''), true)
2022-09-09 11:53:35 +00:00
}
}
2022-09-22 09:37:49 +00:00
return { title, subtitle }
}
export const ArticleCard = (props: ArticleCardProps) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
2022-12-07 18:38:05 +00:00
const mainTopic =
props.article.topics.find((articleTopic) => articleTopic.slug === props.article.mainTopic) ||
props.article.topics[0]
2022-09-22 09:37:49 +00:00
const formattedDate = createMemo<string>(() => {
return new Date(props.article.createdAt)
2023-02-17 09:21:02 +00:00
.toLocaleDateString(lang(), { month: 'long', day: 'numeric', year: 'numeric' })
2022-09-22 09:37:49 +00:00
.replace(' г.', '')
})
const { title, subtitle } = getTitleAndSubtitle(props.article)
const { cover, layout, slug, authors, stat, body } = props.article
2022-09-09 11:53:35 +00:00
2023-04-01 04:20:14 +00:00
const { changeSearchParam } = useRouter<ArticleSearchParams>()
const scrollToComments = () => {
openPage(router, 'article', { slug: slug })
changeSearchParam('scrollTo', 'comments')
}
2022-09-09 11:53:35 +00:00
return (
<section
2022-10-25 21:45:37 +00:00
class={clsx(styles.shoutCard, `${props.settings?.additionalClass || ''}`)}
2022-09-09 11:53:35 +00:00
classList={{
2022-10-25 21:45:37 +00:00
[styles.shoutCardShort]: props.settings?.isShort,
[styles.shoutCardPhotoBottom]: props.settings?.noimage && props.settings?.photoBottom,
[styles.shoutCardFeed]: props.settings?.isFeedMode,
[styles.shoutCardFloorImportant]: props.settings?.isFloorImportant,
[styles.shoutCardWithCover]: props.settings?.isWithCover,
[styles.shoutCardBigTitle]: props.settings?.isBigTitle,
[styles.shoutCardVertical]: props.settings?.isVertical,
[styles.shoutCardWithBorder]: props.settings?.withBorder,
[styles.shoutCardCompact]: props.settings?.isCompact,
2022-11-16 21:08:04 +00:00
[styles.shoutCardSingle]: props.settings?.isSingle,
[styles.shoutCardBeside]: props.settings?.isBeside
2022-09-09 11:53:35 +00:00
}}
>
2022-09-28 10:34:21 +00:00
<Show when={!props.settings?.noimage && cover}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardCoverContainer}>
<div class={styles.shoutCardCover}>
2022-09-28 10:34:21 +00:00
<img src={cover || ''} alt={title || ''} loading="lazy" />
</div>
</div>
</Show>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardContent}>
2022-09-28 10:34:21 +00:00
<Show when={layout && layout !== 'article' && !(props.settings?.noicon || props.settings?.noimage)}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardType}>
2022-11-14 06:52:08 +00:00
<a href={`/expo/${layout}`}>
2022-10-25 21:45:37 +00:00
<Icon name={layout} class={styles.icon} />
2022-09-28 10:34:21 +00:00
</a>
2022-09-09 11:53:35 +00:00
</div>
</Show>
2022-09-28 10:34:21 +00:00
<Show when={!props.settings?.isGroup}>
2022-10-19 14:26:49 +00:00
<CardTopic
title={
2023-02-17 09:21:02 +00:00
lang() === 'ru' && mainTopic.title ? mainTopic.title : mainTopic?.slug?.replace('-', ' ')
2022-10-19 14:26:49 +00:00
}
slug={mainTopic.slug}
isFloorImportant={props.settings?.isFloorImportant}
/>
2022-09-28 10:34:21 +00:00
</Show>
2022-09-09 11:53:35 +00:00
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardTitlesContainer}>
<a href={`/${slug || ''}`}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardTitle}>
<span class={styles.shoutCardLinkContainer}>{title}</span>
2022-09-09 11:53:35 +00:00
</div>
2022-09-28 10:34:21 +00:00
<Show when={!props.settings?.nosubtitle && subtitle}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardSubtitle}>
<span class={styles.shoutCardLinkContainer}>{subtitle}</span>
2022-09-09 11:53:35 +00:00
</div>
2022-09-28 10:34:21 +00:00
</Show>
</a>
</div>
2022-09-09 11:53:35 +00:00
2022-09-28 10:34:21 +00:00
<Show when={!props.settings?.noauthor || !props.settings?.nodate}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutDetails}>
2022-09-28 10:34:21 +00:00
<Show when={!props.settings?.noauthor}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutAuthor}>
2022-09-28 10:34:21 +00:00
<For each={authors}>
{(author, index) => {
2022-12-07 20:11:32 +00:00
let name = author.name
2023-02-17 09:21:02 +00:00
if (lang() !== 'ru') {
2022-12-07 20:11:32 +00:00
name = name === 'Дискурс' ? 'Discours' : translit(name)
}
2022-09-28 10:34:21 +00:00
return (
<>
<Show when={index() > 0}>, </Show>
<a href={`/author/${author.slug}`}>{name}</a>
</>
)
}}
</For>
</div>
</Show>
<Show when={!props.settings?.nodate}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutDate}>{formattedDate()}</div>
2022-09-28 10:34:21 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</div>
2022-09-28 10:34:21 +00:00
</Show>
2022-09-09 11:53:35 +00:00
2022-09-28 10:34:21 +00:00
<Show when={props.settings?.isFeedMode}>
2022-10-25 21:45:37 +00:00
<section class={styles.shoutCardDetails}>
<div class={styles.shoutCardDetailsContent}>
<ShoutRatingControl shout={props.article} class={styles.shoutCardDetailsItem} />
2023-01-25 22:13:01 +00:00
<div class={clsx(styles.shoutCardDetailsItem, styles.shoutCardDetailsViewed)}>
<Icon name="eye" class={clsx(styles.icon, styles.feedControlIcon)} />
2022-09-28 10:34:21 +00:00
{stat?.viewed}
</div>
2023-01-25 22:13:01 +00:00
2022-10-25 21:45:37 +00:00
<div class={clsx(styles.shoutCardDetailsItem, styles.shoutCardComments)}>
2023-04-01 04:20:14 +00:00
<button type="button" onClick={scrollToComments}>
2023-01-25 22:13:01 +00:00
<Icon name="comment" class={clsx(styles.icon, styles.feedControlIcon)} />
{stat?.commented || t('Add comment')}
2023-04-01 04:20:14 +00:00
</button>
2022-09-09 11:53:35 +00:00
</div>
2023-01-25 22:13:01 +00:00
</div>
2022-09-09 11:53:35 +00:00
2023-01-25 22:13:01 +00:00
<div class={styles.shoutCardDetailsContent}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardDetailsItem}>
2022-09-28 10:34:21 +00:00
<button>
2023-01-25 22:13:01 +00:00
<Icon name="pencil-outline" class={clsx(styles.icon, styles.feedControlIcon)} />
2022-09-28 10:34:21 +00:00
</button>
</div>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardDetailsItem}>
2022-09-28 10:34:21 +00:00
<button>
2023-01-25 22:13:01 +00:00
<Icon name="bookmark" class={clsx(styles.icon, styles.feedControlIcon)} />
2022-09-28 10:34:21 +00:00
</button>
</div>
2023-01-25 22:13:01 +00:00
<div class={styles.shoutCardDetailsItem}>
<SharePopup
containerCssClass={stylesHeader.control}
2023-02-07 13:20:07 +00:00
title={title}
description={getDescription(body)}
imageUrl={cover}
2023-01-31 13:58:28 +00:00
shareUrl={getShareUrl({ pathname: `/${slug}` })}
2023-01-25 22:13:01 +00:00
trigger={
<button>
<Icon name="share-outline" class={clsx(styles.icon, styles.feedControlIcon)} />
</button>
}
/>
</div>
<div class={styles.shoutCardDetailsItem}>
2023-02-06 21:35:08 +00:00
<FeedArticlePopup
containerCssClass={stylesHeader.control}
2023-02-07 13:20:07 +00:00
title={title}
description={getDescription(body)}
imageUrl={cover}
2023-02-06 21:35:08 +00:00
shareUrl={getShareUrl({ pathname: `/${slug}` })}
trigger={
<button>
<Icon name="ellipsis" class={clsx(styles.icon, styles.feedControlIcon)} />
</button>
}
/>
2023-01-25 22:13:01 +00:00
</div>
</div>
2022-09-28 10:34:21 +00:00
</section>
</Show>
</div>
2022-09-09 11:53:35 +00:00
</section>
)
}