2022-10-04 16:00:00 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import { HStack, IconButton, Stack, Text, Textarea } from 'soapbox/components/ui';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'chat.input.placeholder', defaultMessage: 'Type a message' },
|
|
|
|
send: { id: 'chat.actions.send', defaultMessage: 'Send' },
|
|
|
|
failedToSend: { id: 'chat.failed_to_send', defaultMessage: 'Message failed to send.' },
|
|
|
|
retry: { id: 'chat.retry', defaultMessage: 'Retry?' },
|
|
|
|
});
|
|
|
|
|
2022-10-04 16:10:58 -07:00
|
|
|
interface IChatComposer extends Pick<React.TextareaHTMLAttributes<HTMLTextAreaElement>, 'onKeyDown' | 'onChange' | 'disabled'> {
|
2022-10-04 16:00:00 -07:00
|
|
|
value: string,
|
|
|
|
onSubmit: () => void,
|
|
|
|
hasErrorSubmittingMessage?: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Textarea input for chats. */
|
2022-10-17 08:50:33 -07:00
|
|
|
const ChatComposer = React.forwardRef<HTMLTextAreaElement | null, IChatComposer>(({
|
2022-10-04 16:00:00 -07:00
|
|
|
onKeyDown,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
onSubmit,
|
|
|
|
hasErrorSubmittingMessage = false,
|
2022-10-04 16:10:58 -07:00
|
|
|
disabled = false,
|
2022-10-04 16:00:00 -07:00
|
|
|
}, ref) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
|
2022-10-04 16:10:58 -07:00
|
|
|
const isSubmitDisabled = disabled || value.length === 0;
|
2022-10-04 16:00:00 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='mt-auto pt-4 px-4 shadow-3xl'>
|
|
|
|
<HStack alignItems='center' justifyContent='between' space={4}>
|
|
|
|
<Stack grow>
|
|
|
|
<Textarea
|
|
|
|
autoFocus
|
|
|
|
ref={ref}
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
isResizeable={false}
|
|
|
|
autoGrow
|
|
|
|
maxRows={5}
|
2022-10-04 16:10:58 -07:00
|
|
|
disabled={disabled}
|
2022-10-04 16:00:00 -07:00
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<IconButton
|
|
|
|
src={require('icons/airplane.svg')}
|
|
|
|
iconClassName='w-5 h-5'
|
|
|
|
className='text-primary-500'
|
|
|
|
disabled={isSubmitDisabled}
|
|
|
|
onClick={onSubmit}
|
|
|
|
/>
|
|
|
|
</HStack>
|
|
|
|
|
|
|
|
<HStack alignItems='center' className='h-5' space={1}>
|
|
|
|
{hasErrorSubmittingMessage && (
|
|
|
|
<>
|
|
|
|
<Text theme='danger' size='xs'>
|
|
|
|
{intl.formatMessage(messages.failedToSend)}
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
<button onClick={onSubmit} className='flex hover:underline'>
|
|
|
|
<Text theme='primary' size='xs' tag='span'>
|
|
|
|
{intl.formatMessage(messages.retry)}
|
|
|
|
</Text>
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</HStack>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ChatComposer;
|