Skip to content

Commit 9b01ea5

Browse files
committed
refactor: Minor update to plugin template
1 parent 3ea267d commit 9b01ea5

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -224,23 +224,29 @@ Below is an overview of the main folders and files in this template:
224224
| ├── constants/
225225
│ │ └── DefaultPluginConfig.ts - Constants for plugin configurations.
226226
│ ├── core/
227-
│ │ └── RcbPlugin.tsx - Contains logic for hook factory and plugin hook.
227+
│ │ └── RcbPlugin.tsx - Contains core logic for the plugin (plugin hook).
228+
| ├── factory/
229+
│ │ └── RcbPlugin.tsx - Contains hook factory that prepares and creates the plugin hook.
228230
| ├── types/
229231
│ │ └── PluginConfig.ts - Contains type definition for plugin configurations.
230232
│ ├── App.tsx - For initializing and testing the plugin.
231233
│ ├── development.tsx - Entry point during development.
232234
│ └── index.tsx - Export file for your plugin.
235+
├── setup.js - Setup script for quick initialization of the project
233236
└── package.json - Update name and author fields here.
234237
```
235238

236239
### Key Files
237240
- .github Folder: Contains templates and workflows for CI/CD, automating builds and releases.
238-
- Constants Folder: Contains constants used in the plugin (e.g. DefaultPluginConfig).
239-
- Factory Folder: Houses RcbPlugin.tsx, where the plugin logic is developed.
241+
- Constants Folder: Contains constants used in the plugin (e.g. _DefaultPluginConfig.ts_).
242+
- Core Folder: Houses the plugin hook, where the core logic for the plugin is developed.
243+
- Factory Folder: Houses the plugin factory, which prepares the plugin hook to be consumed by the core library.
240244
- Types Folder: Specifies the types used in the plugin (e.g. PluginConfig).
241-
- App File: Initializes the chatbot with your plugin; ideal for quick testing.
242-
- Development and Index Files: Used for development setup and exporting plugin components.
243-
- package.json: Update your plugin’s information here before publishing.
245+
- App.tsx File: Initializes the chatbot with your plugin; ideal for quick testing.
246+
- development.tsx File: Used for development setup and testing.
247+
- index.tsx File: Used to export plugin components.
248+
- setup.js: Setup script for quick initialization of the template repository.
249+
- package.json File: Update your plugin’s information here before publishing.
244250

245251
## Creating a Plugin
246252

src/core/useRcbPlugin.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useEffect } from "react";
2+
3+
import { RcbPreInjectMessageEvent, useBotId } from "react-chatbotify";
4+
import { PluginConfig } from "../types/PluginConfig";
5+
6+
/**
7+
* Plugin hook that handles all the core logic.
8+
*
9+
* @param pluginConfig configurations for the plugin
10+
*/
11+
const useRcbPlugin = (pluginConfig?: PluginConfig) => {
12+
// uses hook to get bot id
13+
const { getBotId } = useBotId();
14+
15+
// provides plugin logic
16+
useEffect(() => {
17+
// handles message sent
18+
const handlePreInjectMessage = (event: RcbPreInjectMessageEvent) => {
19+
// checks bot id to prevent conflicts with multiple chatbots
20+
if (getBotId() !== event.detail.botId) {
21+
return;
22+
}
23+
24+
// checks if message is from user
25+
if (event.data.message.sender.toUpperCase() !== "USER") {
26+
return;
27+
}
28+
29+
// checks if message content is a string (it may be a JSX element)
30+
const message = event.data.message;
31+
if (typeof message.content !== "string") {
32+
return;
33+
}
34+
35+
// reverses the message string if it is
36+
message.content = message.content.split("").reverse().join("");
37+
};
38+
39+
// relies on rcb-pre-inject-message event to intercept and modify messages
40+
window.addEventListener("rcb-pre-inject-message", handlePreInjectMessage);
41+
return () => {
42+
window.removeEventListener("rcb-pre-inject-message", handlePreInjectMessage);
43+
};
44+
}, [getBotId]);
45+
46+
// name is a required field, recommended to match the npm package name to be unique
47+
const pluginMetaData = {name: "rcb-example-plugin", settings: {}};
48+
49+
// if auto config is true, then automatically enable the require events (or other settings as required)
50+
if (pluginConfig?.autoConfig) {
51+
pluginMetaData.settings = {
52+
event: {
53+
rcbPreInjectMessage: true
54+
}
55+
}
56+
}
57+
58+
// returns plugin metadata to be consumed by the core library
59+
return pluginMetaData;
60+
};
61+
62+
export default useRcbPlugin;

src/factory/RcbPluginFactory.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import useRcbPlugin from "../core/useRcbPlugin";
2+
import { PluginConfig } from "../types/PluginConfig";
3+
4+
/**
5+
* Factory that prepares the plugin hook to be consumed by the core library.
6+
*
7+
* @param pluginConfig configurations for the plugin
8+
*/
9+
const RcbPluginFactory = ({
10+
pluginConfig,
11+
}: {
12+
pluginConfig?: PluginConfig;
13+
} = {}) => {
14+
// any custom logic to be ran before hook initialization can be done here
15+
16+
// prepares and returns the plugin hook without calling it, because the
17+
// initialization of the hook should be handled by the core library itself
18+
const usePreparedRcbPlugin = () => useRcbPlugin(pluginConfig);
19+
return usePreparedRcbPlugin;
20+
};
21+
22+
export default RcbPluginFactory;

0 commit comments

Comments
 (0)