summaryrefslogtreecommitdiff
path: root/frontend/src/tabs-hook.ts
diff options
context:
space:
mode:
authorJonas Dellinger <jonas@dellinger.dev>2022-05-13 19:14:47 +0200
committerJonas Dellinger <jonas@dellinger.dev>2022-05-13 19:14:47 +0200
commit74438a31458af8bddd08d90eacc6d63677bab844 (patch)
treea7bfc044941f65c7f9971c5386c463eac31be768 /frontend/src/tabs-hook.ts
parent945db5de4788feefebc845817752472419051640 (diff)
downloaddecky-loader-74438a31458af8bddd08d90eacc6d63677bab844.tar.gz
decky-loader-74438a31458af8bddd08d90eacc6d63677bab844.zip
Work on react frontend loader
Diffstat (limited to 'frontend/src/tabs-hook.ts')
-rw-r--r--frontend/src/tabs-hook.ts69
1 files changed, 69 insertions, 0 deletions
diff --git a/frontend/src/tabs-hook.ts b/frontend/src/tabs-hook.ts
new file mode 100644
index 00000000..17f41d91
--- /dev/null
+++ b/frontend/src/tabs-hook.ts
@@ -0,0 +1,69 @@
+import Logger from './logger';
+
+declare global {
+ interface Window {
+ __TABS_HOOK_INSTANCE: any;
+ }
+ interface Array<T> {
+ __filter: any;
+ }
+}
+
+const isTabsArray = (tabs) => {
+ const length = tabs.length;
+ return length === 7 && tabs[length - 1]?.key === 6 && tabs[length - 1]?.tab;
+};
+
+interface Tab {
+ id: string;
+ title: any;
+ content: any;
+ icon: any;
+}
+
+class TabsHook extends Logger {
+ // private keys = 7;
+ tabs: Tab[] = [];
+
+ constructor() {
+ super('TabsHook');
+
+ this.log('Initialized');
+ window.__TABS_HOOK_INSTANCE = this;
+
+ const self = this;
+
+ const filter = Array.prototype.__filter ?? Array.prototype.filter;
+ Array.prototype.__filter = filter;
+ Array.prototype.filter = function (...args) {
+ if (isTabsArray(this)) {
+ self.render(this);
+ }
+ // @ts-ignore
+ return filter.call(this, ...args);
+ };
+ }
+
+ add(tab: Tab) {
+ this.log('Adding tab', tab.id, 'to render array');
+ this.tabs.push(tab);
+ }
+
+ removeById(id: string) {
+ this.log('Removing tab', id);
+ this.tabs = this.tabs.filter((tab) => tab.id !== id);
+ }
+
+ render(existingTabs: any[]) {
+ for (const { title, icon, content, id } of this.tabs) {
+ existingTabs.push({
+ key: id,
+ title,
+ tab: icon,
+ panel: content,
+ });
+ }
+ }
+}
+
+export default TabsHook;