lbry-desktop/ui/component/settingCommentsServer/view.jsx

103 lines
3.7 KiB
React
Raw Permalink Normal View History

// @flow
import { COMMENT_SERVER_API, COMMENT_SERVER_NAME } from 'config';
import React from 'react';
import Comments from 'comments';
2022-02-03 16:13:54 +01:00
import ItemInputRow from './internal/input-radio-panel-addCommentServer';
2022-02-02 07:19:39 +01:00
import Button from 'component/button';
import { FormField } from 'component/common/form-components/form-field';
type Props = {
customServerEnabled: boolean,
customServerUrl: string,
setCustomServerEnabled: (boolean) => void,
setCustomServerUrl: (string) => void,
2022-02-02 07:19:39 +01:00
setCustomServers: (Array<CommentServerDetails>) => void,
customCommentServers: Array<CommentServerDetails>,
};
const defaultServer = { name: COMMENT_SERVER_NAME, url: COMMENT_SERVER_API };
function SettingCommentsServer(props: Props) {
2022-02-02 07:19:39 +01:00
const {
customServerEnabled,
customServerUrl,
setCustomServerEnabled,
setCustomServerUrl,
customCommentServers,
setCustomServers,
} = props;
const [addServer, setAddServer] = React.useState(false);
2022-02-02 07:19:39 +01:00
const customServersString = JSON.stringify(customCommentServers);
// "migrate" to make sure any currently set custom server is in saved list
React.useEffect(() => {
2022-02-02 07:19:39 +01:00
// const servers = JSON.parse(customServersString);
// if customServerUrl is not in servers, make sure it is.
}, [customServerUrl, customServersString, setCustomServers]);
const handleSelectServer = (serverItem: CommentServerDetails) => {
if (serverItem.url !== COMMENT_SERVER_API) {
Comments.setServerUrl(serverItem.url);
setCustomServerUrl(serverItem.url);
setCustomServerEnabled(true);
} else {
Comments.setServerUrl(undefined);
setCustomServerEnabled(false);
}
};
const handleAddServer = (serverItem: CommentServerDetails) => {
const newCustomServers = customCommentServers.slice();
newCustomServers.push(serverItem);
setCustomServers(newCustomServers);
handleSelectServer(serverItem);
setAddServer(false);
2022-02-02 07:19:39 +01:00
};
const handleRemoveServer = (serverItem: CommentServerDetails) => {
2022-02-02 07:19:39 +01:00
handleSelectServer(defaultServer);
const newCustomServers = customCommentServers.slice().filter((server) => {
return server.url !== serverItem.url;
});
setCustomServers(newCustomServers);
};
const commentServerLabel = (serverDetails, onRemove) => (
<span className={'compound-label'}>
<span>{serverDetails.name}</span>
<span className="compound-label__details">{serverDetails.url}</span>
{onRemove && <Button button={'inverse'} label={__('Remove')} onClick={() => onRemove(serverDetails)} />}
</span>
);
const commentServerRadio = (customServerEnabled, customServerUrl, onClick, onRemove, serverDetails) => (
<FormField
type="radio"
checked={(!customServerEnabled && !onRemove) || (customServerEnabled && customServerUrl === serverDetails.url)}
onClick={() => onClick(serverDetails)}
label={commentServerLabel(serverDetails, onRemove)}
name={`${serverDetails.name}${serverDetails.url}`}
key={`${serverDetails.name}${serverDetails.url}`}
/>
);
return (
<React.Fragment>
2022-02-02 07:19:39 +01:00
<div className={'fieldset-section'}>
{commentServerRadio(customServerEnabled, customServerUrl, handleSelectServer, null, defaultServer)}
{customCommentServers.map((server) =>
commentServerRadio(customServerEnabled, customServerUrl, handleSelectServer, handleRemoveServer, server)
)}
2022-02-02 07:19:39 +01:00
</div>
<div className={'fieldset-section'}>
{!addServer && (
<div className="section__actions">
<Button type="button" button="inverse" onClick={() => setAddServer(true)} label={__('Add A Server')} />
</div>
)}
2022-02-02 07:19:39 +01:00
{addServer && <ItemInputRow update={handleAddServer} onCancel={setAddServer} />}
</div>
</React.Fragment>
);
}
export default SettingCommentsServer;