2020-04-04 05:39:53 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
import { Field, Form, Formik, FormikHelpers, useFormikContext } from 'formik';
|
|
|
|
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
|
|
|
|
import { object, string } from 'yup';
|
2020-07-05 04:46:49 +00:00
|
|
|
import debounce from 'debounce';
|
2020-04-04 05:39:53 +00:00
|
|
|
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
|
|
|
import getServers from '@/api/getServers';
|
|
|
|
import { Server } from '@/api/server/getServer';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import { Link } from 'react-router-dom';
|
2020-07-03 21:19:05 +00:00
|
|
|
import styled from 'styled-components/macro';
|
|
|
|
import tw from 'twin.macro';
|
2020-07-04 22:19:46 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
2020-04-04 05:39:53 +00:00
|
|
|
|
|
|
|
type Props = RequiredModalProps;
|
|
|
|
|
|
|
|
interface Values {
|
|
|
|
term: string;
|
|
|
|
}
|
|
|
|
|
2020-04-12 23:19:43 +00:00
|
|
|
const ServerResult = styled(Link)`
|
2020-07-05 20:56:04 +00:00
|
|
|
${tw`flex items-center bg-neutral-900 p-4 rounded border-l-4 border-neutral-900 no-underline transition-all duration-150`};
|
2020-04-12 23:19:43 +00:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
${tw`shadow border-cyan-500`};
|
|
|
|
}
|
|
|
|
|
|
|
|
&:not(:last-of-type) {
|
|
|
|
${tw`mb-2`};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-04-04 05:39:53 +00:00
|
|
|
const SearchWatcher = () => {
|
|
|
|
const { values, submitForm } = useFormikContext<Values>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (values.term.length >= 3) {
|
|
|
|
submitForm();
|
|
|
|
}
|
|
|
|
}, [ values.term ]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ({ ...props }: Props) => {
|
|
|
|
const ref = useRef<HTMLInputElement>(null);
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const [ servers, setServers ] = useState<Server[]>([]);
|
|
|
|
const isAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const search = debounce(({ term }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
setLoading(true);
|
|
|
|
setSubmitting(false);
|
|
|
|
clearFlashes('search');
|
2020-07-05 04:46:49 +00:00
|
|
|
|
2020-04-04 05:39:53 +00:00
|
|
|
getServers(term)
|
|
|
|
.then(servers => setServers(servers.items.filter((_, index) => index < 5)))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'search', message: httpErrorToHuman(error) });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (props.visible) {
|
|
|
|
setTimeout(() => ref.current?.focus(), 250);
|
|
|
|
}
|
|
|
|
}, [ props.visible ]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={search}
|
|
|
|
validationSchema={object().shape({
|
|
|
|
term: string()
|
|
|
|
.min(3, 'Please enter at least three characters to begin searching.')
|
|
|
|
.required('A search term must be provided.'),
|
|
|
|
})}
|
|
|
|
initialValues={{ term: '' } as Values}
|
|
|
|
>
|
|
|
|
<Modal {...props}>
|
|
|
|
<Form>
|
|
|
|
<FormikFieldWrapper
|
|
|
|
name={'term'}
|
|
|
|
label={'Search term'}
|
|
|
|
description={
|
|
|
|
isAdmin
|
|
|
|
? 'Enter a server name, user email, or uuid to begin searching.'
|
|
|
|
: 'Enter a server name to begin searching.'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<SearchWatcher/>
|
|
|
|
<InputSpinner visible={loading}>
|
2020-07-04 22:19:46 +00:00
|
|
|
<Field as={Input} innerRef={ref} name={'term'}/>
|
2020-04-04 05:39:53 +00:00
|
|
|
</InputSpinner>
|
|
|
|
</FormikFieldWrapper>
|
|
|
|
</Form>
|
|
|
|
{servers.length > 0 &&
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-04-04 05:39:53 +00:00
|
|
|
{
|
|
|
|
servers.map(server => (
|
2020-04-12 23:19:43 +00:00
|
|
|
<ServerResult
|
2020-04-04 05:39:53 +00:00
|
|
|
key={server.uuid}
|
|
|
|
to={`/server/${server.id}`}
|
|
|
|
onClick={() => props.onDismissed()}
|
|
|
|
>
|
|
|
|
<div>
|
2020-07-03 21:19:05 +00:00
|
|
|
<p css={tw`text-sm`}>{server.name}</p>
|
|
|
|
<p css={tw`mt-1 text-xs text-neutral-400`}>
|
2020-04-04 05:39:53 +00:00
|
|
|
{
|
2020-07-09 04:42:37 +00:00
|
|
|
server.allocations.filter(alloc => alloc.isDefault).map(allocation => (
|
2020-04-04 05:39:53 +00:00
|
|
|
<span key={allocation.ip + allocation.port.toString()}>{allocation.alias || allocation.ip}:{allocation.port}</span>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`flex-1 text-right`}>
|
|
|
|
<span css={tw`text-xs py-1 px-2 bg-cyan-800 text-cyan-100 rounded`}>
|
2020-04-04 05:39:53 +00:00
|
|
|
{server.node}
|
|
|
|
</span>
|
|
|
|
</div>
|
2020-04-12 23:19:43 +00:00
|
|
|
</ServerResult>
|
2020-04-04 05:39:53 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</Modal>
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|