Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Support context for appetize sdk session #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions examples/android-material-ui/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { nativePreviewHead } from "@storybook/native/dist/previewHeader";
import { nativePreviewHead } from "@storybook/native";

const config = {
const config = {
stories: ["../stories/*.stories.jsx"],
framework: {
name: "@storybook/react-webpack5",
options: {},
options: {}
},
addons: [
"@storybook/addon-docs",
Expand All @@ -14,8 +14,7 @@ const config = {
docs: {
autodocs: true
},
previewHead: nativePreviewHead,

previewHead: nativePreviewHead
};

export default config;
13 changes: 9 additions & 4 deletions examples/cross-platform/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const config = {
stories: [{ directory: '../src', files: '**/*.stories.@(mdx|tsx|ts|jsx|js)' }],
import { nativePreviewHead } from "@storybook/native";

const config = {
stories: [
{ directory: "../src", files: "**/*.stories.@(mdx|tsx|ts|jsx|js)" }
],
framework: {
name: "@storybook/react-webpack5",
options: {},
options: {}
},
addons: [
"@storybook/addon-docs",
Expand All @@ -11,7 +15,8 @@ const config = {
],
docs: {
autodocs: true
}
},
previewHead: nativePreviewHead
};

export default config;
4 changes: 2 additions & 2 deletions examples/cross-platform/src/android.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import rgbHex from "rgb-hex";
import { DeepLinkRenderer } from "@storybook/native-components";
import { EmulatorRenderer } from "@storybook/native-components";

const AndroidRenderer = ({ extraParams, component }) => {
return (
<DeepLinkRenderer
<EmulatorRenderer
apiKey="zv034bdme9je7c9d43chzmc2yg"
platform="android"
storyParams={{ component }}
Expand Down
6 changes: 3 additions & 3 deletions examples/cross-platform/src/comparison.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import { DeepLinkRenderer } from "@storybook/native-components";
import { EmulatorRenderer } from "@storybook/native-components";

const AndroidRenderer = ({ extraParams, component }) => {
return (
<DeepLinkRenderer
<EmulatorRenderer
apiKey="zv034bdme9je7c9d43chzmc2yg"
platform="android"
storyParams={{ component }}
Expand All @@ -16,7 +16,7 @@ const AndroidRenderer = ({ extraParams, component }) => {

const IosRenderer = ({ extraParams, component }) => {
return (
<DeepLinkRenderer
<EmulatorRenderer
apiKey="yc0e33432655wbjnnnemyghhxm"
platform="ios"
storyParams={{ component }}
Expand Down
4 changes: 2 additions & 2 deletions examples/cross-platform/src/ios.stories.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import { DeepLinkRenderer } from "@storybook/native-components";
import { EmulatorRenderer } from "@storybook/native-components";

const IosRenderer = ({ extraParams, component }) => {
return (
<DeepLinkRenderer
<EmulatorRenderer
apiKey="yc0e33432655wbjnnnemyghhxm"
platform="ios"
storyParams={{ component }}
Expand Down
2 changes: 1 addition & 1 deletion packages/addon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ it will make sure that the [Appetize SDK](https://docs.appetize.io/javascript-sd
Update your `main.js` file with the following code:

```js
import { nativePreviewHead } from "@storybook/native/dist/previewHeader";
import { nativePreviewHead } from "@storybook/native";

const config = {
stories: ["../stories/*.stories.jsx"],
Expand Down
30 changes: 25 additions & 5 deletions packages/native-components/src/renderers/EmulatorRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,46 @@ import {
useAppDispatch
} from "@storybook/native-controllers";
import { useLogs, useNetworkLogs } from "@storybook/native-devices";
import { RendererProps } from "../types";
import { DeepLinkRendererProps } from "../types";
import LaunchParamsRenderer from "./LaunchParamsRenderer";
import DeepLinkRenderer from "./DeepLinkRenderer";

const WithStore = (props: RendererProps): React.ReactElement => {
const appetizeSessionMap = new Map<string, Session>();

const WithStore = (props: DeepLinkRendererProps): React.ReactElement => {
const dispatch = useAppDispatch();
const networkLogs = useNetworkLogs();
const logs = useLogs();

const [session, setSession] = React.useState<Session>();

const sessionKey = props.context ?? props.platform;
useEffect(() => {
if (!window.appetize) {
addons.getChannel().emit(EmulatorEvents.onMissingClient, null);
return;
}
window.appetize.getClient("#appetize-iframe").then((client: Client) => {

if (sessionKey && appetizeSessionMap.has(sessionKey)) {
setSession(appetizeSessionMap.get(sessionKey));
resetNetworkLogs(dispatch);
addons.getChannel().emit(EmulatorEvents.onRestNetworkLogs);
return;
}

const frameId = `#appetize-iframe${
props.context ? `-${props.context}` : ""
}`;
window.appetize.getClient(frameId).then((client: Client) => {
addons.getChannel().emit(EmulatorEvents.onClient, client);

client.on("error", (error: string) => {
console.error(error);
appetizeSessionMap.delete(sessionKey);
});

client.on("session", (newSession: Session) => {
if (sessionKey) appetizeSessionMap.set(sessionKey, newSession);
setSession(newSession);
resetNetworkLogs(dispatch);
addons.getChannel().emit(EmulatorEvents.onRestNetworkLogs);
Expand All @@ -52,7 +72,7 @@ const WithStore = (props: RendererProps): React.ReactElement => {
addons.getChannel().emit(EmulatorEvents.onSession, session);
});
});
}, [networkLogs, logs]);
}, [networkLogs, logs, props.context]);

return (
<>
Expand All @@ -64,7 +84,7 @@ const WithStore = (props: RendererProps): React.ReactElement => {
);
};

export default (props: RendererProps): React.ReactElement => {
export default (props: DeepLinkRendererProps): React.ReactElement => {
return (
<Provider store={store}>
<WithStore {...props} />
Expand Down