2021-10-04 23:05:03 +00:00
|
|
|
// Copyright 2019-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
import * as React from 'react';
|
2020-09-28 18:40:26 +00:00
|
|
|
import { FileWithPath } from 'react-dropzone';
|
2019-12-17 20:25:57 +00:00
|
|
|
import * as styles from './DropZone.scss';
|
|
|
|
import { useI18n } from '../util/i18n';
|
2020-09-28 18:40:26 +00:00
|
|
|
import { useStickerDropzone } from '../util/useStickerDropzone';
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
export type Props = {
|
|
|
|
readonly inner?: boolean;
|
|
|
|
onDrop(files: Array<string>): unknown;
|
|
|
|
onDragActive?(active: boolean): unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getClassName = ({ inner }: Props, isDragActive: boolean) => {
|
|
|
|
if (inner) {
|
|
|
|
return styles.base;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDragActive) {
|
|
|
|
return styles.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
return styles.standalone;
|
|
|
|
};
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
export const DropZone: React.ComponentType<Props> = props => {
|
2019-12-17 20:25:57 +00:00
|
|
|
const { inner, onDrop, onDragActive } = props;
|
|
|
|
const i18n = useI18n();
|
|
|
|
|
|
|
|
const handleDrop = React.useCallback(
|
2020-09-14 21:56:35 +00:00
|
|
|
(files: ReadonlyArray<FileWithPath>) => {
|
2019-12-17 20:25:57 +00:00
|
|
|
onDrop(files.map(({ path }) => path));
|
|
|
|
},
|
|
|
|
[onDrop]
|
|
|
|
);
|
|
|
|
|
2020-09-28 18:40:26 +00:00
|
|
|
const { getRootProps, getInputProps, isDragActive } = useStickerDropzone(
|
|
|
|
handleDrop
|
|
|
|
);
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (onDragActive) {
|
|
|
|
onDragActive(isDragActive);
|
|
|
|
}
|
|
|
|
}, [isDragActive, onDragActive]);
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
return (
|
2021-10-04 17:14:00 +00:00
|
|
|
<div
|
|
|
|
{...getRootProps({ className: getClassName(props, isDragActive) })}
|
|
|
|
role="button"
|
|
|
|
>
|
2019-12-17 20:25:57 +00:00
|
|
|
<input {...getInputProps()} />
|
|
|
|
<svg viewBox="0 0 36 36" width="36px" height="36px">
|
|
|
|
<path d="M32 17.25H18.75V4h-1.5v13.25H4v1.5h13.25V32h1.5V18.75H32v-1.5z" />
|
|
|
|
</svg>
|
|
|
|
{!inner ? (
|
|
|
|
<p className={styles.text}>
|
|
|
|
{isDragActive
|
2021-10-04 23:05:03 +00:00
|
|
|
? i18n('StickerCreator--DropZone--activeText')
|
|
|
|
: i18n('StickerCreator--DropZone--staticText')}
|
2019-12-17 20:25:57 +00:00
|
|
|
</p>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|