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

290 lines
10 KiB
TypeScript
Raw Normal View History

2024-01-31 12:34:15 +00:00
import type { Author, Community } from '../../../graphql/schema/core.gen'
import { clsx } from 'clsx'
2024-02-05 15:04:23 +00:00
import { For, Show, createEffect, createMemo, createSignal, onMount } from 'solid-js'
2024-06-24 17:50:27 +00:00
import { FollowsFilter, 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 { 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'
2024-06-06 06:32:35 +00:00
import { FollowingCounters } from '../../_shared/FollowingCounters/FollowingCounters'
2024-02-04 11:25:21 +00:00
import { ShowOnlyOnClient } from '../../_shared/ShowOnlyOnClient'
import { AuthorBadge } from '../AuthorBadge'
import { Userpic } from '../Userpic'
2024-06-24 17:50:27 +00:00
import { useNavigate, useSearchParams } from '@solidjs/router'
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[]
2024-05-20 23:15:52 +00:00
flatFollows?: Array<Author | Topic>
2022-09-09 11:53:35 +00:00
}
2024-06-24 17:50:27 +00:00
export const AuthorCard = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
2024-06-24 17:50:27 +00:00
const navigate = useNavigate()
const { session, isSessionLoaded, requireAuthentication } = useSession()
const author = createMemo<Author>(() => session()?.user?.app_data?.profile as Author)
2024-01-31 12:34:15 +00:00
const [authorSubs, setAuthorSubs] = createSignal<Array<Author | Topic | Community>>([])
2024-05-20 11:16:54 +00:00
const [followsFilter, setFollowsFilter] = createSignal<FollowsFilter>('all')
const [isFollowed, setIsFollowed] = createSignal<boolean>()
2023-12-03 10:22:42 +00:00
const isProfileOwner = createMemo(() => author()?.slug === props.author.slug)
2024-05-20 11:16:54 +00:00
const { follow, unfollow, follows, following } = useFollowing()
2024-02-04 17:40:15 +00:00
2024-02-05 11:11:46 +00:00
onMount(() => {
2024-06-24 17:50:27 +00:00
setAuthorSubs(props.flatFollows || [])
2024-02-05 11:11:46 +00:00
})
createEffect(() => {
2024-05-20 11:16:54 +00:00
if (!(follows && props.author)) return
const followed = follows?.authors?.some((authorEntity) => authorEntity.id === props.author?.id)
setIsFollowed(followed)
2024-02-05 11:11:46 +00:00
})
2022-12-07 20:11:32 +00:00
const name = createMemo(() => {
2024-06-24 17:50:27 +00:00
if (lang() !== 'ru' && isCyrillic(props.author?.name || '')) {
2022-12-07 20:11:32 +00:00
if (props.author.name === 'Дискурс') {
return 'Discours'
}
2024-06-24 17:50:27 +00:00
return translit(props.author?.name || '')
2022-12-07 20:11:32 +00:00
}
return props.author.name
})
2024-06-24 17:50:27 +00:00
const [, changeSearchParams] = useSearchParams()
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-06-24 17:50:27 +00:00
navigate('/inbox')
changeSearchParams({
2024-06-26 08:22:05 +00:00
initChat: props.author?.id.toString()
})
}, 'discussions')
2022-12-17 03:27:00 +00:00
}
createEffect(() => {
2024-05-20 23:15:52 +00:00
if (props.flatFollows) {
2024-05-20 11:16:54 +00:00
if (followsFilter() === 'authors') {
2024-05-20 23:15:52 +00:00
setAuthorSubs(props.flatFollows.filter((s) => 'name' in s))
2024-05-20 11:16:54 +00:00
} else if (followsFilter() === 'topics') {
2024-05-20 23:15:52 +00:00
setAuthorSubs(props.flatFollows.filter((s) => 'title' in s))
2024-05-20 11:16:54 +00:00
} else if (followsFilter() === 'communities') {
2024-05-20 23:15:52 +00:00
setAuthorSubs(props.flatFollows.filter((s) => 'title' in s))
} else {
2024-05-20 23:15:52 +00:00
setAuthorSubs(props.flatFollows)
}
}
})
2024-01-31 12:34:15 +00:00
const handleFollowClick = () => {
requireAuthentication(() => {
2024-05-20 11:16:54 +00:00
isFollowed()
2024-03-15 14:57:03 +00:00
? unfollow(FollowingEntity.Author, props.author.slug)
: follow(FollowingEntity.Author, props.author.slug)
2024-05-20 11:16:54 +00:00
}, 'follow')
2024-01-31 12:34:15 +00:00
}
2023-11-13 18:56:47 +00:00
2024-01-31 12:34:15 +00:00
const followButtonText = createMemo(() => {
2024-05-20 11:16:54 +00:00
if (following()?.slug === props.author.slug) {
2024-06-24 17:50:27 +00:00
return following()?.type === 'follow' ? t('Following...') : t('Unfollowing...')
2024-03-11 05:20:00 +00:00
}
2024-05-20 11:16:54 +00:00
if (isFollowed()) {
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
2024-05-20 23:15:52 +00:00
const FollowersModalView = () => (
<>
<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>
</div>
</div>
</>
)
const FollowingModalView = () => (
<>
<h2>{t('Subscriptions')}</h2>
<ul class="view-switcher">
<li
class={clsx({
2024-06-26 08:22:05 +00:00
'view-switcher__item--selected': followsFilter() === 'all'
2024-05-20 23:15:52 +00:00
})}
>
<button type="button" onClick={() => setFollowsFilter('all')}>
{t('All')}
</button>
2024-06-24 17:50:27 +00:00
<span class="view-switcher__counter">{props.flatFollows?.length}</span>
2024-05-20 23:15:52 +00:00
</li>
<li
class={clsx({
2024-06-26 08:22:05 +00:00
'view-switcher__item--selected': followsFilter() === 'authors'
2024-05-20 23:15:52 +00:00
})}
>
<button type="button" onClick={() => setFollowsFilter('authors')}>
{t('Authors')}
</button>
2024-06-24 17:50:27 +00:00
<span class="view-switcher__counter">{props.flatFollows?.filter((s) => 'name' in s).length}</span>
2024-05-20 23:15:52 +00:00
</li>
<li
class={clsx({
2024-06-26 08:22:05 +00:00
'view-switcher__item--selected': followsFilter() === 'topics'
2024-05-20 23:15:52 +00:00
})}
>
<button type="button" onClick={() => setFollowsFilter('topics')}>
{t('Topics')}
</button>
2024-06-24 17:50:27 +00:00
<span class="view-switcher__counter">
{props.flatFollows?.filter((s) => 'title' in s).length}
</span>
2024-05-20 23:15:52 +00:00
</li>
</ul>
<br />
<div class={styles.listWrapper}>
<div class="row">
<div class="col-24">
<For each={authorSubs()}>
{(subscription) =>
isAuthor(subscription) ? (
<AuthorBadge author={subscription} subscriptionsMode={true} />
) : (
<TopicBadge topic={subscription} subscriptionsMode={true} />
)
}
</For>
</div>
</div>
</div>
</>
)
return (
<div class={clsx(styles.author, 'row')}>
<div class="col-md-5">
<Userpic
size={'XL'}
2024-06-24 17:50:27 +00:00
name={props.author.name || ''}
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}>
2024-06-24 17:50:27 +00:00
<div class={styles.authorAbout} innerHTML={props.author.bio || ''} />
2023-11-28 17:06:13 +00:00
</Show>
2024-06-24 17:50:27 +00:00
<Show when={(props.followers || [])?.length > 0 || (props.flatFollows || []).length > 0}>
2024-05-25 16:27:15 +00:00
<div class={styles.subscribersContainer}>
<FollowingCounters
2024-05-25 16:27:15 +00:00
followers={props.followers}
2024-06-24 17:50:27 +00:00
followersAmount={props.author?.stat?.followers || 0}
2024-05-31 12:18:14 +00:00
following={props.flatFollows}
2024-06-24 17:50:27 +00:00
followingAmount={props.flatFollows?.length || 0}
2024-05-25 16:27:15 +00:00
/>
</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}>
2024-06-24 17:50:27 +00:00
{(link: string | null) => (
2023-10-16 22:14:43 +00:00
<a
class={styles.socialLink}
2024-06-24 17:50:27 +00:00
href={link?.startsWith('http') ? link : `https://${link}`}
2023-10-16 22:14:43 +00:00
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}>
2024-06-24 17:50:27 +00:00
{link?.startsWith('http') ? link : `https://${link}`}
2023-10-16 22:14:43 +00:00
</span>
</a>
)}
</For>
</div>
</Show>
<Show
when={isProfileOwner()}
fallback={
<div class={styles.authorActions}>
2024-03-11 05:20:00 +00:00
<Show when={authorSubs()?.length}>
<Button
onClick={handleFollowClick}
2024-05-20 11:16:54 +00:00
disabled={Boolean(following())}
value={followButtonText()}
isSubscribeButton={true}
class={clsx({
2024-06-26 08:22:05 +00:00
[stylesButton.followed]: 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"
2024-06-24 17:50:27 +00:00
onClick={() => navigate('/profile/settings')}
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
2024-06-24 17:50:27 +00:00
title={props.author.name || ''}
description={props.author.bio || ''}
imageUrl={props.author.pic || ''}
2024-04-08 14:48:58 +00:00
shareUrl={getShareUrl({
2024-06-26 08:22:05 +00:00
pathname: `/author/${props.author.slug}`
2024-04-08 14:48:58 +00:00
})}
2023-10-16 22:14:43 +00:00
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>
2024-05-20 23:15:52 +00:00
<FollowersModalView />
2023-10-13 06:10:24 +00:00
</Modal>
</Show>
2024-05-20 23:15:52 +00:00
<Show when={props.flatFollows}>
<Modal variant="medium" isResponsive={true} name="following" maxHeight>
2024-05-20 23:15:52 +00:00
<FollowingModalView />
2023-10-13 06:10:24 +00:00
</Modal>
</Show>
</div>
</div>
2022-09-09 11:53:35 +00:00
)
}