@activecollab/components
Version:
ActiveCollab Components
172 lines (165 loc) • 8.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useUnfurl = exports.formatFetchedAt = exports.MOCK_LINKS = void 0;
var _react = require("react");
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* Mock unfurl service for the LinkCard concept.
*
* A link preview is a LIFECYCLE, not two static layouts. The real service is
* server-side (SSRF-guarded, oEmbed → OpenGraph → Twitter Cards → <title>,
* cached ~24h with a stored `fetchedAt` and a manual re-unfurl affordance — see
* note 52104 §B3). This is a promise-based fake with a configurable delay and a
* failure toggle so the story can drive the whole lifecycle without a network.
*
* The data arrives on a PROGRESSIVE LADDER of three tiers, each carrying more
* than the last:
* url → just the raw URL (nothing fetched yet)
* basic → favicon + title + domain
* full → + description, hero image, media kind / duration
* The card reserves the next tier's space at every tier, so climbing the ladder
* never reflows the card.
*/
/** The cumulative data shape. Fields fill in as the ladder climbs. */
/** A seed link — what the fake "server" would resolve the URL to at tier full. */
var MOCK_LINKS = exports.MOCK_LINKS = {
vimeo: {
url: "https://vimeo.com/876631121",
faviconColor: "#0F6E56",
title: "Brand refresh — motion explorations for the spring launch",
domain: "vimeo.com",
description: "A reel of motion studies exploring the refreshed brand system: type in motion, the new mark, and transitions for the marketing site.",
heroColor: "#1D9E75",
kind: "Video",
duration: "4:32",
playable: true
},
history: {
url: "https://ww2history.org/timeline",
faviconColor: "#8C5A2B",
title: "The Second World War • Timeline, key battles, and firsthand accounts",
domain: "ww2history.org",
description: "An illustrated timeline of the Second World War with primary sources, maps, and firsthand accounts from each theatre of the war.",
heroColor: "#B5793B",
kind: "Article",
playable: false
}
};
/** Progressive tier data derived from a seed link. */
var tierData = function tierData(link, tier) {
if (tier === "url") return {
url: link.url
};
if (tier === "basic") {
return {
url: link.url,
faviconColor: link.faviconColor,
title: link.title,
domain: link.domain
};
}
return _objectSpread({}, link);
};
/**
* Drives one link through the unfurl lifecycle. Climbs url → basic → full on a
* timer, or lands in `error` if `shouldFail`. `retry` / `reunfurl` re-run it;
* `markStale` moves a resolved card to the stale state so the re-unfurl
* affordance can be shown.
*/
var useUnfurl = exports.useUnfurl = function useUnfurl(_ref) {
var link = _ref.link,
_ref$delayMs = _ref.delayMs,
delayMs = _ref$delayMs === void 0 ? 900 : _ref$delayMs,
_ref$shouldFail = _ref.shouldFail,
shouldFail = _ref$shouldFail === void 0 ? false : _ref$shouldFail;
var _useState = (0, _react.useState)("resolving"),
_useState2 = _slicedToArray(_useState, 2),
status = _useState2[0],
setStatus = _useState2[1];
var _useState3 = (0, _react.useState)("url"),
_useState4 = _slicedToArray(_useState3, 2),
tier = _useState4[0],
setTier = _useState4[1];
var _useState5 = (0, _react.useState)(function () {
return tierData(link, "url");
}),
_useState6 = _slicedToArray(_useState5, 2),
data = _useState6[0],
setData = _useState6[1];
var _useState7 = (0, _react.useState)(null),
_useState8 = _slicedToArray(_useState7, 2),
fetchedAt = _useState8[0],
setFetchedAt = _useState8[1];
var timers = (0, _react.useRef)([]);
var clearTimers = (0, _react.useCallback)(function () {
timers.current.forEach(clearTimeout);
timers.current = [];
}, []);
var start = (0, _react.useCallback)(function () {
clearTimers();
setStatus("resolving");
setTier("url");
setData(tierData(link, "url"));
if (shouldFail) {
// Fail partway: the favicon + title never arrive; we keep the raw URL.
timers.current.push(setTimeout(function () {
return setStatus("error");
}, delayMs));
return;
}
timers.current.push(setTimeout(function () {
setTier("basic");
setData(tierData(link, "basic"));
}, delayMs));
timers.current.push(setTimeout(function () {
setTier("full");
setData(tierData(link, "full"));
setStatus("resolved");
setFetchedAt(Date.now());
}, delayMs * 2));
}, [clearTimers, link, delayMs, shouldFail]);
var markStale = (0, _react.useCallback)(function () {
setStatus(function (prev) {
return prev === "resolved" ? "stale" : prev;
});
}, []);
// Kick off on mount and whenever the config that defines the run changes.
(0, _react.useEffect)(function () {
start();
return clearTimers;
}, [start, clearTimers]);
return {
status,
tier,
data,
fetchedAt,
start,
retry: start,
reunfurl: start,
markStale
};
};
/** Human-readable "fetched N ago" for the stale affordance. */
var formatFetchedAt = exports.formatFetchedAt = function formatFetchedAt(fetchedAt) {
if (!fetchedAt) return "";
var seconds = Math.max(1, Math.round((Date.now() - fetchedAt) / 1000));
if (seconds < 60) return "".concat(seconds, "s ago");
var minutes = Math.round(seconds / 60);
if (minutes < 60) return "".concat(minutes, "m ago");
var hours = Math.round(minutes / 60);
if (hours < 24) return "".concat(hours, "h ago");
return "".concat(Math.round(hours / 24), "d ago");
};
//# sourceMappingURL=unfurl.js.map