{"size": 8375, "ext": "js", "max_stars_repo_path": "studio/node_modules/@sanity/desk-tool/lib/panes/document/timeline/timelineMenu.js", "max_stars_repo_name": "sajustsmile/twitter-blockchain", "max_stars_repo_stars_event_min_datetime": "2022-03-31T20:19:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T20:19:12.000Z", "max_issues_repo_path": "studio/node_modules/@sanity/desk-tool/lib/panes/document/timeline/timelineMenu.js", "max_issues_repo_name": "sajustsmile/twitter-blockchain", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "studio/node_modules/@sanity/desk-tool/lib/panes/document/timeline/timelineMenu.js", "max_forks_repo_name": "sajustsmile/twitter-blockchain", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 55.0986842105, "max_line_length": 804, "alphanum_fraction": 0.6848955224}
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TimelineMenu = TimelineMenu;

var _upperFirst2 = _interopRequireDefault(require("lodash/upperFirst"));

var _hooks = require("@sanity/base/hooks");

var _icons = require("@sanity/icons");

var _ui = require("@sanity/ui");

var _react = _interopRequireWildcard(require("react"));

var _styledComponents = _interopRequireDefault(require("styled-components"));

var _useDocumentPane2 = require("../useDocumentPane");

var _helpers = require("./helpers");

var _timeline = require("./timeline");

var _templateObject;

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; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var Root = (0, _styledComponents.default)(_ui.Popover)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n  & > div {\n    display: flex;\n    flex-direction: column;\n\n    & > [data-ui='Card'] {\n      flex: 1;\n      min-height: 0;\n      display: flex;\n      flex-direction: column;\n\n      /* This is the scrollable container rendered by <Timeline /> */\n      & > div {\n        flex: 1;\n        min-height: 0;\n      }\n    }\n  }\n"])));

function TimelineMenu(_ref) {
  var chunk = _ref.chunk,
      mode = _ref.mode;

  var _useDocumentPane = (0, _useDocumentPane2.useDocumentPane)(),
      historyController = _useDocumentPane.historyController,
      setTimelineRange = _useDocumentPane.setTimelineRange,
      setTimelineMode = _useDocumentPane.setTimelineMode,
      timeline = _useDocumentPane.timeline,
      ready = _useDocumentPane.ready;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var _useState3 = (0, _react.useState)(null),
      _useState4 = _slicedToArray(_useState3, 2),
      buttonRef = _useState4[0],
      setButtonRef = _useState4[1];

  var _useState5 = (0, _react.useState)(null),
      _useState6 = _slicedToArray(_useState5, 2),
      menuContent = _useState6[0],
      setMenuContent = _useState6[1];

  var handleOpen = () => {
    setTimelineMode(mode);
    setOpen(true);
  };

  var handleClose = (0, _react.useCallback)(() => {
    setTimelineMode('closed');
    setOpen(false);
  }, [setTimelineMode]);
  var handleClickOutside = (0, _react.useCallback)(() => {
    handleClose();
  }, [handleClose]);
  (0, _ui.useClickOutside)(handleClickOutside, [menuContent, buttonRef]);
  var selectRev = (0, _react.useCallback)(revChunk => {
    var _historyController$fi = historyController.findRangeForNewRev(revChunk),
        _historyController$fi2 = _slicedToArray(_historyController$fi, 2),
        sinceId = _historyController$fi2[0],
        revId = _historyController$fi2[1];

    setTimelineMode('closed');
    setOpen(false);
    setTimelineRange(sinceId, revId);
  }, [historyController, setTimelineMode, setTimelineRange]);
  var selectSince = (0, _react.useCallback)(sinceChunk => {
    var _historyController$fi3 = historyController.findRangeForNewSince(sinceChunk),
        _historyController$fi4 = _slicedToArray(_historyController$fi3, 2),
        sinceId = _historyController$fi4[0],
        revId = _historyController$fi4[1];

    setTimelineMode('closed');
    setOpen(false);
    setTimelineRange(sinceId, revId);
  }, [historyController, setTimelineMode, setTimelineRange]);
  var loadMoreHistory = (0, _react.useCallback)(state => {
    historyController.setLoadMore(state);
  }, [historyController]);

  var content = open && /*#__PURE__*/_react.default.createElement("div", {
    ref: setMenuContent
  }, mode === 'rev' ? /*#__PURE__*/_react.default.createElement(_timeline.Timeline, _extends({
    onSelect: selectRev,
    onLoadMore: loadMoreHistory,
    timeline: timeline
  }, (0, _helpers.revTimelineProps)(historyController.realRevChunk))) : /*#__PURE__*/_react.default.createElement(_timeline.Timeline, _extends({
    onSelect: selectSince,
    onLoadMore: loadMoreHistory,
    timeline: timeline
  }, (0, _helpers.sinceTimelineProps)(historyController.sinceTime, historyController.realRevChunk))));

  var timeAgo = (0, _hooks.useTimeAgo)((chunk === null || chunk === void 0 ? void 0 : chunk.endTimestamp) || '', {
    agoSuffix: true
  });
  var revLabel = chunk ? "".concat((0, _upperFirst2.default)((0, _helpers.formatTimelineEventLabel)(chunk.type)), " ").concat(timeAgo) : 'Current version';
  var sinceLabel = chunk ? "Since ".concat((0, _helpers.formatTimelineEventLabel)(chunk.type), " ").concat(timeAgo) : 'Since unknown version';
  var openLabel = mode === 'rev' ? 'Select version' : 'Review changes since';
  var buttonLabel = mode === 'rev' ? revLabel : sinceLabel;
  return /*#__PURE__*/_react.default.createElement(Root, {
    constrainSize: true,
    content: content,
    "data-ui": "versionMenu",
    open: open,
    portal: true,
    referenceElement: buttonRef
  }, /*#__PURE__*/_react.default.createElement(_ui.Button, {
    disabled: !ready,
    mode: "bleed",
    fontSize: 1,
    padding: 2,
    iconRight: _icons.SelectIcon,
    onClick: open ? handleClose : handleOpen,
    ref: setButtonRef,
    selected: open,
    text: open ? openLabel : buttonLabel
  }));
}