webapp/src/components/Author/AuthorBadge/AuthorBadge.tsx

164 lines
5.7 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2024-05-20 23:15:52 +00:00
import { Match, Show, Switch, createEffect, createMemo, createSignal, on } from 'solid-js'
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'
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'
import { Button } from '../../_shared/Button'
import { CheckButton } from '../../_shared/CheckButton'
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
2024-05-20 11:16:54 +00:00
import { FollowingButton } from '../../_shared/FollowingButton'
import { Icon } from '../../_shared/Icon'
import { Userpic } from '../Userpic'
2024-02-04 11:25:21 +00:00
import styles from './AuthorBadge.module.scss'
type Props = {
author: Author
2024-05-20 11:16:54 +00:00
minimize?: boolean
showMessageButton?: boolean
iconButtons?: boolean
nameOnly?: boolean
inviteView?: boolean
onInvite?: (id: number) => void
selected?: boolean
subscriptionsMode?: boolean
}
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()
const [isMobileView, setIsMobileView] = createSignal(false)
2024-05-20 23:15:52 +00:00
const [isFollowed, setIsFollowed] = createSignal<boolean>(
2024-06-24 17:50:27 +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(
Boolean(followingAuthors?.some((followedAuthor) => followedAuthor.id === currentAuthor?.id)),
)
2024-05-20 23:15:52 +00:00
},
{ defer: true },
),
)
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()
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-05-18 10:44:43 +00:00
initChat: props.author?.id.toString(),
})
}, '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
}
return (
<div class={clsx(styles.AuthorBadge, { [styles.nameOnly]: props.nameOnly })}>
2023-11-06 08:00:31 +00:00
<div class={styles.basicInfo}>
<Userpic
hasLink={true}
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}
/>
<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-24 17:50:27 +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>
<Show when={props.author?.stat && !props.subscriptionsMode}>
<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>
</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>
</Show>
</div>
</Show>
2023-11-06 08:00:31 +00:00
</Show>
</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}>
<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
/>
<Show when={props.showMessageButton}>
<Button
variant={props.iconButtons ? 'secondary' : 'bordered'}
size="S"
value={props.iconButtons ? <Icon name="inbox-white" /> : t('Message')}
onClick={initChat}
class={clsx(styles.actionButton, { [styles.iconed]: props.iconButtons })}
/>
</Show>
</div>
</Show>
<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)}
/>
</Show>
</div>
)
}