summaryrefslogtreecommitdiff
path: root/frontend/src/components/PluginView.tsx
blob: 27cb386a663422d5325b1935dd1bde70c994fcda (plain)
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
import { Button } from "decky-frontend-lib";
import React from "react"

class PluginView extends React.Component<{}, { runningPlugin: string, plugins: Array<any> }> {
    constructor() {
        super({});
        this.state = {
            plugins: [],
            runningPlugin: ""
        }
    }

    componentDidMount() {
        window.__DeckyEvLoop.addEventListener("pluginClose", (_) => { this.setState({ runningPlugin: "", plugins: this.state.plugins }) });
        window.__DeckyEvLoop.addEventListener("setPlugins", (ev) => { console.log(ev); this.setState({ plugins: ev.data, runningPlugin: this.state.runningPlugin }) });
    }

    private openPlugin(name: string) {
        const ev = new Event("pluginOpen");
        ev.data = name;
        window.__DeckyEvLoop.dispatchEvent(ev);
        this.setState({ runningPlugin: name, plugins: this.state.plugins })
    }

    render() {
        if (this.state.runningPlugin) {
            return this.state.plugins.find(x => x.name == this.state.runningPlugin).content;
        }
        else {
            let buttons = [];
            for (const plugin of this.state.plugins) {
                buttons.push(<Button layout="below" onClick={(_) => this.openPlugin(plugin.name)}>{plugin.icon}{plugin.name}</Button>)
            }
            if (buttons.length == 0) return <div className='staticClasses.Text'>No plugins...</div>;
            return buttons;
        }
    }
}

export default PluginView;