Skip to content

Commit

Permalink
fix!: use globals instead of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DanWebb committed Jan 15, 2025
1 parent 642aa13 commit 34b575f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const preview = {
parameters: {
initialGlobals: {
marker: {
project: '60f162459a86003bf9d741b3',
}
}
},
};

export default preview;
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export default {
}
```

Then create a file called `preview.js` in the same folder and add your [Marker project ID](https://marker.io/blog/integrate-web-app-browser-sdk) as a [parameter](https://storybook.js.org/docs/react/writing-stories/parameters).
Then create a file called `preview.js` in the same folder and add your [Marker project ID](https://marker.io/blog/integrate-web-app-browser-sdk) in the [initalGlobals](https://storybook.js.org/docs/essentials/toolbars-and-globals).

```js
export default {
parameters: {
initialGlobals: {
marker: {
project: 'abcd1234567890', // Your unique project ID
}
}
},
},
}
```

Expand All @@ -39,11 +39,13 @@ Only `project` is required, the [rest of the marker widget params](https://githu
Additionally, the `mode` option of [the browser SDK `capture` method](https://github.com/marker-io/browser-sdk?tab=readme-ov-file#widgetcapturemode) can be added to this config:

```js
export const parameters = {
marker: {
project: 'abcd1234567890', // <- Your unique project ID
mode: 'fullscreen', // fullscreen | advanced
}
export default {
initialGlobals: {
marker: {
project: 'abcd1234567890', // <- Your unique project ID
mode: 'fullscreen', // fullscreen | advanced
},
},
}
```

Expand Down
41 changes: 21 additions & 20 deletions src/FeedbackButton.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import markerSDK from '@marker.io/browser';
import { IconButton } from '@storybook/components';
import { CommentIcon } from '@storybook/icons';
import { useChannel, useParameter } from '@storybook/manager-api';
import { useChannel, useGlobals } from '@storybook/manager-api';
import { styled } from '@storybook/theming';
import React, { useCallback, useEffect, useState } from 'react';

Expand Down Expand Up @@ -28,10 +29,17 @@ const IconButtonLabel = styled.div(({ theme }) => ({

export default function FeedbackButton() {
const [markerLoaded, setMarkerLoaded] = useState(false);
const { destination, project, mode, ...config } = useParameter('marker', {});
const [globals] = useGlobals();
const { project, mode, ...config } = globals.marker ?? {};

const emit = useChannel({
// The loaded event will fire when the marker decorator loads
[EVENTS.LOADED]: () => {
if (window.Marker) {
// Unload the manager version of marker
window.Marker.unload();
}

setMarkerLoaded(true);
},
[EVENTS.CAPTURE]: () => {
Expand All @@ -45,28 +53,21 @@ export default function FeedbackButton() {
emit(EVENTS.CAPTURE);
}, [emit]);

const loadMarkerOnManager = useCallback(() => {
if ((!destination && !project) || markerLoaded || window.Marker) {
return;
}

markerSDK
.loadWidget({
project: project ?? destination,
...config,
})
.then(() => {
hideDefaultMarkerButton();
emit(EVENTS.LOADED);
});
}, [markerLoaded]);

// If the decorator has not loaded within 3 seconds fallback to loading it on the manager.
// Screenshots may appear unstyled, but it's better than no feedback button displaying.
// This might happen on mdx docs stories where no decorators aren't called.
useEffect(() => {
setTimeout(loadMarkerOnManager, 3000);
}, []);
clearTimeout(window.markerTimer);

if (project && !markerLoaded && !window.Marker) {
window.markerTimer = setTimeout(() => {
markerSDK.loadWidget({ project, ...config }).then(() => {
hideDefaultMarkerButton();
setMarkerLoaded(true);
});
}, 3000);
}
}, [project, markerLoaded]);

return markerLoaded ? (
<IconButtonWithLabel key={TOOL_ID} onClick={handleSendFeedback}>
Expand Down
21 changes: 9 additions & 12 deletions src/withMarker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable react-hooks/rules-of-hooks */
import markerSDK from '@marker.io/browser';
import { useChannel, useParameter } from '@storybook/preview-api';
import { useChannel, useGlobals } from '@storybook/preview-api';

import { EVENTS } from './constants';
import { hideDefaultMarkerButton } from './hideDefaultMarkerButton';

export const withMarker = (storyFn) => {
const { destination, project, mode, ...config } = useParameter('marker', {});
const [globals] = useGlobals();
const { project, mode, ...config } = globals.marker ?? {};

const emit = useChannel({
[EVENTS.CAPTURE]: () => {
window.Marker?.capture(mode)?.then(() => {
Expand All @@ -15,19 +17,14 @@ export const withMarker = (storyFn) => {
},
});

if ((!destination && !project) || window.Marker) {
if (!project || window.Marker) {
return storyFn();
}

markerSDK
.loadWidget({
project: project ?? destination,
...config,
})
.then(() => {
hideDefaultMarkerButton();
emit(EVENTS.LOADED);
});
markerSDK.loadWidget({ project, ...config }).then(() => {
hideDefaultMarkerButton();
emit(EVENTS.LOADED);
});

return storyFn();
};

0 comments on commit 34b575f

Please sign in to comment.