webapp/src/components/Author/Userpic/Userpic.tsx

83 lines
2.1 KiB
TypeScript
Raw Normal View History

import { createSignal, Show } from 'solid-js'
2023-08-11 16:42:41 +00:00
import styles from './Userpic.module.scss'
import { clsx } from 'clsx'
import { imageProxy } from '../../../utils/imageProxy'
import { ConditionalWrapper } from '../../_shared/ConditionalWrapper'
import { Loading } from '../../_shared/Loading'
type Props = {
name: string
userpic: string
class?: string
slug?: string
onClick?: () => void
loading?: boolean
hasLink?: boolean
size?: 'XS' | 'S' | 'M' | 'L' | 'XL' // 20 | 28 | 32 | 40 | 168
2023-08-11 16:42:41 +00:00
}
export const Userpic = (props: Props) => {
const [userpicUrl, setUserpicUrl] = createSignal<string>()
2023-08-11 16:42:41 +00:00
const letters = () => {
if (!props.name) return
const names = props.name ? props.name.split(' ') : []
return names[0][0] + (names.length > 1 ? names[1][0] : '')
}
const comutedAvatarSize = () => {
switch (props.size) {
case 'XS': {
return '40x40'
}
case 'S': {
return '56x56'
}
case 'L': {
return '80x80'
}
case 'XL': {
return '336x336'
}
default: {
return '64x64'
}
}
}
setUserpicUrl(
props.userpic && props.userpic.includes('100x')
? props.userpic.replace('100x', comutedAvatarSize())
: props.userpic
)
2023-08-11 16:42:41 +00:00
return (
<div
class={clsx(styles.Userpic, props.class, styles[props.size ?? 'M'], {
2023-09-25 06:13:47 +00:00
['cursorPointer']: props.onClick
2023-08-11 16:42:41 +00:00
})}
onClick={props.onClick}
>
<Show when={!props.loading} fallback={<Loading />}>
<ConditionalWrapper
condition={props.hasLink}
wrapper={(children) => <a href={`/author/${props.slug}`}>{children}</a>}
>
<Show
when={!props.userpic}
fallback={
<img
class={clsx({ [styles.anonymous]: !props.userpic })}
src={imageProxy(userpicUrl()) || '/icons/user-default.svg'}
2023-08-11 16:42:41 +00:00
alt={props.name || ''}
loading="lazy"
/>
}
>
<div class={styles.letters}>{letters()}</div>
</Show>
</ConditionalWrapper>
</Show>
</div>
)
}