From 6d5ca5a811537420965935e5f9e46b4850473208 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sat, 7 May 2022 18:18:14 -0400 Subject: [PATCH] Update java version modal to only suggest allowed images --- resources/scripts/api/swr/getServerStartup.ts | 6 +- .../features/JavaVersionModalFeature.tsx | 113 ++++++++++-------- 2 files changed, 63 insertions(+), 56 deletions(-) diff --git a/resources/scripts/api/swr/getServerStartup.ts b/resources/scripts/api/swr/getServerStartup.ts index 0755be211..94a4faf55 100644 --- a/resources/scripts/api/swr/getServerStartup.ts +++ b/resources/scripts/api/swr/getServerStartup.ts @@ -1,4 +1,4 @@ -import useSWR from 'swr'; +import useSWR, { ConfigInterface } from 'swr'; import http, { FractalResponseList } from '@/api/http'; import { rawDataToServerEggVariable } from '@/api/transformers'; import { ServerEggVariable } from '@/api/server/types'; @@ -9,7 +9,7 @@ interface Response { dockerImages: Record; } -export default (uuid: string, initialData?: Response) => useSWR([ uuid, '/startup' ], async (): Promise => { +export default (uuid: string, initialData?: Response | null, config?: ConfigInterface) => useSWR([ uuid, '/startup' ], async (): Promise => { const { data } = await http.get(`/api/client/servers/${uuid}/startup`); const variables = ((data as FractalResponseList).data || []).map(rawDataToServerEggVariable); @@ -19,4 +19,4 @@ export default (uuid: string, initialData?: Response) => useSWR([ uuid, '/startu invocation: data.meta.startup_command, dockerImages: data.meta.docker_images || {}, }; -}, { initialData, errorRetryCount: 3 }); +}, { initialData: initialData || undefined, errorRetryCount: 3, ...(config || {}) }); diff --git a/resources/scripts/components/server/features/JavaVersionModalFeature.tsx b/resources/scripts/components/server/features/JavaVersionModalFeature.tsx index efae064c5..58c4e614e 100644 --- a/resources/scripts/components/server/features/JavaVersionModalFeature.tsx +++ b/resources/scripts/components/server/features/JavaVersionModalFeature.tsx @@ -8,47 +8,46 @@ import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { SocketEvent, SocketRequest } from '@/components/server/events'; import Select from '@/components/elements/Select'; +import useWebsocketEvent from '@/plugins/useWebsocketEvent'; +import Can from '@/components/elements/Can'; +import getServerStartup from '@/api/swr/getServerStartup'; +import InputSpinner from '@/components/elements/InputSpinner'; -const dockerImageList = [ - { name: 'Java 17', image: 'ghcr.io/pterodactyl/yolks:java_17' }, - { name: 'Java 16', image: 'ghcr.io/pterodactyl/yolks:java_16' }, - { name: 'Java 11', image: 'ghcr.io/pterodactyl/yolks:java_11' }, - { name: 'Java 8', image: 'ghcr.io/pterodactyl/yolks:java_8' }, +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', ]; const JavaVersionModalFeature = () => { const [ visible, setVisible ] = useState(false); const [ loading, setLoading ] = useState(false); - const [ selectedVersion, setSelectedVersion ] = useState('ghcr.io/pterodactyl/yolks:java_17'); + const [ selectedVersion, setSelectedVersion ] = useState(''); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const status = ServerContext.useStoreState(state => state.status.value); const { clearFlashes, clearAndAddHttpError } = useFlash(); - const { connected, instance } = ServerContext.useStoreState(state => state.socket); + const { instance } = ServerContext.useStoreState(state => state.socket); + + const { data, isValidating, mutate } = getServerStartup(uuid, null, { revalidateOnMount: false }); useEffect(() => { - if (!connected || !instance || status === 'running') return; + if (!visible) return; - const 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', - ]; + mutate().then((value) => { + setSelectedVersion(Object.keys(value?.dockerImages || [])[0] || ''); + }); + }, [ visible ]); - const listener = (line: string) => { - if (errors.some(p => line.toLowerCase().includes(p))) { - setVisible(true); - } - }; + useWebsocketEvent(SocketEvent.CONSOLE_OUTPUT, (data) => { + if (status === 'running') return; - instance.addListener(SocketEvent.CONSOLE_OUTPUT, listener); - - return () => { - instance.removeListener(SocketEvent.CONSOLE_OUTPUT, listener); - }; - }, [ connected, instance, status ]); + if (MATCH_ERRORS.some(p => data.toLowerCase().includes(p.toLowerCase()))) { + setVisible(true); + } + }); const updateJava = () => { setLoading(true); @@ -59,14 +58,9 @@ const JavaVersionModalFeature = () => { if (status === 'offline' && instance) { instance.send(SocketRequest.SET_STATE, 'restart'); } - - setLoading(false); setVisible(false); }) - .catch(error => { - console.error(error); - clearAndAddHttpError({ key: 'feature:javaVersion', error }); - }) + .catch(error => clearAndAddHttpError({ key: 'feature:javaVersion', error })) .then(() => setLoading(false)); }; @@ -75,30 +69,43 @@ const JavaVersionModalFeature = () => { }, []); return ( - setVisible(false)} closeOnBackground={false} showSpinnerOverlay={loading}> + setVisible(false)} + closeOnBackground={false} + showSpinnerOverlay={loading} + > -

Invalid Java version, update Docker image?

-

This server is unable to start due to the required Java version not being met.

-

By pressing {'"Update Docker Image"'} below you are acknowledging that the Docker image this server uses will be changed to an image below that has the Java version you are requesting.

-
-

Please select a Java version from the list below.

- -
-
- - + +
);