]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/pages/AosSeed.js
basic aosr patcher
[alttp.git] / resources / js / components / pages / AosSeed.js
diff --git a/resources/js/components/pages/AosSeed.js b/resources/js/components/pages/AosSeed.js
new file mode 100644 (file)
index 0000000..6cea156
--- /dev/null
@@ -0,0 +1,77 @@
+import axios from 'axios';
+import React, { useEffect, useState } from 'react';
+import { useParams } from 'react-router-dom';
+
+import NotFound from './NotFound';
+import Seed from '../aos/Seed';
+import ErrorBoundary from '../common/ErrorBoundary';
+import ErrorMessage from '../common/ErrorMessage';
+import Loading from '../common/Loading';
+
+const AosSeed = () => {
+       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);
+
+       useEffect(() => {
+               setLoading(true);
+               const ctrl = new AbortController();
+               axios
+                       .get(`/api/aos-seed/${hash}`, { signal: ctrl.signal })
+                       .then(response => {
+                               setError(null);
+                               setLoading(false);
+                               setSeed(response.data);
+                               window.document.title = response.data.hash;
+                       })
+                       .catch(error => {
+                               setError(error);
+                               setLoading(false);
+                               setSeed(null);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, [hash]);
+
+       useEffect(() => {
+               setPatch(null);
+               const ctrl = new AbortController();
+               axios
+                       .get(`/aos-seeds/${hash}.bps`, {
+                               responseType: 'arraybuffer',
+                               signal: ctrl.signal,
+                       })
+                       .then(response => {
+                               setPatch(response.data);
+                       })
+                       .catch(error => {
+                               setError(error);
+                       });
+               return () => {
+                       ctrl.abort();
+               };
+       }, [hash]);
+
+       if (loading) {
+               return <Loading />;
+       }
+
+       if (error) {
+               return <ErrorMessage error={error} />;
+       }
+
+       if (!seed) {
+               return <NotFound />;
+       }
+
+       return <ErrorBoundary>
+               <Seed patch={patch} seed={seed} />
+       </ErrorBoundary>;
+};
+
+export default AosSeed;