-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.vue
More file actions
182 lines (166 loc) · 4.47 KB
/
app.vue
File metadata and controls
182 lines (166 loc) · 4.47 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<template>
<UApp :toaster="{ position: 'top-right' }">
<MaintenancePage v-if="isShowMaintenancePage" />
<NuxtLayout v-show="!isShowMaintenancePage">
<NuxtPage class="grow overflow-y-auto" />
</NuxtLayout>
<USlideover
v-model:open="isMobileMenuOpen"
side="left"
>
<template #content>
<UButton
color="neutral"
variant="ghost"
icon="i-heroicons-x-mark-20-solid"
class="absolute top-4 right-4 z-20"
square
@click="isMobileMenuOpen = false"
/>
<SiteNavigation />
</template>
</USlideover>
<UModal
:open="isRestoringSession"
:dismissible="false"
>
<template #content>
<div class="p-4 flex flex-col items-center gap-2">
<span>{{ $t('app.restoring_session') }}</span>
<UProgress animation="carousel" />
</div>
</template>
</UModal>
<UModal
v-model:open="isAuthenticating"
:dismissible="false"
class="max-w-[200px]!"
>
<template #content>
<BlockingModal :title="loginStatus" />
</template>
</UModal>
<UModal
v-model:open="bookstoreApiStore.isShowLoginPanel"
class="max-w-[348px]!"
>
<template #content>
<LoginPanel
@connect="authenticateByConnectorId"
/>
</template>
</UModal>
<WelcomeModal />
<NuxtLoadingIndicator />
</UApp>
</template>
<script setup lang="ts">
const { t: $t } = useI18n()
const { SITE_URL } = useRuntimeConfig().public
const { isShowMaintenancePage } = useMaintenanceMode()
const bookstoreApiStore = useBookstoreApiStore()
const { restoreAuthSession, fetchBookListing, clearSession } = bookstoreApiStore
const { wallet, intercomToken, isAuthenticated } = storeToRefs(bookstoreApiStore)
const { isAuthenticating, loginStatus, authenticateByConnectorId, authenticateBySignature } = useAuth()
const uiStore = useUIStore()
const toast = useToast()
const isRestoringSession = ref(false)
const isMobileMenuOpen = computed({
get: () => uiStore.isSiteMenuOpen,
set: value => uiStore.setSiteMenuOpen(value)
})
const route = useRoute()
const router = useRouter()
// NOTE: Close mobile menu on route change
watch(
() => route.fullPath,
() => {
isMobileMenuOpen.value = false
}
)
watch(() => bookstoreApiStore.isAuthenticated, (isAuthenticated) => {
if (isAuthenticated && bookstoreApiStore.isShowLoginPanel) {
bookstoreApiStore.closeLoginPanel()
}
})
useHead({
htmlAttrs: {
class: 'h-dvh'
},
bodyAttrs: {
class: 'h-dvh text-gray-700 dark:text-gray-200 dark:bg-gray-900'
},
link: [
{
rel: 'icon',
type: 'image/x-icon',
href: `${SITE_URL}/favicon.ico`
}
]
})
useSeoMeta({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - ${$t('app.site_title')}` : $t('app.site_title')
},
ogTitle: () => $t('app.site_title'),
description: () => $t('app.site_description'),
ogDescription: () => $t('app.site_description'),
ogType: 'website',
ogSiteName: () => $t('app.site_title'),
themeColor: '#28646e'
})
onMounted(async () => {
isRestoringSession.value = true
try {
await restoreAuthSession()
if (isAuthenticated.value && wallet.value) {
useSetLogUser(wallet.value, { intercomToken: intercomToken.value })
}
} catch (error) {
console.error(error)
toast.add({
icon: 'i-heroicons-exclamation-circle',
title: `${(error as Error).toString()}, please re-login`,
duration: 0,
color: 'error'
})
clearSession()
} finally {
isRestoringSession.value = false
}
if (isAuthenticated.value) {
try {
await fetchBookListing()
} catch (error) {
// eslint-disable-next-line no-console
console.error(error)
}
} else if (route.query.auth) {
const { auth: payload, ...query } = route.query
if (typeof payload === 'string') {
try {
const {
signature,
message,
wallet,
signMethod,
expiresIn
} = JSON.parse(payload)
if (signature && message && wallet) {
await authenticateBySignature({
signature,
message,
wallet,
signMethod,
expiresIn
})
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('An error occurred when authenticating with signature from query string', error)
}
}
router.replace({ query })
}
})
</script>