webapp/src/components/Author/AuthorCard/AuthorCard.tsx

435 lines
16 KiB
TypeScript
Raw Normal View History

import type { Author } from '../../../graphql/types.gen'
import { Userpic } from '../Userpic'
import { Icon } from '../../_shared/Icon'
2023-05-01 18:32:32 +00:00
import styles from './AuthorCard.module.scss'
import { createEffect, createMemo, createSignal, For, Match, Show, Switch } from 'solid-js'
import { translit } from '../../../utils/ru2en'
import { follow, unfollow } from '../../../stores/zine/common'
import { clsx } from 'clsx'
import { useSession } from '../../../context/session'
import { ShowOnlyOnClient } from '../../_shared/ShowOnlyOnClient'
import { FollowingEntity, Topic } from '../../../graphql/types.gen'
import { router, useRouter } from '../../../stores/router'
import { openPage, redirectPage } from '@nanostores/router'
import { useLocalize } from '../../../context/localize'
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
import { Modal } from '../../Nav/Modal'
import { SubscriptionFilter } from '../../../pages/types'
import { isAuthor } from '../../../utils/isAuthor'
import { AuthorBadge } from '../AuthorBadge'
import { TopicBadge } from '../../Topic/TopicBadge'
import { Button } from '../../_shared/Button'
import { getShareUrl, SharePopup } from '../../Article/SharePopup'
import stylesHeader from '../../Nav/Header/Header.module.scss'
2022-09-09 11:53:35 +00:00
type Props = {
2022-11-19 05:00:54 +00:00
caption?: string
hideWriteButton?: boolean
2022-09-09 11:53:35 +00:00
hideDescription?: boolean
hideFollow?: boolean
hasLink?: boolean
subscribed?: boolean
author: Author
isAuthorPage?: boolean
2022-10-19 21:40:50 +00:00
noSocialButtons?: boolean
2022-11-11 08:58:22 +00:00
isAuthorsList?: boolean
2022-11-16 21:08:04 +00:00
truncateBio?: boolean
liteButtons?: boolean
2023-05-17 20:27:24 +00:00
isTextButton?: boolean
2022-11-26 21:27:54 +00:00
isComments?: boolean
2023-01-26 21:42:47 +00:00
isFeedMode?: boolean
2023-02-13 13:48:05 +00:00
isNowrap?: boolean
2023-06-02 22:01:34 +00:00
class?: string
followers?: Author[]
following?: Array<Author | Topic>
2023-09-02 22:14:34 +00:00
showPublicationsCounter?: boolean
2023-10-05 06:20:23 +00:00
hideBio?: boolean
isCurrentUser?: boolean
2022-09-09 11:53:35 +00:00
}
export const AuthorCard = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
const {
session,
2022-12-06 16:03:55 +00:00
isSessionLoaded,
actions: { loadSession, requireAuthentication }
} = useSession()
const [isSubscribing, setIsSubscribing] = createSignal(false)
const [following, setFollowing] = createSignal<Array<Author | Topic>>(props.following)
const [subscriptionFilter, setSubscriptionFilter] = createSignal<SubscriptionFilter>('all')
const [userpicUrl, setUserpicUrl] = createSignal<string>()
2022-09-30 14:22:33 +00:00
const subscribed = createMemo<boolean>(() => {
return session()?.news?.authors?.some((u) => u === props.author.slug) || false
})
const subscribe = async (really = true) => {
setIsSubscribing(true)
await (really
? follow({ what: FollowingEntity.Author, slug: props.author.slug })
: unfollow({ what: FollowingEntity.Author, slug: props.author.slug }))
await loadSession()
setIsSubscribing(false)
}
2022-09-30 14:22:33 +00:00
const canFollow = createMemo(() => !props.hideFollow && session()?.user?.slug !== props.author.slug)
2022-11-21 00:12:20 +00:00
2022-12-07 20:11:32 +00:00
const name = createMemo(() => {
2023-02-17 09:21:02 +00:00
if (lang() !== 'ru') {
2022-12-07 20:11:32 +00:00
if (props.author.name === 'Дискурс') {
return 'Discours'
}
return translit(props.author.name)
}
return props.author.name
})
2022-09-09 11:53:35 +00:00
// TODO: reimplement AuthorCard
2022-12-17 03:27:00 +00:00
const { changeSearchParam } = useRouter()
const initChat = () => {
requireAuthentication(() => {
openPage(router, `inbox`)
changeSearchParam({
initChat: props.author.id.toString()
})
}, 'discussions')
2022-12-17 03:27:00 +00:00
}
const handleSubscribe = () => {
requireAuthentication(() => {
subscribe(true)
}, 'subscribe')
}
createEffect(() => {
if (props.following) {
if (subscriptionFilter() === 'users') {
setFollowing(props.following.filter((s) => 'name' in s))
} else if (subscriptionFilter() === 'topics') {
setFollowing(props.following.filter((s) => 'title' in s))
} else {
setFollowing(props.following)
}
}
})
2023-09-19 05:07:27 +00:00
if (props.isAuthorPage && props.author.userpic?.includes('assets.discours.io')) {
setUserpicUrl(props.author.userpic.replace('100x', '500x500'))
}
2022-09-09 11:53:35 +00:00
return (
2023-10-13 06:10:24 +00:00
<div
class={clsx(styles.author, props.class)}
classList={{
['row']: props.isAuthorPage,
[styles.authorPage]: props.isAuthorPage,
[styles.authorComments]: props.isComments,
[styles.authorsListItem]: props.isAuthorsList,
[styles.feedMode]: props.isFeedMode,
[styles.nowrapView]: props.isNowrap
}}
>
<Show
when={props.isAuthorPage}
fallback={
<Userpic
name={props.author.name}
userpic={props.author.userpic}
hasLink={props.hasLink}
isBig={props.isAuthorPage}
isAuthorsList={props.isAuthorsList}
isFeedMode={props.isFeedMode}
slug={props.author.slug}
class={styles.circlewrap}
/>
}
>
<div class="col-md-5">
<Userpic
name={props.author.name}
userpic={userpicUrl()}
hasLink={props.hasLink}
isBig={props.isAuthorPage}
isAuthorsList={props.isAuthorsList}
isFeedMode={props.isFeedMode}
slug={props.author.slug}
class={styles.circlewrap}
/>
</div>
</Show>
2023-08-27 21:21:40 +00:00
<div
2023-10-13 06:10:24 +00:00
class={styles.authorDetails}
2023-08-27 21:21:40 +00:00
classList={{
2023-10-13 06:10:24 +00:00
'col-md-15 col-xl-13': props.isAuthorPage,
[styles.authorDetailsShrinked]: props.isAuthorPage
2023-08-27 21:21:40 +00:00
}}
>
2023-10-13 06:10:24 +00:00
<div class={styles.authorDetailsWrapper}>
<div class={styles.authorNameContainer}>
<ConditionalWrapper
condition={props.hasLink}
wrapper={(children) => (
<a class={styles.authorName} href={`/author/${props.author.slug}`}>
{children}
</a>
)}
>
<span class={clsx({ [styles.authorName]: !props.hasLink })}>{name()}</span>
</ConditionalWrapper>
2023-09-02 22:14:34 +00:00
</div>
2023-10-13 06:10:24 +00:00
<Show
when={props.author.bio && !props.hideBio}
fallback={
props.showPublicationsCounter ? (
<div class={styles.authorAbout}>
{t('PublicationsWithCount', { count: props.author.stat?.shouts ?? 0 })}
</div>
) : (
''
)
}
>
<div
class={styles.authorAbout}
classList={{ 'text-truncate': props.truncateBio }}
innerHTML={props.author.bio}
/>
</Show>
2022-11-30 21:50:33 +00:00
2023-10-13 06:10:24 +00:00
<Show
when={
(props.followers && props.followers.length > 0) ||
(props.following && props.following.length > 0)
}
>
<div class={styles.subscribersContainer}>
2023-10-16 22:14:43 +00:00
<Show when={props.followers && props.followers.length > 0}>
<a href="?modal=followers" class={styles.subscribers}>
<For each={props.followers.slice(0, 3)}>
{(f) => <Userpic name={f.name} userpic={f.userpic} class={styles.userpic} />}
</For>
<div class={styles.subscribersCounter}>
{t('SubscriberWithCount', { count: props.followers.length })}
</div>
</a>
</Show>
2023-10-13 21:29:11 +00:00
2023-10-16 22:14:43 +00:00
<Show when={props.following && props.following.length > 0}>
<a href="?modal=following" class={styles.subscribers}>
<For each={props.following.slice(0, 3)}>
{(f) => {
if ('name' in f) {
return <Userpic name={f.name} userpic={f.userpic} class={styles.userpic} />
} else if ('title' in f) {
return <Userpic name={f.title} userpic={f.pic} class={styles.userpic} />
}
return null
}}
</For>
<div class={styles.subscribersCounter}>
{t('SubscriptionWithCount', { count: props?.following.length ?? 0 })}
</div>
</a>
</Show>
</div>
2023-10-13 06:10:24 +00:00
</Show>
</div>
<ShowOnlyOnClient>
2023-10-16 22:14:43 +00:00
<Show when={isSessionLoaded()}>
2023-10-17 06:11:44 +00:00
<Show when={props.author.links && props.author.links.length > 0}>
2023-10-16 22:14:43 +00:00
<div class={styles.authorSubscribeSocial}>
<For each={props.author.links}>
{(link) => (
<a
class={styles.socialLink}
href={link.startsWith('http') ? link : `https://${link}`}
target="_blank"
rel="nofollow noopener"
>
<span class={styles.authorSubscribeSocialLabel}>
{link.startsWith('http') ? link : `https://${link}`}
</span>
</a>
)}
</For>
</div>
</Show>
2023-10-13 06:10:24 +00:00
<Show when={canFollow()}>
<div class={styles.authorSubscribe}>
<Show
when={subscribed()}
fallback={
<button
2023-10-13 06:10:24 +00:00
onClick={handleSubscribe}
class={clsx('button', styles.button)}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList && !props.isTextButton,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList || props.isTextButton,
[styles.buttonWrite]: props.isAuthorsList || props.isTextButton,
[styles.isSubscribing]: isSubscribing()
}}
disabled={isSubscribing()}
>
<Show when={!props.isAuthorsList && !props.isTextButton && !props.isAuthorPage}>
2023-10-13 06:10:24 +00:00
<Icon name="author-subscribe" class={styles.icon} />
</Show>
<Show when={props.isTextButton || props.isAuthorPage}>
2023-10-13 06:10:24 +00:00
<span class={clsx(styles.buttonLabel, styles.buttonLabelVisible)}>
{t('Follow')}
</span>
</Show>
</button>
2023-10-13 06:10:24 +00:00
}
>
<button
onClick={() => subscribe(false)}
class={clsx('button', styles.button)}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList && !props.isTextButton,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList || props.isTextButton,
[styles.buttonWrite]: props.isAuthorsList || props.isTextButton,
[styles.isSubscribing]: isSubscribing()
}}
disabled={isSubscribing()}
>
<Show when={!props.isAuthorsList && !props.isTextButton && !props.isAuthorPage}>
<Icon name="author-unsubscribe" class={styles.icon} />
</Show>
<Show when={props.isTextButton || props.isAuthorPage}>
<span
class={clsx(
styles.buttonLabel,
styles.buttonLabelVisible,
styles.buttonUnfollowLabel
)}
>
{t('Unfollow')}
</span>
<span
class={clsx(
styles.buttonLabel,
styles.buttonLabelVisible,
styles.buttonSubscribedLabel
)}
>
2023-10-16 09:54:14 +00:00
{t('Following')}
2023-10-13 06:10:24 +00:00
</span>
</Show>
</button>
</Show>
2022-09-09 11:53:35 +00:00
2023-10-13 06:10:24 +00:00
<Show when={!props.hideWriteButton}>
<button
class={clsx(styles.button, styles.buttonSubscribe)}
classList={{
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.liteButtons && props.isAuthorsList
}}
onClick={initChat}
>
<Show when={!props.isTextButton && !props.isAuthorPage}>
<Icon name="comment" class={styles.icon} />
</Show>
2023-10-16 09:54:14 +00:00
<Show when={!props.liteButtons || props.isTextButton}>{t('Message')}</Show>
2023-10-13 06:10:24 +00:00
</button>
</Show>
</div>
2022-09-09 11:53:35 +00:00
</Show>
2023-10-16 22:14:43 +00:00
<Show when={props.isCurrentUser}>
<div class={styles.authorSubscribe}>
<Button
variant="secondary"
onClick={() => redirectPage(router, 'profileSettings')}
value={t('Edit profile')}
class={styles.button}
/>
<SharePopup
containerCssClass={styles.shareControl}
title={props.author.name}
description={props.author.bio}
imageUrl={props.author.userpic}
shareUrl={getShareUrl({ pathname: `/author/${props.author.slug}` })}
trigger={<Button variant="secondary" value={t('Share')} />}
/>
</div>
</Show>
2023-10-13 06:10:24 +00:00
</Show>
</ShowOnlyOnClient>
<Show when={props.followers}>
2023-10-15 12:09:31 +00:00
<Modal variant="medium" name="followers" maxHeight>
2023-10-13 06:10:24 +00:00
<>
<h2>{t('Followers')}</h2>
<div class={styles.listWrapper}>
<div class="row">
<div class="col-24">
<For each={props.followers}>
{(follower: Author) => <AuthorBadge author={follower} />}
</For>
</div>
2023-09-02 22:14:34 +00:00
</div>
</div>
2023-10-13 06:10:24 +00:00
</>
</Modal>
</Show>
<Show when={props.following}>
2023-10-15 12:09:31 +00:00
<Modal variant="medium" name="following" maxHeight>
2023-10-13 06:10:24 +00:00
<>
<h2>{t('Subscriptions')}</h2>
<ul class="view-switcher">
<li class={clsx({ 'view-switcher__item--selected': subscriptionFilter() === 'all' })}>
<button type="button" onClick={() => setSubscriptionFilter('all')}>
{t('All')}
</button>
<span class="view-switcher__counter">{props.following.length}</span>
</li>
<li class={clsx({ 'view-switcher__item--selected': subscriptionFilter() === 'users' })}>
<button type="button" onClick={() => setSubscriptionFilter('users')}>
{t('Users')}
</button>
<span class="view-switcher__counter">
{props.following.filter((s) => 'name' in s).length}
</span>
</li>
<li class={clsx({ 'view-switcher__item--selected': subscriptionFilter() === 'topics' })}>
<button type="button" onClick={() => setSubscriptionFilter('topics')}>
{t('Topics')}
</button>
<span class="view-switcher__counter">
{props.following.filter((s) => 'title' in s).length}
</span>
</li>
</ul>
<br />
<div class={styles.listWrapper}>
<div class="row">
<div class="col-24">
<For each={following()}>
{(subscription) =>
isAuthor(subscription) ? (
<AuthorBadge author={subscription} />
) : (
<TopicBadge topic={subscription} />
)
}
</For>
</div>
2023-09-02 22:14:34 +00:00
</div>
</div>
2023-10-13 06:10:24 +00:00
</>
</Modal>
</Show>
</div>
</div>
2022-09-09 11:53:35 +00:00
)
}