webapp/src/components/Author/Card.tsx

198 lines
6.7 KiB
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
import type { Author } from '../../graphql/types.gen'
import Userpic from './Userpic'
2022-11-14 17:41:05 +00:00
import { Icon } from '../_shared/Icon'
2022-11-11 08:58:22 +00:00
import styles from './Card.module.scss'
import { createMemo, createSignal, For, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import { translit } from '../../utils/ru2en'
import { follow, unfollow } from '../../stores/zine/common'
import { clsx } from 'clsx'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2022-11-30 21:50:33 +00:00
import { StatMetrics } from '../_shared/StatMetrics'
import { ShowOnlyOnClient } from '../_shared/ShowOnlyOnClient'
2022-12-17 03:27:00 +00:00
import { FollowingEntity } from '../../graphql/types.gen'
import { router, useRouter } from '../../stores/router'
import { openPage } from '@nanostores/router'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
2022-09-09 11:53:35 +00:00
interface AuthorCardProps {
2022-11-19 05:00:54 +00:00
caption?: string
2022-09-09 11:53:35 +00:00
compact?: boolean
hideDescription?: boolean
hideFollow?: boolean
hasLink?: boolean
subscribed?: boolean
author: Author
isAuthorPage?: boolean
2022-10-19 21:40:50 +00:00
noSocialButtons?: boolean
2022-11-11 08:58:22 +00:00
isAuthorsList?: boolean
2022-11-16 21:08:04 +00:00
truncateBio?: boolean
liteButtons?: boolean
2022-11-26 21:27:54 +00:00
isComments?: boolean
2023-01-26 21:42:47 +00:00
isFeedMode?: boolean
2023-02-13 13:48:05 +00:00
isNowrap?: boolean
2022-09-09 11:53:35 +00:00
}
export const AuthorCard = (props: AuthorCardProps) => {
2023-02-17 09:21:02 +00:00
const { t, lang } = useLocalize()
const {
session,
2022-12-06 16:03:55 +00:00
isSessionLoaded,
actions: { loadSession }
} = useSession()
const [isSubscribing, setIsSubscribing] = createSignal(false)
2022-09-30 14:22:33 +00:00
const subscribed = createMemo<boolean>(() => {
return session()?.news?.authors?.some((u) => u === props.author.slug) || false
})
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 loadSession()
setIsSubscribing(false)
}
2022-09-30 14:22:33 +00:00
const canFollow = createMemo(() => !props.hideFollow && session()?.user?.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
2022-12-17 03:27:00 +00:00
const { changeSearchParam } = useRouter()
const initChat = () => {
openPage(router, `inbox`)
changeSearchParam('initChat', `${props.author.id}`)
}
2022-09-09 11:53:35 +00:00
return (
2022-11-11 08:58:22 +00:00
<div
class={clsx(styles.author)}
classList={{
[styles.authorPage]: props.isAuthorPage,
2022-11-26 21:27:54 +00:00
[styles.authorComments]: props.isComments,
2023-01-26 21:42:47 +00:00
[styles.authorsListItem]: props.isAuthorsList,
2023-02-13 13:48:05 +00:00
[styles.feedMode]: props.isFeedMode,
[styles.nowrapView]: props.isNowrap
2022-11-11 08:58:22 +00:00
}}
>
<Userpic
user={props.author}
hasLink={props.hasLink}
isBig={props.isAuthorPage}
isAuthorsList={props.isAuthorsList}
2022-11-26 21:27:54 +00:00
class={styles.circlewrap}
2022-11-11 08:58:22 +00:00
/>
2022-09-09 11:53:35 +00:00
2022-11-11 08:58:22 +00:00
<div class={styles.authorDetails}>
<div class={styles.authorDetailsWrapper}>
2022-10-04 12:42:11 +00:00
<Show when={props.hasLink}>
2022-11-11 08:58:22 +00:00
<a class={styles.authorName} href={`/author/${props.author.slug}`}>
2022-10-04 12:42:11 +00:00
{name()}
</a>
</Show>
<Show when={!props.hasLink}>
2022-11-11 08:58:22 +00:00
<div class={styles.authorName}>{name()}</div>
2022-10-04 12:42:11 +00:00
</Show>
2022-09-09 11:53:35 +00:00
2022-11-30 21:50:33 +00:00
<Show when={!props.hideDescription && props.author.bio}>
2022-11-16 21:08:04 +00:00
{props.isAuthorsList}
<div
class={styles.authorAbout}
classList={{ 'text-truncate': props.truncateBio }}
2022-11-27 17:02:04 +00:00
innerHTML={props.author.bio}
/>
2022-10-04 12:42:11 +00:00
</Show>
2022-11-30 21:50:33 +00:00
<Show when={props.author.stat}>
<StatMetrics fields={['shouts', 'followers', 'comments']} stat={props.author.stat} />
</Show>
2022-10-04 12:42:11 +00:00
</div>
<ShowOnlyOnClient>
2022-12-06 16:03:55 +00:00
<Show when={isSessionLoaded()}>
<Show when={canFollow()}>
<div class={styles.authorSubscribe}>
<Show
when={subscribed()}
fallback={
<button
onClick={() => subscribe(true)}
class={clsx('button', styles.button)}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.isAuthorsList,
[styles.isSubscribing]: isSubscribing()
}}
disabled={isSubscribing()}
>
<Show when={!props.isAuthorsList}>
2022-12-04 15:10:27 +00:00
<Icon name="author-subscribe" class={styles.icon} />
</Show>
<span class={styles.buttonLabel}>{t('Follow')}</span>
</button>
}
>
<button
onClick={() => subscribe(false)}
class={clsx('button', styles.button)}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.isAuthorsList,
[styles.isSubscribing]: isSubscribing()
}}
disabled={isSubscribing()}
>
<Show when={!props.isAuthorsList}>
<Icon name="author-unsubscribe" class={styles.icon} />
</Show>
<span class={styles.buttonLabel}>{t('Unfollow')}</span>
</button>
2022-11-11 08:58:22 +00:00
</Show>
2022-09-09 11:53:35 +00:00
<Show when={!props.compact && !props.isAuthorsList}>
<button
class={styles.button}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.liteButtons && props.isAuthorsList
}}
2022-12-17 03:27:00 +00:00
onClick={initChat}
>
<Icon name="comment" class={styles.icon} />
<Show when={!props.liteButtons}>{t('Write')}</Show>
</button>
2022-09-09 11:53:35 +00:00
<Show when={!props.noSocialButtons}>
<div class={styles.authorSubscribeSocial}>
<For each={props.author.links}>{(link) => <a href={link} />}</For>
</div>
</Show>
</Show>
</div>
2022-09-09 11:53:35 +00:00
</Show>
</Show>
</ShowOnlyOnClient>
2022-10-04 12:42:11 +00:00
</div>
</div>
2022-09-09 11:53:35 +00:00
)
}