import { Controller, useFormContext } from "react-hook-form"; import { SingleValue } from "react-select"; import { faInfoCircle } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { AppConnectionOption } from "@app/components/app-connections"; import { FilterableSelect, FormControl } from "@app/components/v2"; import { ProjectPermissionSub, useProject, useProjectPermission } from "@app/context"; import { ProjectPermissionAppConnectionActions } from "@app/context/ProjectPermissionContext/types"; import { APP_CONNECTION_MAP } from "@app/helpers/appConnections"; import { SECRET_SYNC_CONNECTION_MAP } from "@app/helpers/secretSyncs"; import { usePopUp } from "@app/hooks"; import { useListAvailableAppConnections } from "@app/hooks/api/appConnections"; import { AddAppConnectionModal } from "@app/pages/organization/AppConnections/AppConnectionsPage/components"; import { TSecretSyncForm } from "./schemas"; type Props = { onChange?: VoidFunction; }; export const SecretSyncConnectionField = ({ onChange: callback }: Props) => { const { permission } = useProjectPermission(); const { control, watch, setValue } = useFormContext(); const { popUp, handlePopUpToggle, handlePopUpOpen } = usePopUp(["addConnection"] as const); const destination = watch("destination"); const app = SECRET_SYNC_CONNECTION_MAP[destination]; const { currentProject } = useProject(); const { data: availableConnections, isPending } = useListAvailableAppConnections( app, currentProject.id ); const connectionName = APP_CONNECTION_MAP[app].name; const canCreateConnection = permission.can( ProjectPermissionAppConnectionActions.Create, ProjectPermissionSub.AppConnections ); const appName = APP_CONNECTION_MAP[SECRET_SYNC_CONNECTION_MAP[destination]].name; return ( <>

Specify the {appName} Connection to use to connect to {connectionName} and configure destination parameters.

( { if ((newValue as SingleValue<{ id: string; name: string }>)?.id === "_create") { handlePopUpOpen("addConnection"); onChange(null); // store for oauth callback connections localStorage.setItem("secretSyncFormData", JSON.stringify(watch())); if (callback) callback(); return; } onChange(newValue); if (callback) callback(); }} isLoading={isPending} options={[ ...(canCreateConnection ? [{ id: "_create", name: "Create Connection" }] : []), ...(availableConnections ?? []) ]} placeholder="Select connection..." getOptionLabel={(option) => option.name} getOptionValue={(option) => option.id} components={{ Option: AppConnectionOption }} /> )} control={control} name="connection" /> {!isPending && !availableConnections?.length && !canCreateConnection && (

You do not have access to any {appName} Connections. Contact an admin to create one.

)} { // remove form storage, not oauth connection localStorage.removeItem("secretSyncFormData"); handlePopUpToggle("addConnection", isOpen); }} projectType={currentProject.type} projectId={currentProject.id} app={app} onComplete={(connection) => { if (connection) { setValue("connection", connection); } }} /> ); };