/
App.vue
152 lines (133 loc) · 5.22 KB
/
App.vue
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
<template>
<branded-loader v-if="isLoading" />
<ErrorPage v-else-if="isErrorPageShown" />
<router-view v-else />
<Notifications />
</template>
<script setup lang="ts">
import { computed, onBeforeMount, ref, watch } from 'vue';
import { useTheme } from 'vuetify';
import { useRoute, useRouter } from 'vue-router';
import { useConfigStore } from '@/store/modules/configStore';
import { useAppStore } from '@/store/modules/appStore';
import { APIError } from '@/utils/error';
import { ErrorUnauthorized } from '@/api/errors/ErrorUnauthorized';
import { useABTestingStore } from '@/store/modules/abTestingStore';
import { useUsersStore } from '@/store/modules/usersStore';
import { useProjectsStore } from '@/store/modules/projectsStore';
import { useAnalyticsStore } from '@/store/modules/analyticsStore';
import { useNotify } from '@/utils/hooks';
import { useBillingStore } from '@/store/modules/billingStore';
import { AnalyticsErrorEventSource, AnalyticsEvent } from '@/utils/constants/analyticsEventNames';
import { RouteConfig } from '@/types/router';
import { ROUTES } from '@/router';
import Notifications from '@/layouts/default/Notifications.vue';
import ErrorPage from '@/components/ErrorPage.vue';
import BrandedLoader from '@/components/utils/BrandedLoader.vue';
const appStore = useAppStore();
const abTestingStore = useABTestingStore();
const billingStore = useBillingStore();
const configStore = useConfigStore();
const usersStore = useUsersStore();
const projectsStore = useProjectsStore();
const analyticsStore = useAnalyticsStore();
const notify = useNotify();
const router = useRouter();
const theme = useTheme();
const route = useRoute();
const isLoading = ref<boolean>(true);
/**
* Indicates whether an error page should be shown in place of the router view.
*/
const isErrorPageShown = computed<boolean>((): boolean => {
return appStore.state.error.visible;
});
/**
* Indicates if billing features are enabled.
*/
const billingEnabled = computed<boolean>(() => configStore.state.config.billingFeaturesEnabled);
/**
* Sets up the app by fetching all necessary data.
*/
async function setup() {
isLoading.value = true;
try {
await usersStore.getUser();
const promises: Promise<void | object | string>[] = [
usersStore.getSettings(),
projectsStore.getProjects(),
projectsStore.getUserInvitations(),
abTestingStore.fetchValues(),
];
if (billingEnabled.value) {
promises.push(billingStore.setupAccount());
}
await Promise.all(promises);
const invites = projectsStore.state.invitations;
const projects = projectsStore.state.projects;
if (appStore.state.hasJustLoggedIn && !invites.length && projects.length <= 1) {
if (!projects.length) {
await projectsStore.createDefaultProject(usersStore.state.user.id);
}
projectsStore.selectProject(projects[0].id);
const project = projectsStore.state.selectedProject;
await router.push({
name: ROUTES.Dashboard.name,
params: { id: project.urlId },
});
analyticsStore.pageVisit(ROUTES.DashboardAnalyticsLink);
analyticsStore.eventTriggered(AnalyticsEvent.NAVIGATE_PROJECTS);
if (usersStore.getShouldPromptPassphrase(project.ownerId === usersStore.state.user.id)) {
appStore.toggleProjectPassphraseDialog(true);
}
}
} catch (error) {
if (!(error instanceof ErrorUnauthorized)) {
notify.notifyError(error, AnalyticsErrorEventSource.OVERALL_APP_WRAPPER_ERROR);
appStore.setErrorPage((error as APIError).status ?? 500, true);
} else {
await new Promise(resolve => setTimeout(resolve, 1000));
if (!RouteConfig.AuthRoutes.includes(route.path)) await router.push(ROUTES.Login.path);
}
}
isLoading.value = false;
}
/**
* Lifecycle hook before initial render.
* Sets up variables from meta tags from config such satellite name, etc.
*/
onBeforeMount(async (): Promise<void> => {
const savedTheme = localStorage.getItem('theme') || 'light';
if ((savedTheme === 'dark' && !theme.global.current.value.dark) || (savedTheme === 'light' && theme.global.current.value.dark)) {
theme.global.name.value = savedTheme;
}
try {
await configStore.getConfig();
} catch (error) {
isLoading.value = false;
notify.notifyError(error, AnalyticsErrorEventSource.OVERALL_APP_WRAPPER_ERROR);
appStore.setErrorPage((error as APIError).status ?? 500, true);
return;
}
await setup();
isLoading.value = false;
});
usersStore.$onAction(({ name, after }) => {
if (name === 'login') {
after((_) => setup());
}
});
/**
* conditionally prompt for project passphrase if project changes
*/
watch(() => projectsStore.state.selectedProject, (project, oldProject) => {
if (project.id === oldProject.id) {
return;
}
if (usersStore.getShouldPromptPassphrase(project.ownerId === usersStore.state.user.id)) {
appStore.toggleProjectPassphraseDialog(true);
}
});
</script>