import React, { useEffect } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { Button } from '../../../../components/ui/Button'; import { Switch } from '../../../../components/ui/Switch'; import { Input } from '../../../../components/ui/Input'; import { validateAppConfig } from '../../utils/validators'; import { FormField } from '../../../../core/types'; interface IProps { formFields: FormField[]; onSubmit: (values: FormValues) => void; initalValues?: { exposed?: boolean; domain?: string } & { [key: string]: string | boolean | undefined }; loading?: boolean; exposable?: boolean | null; } export type FormValues = { exposed?: boolean; domain?: string; [key: string]: string | boolean | undefined; }; const hiddenTypes = ['random']; const typeFilter = (field: FormField) => !hiddenTypes.includes(field.type); export const InstallForm: React.FC = ({ formFields, onSubmit, initalValues, exposable, loading }) => { const { register, handleSubmit, formState: { errors, isDirty }, setValue, watch, setError, control, } = useForm({}); const watchExposed = watch('exposed', false); useEffect(() => { if (initalValues && !isDirty) { Object.entries(initalValues).forEach(([key, value]) => { setValue(key, value); }); } }, [initalValues, isDirty, setValue]); const renderField = (field: FormField) => ( ); const renderExposeForm = () => ( <> } /> {watchExposed && (
Make sure this exact domain contains an A record pointing to your IP.
)} ); const validate = (values: FormValues) => { const validationErrors = validateAppConfig(values, formFields); Object.entries(validationErrors).forEach(([key, value]) => { if (value) { setError(key, { message: value }); } }); if (Object.keys(validationErrors).length === 0) { onSubmit(values); } }; return (
{formFields.filter(typeFilter).map(renderField)} {exposable && renderExposeForm()}
); };