lbry-desktop/ui/component/postEditor/view.jsx
Franco Montenegro 329d434c83
Allow only images in modal image uploader. (#7672)
* Allow only images in modal image uploader.

* Set file path and mime in file selector.

* Refactor WebFile.

* Update get-file-from-path to work with folders; fix file-list component.

* Get rid of File | string for filePath property in components.

* Show instant preview while updating channel thumbnail.

* Fix publish.

* Add jpeg and svg to image filter.
2022-09-02 12:43:35 -04:00

114 lines
2.6 KiB
JavaScript

// @flow
import React, { useEffect } from 'react';
import { FormField } from 'component/common/form';
type Props = {
uri: ?string,
label: ?string,
disabled: ?boolean,
filePath: File,
fileText: ?string,
fileMimeType: ?string,
streamingUrl: ?string,
isStillEditing: boolean,
fetchStreamingUrl: (string) => void,
setPrevFileText: (string) => void,
updatePublishForm: ({}) => void,
setCurrentFileType: (string) => void,
};
function PostEditor(props: Props) {
const {
uri,
label,
disabled,
filePath,
fileText,
streamingUrl,
fileMimeType,
isStillEditing,
setPrevFileText,
fetchStreamingUrl,
updatePublishForm,
setCurrentFileType,
} = props;
const editing = isStillEditing && uri;
const [ready, setReady] = React.useState(!editing);
const [loading, setLoading] = React.useState(false);
useEffect(() => {
if (editing && uri) {
fetchStreamingUrl(uri);
}
}, [uri, editing, fetchStreamingUrl]);
// Ready to edit content
useEffect(() => {
if (!ready && !loading && fileText && streamingUrl) {
setReady(true);
}
}, [ready, loading, fileText, streamingUrl]);
useEffect(() => {
if (fileText && loading) {
setLoading(false);
} else if (!fileText && loading) {
setLoading(true);
}
}, [fileText, loading, setLoading]);
useEffect(() => {
function readFileStream(url) {
return fetch(url).then((res) => res.text());
}
async function updateEditorText(url) {
try {
const text = await readFileStream(url);
if (text) {
// Store original content
setPrevFileText(text);
// Update text editor form
updatePublishForm({ fileText: text });
}
} catch (error) {
console.error(error); // eslint-disable-line
}
}
if (editing) {
// Editing same file (previously published)
// User can use a different file to replace the content
if (!ready && !filePath && !fileText && streamingUrl && fileMimeType === 'text/markdown') {
setCurrentFileType(fileMimeType);
updateEditorText(streamingUrl);
}
}
}, [
ready,
editing,
fileText,
filePath,
fileMimeType,
streamingUrl,
setPrevFileText,
updatePublishForm,
setCurrentFileType,
]);
return (
<FormField
type={'markdown'}
name="content_post"
label={label}
placeholder={__('My content for this post...')}
value={ready ? fileText : __('Loading...')}
disabled={!ready || disabled}
onChange={(value) => updatePublishForm({ fileText: value })}
/>
);
}
export default PostEditor;