2023-10-04 19:04:09 +00:00
|
|
|
import { clsx } from 'clsx'
|
2024-05-20 23:15:52 +00:00
|
|
|
import { Match, Show, Switch, createEffect, createMemo, createSignal, on } from 'solid-js'
|
2023-11-14 15:10:00 +00:00
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
import { useNavigate, useSearchParams } from '@solidjs/router'
|
|
|
|
import { mediaMatches } from '~/utils/media-query'
|
2024-01-31 12:34:15 +00:00
|
|
|
import { useFollowing } from '../../../context/following'
|
2023-10-04 19:04:09 +00:00
|
|
|
import { useLocalize } from '../../../context/localize'
|
|
|
|
import { useSession } from '../../../context/session'
|
2023-11-28 13:18:25 +00:00
|
|
|
import { Author, FollowingEntity } from '../../../graphql/schema/core.gen'
|
2023-12-31 05:01:34 +00:00
|
|
|
import { translit } from '../../../utils/ru2en'
|
2024-02-04 17:40:15 +00:00
|
|
|
import { isCyrillic } from '../../../utils/translate'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { Button } from '../../_shared/Button'
|
2023-10-04 19:04:09 +00:00
|
|
|
import { CheckButton } from '../../_shared/CheckButton'
|
2024-01-25 09:57:57 +00:00
|
|
|
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
|
2024-05-20 11:16:54 +00:00
|
|
|
import { FollowingButton } from '../../_shared/FollowingButton'
|
2023-10-20 16:21:40 +00:00
|
|
|
import { Icon } from '../../_shared/Icon'
|
2023-11-14 15:10:00 +00:00
|
|
|
import { Userpic } from '../Userpic'
|
2024-02-04 11:25:21 +00:00
|
|
|
import styles from './AuthorBadge.module.scss'
|
2023-10-04 19:04:09 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
author: Author
|
2024-05-20 11:16:54 +00:00
|
|
|
minimize?: boolean
|
2023-10-20 16:21:40 +00:00
|
|
|
showMessageButton?: boolean
|
|
|
|
iconButtons?: boolean
|
2023-11-01 08:34:59 +00:00
|
|
|
nameOnly?: boolean
|
2024-01-25 09:57:57 +00:00
|
|
|
inviteView?: boolean
|
|
|
|
onInvite?: (id: number) => void
|
|
|
|
selected?: boolean
|
2024-05-03 08:49:05 +00:00
|
|
|
subscriptionsMode?: boolean
|
2023-10-04 19:04:09 +00:00
|
|
|
}
|
|
|
|
export const AuthorBadge = (props: Props) => {
|
2024-06-24 17:50:27 +00:00
|
|
|
const { session, requireAuthentication } = useSession()
|
|
|
|
const author = createMemo<Author>(() => session()?.user?.app_data?.profile as Author)
|
2024-05-20 11:16:54 +00:00
|
|
|
const { follow, unfollow, follows, following } = useFollowing()
|
2023-12-29 06:39:16 +00:00
|
|
|
const [isMobileView, setIsMobileView] = createSignal(false)
|
2024-05-20 23:15:52 +00:00
|
|
|
const [isFollowed, setIsFollowed] = createSignal<boolean>(
|
2024-06-26 08:22:05 +00:00
|
|
|
Boolean(follows?.authors?.some((authorEntity) => Boolean(authorEntity.id === props.author?.id)))
|
2024-05-20 23:15:52 +00:00
|
|
|
)
|
|
|
|
createEffect(() => setIsMobileView(!mediaMatches.sm))
|
|
|
|
createEffect(
|
|
|
|
on(
|
|
|
|
[() => follows?.authors, () => props.author, following],
|
|
|
|
([followingAuthors, currentAuthor, _]) => {
|
2024-06-24 17:50:27 +00:00
|
|
|
setIsFollowed(
|
2024-06-26 08:22:05 +00:00
|
|
|
Boolean(followingAuthors?.some((followedAuthor) => followedAuthor.id === currentAuthor?.id))
|
2024-06-24 17:50:27 +00:00
|
|
|
)
|
2024-05-20 23:15:52 +00:00
|
|
|
},
|
2024-06-26 08:22:05 +00:00
|
|
|
{ defer: true }
|
|
|
|
)
|
2024-05-20 23:15:52 +00:00
|
|
|
)
|
2023-12-29 06:39:16 +00:00
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
const [, changeSearchParams] = useSearchParams()
|
|
|
|
const navigate = useNavigate()
|
2023-12-28 00:30:09 +00:00
|
|
|
const { t, formatDate, lang } = useLocalize()
|
2023-10-04 19:04:09 +00:00
|
|
|
|
2023-10-20 16:21:40 +00:00
|
|
|
const initChat = () => {
|
2024-01-31 12:34:15 +00:00
|
|
|
// eslint-disable-next-line solid/reactivity
|
2023-10-20 16:21:40 +00:00
|
|
|
requireAuthentication(() => {
|
2024-06-24 17:50:27 +00:00
|
|
|
navigate('/inbox')
|
2023-12-20 08:07:57 +00:00
|
|
|
changeSearchParams({
|
2024-06-26 08:22:05 +00:00
|
|
|
initChat: props.author?.id.toString()
|
2023-10-20 16:21:40 +00:00
|
|
|
})
|
|
|
|
}, 'discussions')
|
|
|
|
}
|
2023-11-02 22:02:11 +00:00
|
|
|
|
2023-12-28 01:22:04 +00:00
|
|
|
const name = createMemo(() => {
|
2024-06-24 17:50:27 +00:00
|
|
|
if (lang() !== 'ru' && isCyrillic(props.author.name || '')) {
|
2023-12-28 01:22:04 +00:00
|
|
|
if (props.author.name === 'Дискурс') {
|
|
|
|
return 'Discours'
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
return translit(props.author.name || '')
|
2023-12-28 01:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return props.author.name
|
|
|
|
})
|
2023-12-28 00:30:09 +00:00
|
|
|
|
2024-01-31 12:34:15 +00:00
|
|
|
const handleFollowClick = () => {
|
2024-05-05 17:04:47 +00:00
|
|
|
requireAuthentication(async () => {
|
2024-05-20 11:16:54 +00:00
|
|
|
const handle = isFollowed() ? unfollow : follow
|
2024-05-05 17:04:47 +00:00
|
|
|
await handle(FollowingEntity.Author, props.author.slug)
|
2024-05-20 11:16:54 +00:00
|
|
|
}, 'follow')
|
2024-01-31 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:04:09 +00:00
|
|
|
return (
|
2023-11-01 08:34:59 +00:00
|
|
|
<div class={clsx(styles.AuthorBadge, { [styles.nameOnly]: props.nameOnly })}>
|
2023-11-06 08:00:31 +00:00
|
|
|
<div class={styles.basicInfo}>
|
|
|
|
<Userpic
|
|
|
|
hasLink={true}
|
2023-12-29 06:39:16 +00:00
|
|
|
size={isMobileView() ? 'M' : 'L'}
|
2024-06-24 17:50:27 +00:00
|
|
|
name={name() || ''}
|
|
|
|
userpic={props.author.pic || ''}
|
2023-11-06 08:00:31 +00:00
|
|
|
slug={props.author.slug}
|
|
|
|
/>
|
2024-01-25 09:57:57 +00:00
|
|
|
<ConditionalWrapper
|
|
|
|
condition={!props.inviteView}
|
|
|
|
wrapper={(children) => (
|
|
|
|
<a href={`/author/${props.author.slug}`} class={styles.info}>
|
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
>
|
2023-11-06 08:00:31 +00:00
|
|
|
<div class={styles.name}>
|
2023-12-28 00:30:09 +00:00
|
|
|
<span>{name()}</span>
|
2023-11-06 08:00:31 +00:00
|
|
|
</div>
|
|
|
|
<Show when={!props.nameOnly}>
|
|
|
|
<Switch
|
|
|
|
fallback={
|
|
|
|
<div class={styles.bio}>
|
2023-11-28 13:18:25 +00:00
|
|
|
{t('Registered since {date}', {
|
2024-06-26 08:22:05 +00:00
|
|
|
date: formatDate(new Date((props.author.created_at || 0) * 1000))
|
2023-11-28 13:18:25 +00:00
|
|
|
})}
|
2023-11-06 08:00:31 +00:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Match when={props.author.bio}>
|
2024-06-24 17:50:27 +00:00
|
|
|
<div class={clsx('text-truncate', styles.bio)} innerHTML={props.author.bio || ''} />
|
2023-11-06 08:00:31 +00:00
|
|
|
</Match>
|
|
|
|
</Switch>
|
2024-05-03 08:49:05 +00:00
|
|
|
<Show when={props.author?.stat && !props.subscriptionsMode}>
|
2024-02-22 07:29:52 +00:00
|
|
|
<div class={styles.bio}>
|
2024-06-24 17:50:27 +00:00
|
|
|
<Show when={(props.author?.stat?.shouts || 0) > 0}>
|
2024-06-06 09:27:49 +00:00
|
|
|
<div>{t('some posts', { count: props.author.stat?.shouts ?? 0 })}</div>
|
2024-02-22 07:29:52 +00:00
|
|
|
</Show>
|
2024-06-24 17:50:27 +00:00
|
|
|
<Show when={(props.author?.stat?.comments || 0) > 0}>
|
2024-06-06 08:36:07 +00:00
|
|
|
<div>{t('some comments', { count: props.author.stat?.comments ?? 0 })}</div>
|
2024-05-01 14:33:37 +00:00
|
|
|
</Show>
|
2024-06-24 17:50:27 +00:00
|
|
|
<Show when={(props.author?.stat?.followers || 0) > 0}>
|
2024-06-05 16:31:31 +00:00
|
|
|
<div>{t('some followers', { count: props.author.stat?.followers ?? 0 })}</div>
|
2024-02-22 07:29:52 +00:00
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
</Show>
|
2023-11-06 08:00:31 +00:00
|
|
|
</Show>
|
2024-01-25 09:57:57 +00:00
|
|
|
</ConditionalWrapper>
|
2023-11-06 08:00:31 +00:00
|
|
|
</div>
|
2023-12-13 23:56:44 +00:00
|
|
|
<Show when={props.author.slug !== author()?.slug && !props.nameOnly}>
|
2023-10-04 19:04:09 +00:00
|
|
|
<div class={styles.actions}>
|
2024-05-20 11:16:54 +00:00
|
|
|
<FollowingButton
|
2024-05-20 23:15:52 +00:00
|
|
|
action={handleFollowClick}
|
2024-05-20 11:16:54 +00:00
|
|
|
isFollowed={isFollowed()}
|
2024-06-24 17:50:27 +00:00
|
|
|
actionMessageType={following()?.slug === props.author.slug ? following()?.type : undefined}
|
2024-03-15 12:58:22 +00:00
|
|
|
/>
|
2023-10-20 16:21:40 +00:00
|
|
|
<Show when={props.showMessageButton}>
|
|
|
|
<Button
|
|
|
|
variant={props.iconButtons ? 'secondary' : 'bordered'}
|
2023-12-29 06:39:16 +00:00
|
|
|
size="S"
|
2023-10-20 16:21:40 +00:00
|
|
|
value={props.iconButtons ? <Icon name="inbox-white" /> : t('Message')}
|
|
|
|
onClick={initChat}
|
|
|
|
class={clsx(styles.actionButton, { [styles.iconed]: props.iconButtons })}
|
|
|
|
/>
|
|
|
|
</Show>
|
2023-10-04 19:04:09 +00:00
|
|
|
</div>
|
|
|
|
</Show>
|
2024-01-25 09:57:57 +00:00
|
|
|
<Show when={props.inviteView}>
|
|
|
|
<CheckButton
|
|
|
|
text={t('Invite')}
|
2024-06-24 17:50:27 +00:00
|
|
|
checked={Boolean(props.selected)}
|
|
|
|
onClick={() => props.onInvite?.(props.author.id)}
|
2024-01-25 09:57:57 +00:00
|
|
|
/>
|
|
|
|
</Show>
|
2023-10-04 19:04:09 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|