webapp/src/components/Inbox/DialogAvatar.tsx

56 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-11-10 15:06:02 +00:00
import { Show, createMemo } from 'solid-js'
import './DialogCard.module.scss'
import styles from './DialogAvatar.module.scss'
import { clsx } from 'clsx'
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) => {
2022-11-15 12:48:42 +00:00
const nameFirstLetter = props.name.slice(0, 1)
2022-11-10 15:06:02 +00:00
const randomBg = createMemo(() => {
return getById(nameFirstLetter)
})
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,
2022-11-24 06:52:31 +00:00
[styles.small]: props.size === 'small'
})}
2022-11-10 15:06:02 +00:00
style={{ 'background-color': `${randomBg()}` }}
>
<Show when={props.url} fallback={() => <div class={styles.letter}>{nameFirstLetter}</div>}>
2022-11-23 04:12:11 +00:00
<div class={styles.imageHolder} style={{ 'background-image': `url(${props.url})` }} />
2022-11-10 15:06:02 +00:00
</Show>
</div>
)
}
export default DialogAvatar