@redocly/theme
Version:
Shared UI components lib
60 lines • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformRevisionsToVersionHistory = transformRevisionsToVersionHistory;
const constants_1 = require("../../core/constants");
const date_1 = require("./date");
function transformRevisionsToVersionHistory({ revisions, currentRevisionDate, currentVersion, locale = typeof navigator !== 'undefined' ? navigator.language : 'en-US', }) {
const normalizedCurrentVersion = currentVersion === null ? constants_1.VERSION_NOT_SPECIFIED : currentVersion;
const versionMap = new Map();
revisions.forEach((revision) => {
const version = revision.version || constants_1.VERSION_NOT_SPECIFIED;
const versionRevisions = versionMap.get(version) || [];
versionRevisions.push(revision);
if (!versionMap.has(version)) {
versionMap.set(version, versionRevisions);
}
});
const versionGroups = Array.from(versionMap.entries()).map(([version, versionRevisions]) => {
var _a, _b, _c;
const latestRevision = versionRevisions[0];
const versionMatches = normalizedCurrentVersion === undefined || normalizedCurrentVersion === version;
let isCurrent;
if (currentRevisionDate !== undefined) {
// Check if revision matches AND version matches
const revisionMatches = versionRevisions.some((rev) => rev.revision === currentRevisionDate);
isCurrent = revisionMatches && versionMatches;
}
else {
// When no revision is specified, use isCurrent flag from the latest revision
// but only if version also matches (or no version filter is applied)
isCurrent = ((_a = latestRevision === null || latestRevision === void 0 ? void 0 : latestRevision.isCurrent) !== null && _a !== void 0 ? _a : false) && versionMatches;
}
// Check if any revision in this version group is the default version
const isDefaultVersion = versionRevisions.some((rev) => rev.isDefaultVersion === true);
const revisions = versionRevisions.map((rev, index) => {
var _a;
const revisionMatches = currentRevisionDate ? rev.revision === currentRevisionDate : false;
const isActiveRevision = revisionMatches && versionMatches;
const isCurrentByDefault = !currentRevisionDate && normalizedCurrentVersion === undefined && index === 0 && isCurrent;
return {
name: `r.${versionRevisions.length - index}`,
date: (0, date_1.toLocalizedShortDateTime)(rev.revision, locale),
revisionDate: rev.revision,
isActive: isActiveRevision || isCurrentByDefault,
isCurrent: (_a = rev.isCurrent) !== null && _a !== void 0 ? _a : false,
};
});
return {
version,
date: (0, date_1.toLocalizedShortDate)((latestRevision === null || latestRevision === void 0 ? void 0 : latestRevision.revision) || null, locale),
isCurrent,
isExpanded: isCurrent,
hasCurrentRevisionFromBackend: (_b = latestRevision === null || latestRevision === void 0 ? void 0 : latestRevision.isCurrent) !== null && _b !== void 0 ? _b : false,
isDefaultVersion,
revisions,
singleRevisionDate: versionRevisions.length === 1 ? (_c = versionRevisions[0]) === null || _c === void 0 ? void 0 : _c.revision : undefined,
};
});
return versionGroups;
}
//# sourceMappingURL=transform-revisions-to-version-history.js.map