Skip to content

The open alternative to Salesforce, designed for AI.

4.4 KB
Raw Blame
import { useState } from 'react';
import { useMutation, useQuery } from '@apollo/client/react';
import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
import { isDefined, normalizeIframeOrigin } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { Section } from 'twenty-ui/components/layout';
import { IconPlus, IconTrash } from 'twenty-ui/icon';
import { Button } from 'twenty-ui/primitives/input';
import { themeCssVariables } from 'twenty-ui/theme';

import { getToastOptionsFromError } from '@/error-handler/utils/getToastOptionsFromError';
import { SettingsTextInput } from '@/ui/input/components/SettingsTextInput';
import {
  GetWorkspaceAllowedEmbeddingOriginsDocument,
  UpdateWorkspaceAllowedEmbeddingOriginsDocument,
} from '~/generated-metadata/graphql';

const StyledList = styled.div`
  display: flex;
  flex-direction: column;
  gap: ${themeCssVariables.spacing[3]};
`;

const StyledRow = styled.div`
  align-items: center;
  display: flex;
  gap: ${themeCssVariables.spacing[2]};
  justify-content: space-between;
`;

const StyledOrigin = styled.span`
  overflow-wrap: anywhere;
`;

export const SettingsSecurityIframeOrigins = () => {
  const { t } = useLingui();
  const { enqueueToast } = useToast();
  const [originInput, setOriginInput] = useState('');
  const [error, setError] = useState<string>();
  const {
    data,
    loading: isLoadingPolicy,
    error: policyError,
    refetch,
  } = useQuery(GetWorkspaceAllowedEmbeddingOriginsDocument, {
    fetchPolicy: 'network-only',
  });
  const [updateOrigins, { loading }] = useMutation(
    UpdateWorkspaceAllowedEmbeddingOriginsDocument,
  );
  const origins = data?.currentWorkspace.allowedEmbeddingOrigins ?? [];
  const isPolicyLoaded = isDefined(
    data?.currentWorkspace.allowedEmbeddingOrigins,
  );
  const isDisabled = loading || isLoadingPolicy || !isPolicyLoaded;

  const saveOrigin = async (
    operation: 'add' | 'remove',
    origin: string,
  ): Promise<boolean> => {
    if (isDisabled) {
      return false;
    }

    try {
      const { data } = await updateOrigins({
        variables: { input: { operation, origin } },
      });
      return isDefined(data?.updateWorkspaceAllowedEmbeddingOrigins);
    } catch (error) {
      enqueueToast(getToastOptionsFromError({ error }));
      return false;
    }
  };

  const handleAddOrigin = async () => {
    const origin = normalizeIframeOrigin(originInput.trim());

    if (!isDefined(origin)) {
      setError(
        t`Enter an HTTP or HTTPS origin without a path or wildcard, such as https://portal.example.com.`,
      );

      return;
    }

    if (await saveOrigin('add', origin)) {
      setOriginInput('');
      setError(undefined);
    }
  };

  return (
    <Section.Root>
      <Section.Header
        title={t`Iframe embedding`}
        description={t`Websites allowed to embed this workspace. Only add ones you fully trust.`}
      />
      <StyledList>
        {isDefined(policyError) && (
          <>
            <StyledOrigin role="alert">{t`Unable to load embedding settings.`}</StyledOrigin>
            <Button onClick={() => refetch()}>{t`Retry`}</Button>
          </>
        )}
        {origins.map((origin) => (
          <StyledRow key={origin}>
            <StyledOrigin>{origin}</StyledOrigin>
            <Button
              aria-label={t`Remove ${origin}`}
              startIcon={<IconTrash size={16} />}
              variant="outline"
              size="sm"
              disabled={isDisabled}
              onClick={() => saveOrigin('remove', origin)}
            >
              {t`Remove`}
            </Button>
          </StyledRow>
        ))}
        <SettingsTextInput
          instanceId="allowed-iframe-origin"
          label={t`Allowed origin`}
          placeholder="https://portal.example.com"
          value={originInput}
          disabled={isDisabled}
          onChange={(value) => {
            setOriginInput(value);
            setError(undefined);
          }}
          onInputEnter={handleAddOrigin}
          error={error}
        />
        <StyledRow>
          <Button
            variant="outline"
            size="sm"
            disabled={isDisabled || originInput.trim() === ''}
            startIcon={<IconPlus size={16} />}
            onClick={handleAddOrigin}
          >
            {t`Add origin`}
          </Button>
        </StyledRow>
      </StyledList>
    </Section.Root>
  );
};