-
Notifications
You must be signed in to change notification settings - Fork 9
/
redirectPage.tsx
90 lines (78 loc) · 3.53 KB
/
redirectPage.tsx
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
import React, { useContext, useEffect, useMemo, useState } from 'react'
import { ServiceWorkerReadyButton } from './components/sw-ready-button.tsx'
import { ServiceWorkerContext } from './context/service-worker-context.tsx'
import { HeliaServiceWorkerCommsChannel } from './lib/channel.ts'
import { setConfig, type ConfigDb } from './lib/config-db.ts'
import { getSubdomainParts } from './lib/get-subdomain-parts'
import { isConfigPage } from './lib/is-config-page'
import { error, trace } from './lib/logger.ts'
const ConfigIframe = (): JSX.Element => {
const { parentDomain } = getSubdomainParts(window.location.href)
const portString = window.location.port === '' ? '' : `:${window.location.port}`
const iframeSrc = `${window.location.protocol}//${parentDomain}${portString}#/ipfs-sw-config@origin=${encodeURIComponent(window.location.origin)}`
return (
<iframe id="redirect-config-iframe" src={iframeSrc} style={{ width: '100vw', height: '100vh', border: 'none' }} />
)
}
const channel = new HeliaServiceWorkerCommsChannel('WINDOW')
export default function RedirectPage (): JSX.Element {
const [isAutoReloadEnabled, setIsAutoReloadEnabled] = useState(false)
const { isServiceWorkerRegistered } = useContext(ServiceWorkerContext)
useEffect(() => {
async function doWork (config: ConfigDb): Promise<void> {
try {
await setConfig(config)
// TODO: show spinner / disable buttons while waiting for response
await channel.messageAndWaitForResponse('SW', { target: 'SW', action: 'RELOAD_CONFIG' })
trace('redirect-page: RELOAD_CONFIG_SUCCESS on %s', window.location.origin)
// try to preload the content
await fetch(window.location.href, { method: 'GET' })
} catch (err) {
error('redirect-page: error setting config on subdomain', err)
}
if (config.autoReload) {
setIsAutoReloadEnabled(config.autoReload)
}
}
const listener = (event: MessageEvent): void => {
if (event.data?.source === 'helia-sw-config-iframe') {
trace('redirect-page: received RELOAD_CONFIG message from iframe', event.data)
const config = event.data?.config
if (config != null) {
void doWork(config as ConfigDb)
}
}
}
window.addEventListener('message', listener)
return () => {
window.removeEventListener('message', listener)
}
}, [])
let reloadUrl = window.location.href
if (isConfigPage(window.location.hash)) {
reloadUrl = window.location.href.replace('#/ipfs-sw-config', '')
}
const displayString = useMemo(() => {
if (!isServiceWorkerRegistered) {
return 'Registering Helia service worker...'
}
if (isAutoReloadEnabled && !isConfigPage(window.location.hash)) {
return 'Redirecting you because Auto Reload is enabled.'
}
return 'Please save your changes to the config to apply them. You can then refresh the page to load your content.'
}, [isAutoReloadEnabled, isServiceWorkerRegistered])
useEffect(() => {
if (isAutoReloadEnabled && isServiceWorkerRegistered && !isConfigPage(window.location.hash)) {
window.location.reload()
}
}, [isAutoReloadEnabled, isServiceWorkerRegistered])
return (
<div className="redirect-page">
<div className="pa4-l mw7 mv5 center pa4">
<h3 className="">{displayString}</h3>
<ServiceWorkerReadyButton id="load-content" label='Load content' waitingLabel='Waiting for service worker registration...' onClick={() => { window.location.href = reloadUrl }} />
</div>
<ConfigIframe />
</div>
)
}