Skip to content

The open alternative to Salesforce, designed for AI.

3.8 KB
Raw Blame
import { NavigationButton } from '@/ui/input/components/NavigationButton';

import { useQuery } from '@apollo/client/react';
import { useLingui } from '@lingui/react/macro';
import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath, isDefined } from 'twenty-shared/utils';
import { Section } from 'twenty-ui/components/layout';
import { IconPlus } from 'twenty-ui/icon';

import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
import { DpaDocumentPreview } from '@/settings/legal/components/DpaDocumentPreview';
import { DpaNotice } from '@/settings/legal/components/DpaNotice';
import { SettingsDpaAgreementsTable } from '@/settings/legal/components/SettingsDpaAgreementsTable';
import { GET_DPA_AGREEMENTS } from '@/settings/legal/graphql/queries/getDpaAgreements';
import { GET_DPA_PREVIEW } from '@/settings/legal/graphql/queries/getDpaPreview';
import {
  type DpaAgreement,
  type DpaDocument,
} from '@/settings/legal/types/Dpa';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
import { SettingsPageLayout } from '@/settings/components/layout/SettingsPageLayout';
import { SettingsSkeletonLoader } from '@/settings/components/SettingsSkeletonLoader';

export const SettingsLegalDpa = () => {
  const { t } = useLingui();
  // DPA queries are served by the core (/graphql) schema, not the default /metadata client.
  const apolloCoreClient = useApolloCoreClient();

  const { data: agreementsData, loading: agreementsLoading } = useQuery<{
    dpaAgreements: DpaAgreement[];
  }>(GET_DPA_AGREEMENTS, { client: apolloCoreClient });

  const agreements = agreementsData?.dpaAgreements ?? [];
  const hasAgreements = agreements.length > 0;

  const { data: previewData, error: previewError } = useQuery<{
    dpaPreview: DpaDocument;
  }>(GET_DPA_PREVIEW, {
    client: apolloCoreClient,
    skip: agreementsLoading || hasAgreements,
  });

  const preview = previewData?.dpaPreview;
  const isPreviewSettled = isDefined(previewData) || isDefined(previewError);
  const isLoading = agreementsLoading || (!hasAgreements && !isPreviewSettled);

  return (
    <SettingsPageLayout
      title={t`Data Processing Agreement`}
      links={[
        { children: t`Workspace`, href: getSettingsPath(SettingsPath.General) },
        { children: t`Legal` },
      ]}
      actionButton={
        <NavigationButton
          to={getSettingsPath(SettingsPath.LegalDpaNew)}
          startIcon={<IconPlus />}
          size="sm"
          variant="solid"
          color="accent"
        >{t`Generate DPA`}</NavigationButton>
      }
    >
      <SettingsPageContainer>
        {isLoading ? (
          <SettingsSkeletonLoader />
        ) : hasAgreements ? (
          <Section.Root>
            <Section.Header
              title={t`Executed copies`}
              description={t`Accepted and signed DPAs for this workspace, with their template version and date.`}
            />
            <SettingsDpaAgreementsTable agreements={agreements} />
          </Section.Root>
        ) : preview ? (
          <>
            {preview.notice && (
              <Section.Root>
                <DpaNotice text={preview.notice} />
              </Section.Root>
            )}
            <Section.Root>
              <Section.Header
                title={t`Data Processing Agreement`}
                description={t`No copy has been generated yet. This is the agreement that applies to your deployment — generate a signed copy from the top-right.`}
              />
              <DpaDocumentPreview document={preview} />
            </Section.Root>
          </>
        ) : (
          <Section.Root>
            <DpaNotice
              text={t`The Data Processing Agreement could not be loaded. Please try again.`}
            />
          </Section.Root>
        )}
      </SettingsPageContainer>
    </SettingsPageLayout>
  );
};