2022-08-31 02:35:06 -07:00
|
|
|
import classNames from 'clsx';
|
2022-06-16 12:32:17 -07:00
|
|
|
import debounce from 'lodash/debounce';
|
2022-01-10 14:17:52 -08:00
|
|
|
import React, { useRef } from 'react';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-07-18 06:54:56 -07:00
|
|
|
import { fetchAccount } from 'soapbox/actions/accounts';
|
2020-09-11 09:37:05 -07:00
|
|
|
import {
|
|
|
|
openProfileHoverCard,
|
|
|
|
closeProfileHoverCard,
|
2022-11-16 05:32:32 -08:00
|
|
|
} from 'soapbox/actions/profile-hover-card';
|
2022-07-18 06:54:56 -07:00
|
|
|
import { useAppDispatch } from 'soapbox/hooks';
|
2022-11-15 12:48:54 -08:00
|
|
|
import { isMobile } from 'soapbox/is-mobile';
|
2020-09-11 09:37:05 -07:00
|
|
|
|
|
|
|
const showProfileHoverCard = debounce((dispatch, ref, accountId) => {
|
|
|
|
dispatch(openProfileHoverCard(ref, accountId));
|
2022-03-21 11:09:01 -07:00
|
|
|
}, 600);
|
2020-09-11 09:37:05 -07:00
|
|
|
|
2022-04-27 18:01:31 -07:00
|
|
|
interface IHoverRefWrapper {
|
|
|
|
accountId: string,
|
2022-06-17 12:39:53 -07:00
|
|
|
inline?: boolean,
|
2022-06-09 12:47:21 -07:00
|
|
|
className?: string,
|
2023-01-10 15:03:15 -08:00
|
|
|
children: React.ReactNode,
|
2022-04-27 18:01:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Makes a profile hover card appear when the wrapped element is hovered. */
|
2022-06-09 12:47:21 -07:00
|
|
|
export const HoverRefWrapper: React.FC<IHoverRefWrapper> = ({ accountId, children, inline = false, className }) => {
|
2022-07-18 06:54:56 -07:00
|
|
|
const dispatch = useAppDispatch();
|
2022-05-01 11:11:20 -07:00
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
2022-04-27 18:01:31 -07:00
|
|
|
const Elem: keyof JSX.IntrinsicElements = inline ? 'span' : 'div';
|
2022-03-21 11:09:01 -07:00
|
|
|
|
|
|
|
const handleMouseEnter = () => {
|
|
|
|
if (!isMobile(window.innerWidth)) {
|
2022-07-18 06:54:56 -07:00
|
|
|
dispatch(fetchAccount(accountId));
|
2020-09-11 09:37:05 -07:00
|
|
|
showProfileHoverCard(dispatch, ref, accountId);
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
2020-09-11 09:37:05 -07:00
|
|
|
};
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
const handleMouseLeave = () => {
|
2020-09-11 09:37:05 -07:00
|
|
|
showProfileHoverCard.cancel();
|
|
|
|
setTimeout(() => dispatch(closeProfileHoverCard()), 300);
|
|
|
|
};
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
const handleClick = () => {
|
2020-09-11 13:30:37 -07:00
|
|
|
showProfileHoverCard.cancel();
|
|
|
|
dispatch(closeProfileHoverCard(true));
|
|
|
|
};
|
2020-09-11 09:37:05 -07:00
|
|
|
|
|
|
|
return (
|
2020-09-11 10:17:32 -07:00
|
|
|
<Elem
|
2020-09-11 09:37:05 -07:00
|
|
|
ref={ref}
|
2022-06-09 12:47:21 -07:00
|
|
|
className={classNames('hover-ref-wrapper', className)}
|
2022-03-21 11:09:01 -07:00
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
onClick={handleClick}
|
2020-09-11 09:37:05 -07:00
|
|
|
>
|
|
|
|
{children}
|
2020-09-11 10:17:32 -07:00
|
|
|
</Elem>
|
2020-09-11 09:37:05 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-21 11:09:01 -07:00
|
|
|
export { HoverRefWrapper as default, showProfileHoverCard };
|