]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/pages/AlttpSeed.js
improved directory structure
[alttp.git] / resources / js / pages / AlttpSeed.js
diff --git a/resources/js/pages/AlttpSeed.js b/resources/js/pages/AlttpSeed.js
new file mode 100644 (file)
index 0000000..5a7e5fd
--- /dev/null
@@ -0,0 +1,108 @@
+import axios from 'axios';
+import React, { useCallback, useEffect, useState } from 'react';
+import { Helmet } from 'react-helmet';
+import { useParams } from 'react-router-dom';
+
+import NotFound from './NotFound';
+import Seed from '../components/alttp-seeds/Seed';
+import ErrorBoundary from '../components/common/ErrorBoundary';
+import ErrorMessage from '../components/common/ErrorMessage';
+import Loading from '../components/common/Loading';
+
+const AlttpSeed = () => {
+       const params = useParams();
+       const { hash } = params;
+
+       const [error, setError] = useState(null);
+       const [loading, setLoading] = useState(true);
+       const [patch, setPatch] = useState(null);
+       const [seed, setSeed] = useState(null);
+
+       const loadSeed = useCallback((hash, ctrl) => {
+               axios
+                       .get(`/api/alttp-seed/${hash}`, { signal: ctrl.signal })
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setSeed(response.data);
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setSeed(null);
+                       });
+       }, []);
+
+       useEffect(() => {
+               setLoading(true);
+               const ctrl = new AbortController();
+               loadSeed(hash, ctrl);
+               return () => {
+                       ctrl.abort();
+               };
+       }, [hash]);
+
+       useEffect(() => {
+               if (!seed || seed.status !== 'pending') {
+                       return;
+               }
+               const ctrl = new AbortController();
+               const timer = setTimeout(() => {
+                       loadSeed(seed.hash, ctrl);
+               }, 2000);
+               return () => {
+                       clearTimeout(timer);
+                       ctrl.abort();
+               };
+       }, [seed]);
+
+       useEffect(() => {
+               setPatch(null);
+               if (!seed || seed.status !== 'generated') {
+                       return;
+               }
+               const ctrl = new AbortController();
+               axios
+                       .get(`/alttp-seeds/${hash}.bps`, {
+                               responseType: 'arraybuffer',
+                               signal: ctrl.signal,
+                       })
+                       .then(response => {
+                               setPatch(response.data);
+                       })
+                       .catch(error => {
+                               setError(error);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, [hash, seed]);
+
+       const retry = useCallback(async () => {
+               await axios.post(`/api/alttp-seed/${hash}/retry`);
+               setSeed(seed => ({ ...seed, status: 'pending' }));
+       });
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!seed) {
+               return <NotFound />;
+       }
+
+       return <ErrorBoundary>
+               <Helmet>
+                       {seed ?
+                               <title>{seed.hash}</title>
+                       : null}
+               </Helmet>
+               <Seed onRetry={retry} patch={patch} seed={seed} />
+       </ErrorBoundary>;
+};
+
+export default AlttpSeed;