// @flow import { COMMENT_SERVER_API, COMMENT_SERVER_NAME } from 'config'; import React from 'react'; import Comments from 'comments'; import ItemInputRow from './internal/input-radio-panel-addCommentServer'; 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, setCustomServers: (Array) => void, customCommentServers: Array, }; const defaultServer = { name: COMMENT_SERVER_NAME, url: COMMENT_SERVER_API }; function SettingCommentsServer(props: Props) { const { customServerEnabled, customServerUrl, setCustomServerEnabled, setCustomServerUrl, customCommentServers, setCustomServers, } = props; const [addServer, setAddServer] = React.useState(false); const customServersString = JSON.stringify(customCommentServers); // "migrate" to make sure any currently set custom server is in saved list React.useEffect(() => { // 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); }; const handleRemoveServer = (serverItem: CommentServerDetails) => { handleSelectServer(defaultServer); const newCustomServers = customCommentServers.slice().filter((server) => { return server.url !== serverItem.url; }); setCustomServers(newCustomServers); }; const commentServerLabel = (serverDetails, onRemove) => ( {serverDetails.name} {serverDetails.url} {onRemove &&