Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

7.1 KB
Raw Blame
import { Controller, useFormContext, useWatch } from "react-hook-form";

import { SecretSyncConnectionField } from "@app/components/secret-syncs/forms/SecretSyncConnectionField";
import {
  Combobox,
  Field,
  FieldContent,
  FieldDescription,
  FieldError,
  FieldGroup,
  FieldLabel
} from "@app/components/v3";
import {
  useNetlifyConnectionListAccounts,
  useNetlifyConnectionListSites
} from "@app/hooks/api/appConnections/netlify";
import { SecretSync } from "@app/hooks/api/secretSyncs";

import { TSecretSyncForm } from "../schemas";
import { NetlifySyncContext } from "../schemas/netlify-sync-destination-schema";

export const NetlifySyncFields = () => {
  const { control, setValue } = useFormContext<
    TSecretSyncForm & { destination: SecretSync.Netlify }
  >();

  const connectionId = useWatch({ name: "connection.id", control });
  const accountId = useWatch({ name: "destinationConfig.accountId", control });

  const { data: accounts = [], isPending: isAccountsLoading } = useNetlifyConnectionListAccounts(
    connectionId,
    {
      enabled: Boolean(connectionId)
    }
  );

  const { data: sites = [], isPending: isSitesLoading } = useNetlifyConnectionListSites(
    connectionId,
    accountId,
    {
      enabled: Boolean(accountId)
    }
  );

  const contexts = Object.entries(NetlifySyncContext).map(([key, value]) => ({
    label: key,
    value
  }));

  return (
    <FieldGroup>
      <SecretSyncConnectionField
        onChange={() => {
          setValue("destinationConfig.accountId", "");
          setValue("destinationConfig.accountName", "");
          setValue("destinationConfig.siteId", "");
          setValue("destinationConfig.siteName", undefined);
        }}
      />
      <Controller
        name="destinationConfig.accountId"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-netlify-account-id-label"
              htmlFor="secret-sync-netlify-account-id"
            >
              Account
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-netlify-account-id-label"
                aria-describedby={error ? "secret-sync-netlify-account-id-error" : undefined}
                id="secret-sync-netlify-account-id"
                isError={Boolean(error)}
                isLoading={isAccountsLoading && Boolean(connectionId)}
                isDisabled={!connectionId}
                value={accounts.find((p) => p.id === value) ?? null}
                onValueChange={(option) => {
                  const v = option;
                  onChange(v?.id ?? null);
                  setValue("destinationConfig.accountName", v?.name ?? "");
                  setValue("destinationConfig.siteId", "");
                  setValue("destinationConfig.siteName", undefined);
                }}
                options={accounts}
                placeholder="Select an account..."
                getOptionLabel={(option) => option.name}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                modal
              />
              <FieldError id="secret-sync-netlify-account-id-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />
      <Controller
        name="destinationConfig.siteId"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-netlify-site-id-label"
              htmlFor="secret-sync-netlify-site-id"
            >
              Site (Optional)
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-netlify-site-id-label"
                aria-describedby={
                  error
                    ? "secret-sync-netlify-site-id-description secret-sync-netlify-site-id-error"
                    : "secret-sync-netlify-site-id-description"
                }
                id="secret-sync-netlify-site-id"
                isError={Boolean(error)}
                isLoading={isSitesLoading && Boolean(accountId)}
                isDisabled={!accountId}
                value={sites.find((p) => p.id === value) ?? null}
                onValueChange={(option) => {
                  onChange(option.id);
                  setValue("destinationConfig.siteName", option.name);
                }}
                onClear={() => {
                  onChange("");
                  setValue("destinationConfig.siteName", undefined);
                }}
                options={sites}
                placeholder="Select a site..."
                getOptionLabel={(option) => option.name}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                modal
              />
              <FieldDescription id="secret-sync-netlify-site-id-description">
                If you do not select a site, the secrets will be synced to all sites in the account.
              </FieldDescription>
              <FieldError id="secret-sync-netlify-site-id-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />
      <Controller
        name="destinationConfig.context"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-netlify-context-label"
              htmlFor="secret-sync-netlify-context"
            >
              Context (Optional)
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-netlify-context-label"
                aria-describedby={
                  error
                    ? "secret-sync-netlify-context-description secret-sync-netlify-context-error"
                    : "secret-sync-netlify-context-description"
                }
                id="secret-sync-netlify-context"
                isError={Boolean(error)}
                isDisabled={!accountId}
                value={contexts.find((p) => p.value === value) ?? undefined}
                onValueChange={(option) => onChange(option.value)}
                onClear={() => onChange(null)}
                options={contexts}
                placeholder="Select a context..."
                getOptionLabel={(option) => option.label}
                getOptionValue={(option) => option.value}
                getOptionKeywords={(option) => [option.value]}
                modal
              />
              <FieldDescription id="secret-sync-netlify-context-description">
                Avoid configuring multiple syncs with overlapping contexts for the same site.
                &quot;All Contexts&quot; overlaps with every context. Overlapping syncs may delete
                each other&apos;s secrets.
              </FieldDescription>
              <FieldError id="secret-sync-netlify-context-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />
    </FieldGroup>
  );
};