2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2022-08-30 13:26:42 -07:00
|
|
|
import debounce from 'lodash/debounce';
|
2022-06-16 21:19:53 -07:00
|
|
|
import React, { useRef } from 'react';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
|
|
|
import {
|
|
|
|
openStatusHoverCard,
|
|
|
|
closeStatusHoverCard,
|
2022-06-17 12:39:17 -07:00
|
|
|
} from 'soapbox/actions/status-hover-card';
|
2022-11-15 12:48:54 -08:00
|
|
|
import { isMobile } from 'soapbox/is-mobile';
|
2022-06-16 21:19:53 -07:00
|
|
|
|
|
|
|
const showStatusHoverCard = debounce((dispatch, ref, statusId) => {
|
|
|
|
dispatch(openStatusHoverCard(ref, statusId));
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
interface IHoverStatusWrapper {
|
2023-02-15 13:26:27 -08:00
|
|
|
statusId: any
|
|
|
|
inline: boolean
|
|
|
|
className?: string
|
|
|
|
children: React.ReactNode
|
2022-06-16 21:19:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Makes a status hover card appear when the wrapped element is hovered. */
|
|
|
|
export const HoverStatusWrapper: React.FC<IHoverStatusWrapper> = ({ statusId, children, inline = false, className }) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
|
|
const Elem: keyof JSX.IntrinsicElements = inline ? 'span' : 'div';
|
|
|
|
|
|
|
|
const handleMouseEnter = () => {
|
|
|
|
if (!isMobile(window.innerWidth)) {
|
|
|
|
showStatusHoverCard(dispatch, ref, statusId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
|
|
|
showStatusHoverCard.cancel();
|
|
|
|
setTimeout(() => dispatch(closeStatusHoverCard()), 200);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
showStatusHoverCard.cancel();
|
|
|
|
dispatch(closeStatusHoverCard(true));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Elem
|
|
|
|
ref={ref}
|
2023-02-06 10:01:03 -08:00
|
|
|
className={clsx('hover-status-wrapper', className)}
|
2022-06-16 21:19:53 -07:00
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Elem>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export { HoverStatusWrapper as default, showStatusHoverCard };
|