webapp/src/components/Author/Card.tsx

128 lines
4.3 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-09-22 09:37:49 +00:00
import { Icon } from '../Nav/Icon'
2022-11-11 08:58:22 +00:00
import styles from './Card.module.scss'
2022-10-31 16:40:55 +00:00
import { createMemo, For, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import { translit } from '../../utils/ru2en'
import { t } from '../../utils/intl'
2022-09-30 14:22:33 +00:00
import { useAuthStore } from '../../stores/auth'
2022-09-23 23:42:19 +00:00
import { locale } from '../../stores/ui'
2022-09-09 11:53:35 +00:00
import { follow, unfollow } from '../../stores/zine/common'
import { clsx } from 'clsx'
2022-09-09 11:53:35 +00:00
interface AuthorCardProps {
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-09-09 11:53:35 +00:00
}
export const AuthorCard = (props: AuthorCardProps) => {
2022-09-30 14:22:33 +00:00
const { session } = useAuthStore()
2022-10-04 12:42:11 +00:00
const subscribed = createMemo<boolean>(
() => session()?.news?.authors?.some((u) => u === props.author.slug) || false
2022-09-09 11:53:35 +00:00
)
2022-09-30 14:22:33 +00:00
const canFollow = createMemo(() => !props.hideFollow && session()?.user?.slug !== props.author.slug)
2022-09-23 23:42:19 +00:00
const bio = () => props.author.bio || t('Our regular contributor')
const name = () => {
2022-09-09 11:53:35 +00:00
return props.author.name === 'Дискурс' && locale() !== 'ru'
? 'Discours'
: translit(props.author.name || '', locale() || 'ru')
2022-09-23 23:42:19 +00:00
}
2022-09-09 11:53:35 +00:00
// TODO: reimplement AuthorCard
return (
2022-11-11 08:58:22 +00:00
<div
class={clsx(styles.author)}
classList={{
[styles.authorPage]: props.isAuthorPage,
[styles.authorsListItem]: props.isAuthorsList
}}
>
<Userpic
user={props.author}
hasLink={props.hasLink}
isBig={props.isAuthorPage}
isAuthorsList={props.isAuthorsList}
/>
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-10-04 12:42:11 +00:00
<Show when={!props.hideDescription}>
2022-11-11 08:58:22 +00:00
<div class={styles.authorAbout} classList={{ 'text-truncate': props.isAuthorsList }}>
{bio()}
</div>
2022-10-04 12:42:11 +00:00
</Show>
</div>
2022-09-09 11:53:35 +00:00
2022-10-04 12:42:11 +00:00
<Show when={canFollow()}>
2022-11-11 08:58:22 +00:00
<div class={styles.authorSubscribe}>
2022-10-04 12:42:11 +00:00
<Show
when={subscribed()}
fallback={
<button
onClick={() => follow}
2022-11-11 08:58:22 +00:00
class={clsx('button', styles.button)}
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.isAuthorsList
}}
>
2022-11-11 08:58:22 +00:00
<Show when={!props.isAuthorsList}>
<Icon name="author-subscribe" class={styles.icon} />
&nbsp;
</Show>
<span class={styles.buttonLabel}>{t('Follow')}</span>
2022-10-04 12:42:11 +00:00
</button>
}
>
<button
onClick={() => unfollow}
2022-11-11 08:58:22 +00:00
classList={{
[styles.buttonSubscribe]: !props.isAuthorsList,
'button--subscribe': !props.isAuthorsList,
'button--subscribe-topic': props.isAuthorsList,
[styles.buttonWrite]: props.isAuthorsList
}}
>
2022-11-11 08:58:22 +00:00
<Show when={!props.isAuthorsList}>
<Icon name="author-unsubscribe" class={styles.icon} />
&nbsp;
</Show>
<span class={styles.buttonLabel}>{t('Unfollow')}</span>
2022-10-04 12:42:11 +00:00
</button>
</Show>
2022-09-09 11:53:35 +00:00
2022-11-11 08:58:22 +00:00
<Show when={!props.compact && !props.isAuthorsList}>
<button class={clsx(styles.buttonWrite, styles.button, 'button button--subscribe-topic')}>
<Icon name="edit" class={styles.icon} />
2022-10-04 12:42:11 +00:00
{t('Write')}
</button>
2022-09-09 11:53:35 +00:00
2022-10-19 21:40:50 +00:00
<Show when={!props.noSocialButtons}>
<For each={props.author.links}>{(link) => <a href={link} />}</For>
</Show>
2022-09-09 11:53:35 +00:00
</Show>
</div>
2022-10-04 12:42:11 +00:00
</Show>
</div>
</div>
2022-09-09 11:53:35 +00:00
)
}