Skip to content

Commit 8709ebd

Browse files
committed
Wrap ConnectionScopeProvider in ConnectionsPlayersProvider if not present
1 parent ffed1ae commit 8709ebd

4 files changed

Lines changed: 71 additions & 10 deletions

File tree

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ If you needed to show personalised buttons for each connection or each human pla
877877

878878
### Connection scope
879879

880-
You can show different buttons to each connection by wrapping a sub-tree in a `ConnectionScopeProvider` and using the `useConnectionScope` hook anywhere within that sub-tree to access the connection object.
880+
You can show different buttons to each connection by wrapping a sub-tree in a `ConnectionScopeProvider`, then using the `useConnectionScope` hook anywhere within that sub-tree to access the connection object.
881881

882882
You don't need to specify the button's UCID in the scope - the correct UCID will be injected automatically.
883883

@@ -909,7 +909,7 @@ function UserNameButton() {
909909

910910
### Human player scope
911911

912-
You can show different buttons to each human player on track by wrapping a sub-tree in a `ConnectionScopeProvider` and `HumanPlayerScopeProvider`, then using the `useHumanPlayerScope` hook anywhere within that sub-tree to access the player object.
912+
You can show different buttons to each human player on track by wrapping a sub-tree in a `HumanPlayerScopeProvider`, then using the `useHumanPlayerScope` hook anywhere within that sub-tree to access the player object.
913913

914914
You don't need to specify the button's UCID in the scope - the correct UCID will be injected automatically.
915915

@@ -922,11 +922,9 @@ import {
922922

923923
function App() {
924924
return (
925-
<ConnectionScopeProvider>
926-
<HumanPlayerScopeProvider>
927-
<PlayerNameButton />
928-
</HumanPlayerScopeProvider>
929-
</ConnectionScopeProvider>
925+
<HumanPlayerScopeProvider>
926+
<PlayerNameButton />
927+
</HumanPlayerScopeProvider>
930928
);
931929
}
932930

src/lib/hooks/ConnectionsPlayersProvider.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ export function ConnectionsPlayersProvider({
197197
);
198198
}
199199

200+
/** @internal */
201+
export function useConnectionsPlayersMaybeContext() {
202+
return useContext(ConnectionsPlayersContext);
203+
}
204+
200205
export function useConnections(): ReadonlyMap<number, Connection> & {
201206
map: <Item>(
202207
callback: (connection: Connection, key: number, map: Connection[]) => Item,

src/lib/scopes/connectionScope.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,41 @@ import { createScope, molecule, use } from 'bunshi';
22
import { ScopeProvider, useMolecule } from 'bunshi/react';
33
import { atom, useAtomValue } from 'jotai';
44
import { type ReactNode } from 'react';
5-
import { type Connection, useConnections } from 'react-node-insim';
5+
import {
6+
type Connection,
7+
ConnectionsPlayersProvider,
8+
useConnections,
9+
} from 'react-node-insim';
10+
11+
import { useConnectionsPlayersMaybeContext } from '../hooks/ConnectionsPlayersProvider';
612

713
export const ConnectionScope = createScope<Connection | null>(null);
814

915
export function ConnectionScopeProvider({ children }: { children: ReactNode }) {
16+
const connectionsPlayersContext = useConnectionsPlayersMaybeContext();
17+
18+
if (!connectionsPlayersContext) {
19+
return (
20+
<ConnectionsPlayersProvider>
21+
<ConnectionScopeProviderWithConnectionsPlayersProvider>
22+
{children}
23+
</ConnectionScopeProviderWithConnectionsPlayersProvider>
24+
</ConnectionsPlayersProvider>
25+
);
26+
}
27+
28+
return (
29+
<ConnectionScopeProviderWithConnectionsPlayersProvider>
30+
{children}
31+
</ConnectionScopeProviderWithConnectionsPlayersProvider>
32+
);
33+
}
34+
35+
export function ConnectionScopeProviderWithConnectionsPlayersProvider({
36+
children,
37+
}: {
38+
children: ReactNode;
39+
}) {
1040
const connections = useConnections();
1141

1242
return (

src/lib/scopes/humanPlayerScope.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,43 @@ import { atom, useAtomValue } from 'jotai';
44
import { PlayerType } from 'node-insim/packets';
55
import { type ReactNode } from 'react';
66
import type { Player } from 'react-node-insim';
7-
import { useConnectionScope } from 'react-node-insim';
8-
import { usePlayers } from 'react-node-insim';
7+
import {
8+
ConnectionScopeProvider,
9+
useConnectionMaybeScope,
10+
useConnectionScope,
11+
usePlayers,
12+
} from 'react-node-insim';
913

1014
export const HumanPlayerScope = createScope<Player | null>(null);
1115

1216
export function HumanPlayerScopeProvider({
1317
children,
1418
}: {
1519
children: ReactNode;
20+
}) {
21+
const connection = useConnectionMaybeScope();
22+
23+
if (!connection) {
24+
return (
25+
<ConnectionScopeProvider>
26+
<HumanPlayerScopeProviderInConnectionScope>
27+
{children}
28+
</HumanPlayerScopeProviderInConnectionScope>
29+
</ConnectionScopeProvider>
30+
);
31+
}
32+
33+
return (
34+
<HumanPlayerScopeProviderInConnectionScope>
35+
{children}
36+
</HumanPlayerScopeProviderInConnectionScope>
37+
);
38+
}
39+
40+
function HumanPlayerScopeProviderInConnectionScope({
41+
children,
42+
}: {
43+
children: ReactNode;
1644
}) {
1745
const connection = useConnectionScope();
1846
const players = usePlayers();

0 commit comments

Comments
 (0)