webapp/src/components/Inbox/DialogAvatar.tsx

70 lines
1.6 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2022-11-10 15:06:02 +00:00
import { Show, createMemo } from 'solid-js'
import { getImageUrl } from '../../utils/getImageUrl'
2022-11-10 15:06:02 +00:00
import './DialogCard.module.scss'
2022-11-10 15:06:02 +00:00
import styles from './DialogAvatar.module.scss'
type Props = {
name: string
2022-11-21 05:06:53 +00:00
url?: string
2022-11-10 15:06:02 +00:00
online?: boolean
2022-11-21 05:06:53 +00:00
size?: 'small'
2022-12-17 03:27:00 +00:00
bordered?: boolean
2023-02-17 09:21:02 +00:00
class?: string
2022-11-10 15:06:02 +00:00
}
const colors = [
'#001219',
'#005f73',
'#0a9396',
'#94d2bd',
'#ee9b00',
'#ca6702',
'#ae2012',
'#9b2226',
2022-11-15 09:45:55 +00:00
'#668cff',
'#c34cfe',
'#e699ff',
'#6633ff',
2022-11-10 15:06:02 +00:00
]
const getById = (letter: string) =>
2022-11-15 12:48:42 +00:00
colors[Math.abs(Number(BigInt(letter.toLowerCase().codePointAt(0) - 97) % BigInt(colors.length)))]
2022-11-10 15:06:02 +00:00
const DialogAvatar = (props: Props) => {
const nameFirstLetter = createMemo(() => props.name.slice(0, 1))
2022-11-10 15:06:02 +00:00
const randomBg = createMemo(() => {
return getById(nameFirstLetter())
2022-11-10 15:06:02 +00:00
})
return (
<div
2023-02-17 09:21:02 +00:00
class={clsx(styles.DialogAvatar, props.class, {
2022-11-24 06:52:31 +00:00
[styles.online]: props.online,
2022-12-17 03:27:00 +00:00
[styles.bordered]: props.bordered,
[styles.small]: props.size === 'small',
2022-11-24 06:52:31 +00:00
})}
2022-11-10 15:06:02 +00:00
style={{ 'background-color': `${randomBg()}` }}
>
<Show when={Boolean(props.url)} fallback={<div class={styles.letter}>{nameFirstLetter()}</div>}>
<div
class={styles.imageHolder}
2024-03-05 13:07:14 +00:00
style={{
'background-image': `url(
${
props.url.includes('discours.io')
? getImageUrl(props.url, { width: 40, height: 40 })
: props.url
}
)`,
}}
/>
2022-11-10 15:06:02 +00:00
</Show>
</div>
)
}
export default DialogAvatar