/*
GoToSocial
Copyright (C) GoToSocial Authors admin@gotosocial.org
SPDX-License-Identifier: AGPL-3.0-or-later
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
import React from "react";
import { useMemo } from "react";
import { useLocation, useParams, useSearch } from "wouter";
import { useTextInput, useBoolInput } from "../../../lib/form";
import useFormSubmit from "../../../lib/form/submit";
import { TextInput, Checkbox, TextArea } from "../../../components/form/inputs";
import Loading from "../../../components/loading";
import BackButton from "../../../components/back-button";
import MutationButton from "../../../components/form/mutation-button";
import {
useDomainAllowsQuery,
useDomainBlocksQuery,
} from "../../../lib/query/admin/domain-permissions/get";
import {
useAddDomainAllowMutation,
useAddDomainBlockMutation,
useRemoveDomainAllowMutation,
useRemoveDomainBlockMutation,
useUpdateDomainAllowMutation,
useUpdateDomainBlockMutation,
} from "../../../lib/query/admin/domain-permissions/update";
import { DomainPerm } from "../../../lib/types/domain-permission";
import { NoArg } from "../../../lib/types/query";
import { Error } from "../../../components/error";
import { useBaseUrl } from "../../../lib/navigation/util";
import { PermType } from "../../../lib/types/perm";
import { useCapitalize } from "../../../lib/util";
import { formDomainValidator } from "../../../lib/util/formvalidators";
import UsernameLozenge from "../../../components/username-lozenge";
import { FormSubmitEvent } from "../../../lib/form/types";
export default function DomainPermView() {
const baseUrl = useBaseUrl();
const search = useSearch();
// Parse perm type from routing params, converting
// "blocks" => "block" and "allows" => "allow".
const params = useParams();
const permTypeRaw = params.permType;
if (permTypeRaw !== "blocks" && permTypeRaw !== "allows") {
throw "unrecognized perm type " + params.permType;
}
const permType = useMemo(() => {
return permTypeRaw.slice(0, -1) as PermType;
}, [permTypeRaw]);
// Conditionally fetch either domain blocks or domain
// allows depending on which perm type we're looking at.
const {
data: blocks = {},
isLoading: loadingBlocks,
isFetching: fetchingBlocks,
} = useDomainBlocksQuery(NoArg, { skip: permType !== "block" });
const {
data: allows = {},
isLoading: loadingAllows,
isFetching: fetchingAllows,
} = useDomainAllowsQuery(NoArg, { skip: permType !== "allow" });
// Wait until we're done loading.
const loading = permType === "block"
? loadingBlocks || fetchingBlocks
: loadingAllows || fetchingAllows;
if (loading) {
return ;
}
// Parse domain from routing params.
let domain = params.domain ?? "unknown";
if (domain === "view") {
// Retrieve domain from form field submission.
const searchParams = new URLSearchParams(search);
const searchDomain = searchParams.get("domain");
if (!searchDomain) {
throw "empty view domain";
}
domain = searchDomain;
}
// Normalize / decode domain
// (it may be URL-encoded).
domain = decodeURIComponent(domain);
// Check if we already have a perm
// of the desired type for this domain.
const existingPerm = permType === "block"
? blocks[domain]
: allows[domain];
const title = Domain {permType} for {domain};
return (
{title}
{ existingPerm
?
: No stored {permType} yet, you can add one below:
}