bigbuffet-rw/app/soapbox/features/embedded-status/index.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-08-12 10:58:35 -07:00
import React, { useEffect, useState } from 'react';
2022-08-21 10:32:51 -07:00
import { useHistory } from 'react-router-dom';
2022-08-12 10:58:35 -07:00
import { fetchStatus } from 'soapbox/actions/statuses';
import MissingIndicator from 'soapbox/components/missing_indicator';
2022-08-21 11:47:23 -07:00
import SiteLogo from 'soapbox/components/site-logo';
2022-08-12 10:58:35 -07:00
import Status from 'soapbox/components/status';
import { Spinner } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
import { iframeId } from 'soapbox/iframe';
2022-08-12 10:58:35 -07:00
import { makeGetStatus } from 'soapbox/selectors';
interface IEmbeddedStatus {
params: {
statusId: string,
},
}
const getStatus = makeGetStatus();
/** Status to be presented in an iframe for embeds on external websites. */
const EmbeddedStatus: React.FC<IEmbeddedStatus> = ({ params }) => {
const dispatch = useAppDispatch();
2022-08-21 10:32:51 -07:00
const history = useHistory();
2022-08-12 10:58:35 -07:00
const status = useAppSelector(state => getStatus(state, { id: params.statusId }));
const [loading, setLoading] = useState(true);
2022-08-21 10:32:51 -07:00
// Prevent navigation for UX and security.
// https://stackoverflow.com/a/71531211
history.block();
2022-08-12 10:58:35 -07:00
useEffect(() => {
dispatch(fetchStatus(params.statusId))
.then(() => setLoading(false))
.catch(() => setLoading(false));
}, []);
useEffect(() => {
window.parent.postMessage({
type: 'setHeight',
id: iframeId,
height: document.getElementsByTagName('html')[0].scrollHeight,
}, '*');
}, [status, loading]);
2022-08-21 11:47:23 -07:00
const logo = (
<div className='flex align-middle justify-center ml-4'>
<SiteLogo className='max-h-[20px] max-w-[112px]' />
2022-08-21 11:47:23 -07:00
</div>
);
2022-08-12 10:58:35 -07:00
const renderInner = () => {
if (loading) {
return <Spinner />;
} else if (status) {
2022-08-21 11:47:23 -07:00
return <Status status={status} accountAction={logo} variant='default' />;
2022-08-12 10:58:35 -07:00
} else {
return <MissingIndicator nested />;
}
};
return (
<a
className='block bg-white dark:bg-primary-900'
href={status?.url || '#'}
onClick={e => e.stopPropagation()}
target='_blank'
>
<div className='p-4 sm:p-6 max-w-3xl pointer-events-none'>
2022-08-12 10:58:35 -07:00
{renderInner()}
</div>
</a>
2022-08-12 10:58:35 -07:00
);
};
export default EmbeddedStatus;