]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/aos/App.js
9c57144c6982b6e0f952a64ba37ac42e958c2cd9
[alttp.git] / resources / js / components / aos / App.js
1 import axios from 'axios';
2 import React, { useEffect, useState } from 'react';
3 import { BrowserRouter, Route, Routes } from 'react-router-dom';
4
5 import Header from './Header';
6 import AosFront from '../pages/AosFront';
7 import AosSeed from '../pages/AosSeed';
8 import User from '../pages/User';
9 import AosBaseRomProvider from '../../helpers/AosBaseRomContext';
10 import UserContext from '../../helpers/UserContext';
11
12 const App = () => {
13         const [user, setUser] = useState(null);
14
15         const checkAuth = async () => {
16                 try {
17                         const response = await axios.get('/api/user');
18                         setUser(response.data);
19                 } catch (e) {
20                         setUser(null);
21                 }
22         };
23
24         const doLogout = async () => {
25                 await axios.post('/logout');
26                 await checkAuth();
27         };
28
29         useEffect(() => {
30                 let timer = null;
31                 axios
32                         .get('/sanctum/csrf-cookie')
33                         .then(() => {
34                                 checkAuth();
35                                 timer = setInterval(checkAuth, 15 * 60 * 1000);
36                         });
37                 return () => {
38                         if (timer) clearInterval(timer);
39                 };
40         }, []);
41
42         useEffect(() => {
43                 window.Echo.channel('App.Control')
44                         .listen('PleaseRefresh', () => {
45                                 location.reload();
46                         });
47                 return () => {
48                         window.Echo.leave('App.Control');
49                 };
50         }, []);
51
52         return <BrowserRouter>
53                 <AosBaseRomProvider>
54                         <UserContext.Provider value={user}>
55                                 <Header doLogout={doLogout} />
56                                 <Routes>
57                                         <Route path="h/:hash" element={<AosSeed />} />
58                                         <Route path="users/:id" element={<User />} />
59                                         <Route path="*" element={<AosFront />} />
60                                 </Routes>
61                         </UserContext.Provider>
62                 </AosBaseRomProvider>
63         </BrowserRouter>;
64 };
65
66 export default App;