// Patch global prototypes
Array.prototype.findAndUpdateObject = function (discriminator, obj) {
let index = this.findIndex(item => item[discriminator] === obj[discriminator])
if (index !== -1) {
this[index] = obj
}
return index
}
Array.prototype.move = function (from, to) {
this.splice(to, 0, this.splice(from, 1)[0])
return this
}
String.prototype.toTitleCase = function () {
return this.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
})
}
Promise.tasked = function (promises) {
return new Promise(async (resolve, reject) => {
let rejected = false
for await (let promise of promises) {
if (rejected) {
return
}
try {
await promise()
} catch (error) {
rejected = true
return reject(error)
}
}
if (!rejected) {
return resolve()
}
})
}
import React from "react"
import { EviteRuntime } from "evite"
import { Helmet } from "react-helmet"
import * as antd from "antd"
import { Translation } from "react-i18next"
import config from "config"
import { Session, User } from "models"
import { NotFound, RenderError, Crash, Login } from "components"
import { Icons } from "components/Icons"
import Layout from "./layout"
import * as Router from "./router"
import "theme/index.less"
class App extends React.Component {
sessionController = new Session()
userController = new User()
state = {
session: null,
user: null,
}
static async initialize() {
window.app.version = config.package.version
}
static publicEvents = {
"clearAllOverlays": function () {
window.app.DrawerController.closeAll()
},
}
eventsHandlers = {
"app.close": () => {
if (window.isElectron) {
window.electron.ipcRenderer.invoke("app.close")
}
},
"app.minimize": () => {
if (window.isElectron) {
window.electron.ipcRenderer.invoke("app.minimize")
}
},
"app.openCreator": (...args) => {
return App.publicMethods.openCreator(...args)
},
"app.createLogin": async () => {
app.DrawerController.open("login", Login, {
componentProps: {
sessionController: this.sessionController
}
})
},
"session.logout": async () => {
await this.sessionController.logout()
},
"session.created": async () => {
await this.flushState()
await this.initialization()
// if is `/login` move to `/`
if (window.location.pathname === "/login") {
app.setLocation("/")
}
},
"session.destroyed": async () => {
await this.flushState()
app.eventBus.emit("app.forceToLogin")
},
"session.regenerated": async () => {
//await this.flushState()
//await this.initialization()
},
"session.invalid": async (error) => {
const token = await Session.token
if (!this.state.session && !token) {
return false
}
await this.sessionController.forgetLocalSession()
await this.flushState()
app.eventBus.emit("app.forceToLogin")
antd.notification.open({
message: