-
Notifications
You must be signed in to change notification settings - Fork 211
/
index.tsx
58 lines (48 loc) · 1.44 KB
/
index.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
import '@dailydotdev/shared/src/lib/lazysizesImport';
import React from 'react';
import ReactDOM from 'react-dom';
import '@dailydotdev/shared/src/styles/globals.css';
import { getLocalBootData } from '@dailydotdev/shared/src/contexts/BootProvider';
import { BootCacheData } from '@dailydotdev/shared/src/lib/boot';
import {
applyTheme,
themeModes,
} from '@dailydotdev/shared/src/contexts/SettingsContext';
import { get as getCache } from 'idb-keyval';
import { browser } from 'webextension-polyfill-ts';
import App from './App';
import { DndSettings } from './DndContext';
declare global {
interface Window {
windowLoaded: boolean;
}
}
window.addEventListener(
'load',
() => {
window.windowLoaded = true;
},
{
once: true,
},
);
const renderApp = (data?: BootCacheData) => {
ReactDOM.render(
<App localBootData={data} />,
document.getElementById('__next'),
);
};
const redirectApp = async (url: string) => {
const tab = await browser.tabs.getCurrent();
window.stop();
await browser.tabs.update(tab.id, { url });
};
(async () => {
const data = getLocalBootData();
if (data?.settings?.theme) applyTheme(themeModes[data.settings.theme]);
const source = window.location.href.split('source=')[1];
if (source) return renderApp(data);
const dnd = await getCache<DndSettings>('dnd');
const isDnd = dnd?.expiration?.getTime() > new Date().getTime();
return isDnd ? redirectApp(dnd.link) : renderApp(data);
})();