detail.js (4674B)
1 /* 2 GoToSocial 3 Copyright (C) GoToSocial Authors admin@gotosocial.org 4 SPDX-License-Identifier: AGPL-3.0-or-later 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 "use strict"; 21 22 const React = require("react"); 23 const { useRoute, Redirect, useLocation } = require("wouter"); 24 25 const query = require("../../lib/query"); 26 27 const { useTextInput, useBoolInput } = require("../../lib/form"); 28 29 const useFormSubmit = require("../../lib/form/submit"); 30 31 const { TextInput, Checkbox, TextArea } = require("../../components/form/inputs"); 32 33 const Loading = require("../../components/loading"); 34 const BackButton = require("../../components/back-button"); 35 const MutationButton = require("../../components/form/mutation-button"); 36 37 module.exports = function InstanceDetail({ baseUrl }) { 38 const { data: blockedInstances = {}, isLoading } = query.useInstanceBlocksQuery(); 39 40 let [_match, { domain }] = useRoute(`${baseUrl}/:domain`); 41 42 if (domain == "view") { // from form field submission 43 domain = (new URL(document.location)).searchParams.get("domain"); 44 } 45 46 const existingBlock = React.useMemo(() => { 47 return blockedInstances[domain]; 48 }, [blockedInstances, domain]); 49 50 if (domain == undefined) { 51 return <Redirect to={baseUrl} />; 52 } 53 54 let infoContent = null; 55 56 if (isLoading) { 57 infoContent = <Loading />; 58 } else if (existingBlock == undefined) { 59 infoContent = <span>No stored block yet, you can add one below:</span>; 60 } else { 61 infoContent = ( 62 <div className="info"> 63 <i className="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i> 64 <b>Editing domain blocks isn't implemented yet, <a href="https://github.com/superseriousbusiness/gotosocial/issues/1198" target="_blank" rel="noopener noreferrer">check here for progress</a></b> 65 </div> 66 ); 67 } 68 69 return ( 70 <div> 71 <h1 className="text-cutoff"><BackButton to={baseUrl} /> Federation settings for: <span title={domain}>{domain}</span></h1> 72 {infoContent} 73 <DomainBlockForm defaultDomain={domain} block={existingBlock} baseUrl={baseUrl} /> 74 </div> 75 ); 76 }; 77 78 function DomainBlockForm({ defaultDomain, block = {}, baseUrl }) { 79 const isExistingBlock = block.domain != undefined; 80 81 const disabledForm = isExistingBlock 82 ? { 83 disabled: true, 84 title: "Domain suspensions currently cannot be edited." 85 } 86 : {}; 87 88 const form = { 89 domain: useTextInput("domain", { source: block, defaultValue: defaultDomain }), 90 obfuscate: useBoolInput("obfuscate", { source: block }), 91 commentPrivate: useTextInput("private_comment", { source: block }), 92 commentPublic: useTextInput("public_comment", { source: block }) 93 }; 94 95 const [submitForm, addResult] = useFormSubmit(form, query.useAddInstanceBlockMutation(), { changedOnly: false }); 96 97 const [removeBlock, removeResult] = query.useRemoveInstanceBlockMutation({ fixedCacheKey: block.id }); 98 99 const [location, setLocation] = useLocation(); 100 101 function verifyUrlThenSubmit(e) { 102 // Adding a new block happens on /settings/admin/federation/domain.com 103 // but if domain input changes, that doesn't match anymore and causes issues later on 104 // so, before submitting the form, silently change url, then submit 105 let correctUrl = `${baseUrl}/${form.domain.value}`; 106 if (location != correctUrl) { 107 setLocation(correctUrl); 108 } 109 return submitForm(e); 110 } 111 112 return ( 113 <form onSubmit={verifyUrlThenSubmit}> 114 <TextInput 115 field={form.domain} 116 label="Domain" 117 placeholder="example.com" 118 {...disabledForm} 119 /> 120 121 <Checkbox 122 field={form.obfuscate} 123 label="Obfuscate domain in public lists" 124 {...disabledForm} 125 /> 126 127 <TextArea 128 field={form.commentPrivate} 129 label="Private comment" 130 rows={3} 131 {...disabledForm} 132 /> 133 134 <TextArea 135 field={form.commentPublic} 136 label="Public comment" 137 rows={3} 138 {...disabledForm} 139 /> 140 141 <MutationButton 142 label="Suspend" 143 result={addResult} 144 {...disabledForm} 145 /> 146 147 { 148 isExistingBlock && 149 <MutationButton 150 type="button" 151 onClick={() => removeBlock(block.id)} 152 label="Remove" 153 result={removeResult} 154 className="button danger" 155 /> 156 } 157 158 </form> 159 ); 160 }