@noya-app/noya-multiplayer-react 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ import { ConnectionEvent, StateManager } from "@noya-app/state-manager";
2
+ import React, { ComponentProps, useLayoutEffect } from "react";
3
+ import { createRoot } from "react-dom/client";
4
+
5
+ import { Root } from "react-dom/client";
6
+ import { StateInspector } from "./StateInspector";
7
+
8
+ function createPortalElement() {
9
+ const el = document.createElement("div");
10
+ el.id = "noya-multiplayer-react-inspector-portal";
11
+ return el;
12
+ }
13
+
14
+ export function useStateInspector<S extends object, M>({
15
+ state,
16
+ connectionEvents,
17
+ disabled = false,
18
+ colorScheme,
19
+ stateManager,
20
+ }: {
21
+ state: S;
22
+ connectionEvents?: ConnectionEvent<S>[];
23
+ disabled: boolean;
24
+ colorScheme: ComponentProps<typeof StateInspector>["colorScheme"];
25
+ stateManager: StateManager<S, M>;
26
+ }) {
27
+ const [root, setRoot] = React.useState<Root | null>(null);
28
+
29
+ useLayoutEffect(() => {
30
+ if (disabled) return;
31
+
32
+ const portalElement = createPortalElement();
33
+
34
+ const root = createRoot(portalElement);
35
+
36
+ document.body.appendChild(portalElement);
37
+
38
+ setRoot(root);
39
+
40
+ return () => {
41
+ document.body.removeChild(portalElement);
42
+ };
43
+ }, [disabled]);
44
+
45
+ useLayoutEffect(() => {
46
+ if (!root) return;
47
+
48
+ root.render(
49
+ <StateInspector
50
+ state={state}
51
+ connectionEvents={connectionEvents}
52
+ colorScheme={colorScheme}
53
+ stateManager={stateManager as StateManager<any, any>}
54
+ />
55
+ );
56
+ }, [colorScheme, connectionEvents, root, state, stateManager]);
57
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "@repo/typescript-config/library.json"
3
+ }