Skip to content

Commit 0f62ec4

Browse files
committed
add extension support
1 parent e973932 commit 0f62ec4

File tree

7 files changed

+131
-7
lines changed

7 files changed

+131
-7
lines changed

package-lock.json

Lines changed: 34 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webdeck",
3-
"version": "1.2.6",
3+
"version": "1.3.0",
44
"private": true,
55
"dependencies": {
66
"@babel/core": "7.23.7",
@@ -58,6 +58,7 @@
5858
},
5959
"devDependencies": {
6060
"@svgr/webpack": "^8.1.0",
61+
"@types/chrome": "^0.0.268",
6162
"@types/styled-components": "^5.1.34",
6263
"@typescript-eslint/eslint-plugin": "^6.21.0",
6364
"@typescript-eslint/parser": "^6.21.0",

src/bootstrap.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@ import { AppContext } from "./contexts/AppContext";
88
import { usePlugins } from "./hooks/usePlugins";
99
import { useProfiles } from "./hooks/useProfiles";
1010
import { useDeck } from "./hooks/useDeck";
11+
import { useExtension } from "./hooks/useExtension";
1112

1213
export type AppContainerType = {
1314
deck: ReturnType<typeof useDeck>;
1415
profiles: ReturnType<typeof useProfiles>;
1516
plugins: ReturnType<typeof usePlugins>;
17+
extension: ReturnType<typeof useExtension>;
1618
};
1719

1820
export const Container = () => {
21+
const extension = useExtension();
1922
const profiles = useProfiles();
2023
const plugins = usePlugins();
21-
const deck = useDeck(profiles, plugins);
24+
const deck = useDeck(profiles, plugins, extension);
25+
2226
return (
23-
<AppContext.Provider value={{ deck, profiles, plugins }}>
27+
<AppContext.Provider value={{ deck, profiles, plugins, extension }}>
2428
{window.location.search.includes("debug") && (
2529
<>
2630
<div>version: {packageJSON.version}</div>

src/components/Controller/Plugin/Plugin.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FC, lazy, useEffect, useState, Suspense } from "react";
22
import { useAppContext } from "../../../contexts/AppContext";
3+
import { EXTENSION_ID } from "../../../hooks/useExtension";
34

45
type FederatedComponent = FC<{
56
key: string;
@@ -10,12 +11,13 @@ type FederatedComponent = FC<{
1011
}>;
1112

1213
export const Plugin: FC = () => {
13-
const { plugins, profiles, deck } = useAppContext();
14+
const { plugins, profiles, deck, extension } = useAppContext();
1415
const [Plugin, setPlugin] = useState<FederatedComponent | null>(null);
1516

1617
const key = deck.selectedKey!;
1718
const keyConfig = profiles.profile.keys[key];
1819
const module = plugins.modules[keyConfig?.plugin];
20+
const manifest = plugins.manifests?.[keyConfig?.plugin];
1921
const loaded = module?.loaded;
2022

2123
useEffect(() => {
@@ -44,6 +46,27 @@ export const Plugin: FC = () => {
4446
overflow: "hidden",
4547
}}
4648
>
49+
{manifest?.extension_required === true &&
50+
extension.installed === false ? (
51+
<div
52+
style={{
53+
marginBottom: 8,
54+
padding: 8,
55+
border: "1px solid black",
56+
background: "orange",
57+
borderRadius: 8,
58+
}}
59+
>
60+
<span>This plugin required the </span>
61+
<a
62+
target="_blank"
63+
rel="noopener noreferrer"
64+
href={`https://chrome.google.com/webstore/detail/${EXTENSION_ID}`}
65+
>
66+
Webdeck Extension
67+
</a>{" "}
68+
</div>
69+
) : null}
4770
<Suspense fallback="Loading System">
4871
{Plugin && keyConfig?.config && module ? (
4972
<Plugin

src/hooks/useDeck.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import {
1313
} from "../utils/physicalDeck";
1414
import { useDrawKey } from "./useDrawKey";
1515
import { DrawKey } from "../types/Module";
16+
import { useExtension } from "./useExtension";
1617

1718
type Initer = Record<string, { plugin: string; destructor: () => void }>;
1819

1920
export const useDeck = (
2021
profiles: ReturnType<typeof useProfiles>,
21-
plugins: ReturnType<typeof usePlugins>
22+
plugins: ReturnType<typeof usePlugins>,
23+
extension: ReturnType<typeof useExtension>
2224
) => {
2325
const [editMode, setEditMode] = useState(true);
2426
const [selectedKey, setSelectedKey] = useState<number | undefined>(0);
@@ -47,6 +49,11 @@ export const useDeck = (
4749
...key,
4850
keyIndex,
4951
setIcon: (icon: string) => profiles.setIcon(keyIndex, icon),
52+
extension: {
53+
fetch: extension.fetch,
54+
data: extension.data,
55+
installed: extension.installed,
56+
},
5057
});
5158
} catch (e) {
5259
console.warn(

src/hooks/useExtension.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { useEffect, useState } from "react";
2+
3+
export const EXTENSION_ID = "njlliemmhglnlfelmmoljccgpdipigff";
4+
5+
export const useExtension = () => {
6+
const [installed, setInstalled] = useState<null | boolean>(null);
7+
const [data, setData] = useState({});
8+
9+
const getExtenison = () => {
10+
if (chrome?.runtime?.sendMessage) {
11+
chrome.runtime.sendMessage(
12+
EXTENSION_ID,
13+
{ type: "health" },
14+
(response) => {
15+
setInstalled(true);
16+
setData(response);
17+
}
18+
);
19+
} else {
20+
setInstalled(false);
21+
setData({});
22+
}
23+
};
24+
25+
const fetch = async (
26+
url: string,
27+
{
28+
method,
29+
body,
30+
options,
31+
}: {
32+
method?: string;
33+
body?: string;
34+
options?: Record<string, unknown>;
35+
} = { method: "get" }
36+
) => {
37+
return new Promise((resolve) => {
38+
if (chrome?.runtime?.sendMessage) {
39+
chrome.runtime.sendMessage(
40+
EXTENSION_ID,
41+
{ type: "fetch", method, url, body, ...options },
42+
(response) => resolve(response)
43+
);
44+
} else {
45+
resolve({ error: true, e: "no_extension" });
46+
}
47+
});
48+
};
49+
50+
useEffect(() => {
51+
getExtenison();
52+
}, []);
53+
54+
return { installed, getExtenison, fetch, data };
55+
};

src/types/Module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type ModuleManifest = {
1212
icons?: Record<string, ModuleIcon>;
1313
version?: string;
1414
bespoke?: boolean;
15+
extension_required?: boolean;
1516
};
1617

1718
export type DrawKey = ({
@@ -26,6 +27,7 @@ type ModuleOnPress = {
2627
config: Record<string | number, unknown>;
2728
keyIndex: number;
2829
setIcon: unknown;
30+
extension: unknown;
2931
};
3032

3133
type ModuleInit = {

0 commit comments

Comments
 (0)