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

322 lines
11 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Author } from '../../../graphql/schema/core.gen'
import { openPage, redirectPage } from '@nanostores/router'
import { clsx } from 'clsx'
import { createEffect, createMemo, createSignal, For, Show } from 'solid-js'
import { useLocalize } from '../../../context/localize'
import { useSession } from '../../../context/session'
2023-11-28 13:18:25 +00:00
import { FollowingEntity, Topic } from '../../../graphql/schema/core.gen'
import { SubscriptionFilter } from '../../../pages/types'
import { router, useRouter } from '../../../stores/router'
import { follow, unfollow } from '../../../stores/zine/common'
import { isAuthor } from '../../../utils/isAuthor'
import { translit } from '../../../utils/ru2en'
import { Button } from '../../_shared/Button'
import { ShowOnlyOnClient } from '../../_shared/ShowOnlyOnClient'
import { getShareUrl, SharePopup } from '../../Article/SharePopup'
import { Modal } from '../../Nav/Modal'
import { TopicBadge } from '../../Topic/TopicBadge'
import { AuthorBadge } from '../AuthorBadge'
import { Userpic } from '../Userpic'
import styles from './AuthorCard.module.scss'
2023-11-08 20:42:13 +00:00
import stylesButton from '../../_shared/Button/Button.module.scss'
2022-09-09 11:53:35 +00:00
type Props = {
2022-09-09 11:53:35 +00:00
author: Author
followers?: Author[]
following?: Array<Author | Topic>
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 {
2023-12-03 10:22:42 +00:00
author,
subscriptions,
2022-12-06 16:03:55 +00:00
isSessionLoaded,
actions: { loadSubscriptions, requireAuthentication },
} = useSession()
const [isSubscribing, setIsSubscribing] = createSignal(false)
const [following, setFollowing] = createSignal<Array<Author | Topic>>(props.following)
const [subscriptionFilter, setSubscriptionFilter] = createSignal<SubscriptionFilter>('all')
2022-09-30 14:22:33 +00:00
const subscribed = createMemo<boolean>(() =>
2023-12-14 00:04:07 +00:00
subscriptions().authors.some((a: Author) => a.slug === props.author.slug),
)
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 loadSubscriptions()
setIsSubscribing(false)
}
2023-12-03 10:22:42 +00:00
const isProfileOwner = createMemo(() => author()?.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
const { changeSearchParams } = useRouter()
2022-12-17 03:27:00 +00:00
const initChat = () => {
requireAuthentication(() => {
openPage(router, `inbox`)
changeSearchParams({
initChat: props.author.id.toString(),
})
}, 'discussions')
2022-12-17 03:27:00 +00:00
}
const handleSubscribe = () => {
requireAuthentication(() => {
subscribe(!subscribed())
}, '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-11-13 18:56:47 +00:00
const followButtonText = createMemo(() => {
if (isSubscribing()) {
2023-11-02 22:02:11 +00:00
return t('subscribing...')
2023-11-13 18:56:47 +00:00
}
if (subscribed()) {
return (
<>
2023-11-08 20:42:13 +00:00
<span class={stylesButton.buttonSubscribeLabel}>{t('Following')}</span>
<span class={stylesButton.buttonSubscribeLabelHovered}>{t('Unfollow')}</span>
</>
)
}
2023-11-13 18:56:47 +00:00
return t('Follow')
})
2023-10-13 06:10:24 +00:00
return (
<div class={clsx(styles.author, 'row')}>
<div class="col-md-5">
<Userpic
size={'XL'}
name={props.author.name}
2023-12-03 10:22:42 +00:00
userpic={props.author.pic}
slug={props.author.slug}
class={styles.circlewrap}
/>
</div>
<div class={clsx('col-md-15 col-xl-13', styles.authorDetails)}>
2023-10-13 06:10:24 +00:00
<div class={styles.authorDetailsWrapper}>
<div class={styles.authorName}>{name()}</div>
2023-11-28 17:06:13 +00:00
<Show when={props.author.bio}>
<div class={styles.authorAbout} innerHTML={props.author.bio} />
</Show>
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) => (
2023-12-03 10:22:42 +00:00
<Userpic size={'XS'} name={f.name} userpic={f.pic} class={styles.subscribersItem} />
)}
2023-10-16 22:14:43 +00:00
</For>
<div class={styles.subscribersCounter}>
{t('SubscriberWithCount', { count: props.followers.length ?? 0 })}
2023-10-16 22:14:43 +00:00
</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
size={'XS'}
name={f.name}
2023-12-03 10:22:42 +00:00
userpic={f.pic}
class={styles.subscribersItem}
/>
)
2023-10-16 22:14:43 +00:00
} else if ('title' in f) {
return (
<Userpic
size={'XS'}
name={f.title}
userpic={f.pic}
class={styles.subscribersItem}
/>
)
2023-10-16 22:14:43 +00:00
}
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>
2022-09-09 11:53:35 +00:00
<Show
when={isProfileOwner()}
fallback={
<div class={styles.authorActions}>
2023-11-08 20:52:56 +00:00
<Button
onClick={handleSubscribe}
value={followButtonText()}
isSubscribeButton={true}
2023-11-08 21:09:50 +00:00
class={clsx({
[stylesButton.subscribed]: subscribed(),
2023-11-08 21:09:50 +00:00
})}
2023-11-08 20:52:56 +00:00
/>
2023-10-31 16:44:00 +00:00
<Button
variant={'secondary'}
value={t('Message')}
onClick={initChat}
2023-11-02 22:02:11 +00:00
class={styles.buttonWriteMessage}
2023-10-31 16:44:00 +00:00
/>
</div>
}
>
<div class={styles.authorActions}>
2023-10-16 22:14:43 +00:00
<Button
variant="secondary"
onClick={() => redirectPage(router, 'profileSettings')}
2023-11-28 17:06:13 +00:00
value={
<>
<span class={styles.authorActionsLabel}>{t('Edit profile')}</span>
<span class={styles.authorActionsLabelMobile}>{t('Edit')}</span>
</>
}
2023-10-16 22:14:43 +00:00
/>
<SharePopup
title={props.author.name}
description={props.author.bio}
2023-12-03 10:22:42 +00:00
imageUrl={props.author.pic}
2023-10-16 22:14:43 +00:00
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')}>
2023-12-27 23:35:43 +00:00
{t('Authors')}
2023-10-13 06:10:24 +00:00
</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
)
}