]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/pages/AosSeed.js
6cea156640001c3228ac93c911db88c82a3eb11f
[alttp.git] / resources / js / components / pages / AosSeed.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { useParams } from 'react-router-dom';
4
5 import NotFound from './NotFound';
6 import Seed from '../aos/Seed';
7 import ErrorBoundary from '../common/ErrorBoundary';
8 import ErrorMessage from '../common/ErrorMessage';
9 import Loading from '../common/Loading';
10
11 const AosSeed = () => {
12         const params = useParams();
13         const { hash } = params;
14
15         const [error, setError] = useState(null);
16         const [loading, setLoading] = useState(true);
17         const [patch, setPatch] = useState(null);
18         const [seed, setSeed] = useState(null);
19
20         useEffect(() => {
21                 setLoading(true);
22                 const ctrl = new AbortController();
23                 axios
24                         .get(`/api/aos-seed/${hash}`, { signal: ctrl.signal })
25                         .then(response => {
26                                 setError(null);
27                                 setLoading(false);
28                                 setSeed(response.data);
29                                 window.document.title = response.data.hash;
30                         })
31                         .catch(error => {
32                                 setError(error);
33                                 setLoading(false);
34                                 setSeed(null);
35                         });
36                 return () => {
37                         ctrl.abort();
38                 };
39         }, [hash]);
40
41         useEffect(() => {
42                 setPatch(null);
43                 const ctrl = new AbortController();
44                 axios
45                         .get(`/aos-seeds/${hash}.bps`, {
46                                 responseType: 'arraybuffer',
47                                 signal: ctrl.signal,
48                         })
49                         .then(response => {
50                                 setPatch(response.data);
51                         })
52                         .catch(error => {
53                                 setError(error);
54                         });
55                 return () => {
56                         ctrl.abort();
57                 };
58         }, [hash]);
59
60         if (loading) {
61                 return <Loading />;
62         }
63
64         if (error) {
65                 return <ErrorMessage error={error} />;
66         }
67
68         if (!seed) {
69                 return <NotFound />;
70         }
71
72         return <ErrorBoundary>
73                 <Seed patch={patch} seed={seed} />
74         </ErrorBoundary>;
75 };
76
77 export default AosSeed;