import { FormikErrors } from 'formik'; import { useCurrentEnvironment } from '@/react/hooks/useCurrentEnvironment'; import { Authorized } from '@/react/hooks/useUser'; import { AccessControlForm } from '@/react/portainer/access-control'; import { AccessControlFormData } from '@/react/portainer/access-control/types'; import { EnvironmentType } from '@/react/portainer/environments/types'; import { NodeSelector } from '@/react/docker/agent/NodeSelector'; import { useIsSwarm } from '@/react/docker/proxy/queries/useInfo'; import { useEnvironmentId } from '@/react/hooks/useEnvironmentId'; import { isAgentEnvironment } from '@/react/portainer/environments/utils'; import { FeatureId } from '@/react/portainer/feature-flags/enums'; import { FormControl } from '@@/form-components/FormControl'; import { FormSection } from '@@/form-components/FormSection'; import { Input } from '@@/form-components/Input'; import { SwitchField } from '@@/form-components/SwitchField'; import { ImageConfigFieldset, ImageConfigValues } from '@@/ImageConfigFieldset'; import { LoadingButton } from '@@/buttons'; import { Widget } from '@@/Widget'; import { PortsMappingField, Values as PortMappingValue, } from './PortsMappingField'; export interface Values { name: string; enableWebhook: boolean; publishAllPorts: boolean; image: ImageConfigValues; alwaysPull: boolean; ports: PortMappingValue; accessControl: AccessControlFormData; nodeName: string; autoRemove: boolean; } function useIsAgentOnSwarm() { const environmentId = useEnvironmentId(); const environmentQuery = useCurrentEnvironment(); const isSwarm = useIsSwarm(environmentId); return ( !!environmentQuery.data && isAgentEnvironment(environmentQuery.data?.Type) && isSwarm ); } export function BaseForm({ values, onChange, errors, setFieldError, isValid, isLoading, }: { values: Values; onChange: (values: Values) => void; errors?: FormikErrors; setFieldError: (field: string, error: string) => void; isValid: boolean; isLoading: boolean; }) { const environmentQuery = useCurrentEnvironment(); const isAgentOnSwarm = useIsAgentOnSwarm(); if (!environmentQuery.data) { return null; } const environment = environmentQuery.data; const canUseWebhook = environment.Type !== EnvironmentType.EdgeAgentOnDocker; return ( onChange({ ...values, name: e.target.value })} placeholder="e.g. myContainer" /> setFieldError('image', valid || '')} fieldNamespace="image" autoComplete checkRateLimits={values.alwaysPull} errors={errors?.image} >
onChange({ ...values, alwaysPull })} />
{canUseWebhook && (
onChange({ ...values, enableWebhook }) } featureId={FeatureId.CONTAINER_WEBHOOK} />
)}
onChange({ ...values, publishAllPorts }) } />
onChange({ ...values, ports })} errors={errors?.ports} />
{isAgentOnSwarm && ( onChange({ ...values, nodeName })} /> )} onChange({ ...values, accessControl })} errors={errors?.accessControl} values={values.accessControl} />
onChange({ ...values, autoRemove })} />
Deploy the container
); }