2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-08-03 03:32:10 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import Modal from '@/components/elements/Modal';
|
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import setSelectedDockerImage from '@/api/server/setSelectedDockerImage';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { SocketEvent, SocketRequest } from '@/components/server/events';
|
|
|
|
import Select from '@/components/elements/Select';
|
2022-05-07 22:18:14 +00:00
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import getServerStartup from '@/api/swr/getServerStartup';
|
|
|
|
import InputSpinner from '@/components/elements/InputSpinner';
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-05-07 22:18:14 +00:00
|
|
|
const MATCH_ERRORS = [
|
|
|
|
'minecraft 1.17 requires running the server with java 16 or above',
|
|
|
|
'minecraft 1.18 requires running the server with java 17 or above',
|
|
|
|
'java.lang.unsupportedclassversionerror',
|
|
|
|
'unsupported major.minor version',
|
|
|
|
'has been compiled by a more recent version of the java runtime',
|
2021-08-03 03:32:10 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const JavaVersionModalFeature = () => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [selectedVersion, setSelectedVersion] = useState('');
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const status = ServerContext.useStoreState(state => state.status.value);
|
2021-08-03 03:32:10 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-11-25 20:25:03 +00:00
|
|
|
const { instance } = ServerContext.useStoreState(state => state.socket);
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const { data, isValidating, mutate } = getServerStartup(uuid, undefined, { revalidateOnMount: false });
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-05-07 22:18:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!visible) return;
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
mutate().then(value => {
|
2022-07-01 00:23:40 +00:00
|
|
|
setSelectedVersion(Object.values(value?.dockerImages || [])[0] || '');
|
2022-05-07 22:18:14 +00:00
|
|
|
});
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [visible]);
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
useWebsocketEvent(SocketEvent.CONSOLE_OUTPUT, data => {
|
2022-05-07 22:18:14 +00:00
|
|
|
if (status === 'running') return;
|
2021-08-03 03:32:10 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
if (MATCH_ERRORS.some(p => data.toLowerCase().includes(p.toLowerCase()))) {
|
2022-05-07 22:18:14 +00:00
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
});
|
2021-08-03 03:32:10 +00:00
|
|
|
|
|
|
|
const updateJava = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('feature:javaVersion');
|
|
|
|
|
|
|
|
setSelectedDockerImage(uuid, selectedVersion)
|
|
|
|
.then(() => {
|
|
|
|
if (status === 'offline' && instance) {
|
|
|
|
instance.send(SocketRequest.SET_STATE, 'restart');
|
|
|
|
}
|
|
|
|
setVisible(false);
|
|
|
|
})
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => clearAndAddHttpError({ key: 'feature:javaVersion', error }))
|
2021-08-03 03:32:10 +00:00
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes('feature:javaVersion');
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2022-05-07 22:18:14 +00:00
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
onDismissed={() => setVisible(false)}
|
|
|
|
closeOnBackground={false}
|
|
|
|
showSpinnerOverlay={loading}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender key={'feature:javaVersion'} css={tw`mb-4`} />
|
2022-05-07 22:18:14 +00:00
|
|
|
<h2 css={tw`text-2xl mb-4 text-neutral-100`}>Unsupported Java Version</h2>
|
|
|
|
<p css={tw`mt-4`}>
|
|
|
|
This server is currently running an unsupported version of Java and cannot be started.
|
|
|
|
<Can action={'startup.docker-image'}>
|
|
|
|
Please select a supported version from the list below to continue starting the server.
|
|
|
|
</Can>
|
|
|
|
</p>
|
|
|
|
<Can action={'startup.docker-image'}>
|
|
|
|
<div css={tw`mt-4`}>
|
|
|
|
<InputSpinner visible={!data || isValidating}>
|
2022-11-25 20:25:03 +00:00
|
|
|
<Select disabled={!data} onChange={e => setSelectedVersion(e.target.value)}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{!data ? (
|
|
|
|
<option disabled />
|
|
|
|
) : (
|
2022-11-25 20:25:03 +00:00
|
|
|
Object.keys(data.dockerImages).map(key => (
|
2022-06-26 19:13:52 +00:00
|
|
|
<option key={key} value={data.dockerImages[key]}>
|
|
|
|
{key}
|
|
|
|
</option>
|
2022-05-07 22:18:14 +00:00
|
|
|
))
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2022-05-07 22:18:14 +00:00
|
|
|
</Select>
|
|
|
|
</InputSpinner>
|
|
|
|
</div>
|
|
|
|
</Can>
|
|
|
|
<div css={tw`mt-8 flex flex-col sm:flex-row justify-end sm:space-x-4 space-y-4 sm:space-y-0`}>
|
|
|
|
<Button isSecondary onClick={() => setVisible(false)} css={tw`w-full sm:w-auto`}>
|
|
|
|
Cancel
|
2021-08-03 03:32:10 +00:00
|
|
|
</Button>
|
2022-05-07 22:18:14 +00:00
|
|
|
<Can action={'startup.docker-image'}>
|
|
|
|
<Button onClick={updateJava} css={tw`w-full sm:w-auto`}>
|
2021-08-03 03:32:10 +00:00
|
|
|
Update Docker Image
|
2022-05-07 22:18:14 +00:00
|
|
|
</Button>
|
|
|
|
</Can>
|
2021-08-03 03:32:10 +00:00
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default JavaVersionModalFeature;
|