lbry-desktop/ui/modal/modalRepost/view.jsx

65 lines
1.6 KiB
React
Raw Normal View History

// @flow
import React from 'react';
import { useHistory } from 'react-router';
import Modal from 'modal/modal';
import RepostCreate from 'component/repostCreate';
import useThrottle from 'effects/use-throttle';
type Props = {
2022-03-03 17:21:36 +01:00
uri?: string,
contentName?: string,
// --- redux ---
closeModal: () => void,
resolveUri: (string) => void,
};
function ModalRepost(props: Props) {
2022-03-03 17:21:36 +01:00
const { uri, contentName, closeModal, resolveUri } = props;
const {
location: { search },
} = useHistory();
const urlParams = new URLSearchParams(search);
2022-03-03 17:21:36 +01:00
const param = urlParams.get('name') || urlParams.get('q') || contentName;
const repostTo = param && param[0] === '@' ? param.slice(1) : param;
const [contentUri, setContentUri] = React.useState('');
const [repostUri, setRepostUri] = React.useState('');
const throttledContentValue = useThrottle(contentUri, 500);
const throttledRepostValue = useThrottle(repostUri, 500);
React.useEffect(() => {
if (throttledContentValue) {
resolveUri(throttledContentValue);
}
}, [throttledContentValue, resolveUri]);
React.useEffect(() => {
if (throttledRepostValue) {
resolveUri(throttledRepostValue);
}
}, [throttledRepostValue, resolveUri]);
React.useEffect(() => {
if (repostTo) {
resolveUri(repostTo);
}
}, [repostTo, resolveUri]);
return (
<Modal onAborted={closeModal} isOpen type="card">
<RepostCreate
uri={uri}
name={repostTo}
contentUri={contentUri}
repostUri={repostUri}
setContentUri={setContentUri}
setRepostUri={setRepostUri}
/>
</Modal>
);
}
export default ModalRepost;