import { observer } from "mobx-react"; import { CloseIcon } from "outline-icons"; import * as React from "react"; import { Trans, useTranslation } from "react-i18next"; import styled from "styled-components"; import Button from "~/components/Button"; import Fade from "~/components/Fade"; import Flex from "~/components/Flex"; import Input from "~/components/Input"; import NudeButton from "~/components/NudeButton"; import Tooltip from "~/components/Tooltip"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useStores from "~/hooks/useStores"; import useToasts from "~/hooks/useToasts"; import SettingRow from "./SettingRow"; type Props = { onSuccess: () => void; }; function DomainManagement({ onSuccess }: Props) { const { auth } = useStores(); const team = useCurrentTeam(); const { t } = useTranslation(); const { showToast } = useToasts(); const [allowedDomains, setAllowedDomains] = React.useState([ ...(team.allowedDomains ?? []), ]); const [lastKnownDomainCount, updateLastKnownDomainCount] = React.useState( allowedDomains.length ); const [existingDomainsTouched, setExistingDomainsTouched] = React.useState(false); const handleSaveDomains = React.useCallback(async () => { try { await auth.updateTeam({ allowedDomains, }); onSuccess(); setExistingDomainsTouched(false); updateLastKnownDomainCount(allowedDomains.length); } catch (err) { showToast(err.message, { type: "error", }); } }, [auth, allowedDomains, onSuccess, showToast]); const handleRemoveDomain = async (index: number) => { const newDomains = allowedDomains.filter((_, i) => index !== i); setAllowedDomains(newDomains); const touchedExistingDomain = index < lastKnownDomainCount; if (touchedExistingDomain) { setExistingDomainsTouched(true); } }; const handleAddDomain = () => { const newDomains = [...allowedDomains, ""]; setAllowedDomains(newDomains); }; const createOnDomainChangedHandler = (index: number) => (ev: React.ChangeEvent) => { const newDomains = allowedDomains.slice(); newDomains[index] = ev.currentTarget.value; setAllowedDomains(newDomains); const touchedExistingDomain = index < lastKnownDomainCount; if (touchedExistingDomain) { setExistingDomainsTouched(true); } }; const showSaveChanges = existingDomainsTouched || allowedDomains.filter((value: string) => value !== "").length > // New domains were added lastKnownDomainCount; return ( {allowedDomains.map((domain, index) => ( handleRemoveDomain(index)}> ))} {!allowedDomains.length || allowedDomains[allowedDomains.length - 1] !== "" ? ( ) : ( )} {showSaveChanges && ( )} ); } const Remove = styled("div")` margin-top: 6px; `; export default observer(DomainManagement);