2023-02-02 11:54:36 -08:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import { List as ImmutableList } from 'immutable';
|
2023-01-26 10:30:26 -08:00
|
|
|
import React from 'react';
|
|
|
|
|
2023-02-02 11:54:36 -08:00
|
|
|
import { openModal } from 'soapbox/actions/modals';
|
2023-02-02 11:06:27 -08:00
|
|
|
import Blurhash from 'soapbox/components/blurhash';
|
2023-01-26 10:30:26 -08:00
|
|
|
import { Icon } from 'soapbox/components/ui';
|
2023-02-02 11:54:36 -08:00
|
|
|
import { useAppDispatch } from 'soapbox/hooks';
|
2023-01-26 10:30:26 -08:00
|
|
|
|
2023-02-02 12:20:40 -08:00
|
|
|
import ChatUploadPreview from './chat-upload-preview';
|
|
|
|
|
2023-01-26 10:30:26 -08:00
|
|
|
import type { Attachment } from 'soapbox/types/entities';
|
|
|
|
|
|
|
|
interface IChatUpload {
|
|
|
|
attachment: Attachment,
|
|
|
|
onDelete?(): void,
|
|
|
|
}
|
|
|
|
|
|
|
|
/** An attachment uploaded to the chat composer, before sending. */
|
|
|
|
const ChatUpload: React.FC<IChatUpload> = ({ attachment, onDelete }) => {
|
2023-02-02 11:54:36 -08:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
const clickable = attachment.type !== 'unknown';
|
|
|
|
|
|
|
|
const handleOpenModal = () => {
|
|
|
|
dispatch(openModal('MEDIA', { media: ImmutableList.of(attachment), index: 0 }));
|
|
|
|
};
|
|
|
|
|
2023-01-26 10:30:26 -08:00
|
|
|
return (
|
2023-02-03 10:03:45 -08:00
|
|
|
<div className='relative isolate inline-block h-24 w-24 overflow-hidden rounded-lg bg-gray-200 dark:bg-primary-900'>
|
|
|
|
<Blurhash hash={attachment.blurhash} className='absolute inset-0 -z-10 h-full w-full' />
|
2023-02-02 11:06:27 -08:00
|
|
|
|
2023-01-26 10:30:26 -08:00
|
|
|
<div className='absolute right-[6px] top-[6px]'>
|
|
|
|
<RemoveButton onClick={onDelete} />
|
|
|
|
</div>
|
|
|
|
|
2023-02-02 11:54:36 -08:00
|
|
|
<button
|
|
|
|
onClick={clickable ? handleOpenModal : undefined}
|
2023-02-03 10:03:45 -08:00
|
|
|
className={clsx('h-full w-full', { 'cursor-zoom-in': clickable, 'cursor-default': !clickable })}
|
2023-02-02 11:54:36 -08:00
|
|
|
>
|
2023-02-02 12:20:40 -08:00
|
|
|
<ChatUploadPreview attachment={attachment} />
|
2023-02-02 11:54:36 -08:00
|
|
|
</button>
|
2023-01-26 10:30:26 -08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
interface IRemoveButton {
|
|
|
|
onClick?: React.MouseEventHandler<HTMLButtonElement>
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Floating button to remove an attachment. */
|
|
|
|
const RemoveButton: React.FC<IRemoveButton> = ({ onClick }) => {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
onClick={onClick}
|
2023-02-03 10:03:45 -08:00
|
|
|
className='flex h-5 w-5 items-center justify-center rounded-full bg-secondary-500 p-1'
|
2023-01-26 10:30:26 -08:00
|
|
|
>
|
|
|
|
<Icon
|
2023-02-06 09:30:36 -08:00
|
|
|
className='h-3 w-3 text-white'
|
2023-01-26 10:30:26 -08:00
|
|
|
src={require('@tabler/icons/x.svg')}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatUpload;
|