misc_pterodactyl-panel/resources/scripts/components/server/features/GSLTokenModalFeature.tsx

106 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { useEffect, useState } from 'react';
2021-12-04 18:29:24 +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 FlashMessageRender from '@/components/FlashMessageRender';
import useFlash from '@/plugins/useFlash';
import { SocketEvent, SocketRequest } from '@/components/server/events';
import Field from '@/components/elements/Field';
import updateStartupVariable from '@/api/server/updateStartupVariable';
2021-12-19 17:50:18 +00:00
import { Form, Formik } from 'formik';
2021-12-04 18:29:24 +00:00
interface Values {
gslToken: string;
}
const GSLTokenModalFeature = () => {
const [visible, setVisible] = useState(false);
const [loading, setLoading] = useState(false);
2021-12-04 18:29:24 +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-12-04 18:29:24 +00:00
const { clearFlashes, clearAndAddHttpError } = useFlash();
2022-11-25 20:25:03 +00:00
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
2021-12-04 18:29:24 +00:00
useEffect(() => {
if (!connected || !instance || status === 'running') return;
const errors = ['(gsl token expired)', '(account not found)'];
2021-12-04 18:29:24 +00:00
const listener = (line: string) => {
2022-11-25 20:25:03 +00:00
if (errors.some(p => line.toLowerCase().includes(p))) {
2021-12-04 18:29:24 +00:00
setVisible(true);
}
};
instance.addListener(SocketEvent.CONSOLE_OUTPUT, listener);
return () => {
instance.removeListener(SocketEvent.CONSOLE_OUTPUT, listener);
};
}, [connected, instance, status]);
2021-12-04 18:29:24 +00:00
2021-12-19 17:50:18 +00:00
const updateGSLToken = (values: Values) => {
2021-12-04 18:29:24 +00:00
setLoading(true);
clearFlashes('feature:gslToken');
updateStartupVariable(uuid, 'STEAM_ACC', values.gslToken)
.then(() => {
if (instance) {
instance.send(SocketRequest.SET_STATE, 'restart');
}
setLoading(false);
setVisible(false);
})
2022-11-25 20:25:03 +00:00
.catch(error => {
2021-12-04 18:29:24 +00:00
console.error(error);
clearAndAddHttpError({ key: 'feature:gslToken', error });
})
.then(() => setLoading(false));
};
useEffect(() => {
clearFlashes('feature:gslToken');
}, []);
return (
<Formik onSubmit={updateGSLToken} initialValues={{ gslToken: '' }}>
<Modal
visible={visible}
onDismissed={() => setVisible(false)}
closeOnBackground={false}
showSpinnerOverlay={loading}
>
<FlashMessageRender key={'feature:gslToken'} css={tw`mb-4`} />
2021-12-04 18:29:24 +00:00
<Form>
<h2 css={tw`text-2xl mb-4 text-neutral-100`}>Invalid GSL token!</h2>
<p css={tw`mt-4`}>
It seems like your Gameserver Login Token (GSL token) is invalid or has expired.
</p>
<p css={tw`mt-4`}>
You can either generate a new one and enter it below or leave the field blank to remove it
completely.
</p>
2021-12-04 18:29:24 +00:00
<div css={tw`sm:flex items-center mt-4`}>
<Field
name={'gslToken'}
label={'GSL Token'}
description={'Visit https://steamcommunity.com/dev/managegameservers to generate a token.'}
autoFocus
/>
</div>
<div css={tw`mt-8 sm:flex items-center justify-end`}>
<Button type={'submit'} css={tw`mt-4 sm:mt-0 sm:ml-4 w-full sm:w-auto`}>
Update GSL Token
</Button>
</div>
</Form>
</Modal>
</Formik>
);
};
export default GSLTokenModalFeature;