@player-devtools/client 0.14.0-next.0 → 0.14.0

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.
@@ -1,234 +0,0 @@
1
- import React, { useRef } from "react";
2
- import type {
3
- MessengerOptions,
4
- ExtensionSupportedEvents,
5
- } from "@player-devtools/types";
6
- import { DataController, Flow, useReactPlayer } from "@player-ui/react";
7
- import { useEffect } from "react";
8
- import { ErrorBoundary } from "react-error-boundary";
9
- import {
10
- Card,
11
- CardBody,
12
- CardHeader,
13
- ChakraProvider,
14
- Container,
15
- extendTheme,
16
- Flex,
17
- FormControl,
18
- FormLabel,
19
- Heading,
20
- Select,
21
- Text,
22
- type ThemeConfig,
23
- } from "@chakra-ui/react";
24
-
25
- import { ThemeProvider, useDarkMode } from "@devtools-ds/themes";
26
-
27
- import { INITIAL_FLOW } from "../constants";
28
- import { PLAYER_PLUGINS, PUBSUB_PLUGIN } from "../plugins";
29
- import { useExtensionState } from "../state";
30
- import { flowDiff } from "../helpers/flowDiff";
31
-
32
- const fallbackRender: ErrorBoundary["props"]["fallbackRender"] = ({
33
- error,
34
- }) => {
35
- return (
36
- <Container centerContent>
37
- <Card>
38
- <CardHeader>
39
- <Heading>Ops, something went wrong.</Heading>
40
- </CardHeader>
41
- <CardBody>
42
- <Text as="pre">{error.message}</Text>
43
- </CardBody>
44
- </Card>
45
- </Container>
46
- );
47
- };
48
-
49
- /**
50
- * Panel Component
51
- *
52
- * This component serves as the main container for the devtools plugin content defined by plugin authors using Player-UI DSL.
53
- *
54
- * Props:
55
- * - `communicationLayer`: An object that allows communication between the devtools and the Player-UI plugins,
56
- * enabling the exchange of data and events.
57
- *
58
- * Features:
59
- * - Error Handling: Utilizes the `ErrorBoundary` component from `react-error-boundary` to gracefully handle and display errors
60
- * that may occur during the rendering of the plugin's content.
61
- * - State Management: Integrates with custom hooks such as `useExtensionState` to manage the state of the plugin and its components.
62
- * - Player Integration: Uses the `useReactPlayer` hook from `player-ui/react` to render interactive player components based on the
63
- * DSL defined by the plugin authors.
64
- *
65
- * Example Usage:
66
- * ```tsx
67
- * <Panel communicationLayer={myCommunicationLayer} />
68
- * ```
69
- *
70
- * Note: The `communicationLayer` prop is essential for the proper functioning of the `Panel` component, as it enables the necessary
71
- * communication and data exchange with the player-ui/react library.
72
- */
73
- export const Panel: React.FC<{
74
- /** the communication layer to use for the extension */
75
- readonly communicationLayer: Pick<
76
- MessengerOptions<ExtensionSupportedEvents>,
77
- "sendMessage" | "addListener" | "removeListener"
78
- >;
79
- baseTheme?: Record<string, unknown>;
80
- }> = ({ communicationLayer, baseTheme }) => {
81
- const { state, selectPlayer, selectPlugin, handleInteraction } =
82
- useExtensionState({
83
- communicationLayer,
84
- });
85
-
86
- const { reactPlayer, playerState } = useReactPlayer({
87
- plugins: PLAYER_PLUGINS,
88
- });
89
-
90
- const dataController = useRef<WeakRef<DataController> | null>(null);
91
-
92
- const currentFlow = useRef<Flow | null>(null);
93
-
94
- useEffect(() => {
95
- reactPlayer.player.hooks.dataController.tap("devtools-panel", (d) => {
96
- dataController.current = new WeakRef(d);
97
- });
98
- }, [reactPlayer]);
99
-
100
- useEffect(() => {
101
- // we subscribe to all messages from the devtools plugin
102
- // so the plugin author can define their own events
103
- PUBSUB_PLUGIN.subscribe("*", (type: string, payload: string) => {
104
- handleInteraction({
105
- type,
106
- payload,
107
- });
108
- });
109
- }, []);
110
-
111
- useEffect(() => {
112
- const { player, plugin } = state.current;
113
-
114
- const flow =
115
- player && plugin
116
- ? state.players[player]?.plugins?.[plugin]?.flow || INITIAL_FLOW
117
- : INITIAL_FLOW;
118
-
119
- if (!currentFlow.current) {
120
- currentFlow.current = flow;
121
- reactPlayer.start(flow);
122
- return;
123
- }
124
-
125
- const diff = flowDiff({
126
- curr: currentFlow.current as Flow,
127
- next: flow,
128
- });
129
-
130
- if (diff) {
131
- const { change, value } = diff;
132
-
133
- if (change === "flow") {
134
- currentFlow.current = value;
135
- reactPlayer.start(value);
136
- } else if (change === "data") {
137
- if (dataController.current) {
138
- dataController.current.deref()?.set(value as Record<string, unknown>);
139
- } else {
140
- reactPlayer.start(flow);
141
- }
142
- }
143
- }
144
- }, [reactPlayer, state]);
145
-
146
- const isDarkMode = useDarkMode();
147
- const colorMode = isDarkMode ? "dark" : "light";
148
-
149
- const config: ThemeConfig = {
150
- initialColorMode: colorMode,
151
- useSystemColorMode: true,
152
- };
153
- const theme = extendTheme({ config, ...baseTheme });
154
-
155
- const Component = reactPlayer.Component as React.FC;
156
-
157
- return (
158
- <ChakraProvider resetCSS={false} theme={theme}>
159
- <ThemeProvider theme="chrome" colorScheme={colorMode}>
160
- <ErrorBoundary fallbackRender={fallbackRender}>
161
- <Flex
162
- direction="column"
163
- h="100vh"
164
- alignItems={"normal"}
165
- overflow="scroll"
166
- >
167
- {state.current.player ? (
168
- <Flex direction="column" gap="20px">
169
- <Flex gap="16px">
170
- <FormControl>
171
- <FormLabel>Player</FormLabel>
172
- <Select
173
- id="player"
174
- value={state.current.player || ""}
175
- onChange={(event) => selectPlayer(event.target.value)}
176
- >
177
- {Object.keys(state.players).map((playerID) => (
178
- <option key={playerID} value={playerID}>
179
- {playerID}
180
- </option>
181
- ))}
182
- </Select>
183
- </FormControl>
184
- <FormControl>
185
- <FormLabel>Plugin</FormLabel>
186
- <Select
187
- id="plugin"
188
- value={state.current.plugin || ""}
189
- onChange={(event) => selectPlugin(event.target.value)}
190
- >
191
- {Object.keys(
192
- state.players[state.current.player]?.plugins ?? [],
193
- ).map((pluginID) => (
194
- <option key={pluginID} value={pluginID}>
195
- {pluginID}
196
- </option>
197
- ))}
198
- </Select>
199
- </FormControl>
200
- </Flex>
201
- <Flex width="100%">
202
- {playerState.status === "error" ? (
203
- fallbackRender({
204
- error: playerState.error,
205
- resetErrorBoundary: () => {},
206
- })
207
- ) : (
208
- <Component />
209
- )}
210
- </Flex>
211
- <details>
212
- <summary>Debug</summary>
213
- <pre style={{ maxHeight: "30vh", overflow: "scroll" }}>
214
- {JSON.stringify(state, null, 2)}
215
- </pre>
216
- </details>
217
- </Flex>
218
- ) : (
219
- <Flex justifyContent="center" padding="6">
220
- <Text>
221
- No Player-UI instance or devtools plugin detected. Visit{" "}
222
- <a href="https://player-ui.github.io/">
223
- https://player-ui.github.io/
224
- </a>{" "}
225
- for more info.
226
- </Text>
227
- </Flex>
228
- )}
229
- </Flex>
230
- </ErrorBoundary>
231
- </ThemeProvider>
232
- </ChakraProvider>
233
- );
234
- };
@@ -1,34 +0,0 @@
1
- import DevtoolsUIAssetsPlugin from "@devtools-ui/plugin";
2
- import { PubSubPlugin } from "@player-ui/pubsub-plugin";
3
- import { CommonExpressionsPlugin } from "@player-ui/common-expressions-plugin";
4
- import { CommonTypesPlugin } from "@player-ui/common-types-plugin";
5
- import { DataChangeListenerPlugin } from "@player-ui/data-change-listener-plugin";
6
- import {
7
- ConsoleLogger,
8
- Player,
9
- PlayerPlugin,
10
- ReactPlayer,
11
- type ReactPlayerPlugin,
12
- } from "@player-ui/react";
13
-
14
- class LogForwarder implements PlayerPlugin, ReactPlayerPlugin {
15
- name = "Log";
16
- apply(player: Player) {
17
- player.logger.addHandler(new ConsoleLogger("trace", console));
18
- }
19
-
20
- applyReact(reactPlayer: ReactPlayer) {
21
- this.apply(reactPlayer.player);
22
- }
23
- }
24
-
25
- export const PUBSUB_PLUGIN = new PubSubPlugin();
26
-
27
- export const PLAYER_PLUGINS: ReactPlayerPlugin[] = [
28
- new CommonTypesPlugin(),
29
- new CommonExpressionsPlugin(),
30
- new DataChangeListenerPlugin(),
31
- new DevtoolsUIAssetsPlugin(),
32
- new LogForwarder(),
33
- PUBSUB_PLUGIN,
34
- ];
@@ -1,33 +0,0 @@
1
- import type { CommunicationLayerMethods } from "@player-devtools/types";
2
- import { useEffect, useMemo, useSyncExternalStore } from "react";
3
-
4
- import { createExtensionClient } from "./client";
5
-
6
- /**
7
- * Thin React adapter over `createExtensionClient`.
8
- *
9
- * Creates the client once per `communicationLayer` identity, subscribes to
10
- * state via `useSyncExternalStore`, and tears down the Messenger on unmount.
11
- */
12
- export const useExtensionState = ({
13
- communicationLayer,
14
- }: {
15
- /** the communication layer to use for the extension */
16
- communicationLayer: CommunicationLayerMethods;
17
- }) => {
18
- const client = useMemo(
19
- () => createExtensionClient(communicationLayer),
20
- [communicationLayer],
21
- );
22
-
23
- useEffect(() => () => client.destroy(), [client]);
24
-
25
- const state = useSyncExternalStore(client.subscribe, client.getState);
26
-
27
- return {
28
- state,
29
- selectPlayer: client.selectPlayer,
30
- selectPlugin: client.selectPlugin,
31
- handleInteraction: client.handleInteraction,
32
- };
33
- };
@@ -1,21 +0,0 @@
1
- import type { Flow } from "@player-ui/react";
2
- /**
3
- * Compares two Flow objects and identifies if there's a change in their structure or data.
4
- *
5
- * This function takes two Flow objects as input, `curr` (current) and `next` (next), and compares them
6
- * to determine if there's a change in the flow's structure or its data. If there's a change in the flow's
7
- * structure (excluding the `data` property), it returns an object indicating a "flow" change along with the
8
- * new flow. If there's a change in the `data` property, it returns an object indicating a "data" change along
9
- * with the new data. If there are no changes, it returns null.
10
- */
11
- export declare const flowDiff: ({ curr, next, }: {
12
- curr: Flow;
13
- next: Flow;
14
- }) => {
15
- change: "data";
16
- value: Flow["data"];
17
- } | {
18
- change: "flow";
19
- value: Flow;
20
- } | null;
21
- //# sourceMappingURL=flowDiff.d.ts.map
@@ -1,32 +0,0 @@
1
- import React from "react";
2
- import type { MessengerOptions, ExtensionSupportedEvents } from "@player-devtools/types";
3
- /**
4
- * Panel Component
5
- *
6
- * This component serves as the main container for the devtools plugin content defined by plugin authors using Player-UI DSL.
7
- *
8
- * Props:
9
- * - `communicationLayer`: An object that allows communication between the devtools and the Player-UI plugins,
10
- * enabling the exchange of data and events.
11
- *
12
- * Features:
13
- * - Error Handling: Utilizes the `ErrorBoundary` component from `react-error-boundary` to gracefully handle and display errors
14
- * that may occur during the rendering of the plugin's content.
15
- * - State Management: Integrates with custom hooks such as `useExtensionState` to manage the state of the plugin and its components.
16
- * - Player Integration: Uses the `useReactPlayer` hook from `player-ui/react` to render interactive player components based on the
17
- * DSL defined by the plugin authors.
18
- *
19
- * Example Usage:
20
- * ```tsx
21
- * <Panel communicationLayer={myCommunicationLayer} />
22
- * ```
23
- *
24
- * Note: The `communicationLayer` prop is essential for the proper functioning of the `Panel` component, as it enables the necessary
25
- * communication and data exchange with the player-ui/react library.
26
- */
27
- export declare const Panel: React.FC<{
28
- /** the communication layer to use for the extension */
29
- readonly communicationLayer: Pick<MessengerOptions<ExtensionSupportedEvents>, "sendMessage" | "addListener" | "removeListener">;
30
- baseTheme?: Record<string, unknown>;
31
- }>;
32
- //# sourceMappingURL=index.d.ts.map
@@ -1,5 +0,0 @@
1
- import { PubSubPlugin } from "@player-ui/pubsub-plugin";
2
- import { type ReactPlayerPlugin } from "@player-ui/react";
3
- export declare const PUBSUB_PLUGIN: PubSubPlugin;
4
- export declare const PLAYER_PLUGINS: ReactPlayerPlugin[];
5
- //# sourceMappingURL=index.d.ts.map
@@ -1,21 +0,0 @@
1
- import type { CommunicationLayerMethods } from "@player-devtools/types";
2
- /**
3
- * Thin React adapter over `createExtensionClient`.
4
- *
5
- * Creates the client once per `communicationLayer` identity, subscribes to
6
- * state via `useSyncExternalStore`, and tears down the Messenger on unmount.
7
- */
8
- export declare const useExtensionState: ({ communicationLayer, }: {
9
- /** the communication layer to use for the extension */
10
- communicationLayer: CommunicationLayerMethods;
11
- }) => {
12
- state: import("@player-devtools/types").ExtensionState;
13
- selectPlayer: (playerID: string) => void;
14
- selectPlugin: (pluginID: string) => void;
15
- handleInteraction: (interaction: {
16
- type: string;
17
- payload?: string;
18
- target?: string;
19
- }) => void;
20
- };
21
- //# sourceMappingURL=index.d.ts.map