@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
88 lines • 3.98 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
/**
* @jsxRuntime classic
* @jsx jsx
*/
import { useEffect, useState } from 'react';
export var useFetchDatasourceInfo = function useFetchDatasourceInfo(_ref) {
var isRegularCardNode = _ref.isRegularCardNode,
url = _ref.url,
cardContext = _ref.cardContext,
nodeParameters = _ref.nodeParameters;
var _useState = useState(undefined),
_useState2 = _slicedToArray(_useState, 2),
datasourceId = _useState2[0],
setDatasourceId = _useState2[1];
var _useState3 = useState(nodeParameters),
_useState4 = _slicedToArray(_useState3, 2),
parameters = _useState4[0],
setParameters = _useState4[1];
// Since fetchData() is async, using this ready check to see if we have the parameters before passing it to the modal.
// Only non-datasource nodes will be not ready initially since we need to fetch data.
var _useState5 = useState(!isRegularCardNode),
_useState6 = _slicedToArray(_useState5, 2),
ready = _useState6[0],
setReady = _useState6[1];
var _useState7 = useState(undefined),
_useState8 = _slicedToArray(_useState7, 2),
extensionKey = _useState8[0],
setExtensionKey = _useState8[1];
useEffect(function () {
var fetchDatasource = /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var _cardContext$connecti, _datasources$, _datasources$2, response, datasources;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
if (!(!url || !cardContext)) {
_context.next = 4;
break;
}
// Don't block rendering of modal of somehow we don't get these two args --> just open with empty params
setReady(true);
return _context.abrupt("return");
case 4:
_context.next = 6;
return cardContext === null || cardContext === void 0 || (_cardContext$connecti = cardContext.connections) === null || _cardContext$connecti === void 0 || (_cardContext$connecti = _cardContext$connecti.client) === null || _cardContext$connecti === void 0 ? void 0 : _cardContext$connecti.fetchData(url);
case 6:
response = _context.sent;
datasources = response && response.datasources || [];
setExtensionKey(response === null || response === void 0 ? void 0 : response.meta.key);
setDatasourceId((_datasources$ = datasources[0]) === null || _datasources$ === void 0 ? void 0 : _datasources$.id);
setParameters((_datasources$2 = datasources[0]) === null || _datasources$2 === void 0 ? void 0 : _datasources$2.parameters);
setReady(true);
_context.next = 20;
break;
case 14:
_context.prev = 14;
_context.t0 = _context["catch"](0);
setDatasourceId(undefined);
setParameters(undefined);
setExtensionKey(undefined);
// If fetch somehow errors, still set ready as true so we don't block the rendering of the modal.
// It will just open with empty params.
setReady(true);
case 20:
case "end":
return _context.stop();
}
}, _callee, null, [[0, 14]]);
}));
return function fetchDatasource() {
return _ref2.apply(this, arguments);
};
}();
if (isRegularCardNode) {
void fetchDatasource();
}
}, [isRegularCardNode, cardContext, url]);
return {
datasourceId: datasourceId,
parameters: parameters,
ready: ready,
extensionKey: extensionKey
};
};