@dxos/react-client 0.7.4 → 0.7.5-main.937ce75

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-client",
3
- "version": "0.7.4",
3
+ "version": "0.7.5-main.937ce75",
4
4
  "description": "React client API",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -77,40 +77,40 @@
77
77
  },
78
78
  "dependencies": {
79
79
  "react-error-boundary": "^4.0.13",
80
- "@dxos/async": "0.7.4",
81
- "@dxos/client": "0.7.4",
82
- "@dxos/codec-protobuf": "0.7.4",
83
- "@dxos/debug": "0.7.4",
84
- "@dxos/echo-schema": "0.7.4",
85
- "@dxos/config": "0.7.4",
86
- "@dxos/echo-signals": "0.7.4",
87
- "@dxos/invariant": "0.7.4",
88
- "@dxos/node-std": "0.7.4",
89
- "@dxos/log": "0.7.4",
90
- "@dxos/protocols": "0.7.4",
91
- "@dxos/react-hooks": "0.7.4",
92
- "@dxos/util": "0.7.4"
80
+ "@dxos/async": "0.7.5-main.937ce75",
81
+ "@dxos/client": "0.7.5-main.937ce75",
82
+ "@dxos/codec-protobuf": "0.7.5-main.937ce75",
83
+ "@dxos/config": "0.7.5-main.937ce75",
84
+ "@dxos/debug": "0.7.5-main.937ce75",
85
+ "@dxos/echo-schema": "0.7.5-main.937ce75",
86
+ "@dxos/echo-signals": "0.7.5-main.937ce75",
87
+ "@dxos/invariant": "0.7.5-main.937ce75",
88
+ "@dxos/log": "0.7.5-main.937ce75",
89
+ "@dxos/react-hooks": "0.7.5-main.937ce75",
90
+ "@dxos/protocols": "0.7.5-main.937ce75",
91
+ "@dxos/util": "0.7.5-main.937ce75",
92
+ "@dxos/node-std": "0.7.5-main.937ce75"
93
93
  },
94
94
  "devDependencies": {
95
95
  "@types/react": "~18.2.0",
96
96
  "@types/react-dom": "~18.2.0",
97
97
  "react": "~18.2.0",
98
98
  "react-dom": "~18.2.0",
99
- "@dxos/react-ui": "0.7.4",
100
- "@dxos/random": "0.7.4",
101
- "@dxos/storybook-utils": "0.7.4",
102
- "@dxos/react-ui-syntax-highlighter": "0.7.4",
103
- "@dxos/test-utils": "0.7.4",
104
- "@dxos/react-ui-theme": "0.7.4"
99
+ "@dxos/react-ui": "0.7.5-main.937ce75",
100
+ "@dxos/random": "0.7.5-main.937ce75",
101
+ "@dxos/react-ui-syntax-highlighter": "0.7.5-main.937ce75",
102
+ "@dxos/react-ui-theme": "0.7.5-main.937ce75",
103
+ "@dxos/test-utils": "0.7.5-main.937ce75",
104
+ "@dxos/storybook-utils": "0.7.5-main.937ce75"
105
105
  },
106
106
  "peerDependencies": {
107
107
  "react": "~18.2.0",
108
108
  "react-dom": "~18.2.0"
109
109
  },
110
110
  "optionalDependencies": {
111
- "@dxos/random": "0.7.4",
112
- "@dxos/react-ui": "0.7.4",
113
- "@dxos/react-ui-theme": "0.7.4"
111
+ "@dxos/random": "0.7.5-main.937ce75",
112
+ "@dxos/react-ui-theme": "0.7.5-main.937ce75",
113
+ "@dxos/react-ui": "0.7.5-main.937ce75"
114
114
  },
