|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import Modal, { RequiredModalProps } from '@/components/elements/Modal'; |
| 3 | +import { Field, Form, Formik, FormikHelpers, useFormikContext } from 'formik'; |
| 4 | +import { Actions, useStoreActions, useStoreState } from 'easy-peasy'; |
| 5 | +import { object, string } from 'yup'; |
| 6 | +import { debounce } from 'lodash-es'; |
| 7 | +import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper'; |
| 8 | +import InputSpinner from '@/components/elements/InputSpinner'; |
| 9 | +import getServers from '@/api/getServers'; |
| 10 | +import { Server } from '@/api/server/getServer'; |
| 11 | +import { ApplicationStore } from '@/state'; |
| 12 | +import { httpErrorToHuman } from '@/api/http'; |
| 13 | +import { Link } from 'react-router-dom'; |
| 14 | + |
| 15 | +type Props = RequiredModalProps; |
| 16 | + |
| 17 | +interface Values { |
| 18 | + term: string; |
| 19 | +} |
| 20 | + |
| 21 | +const SearchWatcher = () => { |
| 22 | + const { values, submitForm } = useFormikContext<Values>(); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (values.term.length >= 3) { |
| 26 | + submitForm(); |
| 27 | + } |
| 28 | + }, [ values.term ]); |
| 29 | + |
| 30 | + return null; |
| 31 | +}; |
| 32 | + |
| 33 | +export default ({ ...props }: Props) => { |
| 34 | + const ref = useRef<HTMLInputElement>(null); |
| 35 | + const [ loading, setLoading ] = useState(false); |
| 36 | + const [ servers, setServers ] = useState<Server[]>([]); |
| 37 | + const isAdmin = useStoreState(state => state.user.data!.rootAdmin); |
| 38 | + const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
| 39 | + |
| 40 | + const search = debounce(({ term }: Values, { setSubmitting }: FormikHelpers<Values>) => { |
| 41 | + setLoading(true); |
| 42 | + setSubmitting(false); |
| 43 | + clearFlashes('search'); |
| 44 | + getServers(term) |
| 45 | + .then(servers => setServers(servers.items.filter((_, index) => index < 5))) |
| 46 | + .catch(error => { |
| 47 | + console.error(error); |
| 48 | + addError({ key: 'search', message: httpErrorToHuman(error) }); |
| 49 | + }) |
| 50 | + .then(() => setLoading(false)); |
| 51 | + }, 500); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (props.visible) { |
| 55 | + setTimeout(() => ref.current?.focus(), 250); |
| 56 | + } |
| 57 | + }, [ props.visible ]); |
| 58 | + |
| 59 | + return ( |
| 60 | + <Formik |
| 61 | + onSubmit={search} |
| 62 | + validationSchema={object().shape({ |
| 63 | + term: string() |
| 64 | + .min(3, 'Please enter at least three characters to begin searching.') |
| 65 | + .required('A search term must be provided.'), |
| 66 | + })} |
| 67 | + initialValues={{ term: '' } as Values} |
| 68 | + > |
| 69 | + <Modal {...props}> |
| 70 | + <Form> |
| 71 | + <FormikFieldWrapper |
| 72 | + name={'term'} |
| 73 | + label={'Search term'} |
| 74 | + description={ |
| 75 | + isAdmin |
| 76 | + ? 'Enter a server name, user email, or uuid to begin searching.' |
| 77 | + : 'Enter a server name to begin searching.' |
| 78 | + } |
| 79 | + > |
| 80 | + <SearchWatcher/> |
| 81 | + <InputSpinner visible={loading}> |
| 82 | + <Field |
| 83 | + innerRef={ref} |
| 84 | + name={'term'} |
| 85 | + className={'input-dark'} |
| 86 | + /> |
| 87 | + </InputSpinner> |
| 88 | + </FormikFieldWrapper> |
| 89 | + </Form> |
| 90 | + {servers.length > 0 && |
| 91 | + <div className={'mt-6'}> |
| 92 | + { |
| 93 | + servers.map(server => ( |
| 94 | + <Link |
| 95 | + key={server.uuid} |
| 96 | + to={`/server/${server.id}`} |
| 97 | + className={'flex items-center block bg-neutral-900 p-4 rounded border-l-4 border-neutral-900 no-underline hover:shadow hover:border-cyan-500 transition-colors duration-250'} |
| 98 | + onClick={() => props.onDismissed()} |
| 99 | + > |
| 100 | + <div> |
| 101 | + <p className={'text-sm'}>{server.name}</p> |
| 102 | + <p className={'mt-1 text-xs text-neutral-400'}> |
| 103 | + { |
| 104 | + server.allocations.filter(alloc => alloc.default).map(allocation => ( |
| 105 | + <span key={allocation.ip + allocation.port.toString()}>{allocation.alias || allocation.ip}:{allocation.port}</span> |
| 106 | + )) |
| 107 | + } |
| 108 | + </p> |
| 109 | + </div> |
| 110 | + <div className={'flex-1 text-right'}> |
| 111 | + <span className={'text-xs py-1 px-2 bg-cyan-800 text-cyan-100 rounded'}> |
| 112 | + {server.node} |
| 113 | + </span> |
| 114 | + </div> |
| 115 | + </Link> |
| 116 | + )) |
| 117 | + } |
| 118 | + </div> |
| 119 | + } |
| 120 | + </Modal> |
| 121 | + </Formik> |
| 122 | + ); |
| 123 | +}; |
0 commit comments