@noya-app/noya-multiplayer-react 0.1.83 → 0.1.85
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.
- package/.turbo/turbo-build.log +11 -11
- package/CHANGELOG.md +14 -0
- package/dist/index.bundle.js +25 -22
- package/dist/index.d.mts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +43 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/NoyaStateContext.tsx +18 -16
- package/src/__tests__/useResourceUrl.test.tsx +92 -0
- package/src/index.ts +1 -0
- package/src/inspector/StateInspector.test.tsx +18 -0
- package/src/inspector/StateInspector.tsx +3 -1
- package/src/inspector/useStateInspector.tsx +4 -1
- package/src/singleton.tsx +5 -0
- package/src/useResourceUrl.ts +19 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noya-app/noya-multiplayer-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.85",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"dev": "npm run build -- --watch"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@noya-app/state-manager": "0.1.
|
|
14
|
+
"@noya-app/state-manager": "0.1.67",
|
|
15
15
|
"@noya-app/emitter": "0.1.0",
|
|
16
16
|
"@noya-app/observable": "0.1.12",
|
|
17
17
|
"@noya-app/task-runner": "0.1.7",
|
package/src/NoyaStateContext.tsx
CHANGED
|
@@ -170,26 +170,28 @@ export const AnyNoyaStateContext = createContext<
|
|
|
170
170
|
AnyNoyaStateContextValue | undefined
|
|
171
171
|
>(undefined);
|
|
172
172
|
|
|
173
|
-
export function
|
|
173
|
+
export function useOptionalAnyNoyaStateContext() {
|
|
174
174
|
const value = useContext(AnyNoyaStateContext);
|
|
175
|
+
const bindings = getNoyaSingletonBindings();
|
|
175
176
|
|
|
176
|
-
if (
|
|
177
|
-
|
|
177
|
+
if (value) return value;
|
|
178
|
+
if (!bindings) return undefined;
|
|
178
179
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
"useNoyaStateContext must be used within a NoyaStateProvider"
|
|
189
|
-
);
|
|
190
|
-
}
|
|
180
|
+
return {
|
|
181
|
+
noyaManager: getNoyaManagerSingleton(),
|
|
182
|
+
theme: bindings.theme,
|
|
183
|
+
viewType: bindings.viewType,
|
|
184
|
+
} as AnyNoyaStateContextValue;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function useAnyNoyaStateContext() {
|
|
188
|
+
const value = useOptionalAnyNoyaStateContext();
|
|
191
189
|
|
|
192
|
-
return value;
|
|
190
|
+
if (value) return value;
|
|
191
|
+
|
|
192
|
+
throw new Error(
|
|
193
|
+
"useNoyaStateContext must be used within a NoyaStateProvider"
|
|
194
|
+
);
|
|
193
195
|
}
|
|
194
196
|
|
|
195
197
|
export function useAnyNoyaManager() {
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Asset, Resource } from "@noya-app/noya-schemas";
|
|
2
|
+
import { NoyaManager } from "@noya-app/state-manager";
|
|
3
|
+
import { act, cleanup, renderHook } from "@testing-library/react";
|
|
4
|
+
import { afterEach, describe, expect, it } from "bun:test";
|
|
5
|
+
import { PropsWithChildren } from "react";
|
|
6
|
+
import { AnyNoyaStateContext } from "../NoyaStateContext";
|
|
7
|
+
import { useResourceUrl } from "../useResourceUrl";
|
|
8
|
+
|
|
9
|
+
const asset = {
|
|
10
|
+
id: "asset-id",
|
|
11
|
+
stableId: "asset-stable-id",
|
|
12
|
+
url: "blob:http://localhost/image",
|
|
13
|
+
} as Asset;
|
|
14
|
+
|
|
15
|
+
const resource = {
|
|
16
|
+
accessibleByFileId: null,
|
|
17
|
+
accessibleByFileVersionId: null,
|
|
18
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
19
|
+
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
20
|
+
id: "resource-id",
|
|
21
|
+
stableId: "resource-stable-id",
|
|
22
|
+
path: "image.png",
|
|
23
|
+
type: "asset",
|
|
24
|
+
assetId: asset.id,
|
|
25
|
+
url: "/api/resources/resource-id",
|
|
26
|
+
} satisfies Resource;
|
|
27
|
+
|
|
28
|
+
afterEach(cleanup);
|
|
29
|
+
|
|
30
|
+
describe("useResourceUrl", () => {
|
|
31
|
+
it("resolves IDs and stable IDs and responds to data updates", () => {
|
|
32
|
+
const noyaManager = new NoyaManager(
|
|
33
|
+
{},
|
|
34
|
+
{
|
|
35
|
+
initialAssets: [asset],
|
|
36
|
+
initialResources: [resource],
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
const wrapper = ({ children }: PropsWithChildren) => (
|
|
40
|
+
<AnyNoyaStateContext.Provider
|
|
41
|
+
value={{ noyaManager, theme: "light", viewType: "editable" }}
|
|
42
|
+
>
|
|
43
|
+
{children}
|
|
44
|
+
</AnyNoyaStateContext.Provider>
|
|
45
|
+
);
|
|
46
|
+
const { result, rerender } = renderHook(
|
|
47
|
+
({ id }: { id: string | undefined }) => useResourceUrl(id),
|
|
48
|
+
{
|
|
49
|
+
initialProps: { id: resource.id },
|
|
50
|
+
wrapper,
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
expect(result.current).toBe(asset.url);
|
|
55
|
+
|
|
56
|
+
rerender({ id: resource.stableId });
|
|
57
|
+
expect(result.current).toBe(asset.url);
|
|
58
|
+
|
|
59
|
+
act(() => {
|
|
60
|
+
noyaManager.assetManager.set([
|
|
61
|
+
{ ...asset, url: "blob:http://localhost/updated-image" },
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
expect(result.current).toBe("blob:http://localhost/updated-image");
|
|
65
|
+
|
|
66
|
+
act(() => {
|
|
67
|
+
noyaManager.assetManager.set([]);
|
|
68
|
+
});
|
|
69
|
+
expect(result.current).toBe(resource.url);
|
|
70
|
+
|
|
71
|
+
act(() => {
|
|
72
|
+
noyaManager.resourceManager.setResources([
|
|
73
|
+
{ ...resource, url: "/api/resources/updated-resource-id" },
|
|
74
|
+
]);
|
|
75
|
+
});
|
|
76
|
+
expect(result.current).toBe("/api/resources/updated-resource-id");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("returns undefined when no resource ID is provided", () => {
|
|
80
|
+
const noyaManager = new NoyaManager({});
|
|
81
|
+
const wrapper = ({ children }: PropsWithChildren) => (
|
|
82
|
+
<AnyNoyaStateContext.Provider
|
|
83
|
+
value={{ noyaManager, theme: "light", viewType: "editable" }}
|
|
84
|
+
>
|
|
85
|
+
{children}
|
|
86
|
+
</AnyNoyaStateContext.Provider>
|
|
87
|
+
);
|
|
88
|
+
const { result } = renderHook(() => useResourceUrl(undefined), { wrapper });
|
|
89
|
+
|
|
90
|
+
expect(result.current).toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -9,5 +9,6 @@ export * from "./noyaApp";
|
|
|
9
9
|
export * from "./NoyaStateContext";
|
|
10
10
|
export * from "./singleton";
|
|
11
11
|
export * from "./useObservable";
|
|
12
|
+
export * from "./useResourceUrl";
|
|
12
13
|
export { WebSocketConnection } from "./WebSocketConnection";
|
|
13
14
|
export type { ConnectionEvent, ConnectionOptions } from "./WebSocketConnection";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { afterEach, expect, it } from "bun:test";
|
|
2
|
+
import { NoyaManager } from "@noya-app/state-manager";
|
|
3
|
+
import { cleanup, render } from "@testing-library/react";
|
|
4
|
+
import { StateInspector } from "./StateInspector";
|
|
5
|
+
|
|
6
|
+
afterEach(() => {
|
|
7
|
+
cleanup();
|
|
8
|
+
localStorage.clear();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("can default to collapsed", () => {
|
|
12
|
+
const noyaManager = new NoyaManager({});
|
|
13
|
+
const view = render(
|
|
14
|
+
<StateInspector noyaManager={noyaManager} defaultCollapsed />
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
expect(view.queryByText("Users")).toBeNull();
|
|
18
|
+
});
|
|
@@ -48,6 +48,7 @@ export type StateInspectorOptions = {
|
|
|
48
48
|
colorScheme?: "light" | "dark";
|
|
49
49
|
anchor?: StateInspectorAnchor;
|
|
50
50
|
advanced?: boolean;
|
|
51
|
+
defaultCollapsed?: boolean;
|
|
51
52
|
};
|
|
52
53
|
|
|
53
54
|
export const StateInspector = memoGeneric(function StateInspector<
|
|
@@ -62,6 +63,7 @@ export const StateInspector = memoGeneric(function StateInspector<
|
|
|
62
63
|
anchor = "right",
|
|
63
64
|
unstyled = false,
|
|
64
65
|
advanced = false,
|
|
66
|
+
defaultCollapsed = false,
|
|
65
67
|
...props
|
|
66
68
|
}: {
|
|
67
69
|
noyaManager: NoyaManager<S, M, E, MenuT, I>;
|
|
@@ -99,7 +101,7 @@ export const StateInspector = memoGeneric(function StateInspector<
|
|
|
99
101
|
|
|
100
102
|
const [showInspector, setShowInspector] = useLocalStorageState(
|
|
101
103
|
"noya-multiplayer-react-show-inspector",
|
|
102
|
-
|
|
104
|
+
!defaultCollapsed
|
|
103
105
|
);
|
|
104
106
|
const [showEvents, setShowEvents] = useLocalStorageState(
|
|
105
107
|
"noya-multiplayer-react-show-events",
|
|
@@ -40,12 +40,14 @@ export function useStateInspector<
|
|
|
40
40
|
noyaManager,
|
|
41
41
|
disabled = false,
|
|
42
42
|
advanced = false,
|
|
43
|
+
defaultCollapsed = false,
|
|
43
44
|
colorScheme,
|
|
44
45
|
anchor,
|
|
45
46
|
}: {
|
|
46
47
|
noyaManager: NoyaManager<S, M, E, MenuT, I>;
|
|
47
48
|
disabled?: boolean;
|
|
48
49
|
advanced?: boolean;
|
|
50
|
+
defaultCollapsed?: boolean;
|
|
49
51
|
colorScheme?: ComponentProps<typeof StateInspector>["colorScheme"];
|
|
50
52
|
anchor?: ComponentProps<typeof StateInspector>["anchor"];
|
|
51
53
|
}) {
|
|
@@ -70,7 +72,8 @@ export function useStateInspector<
|
|
|
70
72
|
anchor={anchor}
|
|
71
73
|
noyaManager={noyaManager}
|
|
72
74
|
advanced={advanced}
|
|
75
|
+
defaultCollapsed={defaultCollapsed}
|
|
73
76
|
/>
|
|
74
77
|
);
|
|
75
|
-
}, [noyaManager, anchor, colorScheme, root, advanced]);
|
|
78
|
+
}, [noyaManager, anchor, colorScheme, root, advanced, defaultCollapsed]);
|
|
76
79
|
}
|
package/src/singleton.tsx
CHANGED
|
@@ -139,6 +139,11 @@ export function initializeNoyaSingleton<
|
|
|
139
139
|
advanced={
|
|
140
140
|
resolvedInspector === true ? false : resolvedInspector.advanced
|
|
141
141
|
}
|
|
142
|
+
defaultCollapsed={
|
|
143
|
+
resolvedInspector === true
|
|
144
|
+
? false
|
|
145
|
+
: resolvedInspector.defaultCollapsed
|
|
146
|
+
}
|
|
142
147
|
/>
|
|
143
148
|
);
|
|
144
149
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { getResourceUrl } from "@noya-app/state-manager";
|
|
4
|
+
import { useAnyNoyaStateContext } from "./NoyaStateContext";
|
|
5
|
+
import { useObservable } from "./useObservable";
|
|
6
|
+
|
|
7
|
+
export function useResourceUrl(
|
|
8
|
+
idOrStableId: string | undefined
|
|
9
|
+
): string | undefined {
|
|
10
|
+
const { noyaManager } = useAnyNoyaStateContext();
|
|
11
|
+
const resources = useObservable(
|
|
12
|
+
noyaManager.resourceManager.optimisticResources$
|
|
13
|
+
);
|
|
14
|
+
const assets = useObservable(noyaManager.assetManager.assets$);
|
|
15
|
+
|
|
16
|
+
if (!idOrStableId) return undefined;
|
|
17
|
+
|
|
18
|
+
return getResourceUrl(idOrStableId, resources, assets);
|
|
19
|
+
}
|