|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { ServerContext } from '@/state/server'; |
| 3 | +import Modal from '@/components/elements/Modal'; |
| 4 | +import tw from 'twin.macro'; |
| 5 | +import Button from '@/components/elements/Button'; |
| 6 | +import FlashMessageRender from '@/components/FlashMessageRender'; |
| 7 | +import useFlash from '@/plugins/useFlash'; |
| 8 | +import { SocketEvent } from '@/components/server/events'; |
| 9 | + |
| 10 | +const HytaleOauthRequireFeature = () => { |
| 11 | + const [visible, setVisible] = useState(false); |
| 12 | + const [link, setLink] = useState(''); |
| 13 | + |
| 14 | + const status = ServerContext.useStoreState((state) => state.status.value); |
| 15 | + const { clearFlashes } = useFlash(); |
| 16 | + const { connected, instance } = ServerContext.useStoreState((state) => state.socket); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + if (!connected || !instance || status === 'running') return; |
| 20 | + |
| 21 | + const listener = (line: string) => { |
| 22 | + if (line.match(/https:\/\/oauth\.accounts\.hytale\.com\/oauth2\/device\/verify\?user_code=(.*)/i)) { |
| 23 | + setLink(line); |
| 24 | + setVisible(true); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + instance.addListener(SocketEvent.CONSOLE_OUTPUT, listener); |
| 29 | + |
| 30 | + return () => { |
| 31 | + instance.removeListener(SocketEvent.CONSOLE_OUTPUT, listener); |
| 32 | + }; |
| 33 | + }, [connected, instance, status]); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + clearFlashes('feature:hytaleOauth'); |
| 37 | + }, []); |
| 38 | + |
| 39 | + const handleLogin = () => { |
| 40 | + if (link) { |
| 41 | + window.open(link, '_blank', 'noopener,noreferrer'); |
| 42 | + setVisible(false); |
| 43 | + setLink(''); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <Modal |
| 49 | + visible={visible} |
| 50 | + onDismissed={() => { |
| 51 | + setVisible(false); |
| 52 | + setLink(''); |
| 53 | + }} |
| 54 | + closeOnBackground={false} |
| 55 | + showSpinnerOverlay={false} |
| 56 | + > |
| 57 | + <FlashMessageRender key={'feature:hytaleOauth'} css={tw`mb-4`} /> |
| 58 | + <h2 css={tw`text-2xl mb-4 text-neutral-100`}>Authentication Required</h2> |
| 59 | + <p css={tw`text-neutral-200`}> |
| 60 | + You need to authenticate with your Hytale account to download or update server files. |
| 61 | + Please log in to continue. |
| 62 | + </p> |
| 63 | + <div css={tw`mt-8 sm:flex items-center justify-end`}> |
| 64 | + <Button isSecondary onClick={() => setVisible(false)} css={tw`w-full sm:w-auto border-transparent`}> |
| 65 | + Cancel |
| 66 | + </Button> |
| 67 | + <Button onClick={handleLogin} css={tw`mt-4 sm:mt-0 sm:ml-4 w-full sm:w-auto`}> |
| 68 | + Log in |
| 69 | + </Button> |
| 70 | + </div> |
| 71 | + </Modal> |
| 72 | + ); |
| 73 | +}; |
| 74 | + |
| 75 | +export default HytaleOauthRequireFeature; |
0 commit comments