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

331 lines
12 KiB
TypeScript
Raw Normal View History

2024-01-31 12:34:15 +00:00
import type { Author, Community } from '../../../graphql/schema/core.gen'
import { openPage, redirectPage } from '@nanostores/router'
import { clsx } from 'clsx'
2024-02-05 15:04:23 +00:00
import { For, Show, createEffect, createMemo, createSignal, onMount } from 'solid-js'
2024-01-31 12:34:15 +00:00
import { useFollowing } from '../../../context/following'
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 { isAuthor } from '../../../utils/isAuthor'
import { translit } from '../../../utils/ru2en'
2024-02-04 17:40:15 +00:00
import { isCyrillic } from '../../../utils/translate'
2024-02-04 11:25:21 +00:00
import { SharePopup, getShareUrl } from '../../Article/SharePopup'
import { Modal } from '../../Nav/Modal'
import { TopicBadge } from '../../Topic/TopicBadge'
2024-02-04 11:25:21 +00:00
import { Button } from '../../_shared/Button'
import { ShowOnlyOnClient } from '../../_shared/ShowOnlyOnClient'
import { AuthorBadge } from '../AuthorBadge'
import { Userpic } from '../Userpic'
2023-11-08 20:42:13 +00:00
import stylesButton from '../../_shared/Button/Button.module.scss'
2024-02-04 11:25:21 +00:00
import styles from './AuthorCard.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()
2024-02-04 17:40:15 +00:00
const { author, isSessionLoaded, requireAuthentication } = useSession()
2024-01-31 12:34:15 +00:00
const [authorSubs, setAuthorSubs] = createSignal<Array<Author | Topic | Community>>([])
const [subscriptionFilter, setSubscriptionFilter] = createSignal<SubscriptionFilter>('all')
const [isFollowed, setIsFollowed] = createSignal<boolean>()
2023-12-03 10:22:42 +00:00
const isProfileOwner = createMemo(() => author()?.slug === props.author.slug)
2024-02-05 12:34:47 +00:00
const { setFollowing, isOwnerSubscribed } = useFollowing()
2024-02-04 17:40:15 +00:00
2024-02-05 11:11:46 +00:00
onMount(() => {
setAuthorSubs(props.following)
})
createEffect(() => {
setIsFollowed(isOwnerSubscribed(props.author?.id))
})
2022-12-07 20:11:32 +00:00
const name = createMemo(() => {
2023-12-28 00:30:09 +00:00
if (lang() !== 'ru' && isCyrillic(props.author.name)) {
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 = () => {
2024-01-31 12:34:15 +00:00
// eslint-disable-next-line solid/reactivity
requireAuthentication(() => {
2024-02-05 15:04:23 +00:00
openPage(router, 'inbox')
changeSearchParams({
initChat: props.author.id.toString(),
})
}, 'discussions')
2022-12-17 03:27:00 +00:00
}
createEffect(() => {
if (props.following) {
2024-01-31 12:34:15 +00:00
if (subscriptionFilter() === 'authors') {
setAuthorSubs(props.following.filter((s) => 'name' in s))
} else if (subscriptionFilter() === 'topics') {
2024-01-31 12:34:15 +00:00
setAuthorSubs(props.following.filter((s) => 'title' in s))
} else if (subscriptionFilter() === 'communities') {
setAuthorSubs(props.following.filter((s) => 'title' in s))
} else {
2024-01-31 12:34:15 +00:00
setAuthorSubs(props.following)
}
}
})
2024-01-31 12:34:15 +00:00
const handleFollowClick = () => {
const value = !isFollowed()
2024-01-31 12:34:15 +00:00
requireAuthentication(() => {
setIsFollowed(value)
2024-01-31 12:34:15 +00:00
setFollowing(FollowingEntity.Author, props.author.slug, value)
}, 'subscribe')
}
2023-11-13 18:56:47 +00:00
2024-01-31 12:34:15 +00:00
const followButtonText = createMemo(() => {
2024-02-05 11:11:46 +00:00
if (isOwnerSubscribed(props.author?.id)) {
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}>
2024-01-27 06:21:48 +00:00
<a href="?m=followers" class={styles.subscribers}>
2023-10-16 22:14:43 +00:00
<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}>
2024-01-27 06:21:48 +00:00
<a href="?m=following" class={styles.subscribers}>
2023-10-16 22:14:43 +00:00
<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}
/>
)
2024-02-04 09:23:20 +00:00
}
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
}
2024-02-04 09:23:20 +00:00
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"
2024-02-04 09:23:20 +00:00
rel="nofollow noopener noreferrer"
2023-10-16 22:14:43 +00:00
>
<span class={styles.authorSubscribeSocialLabel}>
{link.startsWith('http') ? link : `https://${link}`}
</span>
</a>
)}
</For>
</div>
</Show>
<Show
when={isProfileOwner()}
fallback={
<div class={styles.authorActions}>
2024-02-05 11:11:46 +00:00
<Show when={authorSubs().length}>
<Button
onClick={handleFollowClick}
value={followButtonText()}
isSubscribeButton={true}
class={clsx({
[stylesButton.subscribed]: isFollowed(),
})}
/>
</Show>
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}>
<Modal variant="medium" isResponsive={true} 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}>
2024-02-05 11:11:46 +00:00
{(follower: Author) => (
<AuthorBadge
author={follower}
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(follower.id),
}}
/>
)}
2023-10-13 06:10:24 +00:00
</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}>
<Modal variant="medium" isResponsive={true} 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>
2024-01-31 12:34:15 +00:00
<li class={clsx({ 'view-switcher__item--selected': subscriptionFilter() === 'authors' })}>
<button type="button" onClick={() => setSubscriptionFilter('authors')}>
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">
2024-01-31 12:34:15 +00:00
<For each={authorSubs()}>
2023-10-13 06:10:24 +00:00
{(subscription) =>
isAuthor(subscription) ? (
2024-02-05 11:11:46 +00:00
<AuthorBadge
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(subscription.id),
}}
author={subscription}
/>
2023-10-13 06:10:24 +00:00
) : (
2024-02-08 09:11:52 +00:00
<TopicBadge
isFollowed={{
loaded: Boolean(authorSubs()),
value: isOwnerSubscribed(subscription.id),
}}
topic={subscription}
/>
2023-10-13 06:10:24 +00:00
)
}
</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
)
}