mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import React, { FC, useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { PRESETS } from '~/constants/urls';
|
|
import { IUser } from '~/types/auth';
|
|
import { getURL } from '~/utils/dom';
|
|
|
|
import styles from './styles.module.scss';
|
|
|
|
interface IProps {
|
|
cover: IUser['cover'];
|
|
}
|
|
|
|
const CoverBackdrop: FC<IProps> = ({ cover }) => {
|
|
const ref = useRef<HTMLImageElement>(null);
|
|
|
|
const [is_loaded, setIsLoaded] = useState(false);
|
|
|
|
const onLoad = useCallback(() => setIsLoaded(true), [setIsLoaded]);
|
|
|
|
const image = getURL(cover, PRESETS.cover);
|
|
|
|
useEffect(() => {
|
|
if (!cover || !cover.url || !ref || !ref.current) return;
|
|
|
|
ref.current.src = '';
|
|
setIsLoaded(false);
|
|
ref.current.src = getURL(cover, PRESETS.cover);
|
|
}, [cover]);
|
|
|
|
if (!cover) return null;
|
|
|
|
return (
|
|
<div
|
|
className={classNames(styles.cover, { [styles.active]: is_loaded })}
|
|
style={{ backgroundImage: `url("${image}")` }}
|
|
>
|
|
{
|
|
// TODO: use ImageWithSSRLoad here if you will face any bugs
|
|
}
|
|
<img onLoad={onLoad} ref={ref} alt="" />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export { CoverBackdrop };
|