-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathBaseRoutes.jsx
130 lines (116 loc) · 4.38 KB
/
BaseRoutes.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, {useEffect} from 'react'
import {Outlet, Route, Routes, useLocation, useNavigate} from 'react-router-dom'
import CssBaseline from '@mui/material/CssBaseline'
import {ThemeProvider} from '@mui/material/styles'
import * as Sentry from '@sentry/react'
import {useAuth0} from './Auth0/Auth0Proxy'
import {checkOPFSAvailability, setUpGlobalDebugFunctions} from './OPFS/utils'
import ShareRoutes from './ShareRoutes'
import Styles from './Styles'
import About from './pages/About'
import BlogIndex from './pages/blog/BlogIndex'
import Post20241205BldrsAnnouncesLaunchOfShareAndTheConwayEngine from
'./pages/blog/Post20241205BldrsAnnouncesLaunchOfShareAndTheConwayEngine'
import {initializeOctoKitAuthenticated, initializeOctoKitUnauthenticated} from './net/github/OctokitExport'
import useStore from './store/useStore'
import useShareTheme from './theme/Theme'
import debug from './utils/debug'
import {navWith} from './utils/navigate'
const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes)
/**
* From URL design: https://github.com/bldrs-ai/Share/wiki/URL-Structure
* ... We adopt a URL structure similar to Google Apps URL structure:
*
* http://host/<app>/<view>/<object>
*
* which when fully expanded becomes:
*
* http://host/share/v/p/index.ifc
* http://host/share/v/gh/bldrs-ai/Share/main/public/index.ifc
*
* @see https://github.com/bldrs-ai/Share/wiki/Design#ifc-scene-load
* @param {React.Component} testElt For unit test allow use of a stub here instead of loading the app.
* @return {object}
*/
export default function BaseRoutes({testElt = null}) {
const location = useLocation()
const navigate = useNavigate()
const installPrefix = window.location.pathname.startsWith('/Share') ? '/Share' : ''
const basePath = `${installPrefix }/`
const {isLoading, isAuthenticated, getAccessTokenSilently} = useAuth0()
const setAccessToken = useStore((state) => state.setAccessToken)
const appPrefix = `${basePath}share`
const setAppPrefix = useStore((state) => state.setAppPrefix)
const setIsOpfsAvailable = useStore((state) => state.setIsOpfsAvailable)
useEffect(() => {
setAppPrefix(appPrefix)
}, [setAppPrefix, appPrefix])
useEffect(() => {
const checkAvailability = async () => {
const available = await checkOPFSAvailability()
if (available) {
setUpGlobalDebugFunctions()
}
setIsOpfsAvailable(available)
}
checkAvailability()
}, [setIsOpfsAvailable])
useEffect(() => {
if (location.pathname === installPrefix ||
location.pathname === basePath) {
const fwdPath = `${appPrefix}`
debug().log('BaseRoutes#useEffect[], forwarding to: ', fwdPath)
navWith(navigate, fwdPath)
}
if (process.env.NODE_ENV === 'development' && process.env.GITHUB_API_TOKEN) {
setAccessToken(process.env.GITHUB_API_TOKEN)
} else if (!isLoading && isAuthenticated) {
getAccessTokenSilently({
authorizationParams: {
audience: 'https://api.github.com/',
scope: 'openid profile email offline_access repo',
},
}).then((token) => {
if (token !== '') {
initializeOctoKitAuthenticated()
} else {
initializeOctoKitUnauthenticated()
}
setAccessToken(token)
}).catch((err) => {
if (err.error !== 'login_required') {
throw err
}
})
}
}, [appPrefix, setAppPrefix, basePath, installPrefix, location, navigate,
isLoading, isAuthenticated, getAccessTokenSilently, setAccessToken])
const theme = useShareTheme()
return (
<CssBaseline enableColorScheme>
<ThemeProvider theme={theme}>
<Styles theme={theme}/>
<SentryRoutes>
<Route path={basePath} element={<Outlet/>}>
<Route
path='share/*'
element={
testElt ||
<ShareRoutes
installPrefix={installPrefix}
appPrefix={`${appPrefix}`}
/>
}
/>
<Route path='about' element={<About/>}/>
<Route path='blog' element={<BlogIndex/>}/>
<Route
path='blog/2024-12-05-bldrs-announces-launch-of-share-and-the-conway-engine'
element={<Post20241205BldrsAnnouncesLaunchOfShareAndTheConwayEngine/>}
/>
</Route>
</SentryRoutes>
</ThemeProvider>
</CssBaseline>
)
}