added back Debug extension

This commit is contained in:
srgooglo 2022-03-11 01:04:06 +01:00
parent 779e9c763a
commit 67ce4a9f61
3 changed files with 144 additions and 4 deletions

View File

@ -51,7 +51,7 @@ import { StatusBar, Style } from "@capacitor/status-bar"
import { Translation } from "react-i18next"
import { Session, User } from "models"
import { API, SettingsController, Render, Splash, Theme, Sound, Notifications, i18n } from "extensions"
import { API, SettingsController, Render, Splash, Theme, Sound, Notifications, i18n, Debug } from "extensions"
import config from "config"
import { NotFound, RenderError, Crash, Settings, Navigation } from "components"
@ -176,7 +176,10 @@ class App {
content: "Connection restored",
})
}
}
},
"appLoadError": function (error) {
},
}
static windowContext() {
@ -328,7 +331,7 @@ class App {
await Promise.tasked(initializationTasks).catch((reason) => {
console.error(`[App] Initialization failed: ${reason.cause}`)
window.app.eventBus.emit("crash", reason.cause, reason.details)
window.app.eventBus.emit("appLoadError", reason.cause, reason.details)
})
}
@ -399,5 +402,6 @@ export default CreateEviteApp(App, {
Render.extension,
Theme.extension,
SplashExtension,
Debug,
],
})

View File

@ -0,0 +1,135 @@
import React from "react"
import { Window } from "components"
import { Skeleton, Tabs } from "antd"
class DebuggerUI extends React.Component {
state = {
loading: true,
error: null,
debuggers: null,
active: null,
}
toogleLoading = (to = !this.state.loading ?? false) => {
this.setState({ loading: to })
}
loadDebuggers = async () => {
this.toogleLoading(true)
const debuggers = await import(`~/debugComponents`)
let renders = {}
Object.keys(debuggers).forEach((key) => {
renders[key] = debuggers[key]
})
this.setState({ debuggers: renders }, () => {
this.toogleLoading(false)
})
}
componentDidMount = async () => {
await this.loadDebuggers()
}
componentDidCatch = (error, info) => {
this.setState({ error })
}
onChangeTab = (key) => {
console.debug(`Changing tab to ${key}`)
this.setState({ active: key, error: null })
}
renderError = (key, error) => {
return (
<div>
<h2>Debugger Error</h2>
<i>
<h4>
Catch on [<strong>{key}</strong>]
</h4>
</i>
`<code>{error.message}</code>`
<hr />
<code>{error.stack}</code>
</div>
)
}
renderTabs = () => {
return Object.keys(this.state.debuggers).map((key) => {
return <Tabs.TabPane tab={key} key={key} />
})
}
renderDebugger = (_debugger) => {
try {
return React.createElement(window.app.bindContexts(_debugger))
} catch (error) {
return this.renderError(key, error)
}
}
render() {
const { loading, error } = this.state
if (loading) {
return <Skeleton active />
}
return (
<div>
<Tabs
onChange={this.onChangeTab}
activeKey={this.state.active}
>
{this.renderTabs()}
</Tabs>
{error && this.renderError(this.state.active, error)}
{!this.state.active ? (
<div> Select an debugger to start </div>
) : (
this.renderDebugger(this.state.debuggers[this.state.active])
)}
</div>
)
}
}
class Debugger {
constructor(mainContext, params = {}) {
this.mainContext = mainContext
this.params = { ...params }
this.bindings = {}
}
openWindow = () => {
new Window.DOMWindow({ id: "debugger", children: window.app.bindContexts(DebuggerUI) }).create()
}
bind = (id, binding) => {
this.bindings[id] = binding
return binding
}
unbind = (id) => {
delete this.bindings[id]
}
}
export default {
key: "visualDebugger",
expose: [
{
initialization: [
async (app, main) => {
main.setToWindowContext("debug", new Debugger(main))
},
],
},
],
}

View File

@ -6,4 +6,5 @@ export * as i18n from "./i18n"
export * as Notifications from "./notifications"
export { default as SettingsController } from "./settings"
export { default as API } from "./api"
export { default as API } from "./api"
export { default as Debug } from "./debug"