import { FormikErrors } from 'formik'; import { array, object, SchemaOf, string } from 'yup'; import _ from 'lodash'; import { useLoggingPlugins } from '@/react/docker/proxy/queries/useServicePlugins'; import { useEnvironmentId } from '@/react/hooks/useEnvironmentId'; import { FormControl } from '@@/form-components/FormControl'; import { FormSection } from '@@/form-components/FormSection'; import { InputGroup } from '@@/form-components/InputGroup'; import { InputList, ItemProps } from '@@/form-components/InputList'; import { PortainerSelect } from '@@/form-components/PortainerSelect'; import { TextTip } from '@@/Tip/TextTip'; import { FormError } from '@@/form-components/FormError'; export interface LogConfig { type: string; options: Array<{ option: string; value: string }>; } export function LoggerConfig({ value, onChange, apiVersion, errors, }: { value: LogConfig; onChange: (value: LogConfig) => void; apiVersion: number; errors?: FormikErrors; }) { const envId = useEnvironmentId(); const pluginsQuery = useLoggingPlugins(envId, apiVersion < 1.25); if (!pluginsQuery.data) { return null; } const isDisabled = !value.type || value.type === 'none'; const pluginOptions = [ { label: 'Default logging driver', value: '' }, ...pluginsQuery.data.map((p) => ({ label: p, value: p })), { label: 'none', value: 'none' }, ]; return ( onChange({ ...value, type: type || '' })} options={pluginOptions} /> Logging driver that will override the default docker daemon driver. Select Default logging driver if you don't want to override it. Supported logging drivers can be found{' '} in the Docker documentation . handleChange({ options })} value={value.options} item={Item} itemBuilder={() => ({ option: '', value: '' })} disabled={isDisabled} errors={errors?.options} /> ); function handleChange(partial: Partial) { onChange({ ...value, ...partial }); } } function Item({ item: { option, value }, onChange, error, }: ItemProps<{ option: string; value: string }>) { return (
option handleChange({ option: e.target.value })} placeholder="e.g. FOO" /> value handleChange({ value: e.target.value })} placeholder="e.g bar" />
{error && {_.first(Object.values(error))}}
); function handleChange(partial: Partial<{ option: string; value: string }>) { onChange({ option, value, ...partial }); } } export function validation(): SchemaOf { return object({ options: array().of( object({ option: string().required('Option is required'), value: string().required('Value is required'), }) ), type: string().default(''), }); }