@atlaskit/editor-plugin-collab-edit
Version:
Collab Edit plugin for @atlaskit/editor-core
75 lines (73 loc) • 4.29 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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; }
import { NCS_STORAGE } from '@atlaskit/editor-common/ncs-step-metrics';
import { StorageClient } from '@atlaskit/frontend-utilities';
var storageClient = new StorageClient(NCS_STORAGE.NCS_STORAGE_CLIENT_KEY);
/**
* Gets the current step session metrics for a given session ID
* If the session ID does not exist, it initializes a new metrics object.
* It calculates the total size of the steps, the number of steps,
* the maximum step size, and the sum of step sizes for P90 calculation.
*
* @param metrics - The existing metrics object from local storage.
* @param sessionId - The session ID for which to get the metrics.
* @param steps - The steps to calculate the metrics from.
* @returns The updated step session metrics for the given session ID.
*/
export var getNcsSessionStepMetrics = function getNcsSessionStepMetrics(metrics, sessionId, steps) {
var _metrics$sessionId;
var current = (_metrics$sessionId = metrics[sessionId]) !== null && _metrics$sessionId !== void 0 ? _metrics$sessionId : {
ncsSessionId: sessionId,
totalStepSize: 0,
numberOfSteps: 0,
maxStepSize: 0,
stepSizeSumForP90: []
};
steps.forEach(function (step) {
var stepSize = JSON.stringify(step).length;
current.totalStepSize += stepSize;
current.numberOfSteps += 1;
current.maxStepSize = Math.max(current.maxStepSize, stepSize || 0);
current.stepSizeSumForP90.push(stepSize || 0);
});
return current;
};
/**
* Gets the current active sessions from local storage
* If the session ID does not exist, it initializes a new active session.
*
* This is used in the ncsStepMetricsPlugin to determine if the session is still active
* before sending the ncs steps analytics event.
*
* @param sessionId - The session ID to check or update in local storage.
* @returns void
*/
export var updateNcsActiveSessions = function updateNcsActiveSessions(sessionId) {
var currentActiveSessions = JSON.parse(storageClient.getItem(NCS_STORAGE.NCS_ACTIVE_SESSIONS) || '{}');
if (!currentActiveSessions[sessionId]) {
storageClient.setItemWithExpiry(NCS_STORAGE.NCS_ACTIVE_SESSIONS, JSON.stringify(_objectSpread(_objectSpread({}, currentActiveSessions), {}, _defineProperty({}, sessionId, true))));
}
};
// delete this function when cleaning up platform_editor_remove_collab_step_metrics
/**
* Updates the step session metrics in local storage for a given session ID.
* It calculates the metrics based on the provided steps and updates the storage.
*
* @param api - The API to access the collab edit plugin.
* @param steps - The steps to calculate the metrics from.
* @return void
*/
export var updateNcsSessionStepMetrics = function updateNcsSessionStepMetrics(_ref) {
var _api$collabEdit;
var api = _ref.api,
steps = _ref.steps;
var sessionId = api === null || api === void 0 || (_api$collabEdit = api.collabEdit) === null || _api$collabEdit === void 0 || (_api$collabEdit = _api$collabEdit.sharedState.currentState()) === null || _api$collabEdit === void 0 ? void 0 : _api$collabEdit.sessionId;
if (!sessionId) {
return;
}
var existingMetrics = JSON.parse(storageClient.getItem(NCS_STORAGE.NCS_SESSION_STEP_METRICS) || '{}');
var ncsSessionStepMetrics = getNcsSessionStepMetrics(existingMetrics, sessionId, steps);
storageClient.setItemWithExpiry(NCS_STORAGE.NCS_SESSION_STEP_METRICS, JSON.stringify(_objectSpread(_objectSpread({}, existingMetrics), {}, _defineProperty({}, sessionId, ncsSessionStepMetrics))));
updateNcsActiveSessions(sessionId);
};