@noya-app/noya-multiplayer-react 0.1.62 → 0.1.64

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,28 @@
1
+ import React, { CSSProperties } from "react";
2
+
3
+ // Rotates ▶️ or ▼ based on expanded state
4
+ export function StateInspectorArrow({
5
+ expanded,
6
+ onClick,
7
+ style,
8
+ }: {
9
+ expanded: boolean;
10
+ onClick?: () => void;
11
+ style?: CSSProperties;
12
+ }) {
13
+ return (
14
+ <span
15
+ role="button"
16
+ onClick={onClick}
17
+ style={{
18
+ display: "inline-block",
19
+ textAlign: "center",
20
+ transform: expanded ? "rotate(0deg)" : "rotate(-90deg)",
21
+ fontFamily: "Menlo, monospace",
22
+ ...style,
23
+ }}
24
+ >
25
+ {"▼"}
26
+ </span>
27
+ );
28
+ }
@@ -0,0 +1,45 @@
1
+ import React, { CSSProperties } from "react";
2
+ import { lightInspectorTheme } from "./inspectorTheme";
3
+
4
+ export function StateInspectorButton({
5
+ children,
6
+ onClick,
7
+ style,
8
+ theme,
9
+ disabled,
10
+ }: {
11
+ children: React.ReactNode;
12
+ onClick: () => void;
13
+ style?: CSSProperties;
14
+ theme: typeof lightInspectorTheme;
15
+ disabled?: boolean;
16
+ }) {
17
+ return (
18
+ <button
19
+ type="button"
20
+ disabled={disabled}
21
+ onClick={(event) => {
22
+ event.stopPropagation();
23
+ onClick();
24
+ }}
25
+ style={{
26
+ flex: "0",
27
+ appearance: "none",
28
+ background: "none",
29
+ color: theme.BASE_COLOR,
30
+ opacity: disabled ? 0.25 : 1,
31
+ border: "none",
32
+ fontSize: "12px",
33
+ whiteSpace: "nowrap",
34
+ display: "inline-flex",
35
+ alignItems: "center",
36
+ justifyContent: "center",
37
+ padding: "0",
38
+ cursor: "pointer",
39
+ ...style,
40
+ }}
41
+ >
42
+ {children}
43
+ </button>
44
+ );
45
+ }
@@ -0,0 +1,97 @@
1
+ "use client";
2
+
3
+ import React, {
4
+ CSSProperties,
5
+ DetailedHTMLProps,
6
+ ForwardedRef,
7
+ forwardRef,
8
+ HTMLAttributes,
9
+ } from "react";
10
+ import { darkInspectorTheme, lightInspectorTheme } from "./inspectorTheme";
11
+ import { StateInspectorArrow } from "./StateInspectorArrow";
12
+
13
+ export function StateInspectorDisclosureSection({
14
+ open,
15
+ setOpen,
16
+ title,
17
+ right,
18
+ children,
19
+ colorScheme,
20
+ isFirst,
21
+ style,
22
+ }: {
23
+ open: boolean;
24
+ setOpen?: (value: boolean) => void;
25
+ title: React.ReactNode;
26
+ right?: React.ReactNode;
27
+ children: React.ReactNode;
28
+ colorScheme: "light" | "dark";
29
+ isFirst?: boolean;
30
+ style?: CSSProperties;
31
+ }) {
32
+ const theme =
33
+ colorScheme === "light" ? lightInspectorTheme : darkInspectorTheme;
34
+
35
+ const borderColor =
36
+ colorScheme === "light" ? "rgba(0,0,0,0.1)" : "rgba(255,255,255,0.1)";
37
+
38
+ return (
39
+ <div
40
+ style={{
41
+ flex: open ? "1 1 0" : "0",
42
+ display: "flex",
43
+ flexDirection: "column",
44
+ ...style,
45
+ }}
46
+ >
47
+ <div
48
+ onClick={() => setOpen?.(!open)}
49
+ style={{
50
+ cursor: "default",
51
+ fontSize: "12px",
52
+ padding: "4px 10px",
53
+ display: "flex",
54
+ alignItems: "center",
55
+ ...(!isFirst && { borderTop: `1px solid ${borderColor}` }),
56
+ ...(open && { borderBottom: `1px solid ${borderColor}` }),
57
+ }}
58
+ >
59
+ {setOpen && (
60
+ <StateInspectorArrow
61
+ expanded={open}
62
+ style={{
63
+ fontSize: theme.ARROW_FONT_SIZE,
64
+ marginRight: theme.ARROW_MARGIN_RIGHT + 1,
65
+ color: theme.ARROW_COLOR,
66
+ }}
67
+ />
68
+ )}
69
+ <span style={{ flex: "1 1 0", userSelect: "none" }}>{title}</span>
70
+ {right}
71
+ </div>
72
+ {open && children}
73
+ </div>
74
+ );
75
+ }
76
+
77
+ export const StateInspectorDisclosureRowInner = forwardRef(
78
+ function StateInspectorDisclosureRowInner(
79
+ props: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>,
80
+ forwardedRef: ForwardedRef<HTMLDivElement>
81
+ ) {
82
+ return (
83
+ <div
84
+ style={{
85
+ flex: "1 1 0",
86
+ overflowY: "auto",
87
+ overflowX: "hidden",
88
+ display: "flex",
89
+ flexDirection: "column",
90
+ ...props.style,
91
+ }}
92
+ {...props}
93
+ ref={forwardedRef}
94
+ />
95
+ );
96
+ }
97
+ );
@@ -0,0 +1,57 @@
1
+ "use client";
2
+
3
+ import React, { CSSProperties } from "react";
4
+
5
+ export function StateInspectorRow({
6
+ children,
7
+ colorScheme,
8
+ selected,
9
+ style,
10
+ variant,
11
+ }: {
12
+ children: React.ReactNode;
13
+ colorScheme: "light" | "dark";
14
+ selected?: boolean;
15
+ style?: CSSProperties;
16
+ variant?: "up" | "down";
17
+ }) {
18
+ const solidBorderColor =
19
+ colorScheme === "light" ? "rgb(223 223 223)" : "rgb(29 29 29)";
20
+
21
+ return (
22
+ <div
23
+ style={{
24
+ borderBottom: `1px solid ${solidBorderColor}`,
25
+ fontSize: "12px",
26
+ fontFamily: "Menlo, monospace",
27
+ padding: "2px 12px 1px",
28
+ display: "flex",
29
+ alignItems: "center",
30
+ background:
31
+ variant === "up"
32
+ ? "rgba(0,255,0,0.1)"
33
+ : variant === "down"
34
+ ? "transparent"
35
+ : selected
36
+ ? "rgb(59 130 246 / 15%)"
37
+ : undefined,
38
+ // background:
39
+ // colorScheme === "light"
40
+ // ? "rgba(0,0,0,0.05)"
41
+ // : "rgba(255,255,255,0.05)",
42
+ ...style,
43
+ }}
44
+ >
45
+ <span
46
+ style={{
47
+ fontFamily: "Menlo, monospace",
48
+ fontSize: "11px",
49
+ borderRadius: 4,
50
+ display: "inline-block",
51
+ }}
52
+ >
53
+ {children}
54
+ </span>
55
+ </div>
56
+ );
57
+ }
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+
3
+ export function StateInspectorTitleLabel({
4
+ children,
5
+ }: {
6
+ children: React.ReactNode;
7
+ }) {
8
+ return (
9
+ <span style={{ display: "flex", alignItems: "center", gap: "4px" }}>
10
+ {children}
11
+ </span>
12
+ );
13
+ }
@@ -0,0 +1,82 @@
1
+ import React from "react";
2
+ import { StateInspectorAnchor } from "./StateInspector";
3
+ import { lightInspectorTheme } from "./inspectorTheme";
4
+
5
+ export function StateInspectorToggleButton({
6
+ showInspector,
7
+ setShowInspector,
8
+ theme,
9
+ anchor,
10
+ }: {
11
+ showInspector: boolean;
12
+ setShowInspector: (value: boolean) => void;
13
+ theme: typeof lightInspectorTheme;
14
+ anchor: StateInspectorAnchor;
15
+ }) {
16
+ const isRightAnchor = anchor === "right";
17
+
18
+ const rightIcon = (
19
+ <svg
20
+ xmlns="http://www.w3.org/2000/svg"
21
+ fill="none"
22
+ viewBox="0 0 24 24"
23
+ strokeWidth={1.5}
24
+ stroke="currentColor"
25
+ className="size-6"
26
+ >
27
+ <path
28
+ strokeLinecap="round"
29
+ strokeLinejoin="round"
30
+ d="m5.25 4.5 7.5 7.5-7.5 7.5m6-15 7.5 7.5-7.5 7.5"
31
+ />
32
+ </svg>
33
+ );
34
+
35
+ const leftIcon = (
36
+ <svg
37
+ xmlns="http://www.w3.org/2000/svg"
38
+ fill="none"
39
+ viewBox="0 0 24 24"
40
+ strokeWidth={1.5}
41
+ stroke="currentColor"
42
+ className="size-6"
43
+ >
44
+ <path
45
+ strokeLinecap="round"
46
+ strokeLinejoin="round"
47
+ d="m18.75 4.5-7.5 7.5 7.5 7.5m-6-15L5.25 12l7.5 7.5"
48
+ />
49
+ </svg>
50
+ );
51
+
52
+ return (
53
+ <span
54
+ role="button"
55
+ style={{
56
+ flex: "0",
57
+ appearance: "none",
58
+ color: theme.BASE_COLOR,
59
+ border: "none",
60
+ fontSize: "12px",
61
+ whiteSpace: "nowrap",
62
+ display: "inline-flex",
63
+ alignItems: "center",
64
+ justifyContent: "center",
65
+ padding: "2px 0",
66
+ }}
67
+ onClick={(event) => {
68
+ event.stopPropagation();
69
+
70
+ setShowInspector(!showInspector);
71
+ }}
72
+ >
73
+ {showInspector ? (
74
+ "Hide Inspector"
75
+ ) : (
76
+ <span style={{ width: "12px", height: "12px" }}>
77
+ {isRightAnchor ? rightIcon : leftIcon}
78
+ </span>
79
+ )}
80
+ </span>
81
+ );
82
+ }
@@ -0,0 +1,21 @@
1
+ import { chromeDark, chromeLight } from "react-inspector";
2
+
3
+ export const lightInspectorTheme = {
4
+ ...chromeLight,
5
+ BASE_BACKGROUND_COLOR: "transparent",
6
+ OBJECT_NAME_COLOR: "rgba(0,0,0,0.7)",
7
+ } as any;
8
+
9
+ export const darkInspectorTheme = {
10
+ ...chromeDark,
11
+ BASE_BACKGROUND_COLOR: "transparent",
12
+ OBJECT_NAME_COLOR: "rgba(255,255,255,0.7)",
13
+ } as any;
14
+
15
+ export const getStateInspectorTheme = (colorScheme: "light" | "dark") => {
16
+ return colorScheme === "light" ? lightInspectorTheme : darkInspectorTheme;
17
+ };
18
+
19
+ export const getStateInspectorBorderColor = (colorScheme: "light" | "dark") => {
20
+ return colorScheme === "light" ? "rgb(223 223 223)" : "rgb(29 29 29)";
21
+ };
@@ -0,0 +1,136 @@
1
+ import { ActivityEventsManager, StreamFilter } from "@noya-app/state-manager";
2
+ import React, { useState } from "react";
3
+ import { ObjectInspector } from "react-inspector";
4
+ import { useActivityEventsForManager } from "../../NoyaStateContext";
5
+ import { getStateInspectorTheme } from "../inspectorTheme";
6
+ import { StateInspectorButton } from "../StateInspectorButton";
7
+ import {
8
+ StateInspectorDisclosureRowInner,
9
+ StateInspectorDisclosureSection,
10
+ } from "../StateInspectorDisclosureSection";
11
+ import { StateInspectorRow } from "../StateInspectorRow";
12
+
13
+ function isResourceStreamFilter(
14
+ streamFilter: StreamFilter
15
+ ): streamFilter is { resourceId: string } {
16
+ return typeof streamFilter === "object" && "resourceId" in streamFilter;
17
+ }
18
+
19
+ function isResourceActivityEvent(
20
+ activityEvent: unknown
21
+ ): activityEvent is { resourceId: string } {
22
+ return (
23
+ typeof activityEvent === "object" &&
24
+ activityEvent !== null &&
25
+ "type" in activityEvent &&
26
+ activityEvent.type === "resource" &&
27
+ "resourceId" in activityEvent &&
28
+ typeof activityEvent.resourceId === "string"
29
+ );
30
+ }
31
+
32
+ export function ActivityEventsSection({
33
+ showEvents,
34
+ setShowEvents,
35
+ colorScheme,
36
+ activityEventsContainerRef,
37
+ activityEventsManager,
38
+ }: {
39
+ showEvents: boolean;
40
+ setShowEvents: (value: boolean) => void;
41
+ colorScheme: "light" | "dark";
42
+ activityEventsContainerRef: React.RefObject<HTMLDivElement | null>;
43
+ activityEventsManager: ActivityEventsManager;
44
+ }) {
45
+ const [streamFilter, setStreamFilter] = useState<StreamFilter>("all");
46
+ const streamFilterType = isResourceStreamFilter(streamFilter)
47
+ ? "resource"
48
+ : "all";
49
+ const theme = getStateInspectorTheme(colorScheme);
50
+ const activityEvents = useActivityEventsForManager(
51
+ showEvents ? streamFilter : undefined,
52
+ activityEventsManager
53
+ );
54
+
55
+ return (
56
+ <StateInspectorDisclosureSection
57
+ open={showEvents}
58
+ setOpen={setShowEvents}
59
+ title={`Activity Events`}
60
+ colorScheme={colorScheme}
61
+ >
62
+ <StateInspectorDisclosureRowInner ref={activityEventsContainerRef}>
63
+ <div style={{ padding: "4px 12px", display: "flex", gap: "4px" }}>
64
+ <select
65
+ name="streamFilterType"
66
+ value={streamFilterType}
67
+ style={{ flex: "0 0 auto", height: "19px" }}
68
+ onChange={(e) => {
69
+ const value = e.target.value as string;
70
+ if (value === "all") {
71
+ setStreamFilter("all");
72
+ } else {
73
+ setStreamFilter({ resourceId: "" });
74
+ }
75
+ }}
76
+ >
77
+ <option value="all">All</option>
78
+ <option value="resource">Resource</option>
79
+ </select>
80
+ {isResourceStreamFilter(streamFilter) && (
81
+ <input
82
+ name="resourceId"
83
+ type="text"
84
+ placeholder="Resource ID"
85
+ value={streamFilter.resourceId}
86
+ style={{
87
+ flex: "1 1 auto",
88
+ height: "19px",
89
+ padding: "0px 4px",
90
+ boxSizing: "border-box",
91
+ }}
92
+ onChange={(e) =>
93
+ setStreamFilter({ ...streamFilter, resourceId: e.target.value })
94
+ }
95
+ />
96
+ )}
97
+ </div>
98
+ {activityEvents?.map((activityEvent, index) => (
99
+ <StateInspectorRow
100
+ key={index}
101
+ colorScheme={colorScheme}
102
+ style={{
103
+ padding: "0px 12px 1px",
104
+ }}
105
+ >
106
+ <ObjectInspector data={activityEvent} theme={theme} />
107
+ {streamFilterType === "all" &&
108
+ isResourceActivityEvent(activityEvent) && (
109
+ <StateInspectorButton
110
+ theme={theme}
111
+ onClick={() => {
112
+ setStreamFilter({ resourceId: activityEvent.resourceId });
113
+ }}
114
+ >
115
+ Watch this resource
116
+ </StateInspectorButton>
117
+ )}
118
+ </StateInspectorRow>
119
+ ))}
120
+ {!activityEvents && (
121
+ <div
122
+ style={{
123
+ padding: "12px",
124
+ fontSize: "12px",
125
+ display: "flex",
126
+ flexDirection: "column",
127
+ gap: "4px",
128
+ }}
129
+ >
130
+ <span>No activity events</span>
131
+ </div>
132
+ )}
133
+ </StateInspectorDisclosureRowInner>
134
+ </StateInspectorDisclosureSection>
135
+ );
136
+ }
@@ -0,0 +1,89 @@
1
+ import { ConnectionEvent } from "@noya-app/state-manager";
2
+ import React from "react";
3
+ import { ObjectInspector, ObjectLabel } from "react-inspector";
4
+ import { getStateInspectorTheme } from "../inspectorTheme";
5
+ import { ObjectRootLabel } from "../ObjectRootLabel";
6
+ import {
7
+ StateInspectorDisclosureRowInner,
8
+ StateInspectorDisclosureSection,
9
+ } from "../StateInspectorDisclosureSection";
10
+ import { StateInspectorRow } from "../StateInspectorRow";
11
+
12
+ export function EventsSection<S>({
13
+ showEvents,
14
+ setShowEvents,
15
+ colorScheme,
16
+ eventsContainerRef,
17
+ connectionEvents,
18
+ }: {
19
+ showEvents: boolean;
20
+ setShowEvents: (value: boolean) => void;
21
+ colorScheme: "light" | "dark";
22
+ eventsContainerRef: React.RefObject<HTMLDivElement | null>;
23
+ connectionEvents: ConnectionEvent<S>[];
24
+ }) {
25
+ const theme = getStateInspectorTheme(colorScheme);
26
+
27
+ return (
28
+ <StateInspectorDisclosureSection
29
+ open={showEvents}
30
+ setOpen={setShowEvents}
31
+ title="Events"
32
+ colorScheme={colorScheme}
33
+ >
34
+ <StateInspectorDisclosureRowInner ref={eventsContainerRef}>
35
+ {connectionEvents?.map((event, index) =>
36
+ event.type === "stateChange" ? (
37
+ <StateInspectorRow key={index} colorScheme={colorScheme}>
38
+ connection:{" "}
39
+ <span style={{ fontStyle: "italic" }}>
40
+ {event.state.toLowerCase()}
41
+ </span>
42
+ </StateInspectorRow>
43
+ ) : (
44
+ <StateInspectorRow
45
+ key={index}
46
+ colorScheme={colorScheme}
47
+ variant={event.type === "send" ? "up" : "down"}
48
+ style={{
49
+ padding: "0px 12px 1px",
50
+ }}
51
+ >
52
+ <ObjectInspector
53
+ data={event.type === "error" ? event.error : event.message}
54
+ theme={theme}
55
+ nodeRenderer={({ depth, name, data, isNonenumerable }: any) => {
56
+ const direction = event.type === "send" ? "up" : "down";
57
+
58
+ return depth === 0 ? (
59
+ <ObjectRootLabel direction={direction} data={data} />
60
+ ) : (
61
+ <ObjectLabel
62
+ direction={direction}
63
+ name={name}
64
+ data={data}
65
+ isNonenumerable={isNonenumerable}
66
+ />
67
+ );
68
+ }}
69
+ />
70
+ </StateInspectorRow>
71
+ )
72
+ )}
73
+ {!connectionEvents && (
74
+ <div
75
+ style={{
76
+ padding: "12px",
77
+ fontSize: "12px",
78
+ display: "flex",
79
+ flexDirection: "column",
80
+ gap: "4px",
81
+ }}
82
+ >
83
+ <span>No recorded events</span>
84
+ </div>
85
+ )}
86
+ </StateInspectorDisclosureRowInner>
87
+ </StateInspectorDisclosureSection>
88
+ );
89
+ }