mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-18 06:54:15 +00:00
Move hooks to single files
This commit is contained in:
parent
c872bbd223
commit
9f29043e47
92
packages/app/src/hooks/onPageMount.js
Normal file
92
packages/app/src/hooks/onPageMount.js
Normal file
@ -0,0 +1,92 @@
|
||||
import config from "@config"
|
||||
|
||||
const isAuthenticated = () => {
|
||||
return !!app.userData
|
||||
}
|
||||
|
||||
const handleAuthentication = (declaration) => {
|
||||
if (
|
||||
!isAuthenticated() &&
|
||||
!declaration.public &&
|
||||
window.location.pathname !== config.app?.authPath
|
||||
) {
|
||||
const authPath = config.app?.authPath ?? "/login"
|
||||
|
||||
if (typeof window.app?.location?.push === "function") {
|
||||
window.app.location.push(authPath)
|
||||
|
||||
if (app.cores?.notifications?.new) {
|
||||
app.cores.notifications.new({
|
||||
title: "Please login to use this feature.",
|
||||
duration: 15,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
window.location.href = authPath
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const handleLayout = (declaration) => {
|
||||
if (declaration.useLayout && app.layout?.set) {
|
||||
app.layout.set(declaration.useLayout)
|
||||
}
|
||||
}
|
||||
|
||||
const handleCenteredContent = (declaration) => {
|
||||
if (
|
||||
typeof declaration.centeredContent !== "undefined" &&
|
||||
app.layout?.toggleCenteredContent
|
||||
) {
|
||||
let finalBool = null
|
||||
|
||||
if (typeof declaration.centeredContent === "boolean") {
|
||||
finalBool = declaration.centeredContent
|
||||
} else {
|
||||
finalBool = app.isMobile
|
||||
? (declaration.centeredContent?.mobile ?? null)
|
||||
: (declaration.centeredContent?.desktop ?? null)
|
||||
}
|
||||
|
||||
app.layout.toggleCenteredContent(finalBool)
|
||||
}
|
||||
}
|
||||
|
||||
const handleTitle = (declaration) => {
|
||||
if (typeof declaration.useTitle !== "undefined") {
|
||||
let title = declaration.useTitle
|
||||
|
||||
document.title = `${title} - ${config.app.siteName}`
|
||||
} else {
|
||||
document.title = config.app.siteName
|
||||
}
|
||||
}
|
||||
|
||||
export default ({ element, declaration }) => {
|
||||
const options = element.options ?? {}
|
||||
|
||||
// Handle authentication first
|
||||
const isAuthorized = handleAuthentication(declaration)
|
||||
|
||||
if (isAuthorized) {
|
||||
handleLayout(declaration)
|
||||
handleCenteredContent(declaration)
|
||||
handleTitle(declaration)
|
||||
}
|
||||
|
||||
if (options.layout) {
|
||||
if (typeof options.layout.type === "string" && app.layout?.set) {
|
||||
app.layout.set(options.layout.type)
|
||||
}
|
||||
|
||||
if (
|
||||
typeof options.layout.centeredContent === "boolean" &&
|
||||
app.layout?.toggleCenteredContent
|
||||
) {
|
||||
app.layout.toggleCenteredContent()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user