UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

56 lines 2.57 kB
import { __awaiter, __generator } from "tslib"; import * as React from 'react'; import { transformData } from './transformStoryStructure'; import { getCustomQueryParams } from './getCustomQueryParams'; var baseUrl = 'https://mykolass.wixsite.com/storybook-builder/_functions'; var buildEndpoint = function (_a) { var library = _a.library, storyName = _a.storyName, previewId = _a.previewId; if (previewId) { return "".concat(baseUrl, "/getPreview?id=").concat(previewId); } return "".concat(baseUrl, "/getStory?storyName=").concat(storyName, "&library=").concat(library); }; function fetchStory(_a) { var library = _a.library, storyName = _a.storyName, previewId = _a.previewId; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_b) { return [2 /*return*/, fetch(buildEndpoint({ library: library, storyName: storyName, previewId: previewId })) .then(function (res) { return res.json(); }) .then(function (res) { if (res.error) { return null; } return res.story.content; })]; }); }); } export var useStorybookBuilderApi = function (_a) { var library = _a.library, storyName = _a.storyName; var _b = React.useState(true), loading = _b[0], setLoading = _b[1]; var _c = React.useState(null), data = _c[0], setData = _c[1]; var _d = getCustomQueryParams(storyName), previewId = _d.previewId, enabled = _d.enabled; React.useEffect(function () { if (enabled) { setLoading(true); fetchStory({ library: library, storyName: storyName, previewId: previewId }) .then(function (res) { setTimeout(function () { return setLoading(false); }, 150); setData(res); }) .catch(function (error) { setTimeout(function () { return setLoading(false); }, 100); console.error(error); }); } }, []); if (window.parent.window.STORYBOOK_CMS_DATA && !enabled) { var foundStory = window.parent.window.STORYBOOK_CMS_DATA.find(function (story) { return story.storyName === storyName; }); return { loading: false, response: foundStory ? transformData(foundStory.content) : null, }; } return { loading: loading, response: data && transformData(data), enabled: enabled }; }; //# sourceMappingURL=useStorybookBuilderApi.js.map