@player-tools/devtools-client 0.5.3-next.0 → 0.6.0-next.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.
@@ -0,0 +1,32 @@
1
+ import type { ExtensionState } from "@player-tools/devtools-types";
2
+ import type { Flow } from "@player-ui/react";
3
+
4
+ export const INITIAL_FLOW: Flow = {
5
+ id: "initial-flow",
6
+ views: [
7
+ {
8
+ id: "view-1",
9
+ type: "text",
10
+ value: "connecting...",
11
+ },
12
+ ],
13
+ navigation: {
14
+ BEGIN: "FLOW_1",
15
+ FLOW_1: {
16
+ startState: "VIEW_1",
17
+ VIEW_1: {
18
+ state_type: "VIEW",
19
+ ref: "view-1",
20
+ transitions: {},
21
+ },
22
+ },
23
+ },
24
+ };
25
+
26
+ export const INITIAL_EXTENSION_STATE: ExtensionState = {
27
+ current: {
28
+ player: null,
29
+ plugin: null,
30
+ },
31
+ players: {},
32
+ };
@@ -0,0 +1,72 @@
1
+ import { describe, test, expect } from "vitest";
2
+ import type { Flow } from "@player-ui/react";
3
+ import { flowDiff } from "../flowDiff";
4
+
5
+ const mockFlow1: Flow = {
6
+ id: "flow",
7
+ views: [
8
+ {
9
+ id: "view",
10
+ type: "info",
11
+ },
12
+ ],
13
+ data: {
14
+ foo: "bar",
15
+ },
16
+ navigation: {
17
+ BEGIN: "BEGIN",
18
+ },
19
+ };
20
+
21
+ const mockFlow2: Flow = {
22
+ id: "flow",
23
+ views: [
24
+ {
25
+ id: "view",
26
+ type: "info",
27
+ },
28
+ ],
29
+ data: {
30
+ foo: "bar",
31
+ something: "else",
32
+ },
33
+ navigation: {
34
+ BEGIN: "BEGIN",
35
+ },
36
+ };
37
+
38
+ const mockFlow3: Flow = {
39
+ id: "another_flow",
40
+ views: [
41
+ {
42
+ id: "view",
43
+ type: "info",
44
+ },
45
+ ],
46
+ data: {
47
+ foo: "bar",
48
+ },
49
+ navigation: {
50
+ BEGIN: "BEGIN",
51
+ },
52
+ };
53
+
54
+ describe("flowDiff", () => {
55
+ test("returns null if no changes", () => {
56
+ const result = flowDiff({ curr: mockFlow1, next: mockFlow1 });
57
+
58
+ expect(result).toBeNull();
59
+ });
60
+
61
+ test("returns flow change if base flow is different", () => {
62
+ const result = flowDiff({ curr: mockFlow1, next: mockFlow3 });
63
+
64
+ expect(result).toEqual({ change: "flow", value: mockFlow3 });
65
+ });
66
+
67
+ test("returns data change if data is different", () => {
68
+ const result = flowDiff({ curr: mockFlow1, next: mockFlow2 });
69
+
70
+ expect(result).toEqual({ change: "data", value: mockFlow2.data });
71
+ });
72
+ });
@@ -0,0 +1,44 @@
1
+ import type { Flow } from "@player-ui/react";
2
+ import { dequal } from "dequal";
3
+
4
+ /**
5
+ * Compares two Flow objects and identifies if there's a change in their structure or data.
6
+ *
7
+ * This function takes two Flow objects as input, `curr` (current) and `next` (next), and compares them
8
+ * to determine if there's a change in the flow's structure or its data. If there's a change in the flow's
9
+ * structure (excluding the `data` property), it returns an object indicating a "flow" change along with the
10
+ * new flow. If there's a change in the `data` property, it returns an object indicating a "data" change along
11
+ * with the new data. If there are no changes, it returns null.
12
+ */
13
+ export const flowDiff = ({
14
+ curr,
15
+ next,
16
+ }: {
17
+ curr: Flow;
18
+ next: Flow;
19
+ }):
20
+ | { change: "data"; value: Flow["data"] }
21
+ | { change: "flow"; value: Flow }
22
+ | null => {
23
+ // compare flows except for the `data` property
24
+ const currCopy = { ...curr, data: null };
25
+ const nextCopy = { ...next, data: null };
26
+
27
+ const baseFlowIsEqual = dequal(currCopy, nextCopy);
28
+
29
+ if (!baseFlowIsEqual) {
30
+ return { change: "flow", value: next };
31
+ }
32
+
33
+ // compare data
34
+ const currData = curr.data;
35
+ const nextData = next.data;
36
+
37
+ const dataIsEqual = dequal(currData, nextData);
38
+
39
+ if (!dataIsEqual) {
40
+ return { change: "data", value: nextData };
41
+ }
42
+
43
+ return null;
44
+ };
package/src/index.ts CHANGED
@@ -1,3 +1 @@
1
- export * from "./redux";
2
- export * from "./rpc";
3
- export * from "@player-tools/devtools-common";
1
+ export { Panel } from "./panel";
@@ -0,0 +1,212 @@
1
+ import React, { useRef } from "react";
2
+ import type {
3
+ MessengerOptions,
4
+ ExtensionSupportedEvents,
5
+ } from "@player-tools/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
+ Flex,
16
+ FormControl,
17
+ FormLabel,
18
+ Heading,
19
+ HStack,
20
+ Select,
21
+ Text,
22
+ VStack,
23
+ } from "@chakra-ui/react";
24
+ import { ThemeProvider } from "@devtools-ds/themes";
25
+
26
+ import { INITIAL_FLOW } from "../constants";
27
+ import { PLAYER_PLUGINS, PUBSUB_PLUGIN } from "../plugins";
28
+ import { useExtensionState } from "../state";
29
+ import { flowDiff } from "../helpers/flowDiff";
30
+ import { theme } from "./theme";
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
+ </Card>
42
+ <CardBody>
43
+ <Text as="pre">{error.message}</Text>
44
+ </CardBody>
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 = ({
74
+ communicationLayer,
75
+ }: {
76
+ /** the communication layer to use for the extension */
77
+ readonly communicationLayer: Pick<
78
+ MessengerOptions<ExtensionSupportedEvents>,
79
+ "sendMessage" | "addListener" | "removeListener"
80
+ >;
81
+ }) => {
82
+ const { state, selectPlayer, selectPlugin, handleInteraction } =
83
+ useExtensionState({
84
+ communicationLayer,
85
+ });
86
+
87
+ const { reactPlayer } = useReactPlayer({
88
+ plugins: PLAYER_PLUGINS,
89
+ });
90
+
91
+ const dataController = useRef<WeakRef<DataController> | null>(null);
92
+
93
+ const currentFlow = useRef<Flow | null>(null);
94
+
95
+ useEffect(() => {
96
+ reactPlayer.player.hooks.dataController.tap("devtools-panel", (d) => {
97
+ dataController.current = new WeakRef(d);
98
+ });
99
+ }, [reactPlayer]);
100
+
101
+ useEffect(() => {
102
+ // we subscribe to all messages from the devtools plugin
103
+ // so the plugin author can define their own events
104
+ PUBSUB_PLUGIN.subscribe("*", (type: string, payload: string) => {
105
+ handleInteraction({
106
+ type,
107
+ payload,
108
+ });
109
+ });
110
+ }, []);
111
+
112
+ useEffect(() => {
113
+ const { player, plugin } = state.current;
114
+
115
+ const flow =
116
+ player && plugin
117
+ ? state.players[player]?.plugins?.[plugin]?.flow || INITIAL_FLOW
118
+ : INITIAL_FLOW;
119
+
120
+ if (!currentFlow.current) {
121
+ currentFlow.current = flow;
122
+ reactPlayer.start(flow);
123
+ return;
124
+ }
125
+
126
+ const diff = flowDiff({
127
+ curr: currentFlow.current as Flow,
128
+ next: flow,
129
+ });
130
+
131
+ if (diff) {
132
+ const { change, value } = diff;
133
+
134
+ if (change === "flow") {
135
+ currentFlow.current = value;
136
+ reactPlayer.start(value);
137
+ } else if (change === "data") {
138
+ dataController.current
139
+ ? dataController.current
140
+ .deref()
141
+ ?.set(value as Record<string, unknown>)
142
+ : reactPlayer.start(flow);
143
+ }
144
+ }
145
+ }, [reactPlayer, state]);
146
+
147
+ const Component = reactPlayer.Component as React.FC;
148
+
149
+ return (
150
+ <ChakraProvider theme={theme}>
151
+ <ThemeProvider colorScheme="dark">
152
+ <ErrorBoundary fallbackRender={fallbackRender}>
153
+ <VStack w="100vw" h="100vh">
154
+ {state.current.player ? (
155
+ <Flex direction="column" marginTop="4">
156
+ <HStack spacing="4">
157
+ <FormControl>
158
+ <FormLabel>Player</FormLabel>
159
+ <Select
160
+ id="player"
161
+ value={state.current.player || ""}
162
+ onChange={(event) => selectPlayer(event.target.value)}
163
+ >
164
+ {Object.keys(state.players).map((playerID) => (
165
+ <option key={playerID} value={playerID}>
166
+ {playerID}
167
+ </option>
168
+ ))}
169
+ </Select>
170
+ </FormControl>
171
+ <FormControl>
172
+ <FormLabel>Plugin</FormLabel>
173
+ <Select
174
+ id="plugin"
175
+ value={state.current.plugin || ""}
176
+ onChange={(event) => selectPlugin(event.target.value)}
177
+ >
178
+ {Object.keys(
179
+ state.players[state.current.player].plugins
180
+ ).map((pluginID) => (
181
+ <option key={pluginID} value={pluginID}>
182
+ {pluginID}
183
+ </option>
184
+ ))}
185
+ </Select>
186
+ </FormControl>
187
+ </HStack>
188
+ <Container marginY="6">
189
+ <Component />
190
+ </Container>
191
+ <details>
192
+ <summary>Debug</summary>
193
+ <pre>{JSON.stringify(state, null, 2)}</pre>
194
+ </details>
195
+ </Flex>
196
+ ) : (
197
+ <Flex justifyContent="center" padding="6">
198
+ <Text>
199
+ No Player-UI instance or devtools plugin detected. Visit{" "}
200
+ <a href="https://player-ui.github.io/">
201
+ https://player-ui.github.io/
202
+ </a>{" "}
203
+ for more info.
204
+ </Text>
205
+ </Flex>
206
+ )}
207
+ </VStack>
208
+ </ErrorBoundary>
209
+ </ThemeProvider>
210
+ </ChakraProvider>
211
+ );
212
+ };
@@ -0,0 +1,8 @@
1
+ import { extendTheme, type ThemeConfig } from "@chakra-ui/react";
2
+
3
+ const config: ThemeConfig = {
4
+ initialColorMode: "dark",
5
+ useSystemColorMode: false,
6
+ };
7
+
8
+ export const theme = extendTheme({ config });
@@ -0,0 +1,10 @@
1
+ import DevtoolsUIAssetsPlugin from "@devtools-ui/plugin";
2
+ import { PubSubPlugin } from "@player-ui/pubsub-plugin";
3
+ import type { ReactPlayerPlugin } from "@player-ui/react";
4
+
5
+ export const PUBSUB_PLUGIN = new PubSubPlugin();
6
+
7
+ export const PLAYER_PLUGINS: ReactPlayerPlugin[] = [
8
+ new DevtoolsUIAssetsPlugin(),
9
+ PUBSUB_PLUGIN,
10
+ ];