Skip to content

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

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

import { SecretSyncConnectionField } from "@app/components/secret-syncs/forms/SecretSyncConnectionField";
import {
  Combobox,
  Field,
  FieldContent,
  FieldError,
  FieldGroup,
  FieldLabel,
  Tooltip,
  TooltipContent,
  TooltipTrigger
} from "@app/components/v3";
import {
  useOCIConnectionListCompartments,
  useOCIConnectionListVaultKeys,
  useOCIConnectionListVaults
} from "@app/hooks/api/appConnections/oci";
import { SecretSync } from "@app/hooks/api/secretSyncs";

import { TSecretSyncForm } from "../schemas";

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

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

  const { data: compartments, isLoading: isCompartmentsLoading } = useOCIConnectionListCompartments(
    connectionId,
    {
      enabled: Boolean(connectionId)
    }
  );

  const selectedCompartment = useWatch({ name: "destinationConfig.compartmentOcid", control });
  const { data: vaults, isLoading: isVaultsLoading } = useOCIConnectionListVaults(
    { connectionId, compartmentOcid: selectedCompartment },
    {
      enabled: Boolean(connectionId && selectedCompartment)
    }
  );

  const selectedVault = useWatch({ name: "destinationConfig.vaultOcid", control });
  const { data: keys, isLoading: isKeysLoading } = useOCIConnectionListVaultKeys(
    { connectionId, compartmentOcid: selectedCompartment, vaultOcid: selectedVault },
    {
      enabled: Boolean(connectionId && selectedCompartment && selectedVault)
    }
  );

  return (
    <FieldGroup>
      <SecretSyncConnectionField
        onChange={() => {
          setValue("destinationConfig.compartmentOcid", "");
          setValue("destinationConfig.vaultOcid", "");
          setValue("destinationConfig.keyOcid", "");
        }}
      />

      <Controller
        name="destinationConfig.compartmentOcid"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-ocivault-compartment-ocid-label"
              htmlFor="secret-sync-ocivault-compartment-ocid"
            >
              Compartment
              <Tooltip>
                <TooltipTrigger asChild>
                  <Info />
                </TooltipTrigger>
                <TooltipContent className="max-w-md">
                  Ensure the compartment exists and that the connection has permission to view it.
                </TooltipContent>
              </Tooltip>
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-ocivault-compartment-ocid-label"
                aria-describedby={error ? "secret-sync-ocivault-compartment-ocid-error" : undefined}
                id="secret-sync-ocivault-compartment-ocid"
                isError={Boolean(error)}
                isLoading={isCompartmentsLoading && Boolean(connectionId)}
                isDisabled={!connectionId}
                value={compartments?.find((c) => c.id === value) ?? null}
                onValueChange={(option) => {
                  onChange(option?.id ?? null);
                  setValue("destinationConfig.vaultOcid", "");
                  setValue("destinationConfig.keyOcid", "");
                }}
                options={compartments}
                placeholder="Select a compartment..."
                getOptionLabel={(option) => option.name}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                modal
              />
              <FieldError id="secret-sync-ocivault-compartment-ocid-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />

      <Controller
        name="destinationConfig.vaultOcid"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-ocivault-vault-ocid-label"
              htmlFor="secret-sync-ocivault-vault-ocid"
            >
              Vault
              <Tooltip>
                <TooltipTrigger asChild>
                  <Info />
                </TooltipTrigger>
                <TooltipContent className="max-w-md">
                  Ensure the vault exists in the selected compartment and that the connection has
                  permission to view it.
                </TooltipContent>
              </Tooltip>
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-ocivault-vault-ocid-label"
                aria-describedby={error ? "secret-sync-ocivault-vault-ocid-error" : undefined}
                id="secret-sync-ocivault-vault-ocid"
                isError={Boolean(error)}
                isLoading={isVaultsLoading && Boolean(connectionId)}
                isDisabled={!connectionId || !selectedCompartment}
                value={vaults?.find((v) => v.id === value) || null}
                onValueChange={(option) => {
                  onChange(option?.id ?? null);
                  setValue("destinationConfig.keyOcid", "");
                }}
                options={vaults}
                placeholder="Select a vault..."
                getOptionLabel={(option) => option.displayName}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                modal
              />
              <FieldError id="secret-sync-ocivault-vault-ocid-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />

      <Controller
        name="destinationConfig.keyOcid"
        control={control}
        render={({ field: { value, onChange }, fieldState: { error } }) => (
          <Field>
            <FieldLabel
              id="secret-sync-ocivault-key-ocid-label"
              htmlFor="secret-sync-ocivault-key-ocid"
            >
              Encryption Key
              <Tooltip>
                <TooltipTrigger asChild>
                  <Info />
                </TooltipTrigger>
                <TooltipContent className="max-w-md">
                  Ensure the key exists in the selected vault and that the connection has permission
                  to view it.
                </TooltipContent>
              </Tooltip>
            </FieldLabel>
            <FieldContent>
              <Combobox
                aria-labelledby="secret-sync-ocivault-key-ocid-label"
                aria-describedby={error ? "secret-sync-ocivault-key-ocid-error" : undefined}
                id="secret-sync-ocivault-key-ocid"
                isError={Boolean(error)}
                isLoading={isKeysLoading && Boolean(connectionId)}
                isDisabled={!connectionId || !selectedCompartment || !selectedVault}
                value={keys?.find((v) => v.id === value) ?? null}
                onValueChange={(option) => {
                  onChange(option?.id ?? null);
                }}
                options={keys}
                placeholder="Select a key..."
                getOptionLabel={(option) => option.displayName}
                getOptionValue={(option) => option.id}
                getOptionKeywords={(option) => [option.id]}
                modal
              />
              <FieldError id="secret-sync-ocivault-key-ocid-error" errors={[error]} />
            </FieldContent>
          </Field>
        )}
      />
    </FieldGroup>
  );
};