lbry-desktop/ui/component/settingCommentsServer/view.jsx
infinite-persistence 64323013cf
Desktop: Fix comment-server set to null unintentionally
## Issue
6956 Desktop: something wrong with Custom Comment Server setting

## Notes
- Should be calling `Comments.setServerUrl` to ensure all private variables are updated.
- Skip the react-setting update if there is no change.
2021-08-26 17:22:53 +08:00

72 lines
2 KiB
JavaScript

// @flow
import { COMMENT_SERVER_NAME } from 'config';
import React from 'react';
import Comments from 'comments';
import { FormField } from 'component/common/form';
const DEBOUNCE_TEXT_INPUT_MS = 500;
type Props = {
customServerEnabled: boolean,
customServerUrl: string,
setCustomServerEnabled: (boolean) => void,
setCustomServerUrl: (string) => void,
};
function SettingCommentsServer(props: Props) {
const { customServerEnabled, customServerUrl, setCustomServerEnabled, setCustomServerUrl } = props;
const [url, setUrl] = React.useState(customServerUrl);
React.useEffect(() => {
const timer = setTimeout(() => {
Comments.setServerUrl(customServerEnabled ? url : undefined);
if (url !== customServerUrl) {
setCustomServerUrl(url);
}
}, DEBOUNCE_TEXT_INPUT_MS);
return () => clearTimeout(timer);
}, [url, customServerUrl, customServerEnabled, setCustomServerUrl]);
return (
<React.Fragment>
<fieldset-section>
<FormField
type="radio"
name="use_default_comments_server"
label={__('Default comments server (%name%)', { name: COMMENT_SERVER_NAME })}
checked={!customServerEnabled}
onChange={(e) => {
if (e.target.checked) {
setCustomServerEnabled(false);
}
}}
/>
<FormField
type="radio"
name="use_custom_comments_server"
label={__('Custom comments server')}
checked={customServerEnabled}
onChange={(e) => {
if (e.target.checked) {
setCustomServerEnabled(true);
}
}}
/>
{customServerEnabled && (
<div className="section__body">
<FormField
type="text"
placeholder="https://comment.mysite.com"
value={url}
onChange={(e) => setUrl(e.target.value)}
/>
</div>
)}
</fieldset-section>
</React.Fragment>
);
}
export default SettingCommentsServer;