Skip to content

Commit 6a9175c

Browse files
authored
Implement UI event dispatcher/listener (#4492)
* page change event * expose event to plugin api * Update UIPluginApi.md * Add to example plugin --------- Co-authored-by: WithoutPants <[email protected]>
1 parent 56896d7 commit 6a9175c

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

Diff for: pkg/plugin/examples/react-component/src/testReact.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
interface IPluginApi {
22
React: typeof React;
33
GQL: any;
4+
Event: {
5+
addEventListener: (event: string, callback: (e: CustomEvent) => void) => void;
6+
};
47
libraries: {
58
ReactRouterDOM: {
69
Link: React.FC<any>;
@@ -53,6 +56,8 @@ interface IPluginApi {
5356
NavUtils
5457
} = PluginApi.utils;
5558

59+
PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname, e.detail.data.location.search))
60+
5661
const ScenePerformer: React.FC<{
5762
performer: any;
5863
}> = ({ performer }) => {

Diff for: ui/v2.5/src/App.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { lazyComponent } from "./utils/lazyComponent";
4242
import { isPlatformUniquelyRenderedByApple } from "./utils/apple";
4343
import useScript, { useCSS } from "./hooks/useScript";
4444
import { useMemoOnce } from "./hooks/state";
45+
import Event from "./hooks/event";
4546
import { uniq } from "lodash-es";
4647

4748
import { PluginRoutes } from "./plugins";
@@ -249,6 +250,11 @@ export const App: React.FC = () => {
249250
const history = useHistory();
250251
const setupMatch = useRouteMatch(["/setup", "/migrate"]);
251252

253+
// dispatch event when location changes
254+
useEffect(() => {
255+
Event.dispatch("location", "", { location });
256+
}, [location]);
257+
252258
// redirect to setup or migrate as needed
253259
useEffect(() => {
254260
if (!systemStatusData) {

Diff for: ui/v2.5/src/docs/en/Manual/UIPluginApi.md

+8
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,11 @@ Registers an after function. An after function is called after the render functi
136136
| `fn` | `Function` | The after function. It accepts the same arguments as the original render function, plus the result of the original render function, and is expected to return the rendered component. |
137137

138138
Returns `void`.
139+
140+
#### `PluginApi.Event`
141+
142+
Allows plugins to listen for Stash's events.
143+
144+
```js
145+
PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname))
146+
```

Diff for: ui/v2.5/src/hooks/event.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class StashEvent extends EventTarget {
2+
dispatch(event: string, id?: string, data?: object) {
3+
event = `stash:${event}${id ? `:${id}` : ""}`;
4+
5+
this.dispatchEvent(
6+
new CustomEvent(event, {
7+
detail: {
8+
event: event,
9+
...(id ? { id } : {}),
10+
...(data ? { data } : {}),
11+
},
12+
})
13+
);
14+
}
15+
}
16+
17+
const Event = new StashEvent();
18+
19+
export default Event;

Diff for: ui/v2.5/src/pluginApi.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as FontAwesomeSolid from "@fortawesome/free-solid-svg-icons";
1313
import * as FontAwesomeRegular from "@fortawesome/free-regular-svg-icons";
1414
import { useSpriteInfo } from "./hooks/sprite";
1515
import { useToast } from "./hooks/Toast";
16+
import Event from "./hooks/event";
1617
import { before, instead, after, components, RegisterComponent } from "./patch";
1718

1819
// due to code splitting, some components may not have been loaded when a plugin
@@ -106,6 +107,7 @@ export const PluginApi = {
106107
// and the result of the original function
107108
after,
108109
},
110+
Event: Event,
109111
};
110112

111113
export default PluginApi;

0 commit comments

Comments
 (0)