115
115
  "publishConfig": {
116
116
  "access": "public"
@@ -6,7 +6,7 @@ import React, { useState, type FC, useEffect, useRef } from 'react';
6
6
 
7
7
  import { Client, type PublicKey } from '@dxos/client';
8
8
  import { TestBuilder, performInvitation } from '@dxos/client/testing';
9
- import { type AbstractTypedObject } from '@dxos/echo-schema';
9
+ import { type TypedObject } from '@dxos/echo-schema';
10
10
  import { registerSignalsRuntime } from '@dxos/echo-signals/react';
11
11
  import { faker } from '@dxos/random';
12
12
 
@@ -24,7 +24,7 @@ export type ClientRepeaterProps<P extends ClientRepeatedComponentProps> = {
24
24
  controls?: FC<ClientRepeaterControlsProps>;
25
25
  count?: number;
26
26
  clients?: Client[];
27
- types?: AbstractTypedObject[];
27
+ types?: TypedObject[];
28
28
  args?: Omit<P, 'id' | 'count'>;
29
29
  } & Pick<WithClientProviderProps, 'createIdentity' | 'createSpace' | 'onSpaceCreated'>;
30
30
 
@@ -71,7 +71,7 @@ export const ClientRepeater = <P extends ClientRepeatedComponentProps>(props: Cl
71
71
  const client = clients[0];
72
72
  const space = await client.spaces.create({ name: faker.commerce.productName() });
73
73
  setSpaceKey(space.key);
74
- await onSpaceCreated?.({ client, space });
74
+ await onSpaceCreated?.({ client, space }, {});
75
75
  await Promise.all(
76
76
  clients.slice(1).flatMap((client) => performInvitation({ host: space, guest: client.spaces })),
77
77
  );
@@ -0,0 +1,19 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { createContext, useContext } from 'react';
6
+
7
+ import { type Space } from '@dxos/client/echo';
8
+ import { raise } from '@dxos/debug';
9
+
10
+ export type ClientStory = {
11
+ space?: Space;
12
+ };
13
+
14
+ // TODO(wittjosiah): Add to multi-client as well.
15
+ export const ClientStory = createContext<ClientStory | undefined>(undefined);
16
+
17
+ export const useClientProvider = (): ClientStory => {
18
+ return useContext(ClientStory) ?? raise(new Error('Missing ClientStory'));
19
+ };
@@ -4,5 +4,6 @@
4
4
 
5
5
  export * from '@dxos/client/testing';
6
6
 
7
+ export * from './context';
7
8
  export * from './ClientRepeater';
8
9
  export * from './withClientProvider';
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Decorator } from '@storybook/react';
5
+ import { type StoryContext, type Decorator } from '@storybook/react';
6
6
  import React, { createContext, type PropsWithChildren, useContext, useEffect, useRef, useState } from 'react';
7
7
  import { type FallbackProps, ErrorBoundary as NativeErrorBoundary } from 'react-error-boundary';
8
8
 
@@ -10,18 +10,19 @@ import { Trigger } from '@dxos/async';
10
10
  import { type Client } from '@dxos/client';
11
11
  import { type Space } from '@dxos/client/echo';
12
12
  import { performInvitation, TestBuilder } from '@dxos/client/testing';
13
- import { raise } from '@dxos/debug';
14
13
  import { log } from '@dxos/log';
15
14
  import { type MaybePromise } from '@dxos/util';
16
15
 
16
+ import { ClientStory } from './context';
17
17
  import { ClientProvider, type ClientProviderProps } from '../client';
18
18
 
19
19
  type InitializeProps = {
20
20
  createIdentity?: boolean;
21
21
  createSpace?: boolean;
22
- onInitialized?: (client: Client) => MaybePromise<Record<string, any> | void>;
23
- onIdentityCreated?: (props: { client: Client }) => MaybePromise<Record<string, any> | void>;
24
- onSpaceCreated?: (props: { client: Client; space: Space }) => MaybePromise<Record<string, any> | void>;
22
+ onInitialized?: (client: Client) => MaybePromise<void>;
23
+ onIdentityCreated?: (props: { client: Client }) => MaybePromise<void>;
24
+ // NOTE: context must be untyped until ClientRepeater is removed.
25
+ onSpaceCreated?: (props: { client: Client; space: Space }, context: StoryContext | any) => MaybePromise<void>;
25
26
  };
26
27
 
27
28
  /**
@@ -30,35 +31,24 @@ type InitializeProps = {
30
31
  const initializeClient = async (
31
32
  client: Client,
32
33
  { createIdentity, createSpace, onSpaceCreated, onIdentityCreated, onInitialized }: InitializeProps,
33
- ): Promise<StoryClientProps> => {
34
- const clientData = await onInitialized?.(client);
34
+ context: StoryContext,
35
+ ): Promise<ClientStory> => {
36
+ await onInitialized?.(client);
35
37
 
36
- let identityData: Record<string, any> | void = {};
37
38
  if (createIdentity || createSpace) {
38
39
  if (!client.halo.identity.get()) {
39
40
  await client.halo.createIdentity();
40
- identityData = await onIdentityCreated?.({ client });
41
+ await onIdentityCreated?.({ client });
41
42
  }
42
43
  }
43
44
 
44
45
  let space: Space | undefined;
45
- let spaceData: Record<string, any> | void = {};
46
46
  if (createSpace) {
47
47
  space = await client.spaces.create({ name: 'Test Space' });
48
- spaceData = await onSpaceCreated?.({ client, space });
48
+ await onSpaceCreated?.({ client, space }, context);
49
49
  }
50
50
 
51
- return { space, ...clientData, ...identityData, ...spaceData };
52
- };
53
-
54
- type StoryClientProps<T extends Record<string, any> = Record<string, unknown>> = T & { space?: Space };
55
-
56
- // TODO(wittjosiah): Add to multi-client as well.
57
- const StoryClientContext = createContext<StoryClientProps | undefined>(undefined);
58
-
59
- export const useStoryClientData = <T extends Record<string, any> = Record<string, unknown>>() => {
60
- const data = useContext(StoryClientContext) ?? raise(new Error('Missing withClientProvider decorator.'));
61
- return data as StoryClientProps<T>;
51
+ return { space };
62
52
  };
63
53
 
64
54
  export type WithClientProviderProps = InitializeProps & Omit<ClientProviderProps, 'onInitialized'>;
@@ -74,27 +64,30 @@ export const withClientProvider = ({
74
64
  onInitialized,
75
65
  ...props
76
66
  }: WithClientProviderProps = {}): Decorator => {
77
- return (Story) => {
78
- const [data, setData] = useState<StoryClientProps>({});
79
-
67
+ return (Story, context) => {
68
+ const [data, setData] = useState<ClientStory>({});
80
69
  const handleInitialized = async (client: Client) => {
81
- setData(
82
- await initializeClient(client, {
70
+ const data = await initializeClient(
71
+ client,
72
+ {
83
73
  createIdentity,
84
74
  createSpace,
85
75
  onSpaceCreated,
86
76
  onIdentityCreated,
87
77
  onInitialized,
88
- }),
78
+ },
79
+ context,
89
80
  );
81
+
82
+ setData(data);
90
83
  };
91
84
 
92
85
  return (
93
86
  <ErrorBoundary>
94
87
  <ClientProvider onInitialized={handleInitialized} {...props}>
95
- <StoryClientContext.Provider value={data}>
88
+ <ClientStory.Provider value={data}>
96
89
  <Story />
97
- </StoryClientContext.Provider>
90
+ </ClientStory.Provider>
98
91
  </ClientProvider>
99
92
  </ErrorBoundary>
100
93
  );
@@ -125,7 +118,7 @@ export const withMultiClientProvider = ({
125
118
  onInitialized,
126
119
  ...props
127
120
  }: WithMultiClientProviderProps): Decorator => {
128
- return (Story) => {
121
+ return (Story, context) => {
129
122
  const builder = useRef(new TestBuilder());
130
123
  const hostRef = useRef<Client>();
131
124
  const spaceReady = useRef(new Trigger<Space | undefined>());
@@ -137,17 +130,22 @@ export const withMultiClientProvider = ({
137
130
  if (createSpace) {
138
131
  if (!hostRef.current) {
139
132
  hostRef.current = client;
140
- const { space } = await initializeClient(client, {
141
- createIdentity,
142
- createSpace,
143
- onSpaceCreated,
144
- onIdentityCreated,
145
- onInitialized,
146
- });
133
+ const { space } = await initializeClient(
134
+ client,
135
+ {
136
+ createIdentity,
137
+ createSpace,
138
+ onSpaceCreated,
139
+ onIdentityCreated,
140
+ onInitialized,
141
+ },
142
+ context,
143
+ );
144
+
147
145
  spaceReady.current.wake(space);
148
146
  log.info('inviting', { index });
149
147
  } else {
150
- await initializeClient(client, { createIdentity, onInitialized });
148
+ await initializeClient(client, { createIdentity, onInitialized }, context);
151
149
  const space = await spaceReady.current.wait();
152
150
  if (space) {
153
151
  log.info('joining', { index });
package/tsconfig.json CHANGED
@@ -12,5 +12,63 @@
12
12
  "playwright",
13
13
  "src"
14
14
  ],
15
- "references": []
15
+ "references": [
16
+ {
17
+ "path": "../../common/async"
18
+ },
19
+ {
20
+ "path": "../../common/codec-protobuf"
21
+ },
22
+ {
23
+ "path": "../../common/debug"
24
+ },
25
+ {
26
+ "path": "../../common/invariant"
27
+ },
28
+ {
29
+ "path": "../../common/log"
30
+ },
31
+ {
32
+ "path": "../../common/node-std"
33
+ },
34
+ {
35
+ "path": "../../common/random"
36
+ },
37
+ {
38
+ "path": "../../common/storybook-utils"
39
+ },
40
+ {
41
+ "path": "../../common/test-utils"
42
+ },
43
+ {
44
+ "path": "../../common/util"
45
+ },
46
+ {
47
+ "path": "../../core/echo/echo-schema"
48
+ },
49
+ {
50
+ "path": "../../core/echo/echo-signals"
51
+ },
52
+ {
53
+ "path": "../../core/protocols"
54
+ },
55
+ {
56
+ "path": "../../ui/primitives/react-hooks"
57
+ },
58
+ {
59
+ "path": "../../ui/react-ui"
60
+ },
61
+ {
62
+ "path": "../../ui/react-ui-syntax-highlighter"
63
+ },
64
+ {
65
+ "path": "../../ui/react-ui-theme"
66
+ },
67
+ {
68
+ "path": "../client"
69
+ },
70
+ {
71
+ "path": "../config"
72
+ }
73
+ ]
16
74
  }