bigbuffet-rw/app/soapbox/features/ui/components/embed_modal.tsx

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-08-21 09:37:14 -07:00
import React, { useEffect } from 'react';
2022-05-03 14:43:04 -07:00
import { FormattedMessage } from 'react-intl';
import SafeEmbed from 'soapbox/components/safe-embed';
2022-05-03 14:43:04 -07:00
import { Modal, Stack, Text, Input } from 'soapbox/components/ui';
2022-08-21 09:37:14 -07:00
import useEmbed from 'soapbox/queries/embed';
2022-05-03 14:43:04 -07:00
interface IEmbedModal {
2022-08-21 08:21:25 -07:00
url: string,
onError: (error: any) => void,
2022-05-03 14:43:04 -07:00
}
2022-08-21 08:21:25 -07:00
const EmbedModal: React.FC<IEmbedModal> = ({ url, onError }) => {
2022-08-21 09:37:14 -07:00
const { data: embed, error, isError } = useEmbed(url);
2022-08-21 08:21:25 -07:00
useEffect(() => {
2022-08-21 09:37:14 -07:00
if (error && isError) {
2022-08-21 08:21:25 -07:00
onError(error);
2022-08-21 09:37:14 -07:00
}
}, [isError]);
2022-05-03 14:43:04 -07:00
const handleInputClick: React.MouseEventHandler<HTMLInputElement> = (e) => {
e.currentTarget.select();
};
return (
<Modal title={<FormattedMessage id='status.embed' defaultMessage='Embed' />}>
<Stack space={4}>
<Stack>
<Text theme='muted' size='sm'>
<FormattedMessage id='embed.instructions' defaultMessage='Embed this post on your website by copying the code below.' />
</Text>
<Input
type='text'
readOnly
2022-08-21 09:37:14 -07:00
value={embed?.html || ''}
2022-05-03 14:43:04 -07:00
onClick={handleInputClick}
/>
</Stack>
<SafeEmbed
2022-08-21 08:21:25 -07:00
className='inline-flex rounded-xl overflow-hidden max-w-full'
2022-08-21 12:08:05 -07:00
sandbox='allow-same-origin allow-scripts'
title='embedded-status'
2022-08-21 09:37:14 -07:00
html={embed?.html}
2022-08-21 08:21:25 -07:00
/>
2022-05-03 14:43:04 -07:00
</Stack>
</Modal>
);
};
export default EmbedModal;