Skip to content

Commit

Permalink
Implement UI event dispatcher/listener (stashapp#4492)
Browse files Browse the repository at this point in the history
* page change event
* expose event to plugin api
* Update UIPluginApi.md
* Add to example plugin
---------
Co-authored-by: WithoutPants <[email protected]>
  • Loading branch information
Tetrax-10 authored and halkeye committed Sep 1, 2024
1 parent f54a2ed commit db3f04b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/plugin/examples/react-component/src/testReact.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
interface IPluginApi {
React: typeof React;
GQL: any;
Event: {
addEventListener: (event: string, callback: (e: CustomEvent) => void) => void;
};
libraries: {
ReactRouterDOM: {
Link: React.FC<any>;
Expand Down Expand Up @@ -53,6 +56,8 @@ interface IPluginApi {
NavUtils
} = PluginApi.utils;

PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname, e.detail.data.location.search))

const ScenePerformer: React.FC<{
performer: any;
}> = ({ performer }) => {
Expand Down
6 changes: 6 additions & 0 deletions ui/v2.5/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { lazyComponent } from "./utils/lazyComponent";
import { isPlatformUniquelyRenderedByApple } from "./utils/apple";
import useScript, { useCSS } from "./hooks/useScript";
import { useMemoOnce } from "./hooks/state";
import Event from "./hooks/event";
import { uniq } from "lodash-es";

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

// dispatch event when location changes
useEffect(() => {
Event.dispatch("location", "", { location });
}, [location]);

// redirect to setup or migrate as needed
useEffect(() => {
if (!systemStatusData) {
Expand Down
8 changes: 8 additions & 0 deletions ui/v2.5/src/docs/en/Manual/UIPluginApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,11 @@ Registers an after function. An after function is called after the render functi
| `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. |

Returns `void`.

#### `PluginApi.Event`

Allows plugins to listen for Stash's events.

```js
PluginApi.Event.addEventListener("stash:location", (e) => console.log("Page Changed", e.detail.data.location.pathname))
```
19 changes: 19 additions & 0 deletions ui/v2.5/src/hooks/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class StashEvent extends EventTarget {
dispatch(event: string, id?: string, data?: object) {
event = `stash:${event}${id ? `:${id}` : ""}`;

this.dispatchEvent(
new CustomEvent(event, {
detail: {
event: event,
...(id ? { id } : {}),
...(data ? { data } : {}),
},
})
);
}
}

const Event = new StashEvent();

export default Event;
2 changes: 2 additions & 0 deletions ui/v2.5/src/pluginApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as FontAwesomeSolid from "@fortawesome/free-solid-svg-icons";
import * as FontAwesomeRegular from "@fortawesome/free-regular-svg-icons";
import { useSpriteInfo } from "./hooks/sprite";
import { useToast } from "./hooks/Toast";
import Event from "./hooks/event";
import { before, instead, after, components, RegisterComponent } from "./patch";

// due to code splitting, some components may not have been loaded when a plugin
Expand Down Expand Up @@ -106,6 +107,7 @@ export const PluginApi = {
// and the result of the original function
after,
},
Event: Event,
};

export default PluginApi;
Expand Down

0 comments on commit db3f04b

Please sign in to comment.