webapp/src/components/Feed/ArticleCard.tsx

319 lines
12 KiB
TypeScript
Raw Normal View History

2023-05-16 19:17:47 +00:00
import { createMemo, createSignal, For, Show } from 'solid-js'
2023-05-07 13:47:10 +00:00
import type { Shout } from '../../graphql/types.gen'
import { capitalize, formatDate } from '../../utils'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2023-05-01 18:32:32 +00:00
import styles from './ArticleCard.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-05-08 17:21:06 +00:00
import { getPagePath, openPage } from '@nanostores/router'
2023-04-20 14:01:15 +00:00
import { router, useRouter } from '../../stores/router'
2023-05-11 14:38:20 +00:00
import { imageProxy } from '../../utils/imageProxy'
import { Popover } from '../_shared/Popover'
2023-06-21 20:32:16 +00:00
import { AuthorCard } from '../Author/AuthorCard'
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
2023-07-09 18:34:59 +00:00
withViewed?: boolean
noAuthorLink?: 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 formatDate(new Date(props.article.createdAt), { month: 'long', day: 'numeric', year: 'numeric' })
2022-09-22 09:37:49 +00:00
})
const { title, subtitle } = getTitleAndSubtitle(props.article)
2023-05-08 17:21:06 +00:00
const { id, cover, layout, slug, authors, stat, body } = props.article
2022-09-09 11:53:35 +00:00
2023-04-20 14:01:15 +00:00
const { changeSearchParam } = useRouter()
const scrollToComments = (event) => {
event.preventDefault()
openPage(router, 'article', { slug })
2023-04-20 14:01:15 +00:00
changeSearchParam('scrollTo', 'comments')
}
const [isActionPopupActive, setIsActionPopupActive] = createSignal(false)
2023-05-16 19:17:47 +00:00
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,
[styles.shoutCardNoImage]: !cover
2022-09-09 11:53:35 +00:00
}}
>
2023-06-21 20:32:16 +00:00
<Show when={!props.settings?.noimage && cover && !props.settings?.isFeedMode}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardCoverContainer}>
<div class={styles.shoutCardCover}>
2023-05-11 14:38:20 +00:00
<img src={imageProxy(cover)} alt={title || ''} loading="lazy" />
2022-09-28 10:34:21 +00:00
</div>
</div>
</Show>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardContent}>
2023-06-21 20:32:16 +00:00
<Show
when={
layout &&
layout !== 'article' &&
!(props.settings?.noicon || props.settings?.noimage) &&
!props.settings?.isFeedMode
}
>
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} />
2023-07-09 18:34:59 +00:00
{/*<Icon name={`${layout}-hover`} class={clsx(styles.icon, styles.iconHover)} />*/}
2022-09-28 10:34:21 +00:00
</a>
2022-09-09 11:53:35 +00:00
</div>
</Show>
<Show when={!props.settings?.isGroup && mainTopic}>
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}
2023-06-21 20:32:16 +00:00
isFeedMode={true}
2023-06-05 21:56:36 +00:00
class={clsx(styles.shoutTopic, { [styles.shoutTopicTop]: props.settings.isShort })}
2022-10-19 14:26:49 +00:00
/>
2022-09-28 10:34:21 +00:00
</Show>
2022-09-09 11:53:35 +00:00
2023-06-21 20:32:16 +00:00
<div
class={clsx(styles.shoutCardTitlesContainer, {
[styles.shoutCardTitlesContainerFeedMode]: props.settings?.isFeedMode
})}
>
<a href={`/${slug || ''}`}>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardTitle}>
2023-07-09 18:34:59 +00:00
<span class={styles.shoutCardLinkWrapper}>
<span class={styles.shoutCardLinkContainer}>{title}</span>
</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}>
2023-06-21 20:32:16 +00:00
<div
class={clsx(styles.shoutDetails, { [styles.shoutDetailsFeedMode]: props.settings?.isFeedMode })}
>
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}>
2023-06-21 20:32:16 +00:00
{(author) => {
2022-09-28 10:34:21 +00:00
return (
2023-06-21 20:32:16 +00:00
<AuthorCard
author={author}
hideWriteButton={true}
hideFollow={true}
isFeedMode={true}
hasLink={!props.settings?.noAuthorLink}
2023-06-21 20:32:16 +00:00
/>
2022-09-28 10:34:21 +00:00
)
}}
</For>
</div>
</Show>
<Show when={!props.settings?.nodate}>
<time class={styles.shoutDate}>{formattedDate()}</time>
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}>
2023-06-21 20:32:16 +00:00
<Show when={!props.settings?.noimage && cover}>
<div class={styles.shoutCardCoverContainer}>
<Show
when={
layout && layout !== 'article' && !(props.settings?.noicon || props.settings?.noimage)
}
>
<div class={styles.shoutCardType}>
<a href={`/expo/${layout}`}>
2023-07-09 18:34:59 +00:00
<Icon name={layout} class={styles.icon} />
{/*<Icon name={`${layout}-hover`} class={clsx(styles.icon, styles.iconHover)} />*/}
2023-06-21 20:32:16 +00:00
</a>
</div>
</Show>
<div class={styles.shoutCardCover}>
<img src={imageProxy(cover)} alt={title || ''} loading="lazy" />
</div>
</div>
</Show>
2023-05-16 19:17:47 +00:00
<section
class={styles.shoutCardDetails}
classList={{ [styles.shoutCardDetailsActive]: isActionPopupActive() }}
2023-05-16 19:17:47 +00:00
>
2022-10-25 21:45:37 +00:00
<div class={styles.shoutCardDetailsContent}>
<ShoutRatingControl shout={props.article} class={styles.shoutCardDetailsItem} />
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-20 14:01:15 +00:00
<a href="#" onClick={(event) => scrollToComments(event)}>
2023-01-25 22:13:01 +00:00
<Icon name="comment" class={clsx(styles.icon, styles.feedControlIcon)} />
<Icon
name="comment-hover"
class={clsx(styles.icon, styles.iconHover, styles.feedControlIcon)}
/>
2023-06-27 22:47:47 +00:00
<span class={styles.shoutCardLinkContainer}>{stat?.commented || t('Add comment')}</span>
</a>
2022-09-09 11:53:35 +00:00
</div>
2023-07-09 18:34:59 +00:00
<Show when={props.settings?.withViewed}>
<div class={clsx(styles.shoutCardDetailsItem, styles.shoutCardDetailsViewed)}>
<Icon name="eye" class={clsx(styles.icon, styles.feedControlIcon)} />
{stat?.viewed}
</div>
</Show>
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}>
<Popover content={t('Edit')}>
{(triggerRef: (el) => void) => (
<div class={styles.shoutCardDetailsItem} ref={triggerRef}>
<a href={getPagePath(router, 'edit', { shoutId: id.toString() })}>
<Icon name="pencil-outline" class={clsx(styles.icon, styles.feedControlIcon)} />
<Icon
name="pencil-outline-hover"
class={clsx(styles.icon, styles.iconHover, styles.feedControlIcon)}
/>
</a>
</div>
)}
</Popover>
2022-09-28 10:34:21 +00:00
<Popover content={t('Add to bookmarks')}>
{(triggerRef: (el) => void) => (
<div class={styles.shoutCardDetailsItem} ref={triggerRef}>
2023-01-25 22:13:01 +00:00
<button>
<Icon name="bookmark" class={clsx(styles.icon, styles.feedControlIcon)} />
<Icon
name="bookmark-hover"
class={clsx(styles.icon, styles.iconHover, styles.feedControlIcon)}
/>
2023-01-25 22:13:01 +00:00
</button>
</div>
)}
</Popover>
<Popover content={t('Share')} disabled={isActionPopupActive()}>
{(triggerRef: (el) => void) => (
<div class={styles.shoutCardDetailsItem} ref={triggerRef}>
<SharePopup
containerCssClass={stylesHeader.control}
title={title}
description={getDescription(body)}
imageUrl={cover}
shareUrl={getShareUrl({ pathname: `/${slug}` })}
isVisible={(value) => setIsActionPopupActive(value)}
trigger={
<button>
<Icon name="share-outline" class={clsx(styles.icon, styles.feedControlIcon)} />
<Icon
name="share-outline-hover"
class={clsx(styles.icon, styles.iconHover, styles.feedControlIcon)}
/>
</button>
}
/>
</div>
)}
</Popover>
2023-01-25 22:13:01 +00:00
<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}` })}
isVisible={(value) => setIsActionPopupActive(value)}
2023-02-06 21:35:08 +00:00
trigger={
<button>
<Icon name="ellipsis" class={clsx(styles.icon, styles.feedControlIcon)} />
<Icon
name="ellipsis"
class={clsx(styles.icon, styles.iconHover, styles.feedControlIcon)}
/>
2023-02-06 21:35:08 +00:00
</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>
)
}