diff options
Diffstat (limited to 'frontend/src/plugin-loader.tsx')
| -rw-r--r-- | frontend/src/plugin-loader.tsx | 98 |
1 files changed, 47 insertions, 51 deletions
diff --git a/frontend/src/plugin-loader.tsx b/frontend/src/plugin-loader.tsx index cf15e099..ddb92542 100644 --- a/frontend/src/plugin-loader.tsx +++ b/frontend/src/plugin-loader.tsx @@ -1,64 +1,61 @@ +import { FaPlug } from 'react-icons/fa'; + +import { DeckyState, DeckyStateContextProvider } from './components/DeckyState'; +import LegacyPlugin from './components/LegacyPlugin'; +import PluginView from './components/PluginView'; +import TitleView from './components/TitleView'; import Logger from './logger'; +import { Plugin } from './plugin'; import TabsHook from './tabs-hook'; -import { FaPlug } from "react-icons/fa"; - -import PluginView from "./components/PluginView"; -import TitleView from "./components/TitleView"; -import LegacyPlugin from "./components/LegacyPlugin" - -interface Plugin { - name: any; - content: any; - icon: any; - onDismount?(): void; -} declare global { - interface Window { - __DeckyEvLoop: PluginEventTarget; - __DeckyRunningPlugin: string; - } + interface Window {} } -class PluginEventTarget extends EventTarget { } -window.__DeckyEvLoop = new PluginEventTarget(); class PluginLoader extends Logger { private plugins: Plugin[] = []; private tabsHook: TabsHook = new TabsHook(); + private deckyState: DeckyState = new DeckyState(); constructor() { super(PluginLoader.name); this.log('Initialized'); + this.tabsHook.add({ - id: "main", - title: <TitleView />, - content: <PluginView />, - icon: <FaPlug /> + id: 'main', + title: ( + <DeckyStateContextProvider deckyState={this.deckyState}> + <TitleView /> + </DeckyStateContextProvider> + ), + content: ( + <DeckyStateContextProvider deckyState={this.deckyState}> + <PluginView /> + </DeckyStateContextProvider> + ), + icon: <FaPlug />, }); - SteamClient.Input.RegisterForControllerInputMessages(this.handleBack); - window.__DeckyEvLoop.addEventListener("pluginOpen", (x) => window.__DeckyRunningPlugin = x.data); - window.__DeckyEvLoop.addEventListener("pluginClose", (_) => window.__DeckyRunningPlugin = ""); } - private handleBack(ev) { - const e = ev[0]; - if (e.strActionName == "B" && window.__DeckyRunningPlugin) - window.__DeckyEvLoop.dispatchEvent(new Event("pluginClose")); + public dismountAll() { + for (const plugin of this.plugins) { + this.log(`Dismounting ${plugin.name}`); + plugin.onDismount?.(); + } } public async importPlugin(name: string) { this.log(`Trying to load ${name}`); - let find = this.plugins.find(x => x.name == name); - if (find) - this.plugins.splice(this.plugins.indexOf(find), 1); - if (name.startsWith("$LEGACY_")) - this.importLegacyPlugin(name.replace("$LEGACY_", "")); - else - this.importReactPlugin(name); + let find = this.plugins.find((x) => x.name == name); + if (find) this.plugins.splice(this.plugins.indexOf(find), 1); + if (name.startsWith('$LEGACY_')) { + await this.importLegacyPlugin(name.replace('$LEGACY_', '')); + } else { + await this.importReactPlugin(name); + } this.log(`Loaded ${name}`); - const ev = new Event("setPlugins"); - ev.data = this.plugins; - window.__DeckyEvLoop.dispatchEvent(ev); + + this.deckyState.setPlugins(this.plugins); } private async importReactPlugin(name: string) { @@ -68,10 +65,9 @@ class PluginLoader extends Logger { this.plugins.push({ name: name, icon: content.icon, - content: content.content + content: content.content, }); - } - else throw new Error(`${name} frontend_bundle not OK`); + } else throw new Error(`${name} frontend_bundle not OK`); } private async importLegacyPlugin(name: string) { @@ -79,13 +75,13 @@ class PluginLoader extends Logger { this.plugins.push({ name: name, icon: <FaPlug />, - content: <LegacyPlugin url={ url } /> + content: <LegacyPlugin url={url} />, }); } - static createPluginAPI(pluginName) { + static createPluginAPI(pluginName: string) { return { - async callServerMethod(methodName, args = {}) { + async callServerMethod(methodName: string, args = {}) { const response = await fetch(`http://127.0.0.1:1337/methods/${methodName}`, { method: 'POST', headers: { @@ -96,7 +92,7 @@ class PluginLoader extends Logger { return response.json(); }, - async callPluginMethod(methodName, args = {}) { + async callPluginMethod(methodName: string, args = {}) { const response = await fetch(`http://127.0.0.1:1337/plugins/${pluginName}/methods/${methodName}`, { method: 'POST', headers: { @@ -109,25 +105,25 @@ class PluginLoader extends Logger { return response.json(); }, - fetchNoCors(url, request: any = {}) { + fetchNoCors(url: string, request: any = {}) { let args = { method: 'POST', headers: {}, body: '' }; const req = { ...args, ...request, url, data: request.body }; return this.callServerMethod('http_request', req); }, - executeInTab(tab, runAsync, code) { + executeInTab(tab: string, runAsync: boolean, code: string) { return this.callServerMethod('execute_in_tab', { tab, run_async: runAsync, code, }); }, - injectCssIntoTab(tab, style) { + injectCssIntoTab(tab: string, style: string) { return this.callServerMethod('inject_css_into_tab', { tab, style, }); }, - removeCssFromTab(tab, cssId) { + removeCssFromTab(tab: string, cssId: any) { return this.callServerMethod('remove_css_from_tab', { tab, css_id: cssId, @@ -137,4 +133,4 @@ class PluginLoader extends Logger { } } -export default PluginLoader;
\ No newline at end of file +export default PluginLoader; |
