-
Notifications
You must be signed in to change notification settings - Fork 386
/
index.ts
76 lines (63 loc) · 2.29 KB
/
index.ts
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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
/**
* plugins/index.ts
*
* Automatically included in `./src/main.ts`
*/
// Plugins
import { App, watch } from 'vue';
import { createPinia, setActivePinia } from 'pinia';
import THEME_URLS from 'virtual:vuetify-theme-css';
import { setupRouter } from '../router';
import vuetify from './vuetify';
import NotificatorPlugin, { Notificator } from '@/utils/plugins/notificator';
import { useConfigStore } from '@/store/modules/configStore';
import { FrontendConfig } from '@/types/config.gen';
const pinia = createPinia();
setActivePinia(pinia);
// Vuetify's way of applying themes uses a dynamic inline stylesheet.
// This is incompatible with our CSP policy, so circumvent it.
function setupTheme() {
const oldAppend = document.head.appendChild.bind(document.head);
document.head.appendChild = function<T extends Node>(node: T): T {
if (node instanceof HTMLStyleElement && node.id === 'vuetify-theme-stylesheet') {
node.remove();
return node;
}
return oldAppend(node);
};
const themeLinks: Record<string, HTMLLinkElement> = {};
for (const [name, url] of Object.entries(THEME_URLS)) {
let link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
link.disabled = name !== vuetify.theme.global.name.value;
document.head.appendChild(link);
themeLinks[name] = link;
// If we don't preload the style, there will be a delay after
// toggling to it for the first time.
link = document.createElement('link');
link.rel = 'preload';
link.as = 'style';
link.href = url;
document.head.appendChild(link);
}
watch(() => vuetify.theme.global.name.value, newName => {
for (const [name, link] of Object.entries(themeLinks)) {
link.disabled = name !== newName;
}
});
}
export async function registerPlugins(app: App<Element>) {
setupTheme();
app.use(vuetify).use(pinia).use(NotificatorPlugin);
let config = new FrontendConfig();
try {
config = await useConfigStore().getConfig();
} catch (e) {
new Notificator().notifyError(e);
}
const router = setupRouter(config);
app.use(router);
}