client/components/pages/entities/wikipedia-extract.js

"use strict";

require("core-js/modules/es.weak-map.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/web.url.js");
require("core-js/modules/web.url-search-params.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.string.search.js");
require("core-js/modules/es.regexp.to-string.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.promise.js");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _uniq2 = _interopRequireDefault(require("lodash/uniq"));
var _reactBootstrap = require("react-bootstrap");
var _react = _interopRequireWildcard(require("react"));
var _wikimedia = require("../../../../common/helpers/wikimedia");
var _isomorphicDompurify = _interopRequireDefault(require("isomorphic-dompurify"));
var _utils = require("../../../../common/helpers/utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright (C) 2023  David Kellner
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
/**
 * Fetches a Wikipedia extract for the given Wikidata item.
 * @param {string} wikidataId - Wikidata item ID.
 * @param {string[]} preferredLanguages - List of language codes, preference in descending order.
 */
function getWikipediaExtractForWikidata(_x) {
  return _getWikipediaExtractForWikidata.apply(this, arguments);
}
function _getWikipediaExtractForWikidata() {
  _getWikipediaExtractForWikidata = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(wikidataId) {
    var preferredLanguages,
      apiUrl,
      response,
      _args = arguments;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            preferredLanguages = _args.length > 1 && _args[1] !== undefined ? _args[1] : ['en'];
            apiUrl = new URL("/wikidata/".concat(wikidataId, "/wikipedia-extract"), document.location.href);
            apiUrl.search = new URLSearchParams(preferredLanguages.map(function (lang) {
              return ['language', lang];
            })).toString();
            _context.next = 5;
            return fetch(apiUrl);
          case 5:
            response = _context.sent;
            return _context.abrupt("return", response.json());
          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _getWikipediaExtractForWikidata.apply(this, arguments);
}
function WikipediaExtract(_ref) {
  var _buildWikipediaUrl;
  var entity = _ref.entity,
    articleExtract = _ref.articleExtract;
  var _useState = (0, _react.useState)(articleExtract),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    state = _useState2[0],
    setState = _useState2[1];
  (0, _react.useEffect)(function () {
    if (!state.extract) {
      var wikidataId = (0, _wikimedia.getWikidataId)(entity);
      if (!wikidataId) {
        return;
      }
      var aliasLanguages = (0, _utils.getAliasLanguageCodes)(entity);
      var preferredLanguages = (0, _uniq2.default)(['en'].concat((0, _toConsumableArray2.default)(aliasLanguages)));
      getWikipediaExtractForWikidata(wikidataId, preferredLanguages).then(function (result) {
        if (result.extract) {
          setState(result);
        }
      }).catch(function (error) {
        // eslint-disable-next-line no-console -- no other logger available for browser
        console.warn('Failed to load Wikipedia extract:', error);
      });
    }
  }, [entity]);
  var extract = state.extract,
    article = state.article;
  var licenseUrl = 'https://creativecommons.org/licenses/by-sa/3.0/';
  return extract ? /*#__PURE__*/_react.default.createElement(_reactBootstrap.Row, {
    className: "wikipedia-extract"
  }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Col, null, /*#__PURE__*/_react.default.createElement("h2", null, "Wikipedia"), /*#__PURE__*/_react.default.createElement("div", {
    dangerouslySetInnerHTML: {
      __html: _isomorphicDompurify.default.sanitize(extract)
    }
  }), /*#__PURE__*/_react.default.createElement("a", {
    href: (_buildWikipediaUrl = (0, _wikimedia.buildWikipediaUrl)(article)) === null || _buildWikipediaUrl === void 0 ? void 0 : _buildWikipediaUrl.href
  }, "Continue reading at Wikipedia..."), ' ', /*#__PURE__*/_react.default.createElement("small", null, "Wikipedia content provided under the terms of the ", /*#__PURE__*/_react.default.createElement("a", {
    href: licenseUrl
  }, "Creative Commons BY-SA license")))) : null;
}
WikipediaExtract.defaultProps = {
  articleExtract: {}
};
var _default = WikipediaExtract;
exports.default = _default;
//# sourceMappingURL=wikipedia-extract.js.map