2022-08-30 13:26:42 -07:00
|
|
|
import React, { useRef } from 'react';
|
2022-12-15 14:51:30 -08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-08-30 13:26:42 -07:00
|
|
|
|
2022-12-15 14:51:30 -08:00
|
|
|
import Icon from 'soapbox/components/icon';
|
|
|
|
import { HStack, Text } from 'soapbox/components/ui';
|
2022-08-30 13:26:42 -07:00
|
|
|
import { useAppSelector } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
import type { List as ImmutableList } from 'immutable';
|
|
|
|
|
|
|
|
interface IUploadButton {
|
2023-02-15 13:26:27 -08:00
|
|
|
disabled?: boolean
|
|
|
|
onSelectFile: (files: FileList) => void
|
2022-08-30 13:26:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const UploadButton: React.FC<IUploadButton> = ({ disabled, onSelectFile }) => {
|
|
|
|
const fileElement = useRef<HTMLInputElement>(null);
|
|
|
|
const attachmentTypes = useAppSelector(state => state.instance.configuration.getIn(['media_attachments', 'supported_mime_types']) as ImmutableList<string>)?.filter(type => type.startsWith('image/'));
|
|
|
|
|
|
|
|
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
if (e.target.files?.length) {
|
|
|
|
onSelectFile(e.target.files);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
fileElement.current?.click();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-02-01 14:13:42 -08:00
|
|
|
<HStack className='h-full w-full cursor-pointer text-primary-500 dark:text-accent-blue' space={3} alignItems='center' justifyContent='center' element='label'>
|
2022-12-15 14:51:30 -08:00
|
|
|
<Icon
|
2022-08-30 13:26:42 -07:00
|
|
|
src={require('@tabler/icons/photo-plus.svg')}
|
2022-12-15 14:51:30 -08:00
|
|
|
className='h-7 w-7'
|
2022-08-30 13:26:42 -07:00
|
|
|
onClick={handleClick}
|
|
|
|
/>
|
|
|
|
|
2022-12-15 14:51:30 -08:00
|
|
|
<Text size='sm' theme='primary' weight='semibold' transform='uppercase'>
|
|
|
|
<FormattedMessage id='compose_event.upload_banner' defaultMessage='Upload photo' />
|
|
|
|
</Text>
|
|
|
|
<input
|
|
|
|
ref={fileElement}
|
|
|
|
type='file'
|
|
|
|
accept={attachmentTypes && attachmentTypes.toArray().join(',')}
|
|
|
|
onChange={handleChange}
|
|
|
|
disabled={disabled}
|
|
|
|
className='hidden'
|
|
|
|
/>
|
|
|
|
</HStack>
|
2022-08-30 13:26:42 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UploadButton;
|