@atlaskit/react-ufo 4.0.3 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/custom-cohort-data/package.json +15 -0
  3. package/dist/cjs/custom-cohort-data/index.js +37 -0
  4. package/dist/cjs/custom-cohort-data/types.js +1 -0
  5. package/dist/cjs/vc/vc-observer-new/viewport-observer/index.js +10 -13
  6. package/dist/cjs/vc/vc-observer-new/viewport-observer/utils/{get-component-name-and-child-props.js → check-within-component-and-extract-child-props.js} +6 -39
  7. package/dist/cjs/vc/vc-observer-new/viewport-observer/utils/find-react-fiber.js +16 -0
  8. package/dist/cjs/vc/vc-observer-new/viewport-observer/utils/get-component-name.js +19 -0
  9. package/dist/es2019/custom-cohort-data/index.js +30 -0
  10. package/dist/es2019/custom-cohort-data/types.js +0 -0
  11. package/dist/es2019/vc/vc-observer-new/viewport-observer/index.js +7 -11
  12. package/dist/es2019/vc/vc-observer-new/viewport-observer/utils/{get-component-name-and-child-props.js → check-within-component-and-extract-child-props.js} +4 -31
  13. package/dist/es2019/vc/vc-observer-new/viewport-observer/utils/find-react-fiber.js +10 -0
  14. package/dist/es2019/vc/vc-observer-new/viewport-observer/utils/get-component-name.js +11 -0
  15. package/dist/esm/custom-cohort-data/index.js +29 -0
  16. package/dist/esm/custom-cohort-data/types.js +0 -0
  17. package/dist/esm/vc/vc-observer-new/viewport-observer/index.js +10 -13
  18. package/dist/esm/vc/vc-observer-new/viewport-observer/utils/{get-component-name-and-child-props.js → check-within-component-and-extract-child-props.js} +4 -34
  19. package/dist/esm/vc/vc-observer-new/viewport-observer/utils/find-react-fiber.js +10 -0
  20. package/dist/esm/vc/vc-observer-new/viewport-observer/utils/get-component-name.js +12 -0
  21. package/dist/types/custom-cohort-data/index.d.ts +4 -0
  22. package/dist/types/custom-cohort-data/types.d.ts +4 -0
  23. package/dist/types/vc/vc-observer-new/viewport-observer/utils/check-within-component-and-extract-child-props.d.ts +8 -0
  24. package/dist/types/vc/vc-observer-new/viewport-observer/utils/find-react-fiber.d.ts +1 -0
  25. package/dist/types/vc/vc-observer-new/viewport-observer/utils/get-component-name.d.ts +1 -0
  26. package/dist/types-ts4.5/custom-cohort-data/index.d.ts +4 -0
  27. package/dist/types-ts4.5/custom-cohort-data/types.d.ts +4 -0
  28. package/dist/types-ts4.5/vc/vc-observer-new/viewport-observer/utils/check-within-component-and-extract-child-props.d.ts +8 -0
  29. package/dist/types-ts4.5/vc/vc-observer-new/viewport-observer/utils/find-react-fiber.d.ts +1 -0
  30. package/dist/types-ts4.5/vc/vc-observer-new/viewport-observer/utils/get-component-name.d.ts +1 -0
  31. package/package.json +2 -1
  32. package/dist/types/vc/vc-observer-new/viewport-observer/utils/get-component-name-and-child-props.d.ts +0 -15
  33. package/dist/types-ts4.5/vc/vc-observer-new/viewport-observer/utils/get-component-name-and-child-props.d.ts +0 -15
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @atlaskit/ufo-interaction-ignore
2
2
 
3
+ ## 4.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#185569](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/185569)
8
+ [`1c7b682d3bd6a`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/1c7b682d3bd6a) -
9
+ Adds UFOCustomCohortData component and addUFOCustomCohortData function to separate cohorting data
10
+ (important for analytics) from general debugging data.
11
+
3
12
  ## 4.0.3
4
13
 
5
14
  ### Patch Changes
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@atlaskit/react-ufo/custom-cohort-data",
3
+ "main": "../dist/cjs/custom-cohort-data/index.js",
4
+ "module": "../dist/esm/custom-cohort-data/index.js",
5
+ "module:es2019": "../dist/es2019/custom-cohort-data/index.js",
6
+ "sideEffects": false,
7
+ "types": "../dist/types/custom-cohort-data/index.d.ts",
8
+ "typesVersions": {
9
+ ">=4.5 <5.4": {
10
+ "*": [
11
+ "../dist/types-ts4.5/custom-cohort-data/index.d.ts"
12
+ ]
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.addUFOCustomCohortData = addUFOCustomCohortData;
8
+ exports.default = UFOCustomCohortData;
9
+ var _react = require("react");
10
+ var _interactionContext = _interopRequireDefault(require("../interaction-context"));
11
+ var _interactionIdContext = require("../interaction-id-context");
12
+ var _interactionMetrics = require("../interaction-metrics");
13
+ function UFOCustomCohortData(_ref) {
14
+ var dataKey = _ref.dataKey,
15
+ value = _ref.value;
16
+ var interactionContext = (0, _react.useContext)(_interactionContext.default);
17
+ (0, _react.useMemo)(function () {
18
+ if (!interactionContext) {
19
+ return;
20
+ }
21
+ var interactionId = (0, _interactionIdContext.getInteractionId)();
22
+ var currentInteractionId = interactionId.current;
23
+ if (!currentInteractionId) {
24
+ return;
25
+ }
26
+ (0, _interactionMetrics.addCohortingCustomData)(currentInteractionId, dataKey, value);
27
+ }, [dataKey, value, interactionContext]);
28
+ return null;
29
+ }
30
+ function addUFOCustomCohortData(key, value) {
31
+ var interactionId = (0, _interactionIdContext.getInteractionId)();
32
+ var currentInteractionId = interactionId.current;
33
+ if (!currentInteractionId) {
34
+ return;
35
+ }
36
+ (0, _interactionMetrics.addCohortingCustomData)(currentInteractionId, key, value);
37
+ }
@@ -0,0 +1 @@
1
+ "use strict";
@@ -17,7 +17,7 @@ var _rllPlaceholders = require("../../vc-observer/observers/rll-placeholders");
17
17
  var _intersectionObserver = require("./intersection-observer");
18
18
  var _mutationObserver = _interopRequireDefault(require("./mutation-observer"));
19
19
  var _performanceObserver = _interopRequireDefault(require("./performance-observer"));
20
- var _getComponentNameAndChildProps = require("./utils/get-component-name-and-child-props");
20
+ var _checkWithinComponentAndExtractChildProps = _interopRequireDefault(require("./utils/check-within-component-and-extract-child-props"));
21
21
  var _isInVcIgnoreIfNoLayoutShiftMarker = _interopRequireDefault(require("./utils/is-in-vc-ignore-if-no-layout-shift-marker"));
22
22
  function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
23
23
  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; } }
@@ -125,7 +125,7 @@ var ViewportObserver = exports.default = /*#__PURE__*/function () {
125
125
  _context2.prev = 4;
126
126
  _loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
127
127
  var _this$intersectionObs8;
128
- var addedNodeRef, addedNode, ssrState, SSRStateEnum, _this$intersectionObs, _this$intersectionObs2, ssrPlaceholderHandler, result, _this$intersectionObs3, _result, _this$intersectionObs4, sameDeletedNode, isInIgnoreLsMarker, isNoLsMarkerEnabled, _this$intersectionObs5, _this$intersectionObs6, _checkThirdPartySegme, isWithinThirdPartySegment, ignoredReason, _this$intersectionObs7, assignedReason;
128
+ var addedNodeRef, addedNode, ssrState, SSRStateEnum, _this$intersectionObs, _this$intersectionObs2, ssrPlaceholderHandler, result, _this$intersectionObs3, _result, _this$intersectionObs4, sameDeletedNode, isInIgnoreLsMarker, isNoLsMarkerEnabled, _this$intersectionObs5, _this$intersectionObs6, _checkWithinComponent, isWithinThirdPartySegment, _this$intersectionObs7;
129
129
  return _regenerator.default.wrap(function _loop$(_context) {
130
130
  while (1) switch (_context.prev = _context.next) {
131
131
  case 0:
@@ -235,17 +235,16 @@ var ViewportObserver = exports.default = /*#__PURE__*/function () {
235
235
  (_this$intersectionObs6 = _this.intersectionObserver) === null || _this$intersectionObs6 === void 0 || _this$intersectionObs6.watchAndTag(addedNode, 'mutation:media');
236
236
  return _context.abrupt("return", 0);
237
237
  case 45:
238
- _checkThirdPartySegme = (0, _getComponentNameAndChildProps.checkThirdPartySegmentWithIgnoreReason)(addedNode), isWithinThirdPartySegment = _checkThirdPartySegme.isWithinThirdPartySegment, ignoredReason = _checkThirdPartySegme.ignoredReason;
238
+ _checkWithinComponent = (0, _checkWithinComponentAndExtractChildProps.default)(addedNode, 'UFOThirdPartySegment'), isWithinThirdPartySegment = _checkWithinComponent.isWithin;
239
239
  if (!isWithinThirdPartySegment) {
240
- _context.next = 50;
240
+ _context.next = 49;
241
241
  break;
242
242
  }
243
- assignedReason = (0, _getComponentNameAndChildProps.createMutationTypeWithIgnoredReason)(ignoredReason || 'third-party-element');
244
- (_this$intersectionObs7 = _this.intersectionObserver) === null || _this$intersectionObs7 === void 0 || _this$intersectionObs7.watchAndTag(addedNode, assignedReason);
243
+ (_this$intersectionObs7 = _this.intersectionObserver) === null || _this$intersectionObs7 === void 0 || _this$intersectionObs7.watchAndTag(addedNode, 'mutation:third-party-element');
245
244
  return _context.abrupt("return", 0);
246
- case 50:
245
+ case 49:
247
246
  (_this$intersectionObs8 = _this.intersectionObserver) === null || _this$intersectionObs8 === void 0 || _this$intersectionObs8.watchAndTag(addedNode, createElementMutationsWatcher(removedNodeRects));
248
- case 51:
247
+ case 50:
249
248
  case "end":
250
249
  return _context.stop();
251
250
  }
@@ -333,13 +332,11 @@ var ViewportObserver = exports.default = /*#__PURE__*/function () {
333
332
  }
334
333
  };
335
334
  }
336
- var _checkThirdPartySegme2 = (0, _getComponentNameAndChildProps.checkThirdPartySegmentWithIgnoreReason)(target),
337
- isWithinThirdPartySegment = _checkThirdPartySegme2.isWithinThirdPartySegment,
338
- ignoredReason = _checkThirdPartySegme2.ignoredReason;
335
+ var _checkWithinComponent2 = (0, _checkWithinComponentAndExtractChildProps.default)(target, 'UFOThirdPartySegment'),
336
+ isWithinThirdPartySegment = _checkWithinComponent2.isWithin;
339
337
  if (isWithinThirdPartySegment) {
340
- var assignedReason = (0, _getComponentNameAndChildProps.createMutationTypeWithIgnoredReason)(ignoredReason || 'third-party-element');
341
338
  return {
342
- type: assignedReason,
339
+ type: 'mutation:third-party-element',
343
340
  mutationData: {
344
341
  attributeName: attributeName,
345
342
  oldValue: oldValue,
@@ -4,22 +4,13 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.checkThirdPartySegmentWithIgnoreReason = checkThirdPartySegmentWithIgnoreReason;
8
- exports.checkWithinComponentAndExtractChildProps = checkWithinComponentAndExtractChildProps;
9
- exports.createMutationTypeWithIgnoredReason = createMutationTypeWithIgnoredReason;
7
+ exports.default = checkWithinComponentAndExtractChildProps;
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
- var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
12
9
  var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
10
+ var _findReactFiber = _interopRequireDefault(require("./find-react-fiber"));
11
+ var _getComponentName = _interopRequireDefault(require("./get-component-name"));
13
12
  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; }
14
13
  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) { (0, _defineProperty2.default)(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; }
15
- // Helper function to find React fiber on an element
16
- var findReactFiber = function findReactFiber(element) {
17
- var key = Object.keys(element).find(function (key) {
18
- return key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$');
19
- });
20
- return key ? element[key] : null;
21
- };
22
-
23
14
  // Using the React Fiber tree to traverse up the DOM and check if a node is within a specific component
24
15
  // and extract child component props if needed.
25
16
  function checkWithinComponentAndExtractChildProps(node, targetComponentName, childComponentConfig) {
@@ -28,13 +19,13 @@ function checkWithinComponentAndExtractChildProps(node, targetComponentName, chi
28
19
  // Walk up the DOM tree to find React fiber (handles non-React-rendered elements)
29
20
  var currentElement = node;
30
21
  while (currentElement && !fiber) {
31
- fiber = findReactFiber(currentElement);
22
+ fiber = (0, _findReactFiber.default)(currentElement);
32
23
  if (!fiber) {
33
24
  currentElement = currentElement.parentElement;
34
25
  }
35
26
  }
36
27
  } else {
37
- fiber = findReactFiber(node);
28
+ fiber = (0, _findReactFiber.default)(node);
38
29
  }
39
30
 
40
31
  // If no React fiber found, return false
@@ -48,14 +39,7 @@ function checkWithinComponentAndExtractChildProps(node, targetComponentName, chi
48
39
  var currentFiber = fiber;
49
40
  var childProp;
50
41
  while (currentFiber) {
51
- var componentName = void 0;
52
- if (currentFiber.type) {
53
- if (typeof currentFiber.type === 'function') {
54
- componentName = currentFiber.type.displayName || currentFiber.type.name;
55
- } else if ((0, _typeof2.default)(currentFiber.type) === 'object' && (currentFiber.type.displayName || currentFiber.type.name)) {
56
- componentName = currentFiber.type.displayName || currentFiber.type.name;
57
- }
58
- }
42
+ var componentName = (0, _getComponentName.default)(currentFiber);
59
43
 
60
44
  // Check if this is a child component we're looking for
61
45
  if (childComponentConfig && componentName === childComponentConfig.componentName) {
@@ -79,21 +63,4 @@ function checkWithinComponentAndExtractChildProps(node, targetComponentName, chi
79
63
  return {
80
64
  isWithin: false
81
65
  };
82
- }
83
-
84
- // Check if the node is within a UFOThirdPartySegment and extract any UFOIgnoreHolds reason
85
- function checkThirdPartySegmentWithIgnoreReason(node) {
86
- var result = checkWithinComponentAndExtractChildProps(node, 'UFOThirdPartySegment', {
87
- componentName: 'UFOIgnoreHolds',
88
- propName: 'reason'
89
- });
90
- return {
91
- isWithinThirdPartySegment: result.isWithin,
92
- ignoredReason: result.childProp
93
- };
94
- }
95
-
96
- // Helper function to create mutation type from UFOIgnoreHoldsReason
97
- function createMutationTypeWithIgnoredReason(reason) {
98
- return "mutation:".concat(reason);
99
66
  }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = findReactFiber;
7
+ // Helper function to find React fiber on an element
8
+ function findReactFiber(element) {
9
+ // Use for...in loop instead of Object.keys().find() to avoid creating an array
10
+ for (var key in element) {
11
+ if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) {
12
+ return element[key];
13
+ }
14
+ }
15
+ return null;
16
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = getComponentName;
8
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
9
+ // Helper function to extract component name from fiber
10
+ function getComponentName(fiber) {
11
+ var type = fiber === null || fiber === void 0 ? void 0 : fiber.type;
12
+ if (!type) {
13
+ return undefined;
14
+ }
15
+ if (typeof type === 'function' || (0, _typeof2.default)(type) === 'object') {
16
+ return type.displayName || type.name;
17
+ }
18
+ return undefined;
19
+ }
@@ -0,0 +1,30 @@
1
+ import { useContext, useMemo } from 'react';
2
+ import UFOInteractionContext from '../interaction-context';
3
+ import { getInteractionId } from '../interaction-id-context';
4
+ import { addCohortingCustomData } from '../interaction-metrics';
5
+ export default function UFOCustomCohortData({
6
+ dataKey,
7
+ value
8
+ }) {
9
+ const interactionContext = useContext(UFOInteractionContext);
10
+ useMemo(() => {
11
+ if (!interactionContext) {
12
+ return;
13
+ }
14
+ const interactionId = getInteractionId();
15
+ const currentInteractionId = interactionId.current;
16
+ if (!currentInteractionId) {
17
+ return;
18
+ }
19
+ addCohortingCustomData(currentInteractionId, dataKey, value);
20
+ }, [dataKey, value, interactionContext]);
21
+ return null;
22
+ }
23
+ export function addUFOCustomCohortData(key, value) {
24
+ const interactionId = getInteractionId();
25
+ const currentInteractionId = interactionId.current;
26
+ if (!currentInteractionId) {
27
+ return;
28
+ }
29
+ addCohortingCustomData(currentInteractionId, key, value);
30
+ }
File without changes
@@ -6,7 +6,7 @@ import { RLLPlaceholderHandlers } from '../../vc-observer/observers/rll-placehol
6
6
  import { createIntersectionObserver } from './intersection-observer';
7
7
  import createMutationObserver from './mutation-observer';
8
8
  import createPerformanceObserver from './performance-observer';
9
- import { checkThirdPartySegmentWithIgnoreReason, createMutationTypeWithIgnoredReason } from './utils/get-component-name-and-child-props';
9
+ import checkWithinComponentAndExtractChildProps from './utils/check-within-component-and-extract-child-props';
10
10
  import isInVCIgnoreIfNoLayoutShiftMarker from './utils/is-in-vc-ignore-if-no-layout-shift-marker';
11
11
  function isElementVisible(element) {
12
12
  if (!(element instanceof HTMLElement)) {
@@ -186,13 +186,11 @@ export default class ViewportObserver {
186
186
  continue;
187
187
  }
188
188
  const {
189
- isWithinThirdPartySegment,
190
- ignoredReason
191
- } = checkThirdPartySegmentWithIgnoreReason(addedNode);
189
+ isWithin: isWithinThirdPartySegment
190
+ } = checkWithinComponentAndExtractChildProps(addedNode, 'UFOThirdPartySegment');
192
191
  if (isWithinThirdPartySegment) {
193
192
  var _this$intersectionObs7;
194
- const assignedReason = createMutationTypeWithIgnoredReason(ignoredReason || 'third-party-element');
195
- (_this$intersectionObs7 = this.intersectionObserver) === null || _this$intersectionObs7 === void 0 ? void 0 : _this$intersectionObs7.watchAndTag(addedNode, assignedReason);
193
+ (_this$intersectionObs7 = this.intersectionObserver) === null || _this$intersectionObs7 === void 0 ? void 0 : _this$intersectionObs7.watchAndTag(addedNode, 'mutation:third-party-element');
196
194
  continue;
197
195
  }
198
196
  (_this$intersectionObs8 = this.intersectionObserver) === null || _this$intersectionObs8 === void 0 ? void 0 : _this$intersectionObs8.watchAndTag(addedNode, createElementMutationsWatcher(removedNodeRects));
@@ -245,13 +243,11 @@ export default class ViewportObserver {
245
243
  };
246
244
  }
247
245
  const {
248
- isWithinThirdPartySegment,
249
- ignoredReason
250
- } = checkThirdPartySegmentWithIgnoreReason(target);
246
+ isWithin: isWithinThirdPartySegment
247
+ } = checkWithinComponentAndExtractChildProps(target, 'UFOThirdPartySegment');
251
248
  if (isWithinThirdPartySegment) {
252
- const assignedReason = createMutationTypeWithIgnoredReason(ignoredReason || 'third-party-element');
253
249
  return {
254
- type: assignedReason,
250
+ type: 'mutation:third-party-element',
255
251
  mutationData: {
256
252
  attributeName,
257
253
  oldValue,
@@ -1,13 +1,10 @@
1
1
  import { fg } from '@atlaskit/platform-feature-flags';
2
- // Helper function to find React fiber on an element
3
- const findReactFiber = element => {
4
- const key = Object.keys(element).find(key => key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$'));
5
- return key ? element[key] : null;
6
- };
2
+ import findReactFiber from './find-react-fiber';
3
+ import getComponentName from './get-component-name';
7
4
 
8
5
  // Using the React Fiber tree to traverse up the DOM and check if a node is within a specific component
9
6
  // and extract child component props if needed.
10
- export function checkWithinComponentAndExtractChildProps(node, targetComponentName, childComponentConfig) {
7
+ export default function checkWithinComponentAndExtractChildProps(node, targetComponentName, childComponentConfig) {
11
8
  let fiber = null;
12
9
  if (fg('platform_ufo_handle_non_react_element_for_3p')) {
13
10
  // Walk up the DOM tree to find React fiber (handles non-React-rendered elements)
@@ -33,14 +30,7 @@ export function checkWithinComponentAndExtractChildProps(node, targetComponentNa
33
30
  let currentFiber = fiber;
34
31
  let childProp;
35
32
  while (currentFiber) {
36
- let componentName;
37
- if (currentFiber.type) {
38
- if (typeof currentFiber.type === 'function') {
39
- componentName = currentFiber.type.displayName || currentFiber.type.name;
40
- } else if (typeof currentFiber.type === 'object' && (currentFiber.type.displayName || currentFiber.type.name)) {
41
- componentName = currentFiber.type.displayName || currentFiber.type.name;
42
- }
43
- }
33
+ const componentName = getComponentName(currentFiber);
44
34
 
45
35
  // Check if this is a child component we're looking for
46
36
  if (childComponentConfig && componentName === childComponentConfig.componentName) {
@@ -65,21 +55,4 @@ export function checkWithinComponentAndExtractChildProps(node, targetComponentNa
65
55
  return {
66
56
  isWithin: false
67
57
  };
68
- }
69
-
70
- // Check if the node is within a UFOThirdPartySegment and extract any UFOIgnoreHolds reason
71
- export function checkThirdPartySegmentWithIgnoreReason(node) {
72
- const result = checkWithinComponentAndExtractChildProps(node, 'UFOThirdPartySegment', {
73
- componentName: 'UFOIgnoreHolds',
74
- propName: 'reason'
75
- });
76
- return {
77
- isWithinThirdPartySegment: result.isWithin,
78
- ignoredReason: result.childProp
79
- };
80
- }
81
-
82
- // Helper function to create mutation type from UFOIgnoreHoldsReason
83
- export function createMutationTypeWithIgnoredReason(reason) {
84
- return `mutation:${reason}`;
85
58
  }
@@ -0,0 +1,10 @@
1
+ // Helper function to find React fiber on an element
2
+ export default function findReactFiber(element) {
3
+ // Use for...in loop instead of Object.keys().find() to avoid creating an array
4
+ for (const key in element) {
5
+ if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) {
6
+ return element[key];
7
+ }
8
+ }
9
+ return null;
10
+ }
@@ -0,0 +1,11 @@
1
+ // Helper function to extract component name from fiber
2
+ export default function getComponentName(fiber) {
3
+ const type = fiber === null || fiber === void 0 ? void 0 : fiber.type;
4
+ if (!type) {
5
+ return undefined;
6
+ }
7
+ if (typeof type === 'function' || typeof type === 'object') {
8
+ return type.displayName || type.name;
9
+ }
10
+ return undefined;
11
+ }
@@ -0,0 +1,29 @@
1
+ import { useContext, useMemo } from 'react';
2
+ import UFOInteractionContext from '../interaction-context';
3
+ import { getInteractionId } from '../interaction-id-context';
4
+ import { addCohortingCustomData } from '../interaction-metrics';
5
+ export default function UFOCustomCohortData(_ref) {
6
+ var dataKey = _ref.dataKey,
7
+ value = _ref.value;
8
+ var interactionContext = useContext(UFOInteractionContext);
9
+ useMemo(function () {
10
+ if (!interactionContext) {
11
+ return;
12
+ }
13
+ var interactionId = getInteractionId();
14
+ var currentInteractionId = interactionId.current;
15
+ if (!currentInteractionId) {
16
+ return;
17
+ }
18
+ addCohortingCustomData(currentInteractionId, dataKey, value);
19
+ }, [dataKey, value, interactionContext]);
20
+ return null;
21
+ }
22
+ export function addUFOCustomCohortData(key, value) {
23
+ var interactionId = getInteractionId();
24
+ var currentInteractionId = interactionId.current;
25
+ if (!currentInteractionId) {
26
+ return;
27
+ }
28
+ addCohortingCustomData(currentInteractionId, key, value);
29
+ }
File without changes
@@ -13,7 +13,7 @@ import { RLLPlaceholderHandlers } from '../../vc-observer/observers/rll-placehol
13
13
  import { createIntersectionObserver } from './intersection-observer';
14
14
  import createMutationObserver from './mutation-observer';
15
15
  import createPerformanceObserver from './performance-observer';
16
- import { checkThirdPartySegmentWithIgnoreReason, createMutationTypeWithIgnoredReason } from './utils/get-component-name-and-child-props';
16
+ import checkWithinComponentAndExtractChildProps from './utils/check-within-component-and-extract-child-props';
17
17
  import isInVCIgnoreIfNoLayoutShiftMarker from './utils/is-in-vc-ignore-if-no-layout-shift-marker';
18
18
  function isElementVisible(element) {
19
19
  if (!(element instanceof HTMLElement)) {
@@ -118,7 +118,7 @@ var ViewportObserver = /*#__PURE__*/function () {
118
118
  _context2.prev = 4;
119
119
  _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() {
120
120
  var _this$intersectionObs8;
121
- var addedNodeRef, addedNode, ssrState, SSRStateEnum, _this$intersectionObs, _this$intersectionObs2, ssrPlaceholderHandler, result, _this$intersectionObs3, _result, _this$intersectionObs4, sameDeletedNode, isInIgnoreLsMarker, isNoLsMarkerEnabled, _this$intersectionObs5, _this$intersectionObs6, _checkThirdPartySegme, isWithinThirdPartySegment, ignoredReason, _this$intersectionObs7, assignedReason;
121
+ var addedNodeRef, addedNode, ssrState, SSRStateEnum, _this$intersectionObs, _this$intersectionObs2, ssrPlaceholderHandler, result, _this$intersectionObs3, _result, _this$intersectionObs4, sameDeletedNode, isInIgnoreLsMarker, isNoLsMarkerEnabled, _this$intersectionObs5, _this$intersectionObs6, _checkWithinComponent, isWithinThirdPartySegment, _this$intersectionObs7;
122
122
  return _regeneratorRuntime.wrap(function _loop$(_context) {
123
123
  while (1) switch (_context.prev = _context.next) {
124
124
  case 0:
@@ -228,17 +228,16 @@ var ViewportObserver = /*#__PURE__*/function () {
228
228
  (_this$intersectionObs6 = _this.intersectionObserver) === null || _this$intersectionObs6 === void 0 || _this$intersectionObs6.watchAndTag(addedNode, 'mutation:media');
229
229
  return _context.abrupt("return", 0);
230
230
  case 45:
231
- _checkThirdPartySegme = checkThirdPartySegmentWithIgnoreReason(addedNode), isWithinThirdPartySegment = _checkThirdPartySegme.isWithinThirdPartySegment, ignoredReason = _checkThirdPartySegme.ignoredReason;
231
+ _checkWithinComponent = checkWithinComponentAndExtractChildProps(addedNode, 'UFOThirdPartySegment'), isWithinThirdPartySegment = _checkWithinComponent.isWithin;
232
232
  if (!isWithinThirdPartySegment) {
233
- _context.next = 50;
233
+ _context.next = 49;
234
234
  break;
235
235
  }
236
- assignedReason = createMutationTypeWithIgnoredReason(ignoredReason || 'third-party-element');
237
- (_this$intersectionObs7 = _this.intersectionObserver) === null || _this$intersectionObs7 === void 0 || _this$intersectionObs7.watchAndTag(addedNode, assignedReason);
236
+ (_this$intersectionObs7 = _this.intersectionObserver) === null || _this$intersectionObs7 === void 0 || _this$intersectionObs7.watchAndTag(addedNode, 'mutation:third-party-element');
238
237
  return _context.abrupt("return", 0);
239
- case 50:
238
+ case 49:
240
239
  (_this$intersectionObs8 = _this.intersectionObserver) === null || _this$intersectionObs8 === void 0 || _this$intersectionObs8.watchAndTag(addedNode, createElementMutationsWatcher(removedNodeRects));
241
- case 51:
240
+ case 50:
242
241
  case "end":
243
242
  return _context.stop();
244
243
  }
@@ -326,13 +325,11 @@ var ViewportObserver = /*#__PURE__*/function () {
326
325
  }
327
326
  };
328
327
  }
329
- var _checkThirdPartySegme2 = checkThirdPartySegmentWithIgnoreReason(target),
330
- isWithinThirdPartySegment = _checkThirdPartySegme2.isWithinThirdPartySegment,
331
- ignoredReason = _checkThirdPartySegme2.ignoredReason;
328
+ var _checkWithinComponent2 = checkWithinComponentAndExtractChildProps(target, 'UFOThirdPartySegment'),
329
+ isWithinThirdPartySegment = _checkWithinComponent2.isWithin;
332
330
  if (isWithinThirdPartySegment) {
333
- var assignedReason = createMutationTypeWithIgnoredReason(ignoredReason || 'third-party-element');
334
331
  return {
335
- type: assignedReason,
332
+ type: 'mutation:third-party-element',
336
333
  mutationData: {
337
334
  attributeName: attributeName,
338
335
  oldValue: oldValue,
@@ -1,19 +1,13 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _typeof from "@babel/runtime/helpers/typeof";
3
2
  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; }
4
3
  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; }
5
4
  import { fg } from '@atlaskit/platform-feature-flags';
6
- // Helper function to find React fiber on an element
7
- var findReactFiber = function findReactFiber(element) {
8
- var key = Object.keys(element).find(function (key) {
9
- return key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$');
10
- });
11
- return key ? element[key] : null;
12
- };
5
+ import findReactFiber from './find-react-fiber';
6
+ import getComponentName from './get-component-name';
13
7
 
14
8
  // Using the React Fiber tree to traverse up the DOM and check if a node is within a specific component
15
9
  // and extract child component props if needed.
16
- export function checkWithinComponentAndExtractChildProps(node, targetComponentName, childComponentConfig) {
10
+ export default function checkWithinComponentAndExtractChildProps(node, targetComponentName, childComponentConfig) {
17
11
  var fiber = null;
18
12
  if (fg('platform_ufo_handle_non_react_element_for_3p')) {
19
13
  // Walk up the DOM tree to find React fiber (handles non-React-rendered elements)
@@ -39,14 +33,7 @@ export function checkWithinComponentAndExtractChildProps(node, targetComponentNa
39
33
  var currentFiber = fiber;
40
34
  var childProp;
41
35
  while (currentFiber) {
42
- var componentName = void 0;
43
- if (currentFiber.type) {
44
- if (typeof currentFiber.type === 'function') {
45
- componentName = currentFiber.type.displayName || currentFiber.type.name;
46
- } else if (_typeof(currentFiber.type) === 'object' && (currentFiber.type.displayName || currentFiber.type.name)) {
47
- componentName = currentFiber.type.displayName || currentFiber.type.name;
48
- }
49
- }
36
+ var componentName = getComponentName(currentFiber);
50
37
 
51
38
  // Check if this is a child component we're looking for
52
39
  if (childComponentConfig && componentName === childComponentConfig.componentName) {
@@ -70,21 +57,4 @@ export function checkWithinComponentAndExtractChildProps(node, targetComponentNa
70
57
  return {
71
58
  isWithin: false
72
59
  };
73
- }
74
-
75
- // Check if the node is within a UFOThirdPartySegment and extract any UFOIgnoreHolds reason
76
- export function checkThirdPartySegmentWithIgnoreReason(node) {
77
- var result = checkWithinComponentAndExtractChildProps(node, 'UFOThirdPartySegment', {
78
- componentName: 'UFOIgnoreHolds',
79
- propName: 'reason'
80
- });
81
- return {
82
- isWithinThirdPartySegment: result.isWithin,
83
- ignoredReason: result.childProp
84
- };
85
- }
86
-
87
- // Helper function to create mutation type from UFOIgnoreHoldsReason
88
- export function createMutationTypeWithIgnoredReason(reason) {
89
- return "mutation:".concat(reason);
90
60
  }
@@ -0,0 +1,10 @@
1
+ // Helper function to find React fiber on an element
2
+ export default function findReactFiber(element) {
3
+ // Use for...in loop instead of Object.keys().find() to avoid creating an array
4
+ for (var key in element) {
5
+ if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) {
6
+ return element[key];
7
+ }
8
+ }
9
+ return null;
10
+ }
@@ -0,0 +1,12 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ // Helper function to extract component name from fiber
3
+ export default function getComponentName(fiber) {
4
+ var type = fiber === null || fiber === void 0 ? void 0 : fiber.type;
5
+ if (!type) {
6
+ return undefined;
7
+ }
8
+ if (typeof type === 'function' || _typeof(type) === 'object') {
9
+ return type.displayName || type.name;
10
+ }
11
+ return undefined;
12
+ }
@@ -0,0 +1,4 @@
1
+ import type { UFOCustomCohortDataProps } from './types';
2
+ export type { UFOCustomCohortDataProps } from './types';
3
+ export default function UFOCustomCohortData({ dataKey, value }: UFOCustomCohortDataProps): null;
4
+ export declare function addUFOCustomCohortData(key: string, value: number | boolean | string | null | undefined): void;
@@ -0,0 +1,4 @@
1
+ export type UFOCustomCohortDataProps = {
2
+ dataKey: string;
3
+ value: number | boolean | string | null | undefined;
4
+ };
@@ -0,0 +1,8 @@
1
+ export default function checkWithinComponentAndExtractChildProps<T = string>(node: HTMLElement, targetComponentName: string, childComponentConfig?: {
2
+ componentName: string;
3
+ propName: string;
4
+ extractValue?: (props: any) => T;
5
+ }): {
6
+ isWithin: boolean;
7
+ childProp?: T;
8
+ };
@@ -0,0 +1 @@
1
+ export default function findReactFiber(element: HTMLElement): any;
@@ -0,0 +1 @@
1
+ export default function getComponentName(fiber: any): string | undefined;
@@ -0,0 +1,4 @@
1
+ import type { UFOCustomCohortDataProps } from './types';
2
+ export type { UFOCustomCohortDataProps } from './types';
3
+ export default function UFOCustomCohortData({ dataKey, value }: UFOCustomCohortDataProps): null;
4
+ export declare function addUFOCustomCohortData(key: string, value: number | boolean | string | null | undefined): void;
@@ -0,0 +1,4 @@
1
+ export type UFOCustomCohortDataProps = {
2
+ dataKey: string;
3
+ value: number | boolean | string | null | undefined;
4
+ };
@@ -0,0 +1,8 @@
1
+ export default function checkWithinComponentAndExtractChildProps<T = string>(node: HTMLElement, targetComponentName: string, childComponentConfig?: {
2
+ componentName: string;
3
+ propName: string;
4
+ extractValue?: (props: any) => T;
5
+ }): {
6
+ isWithin: boolean;
7
+ childProp?: T;
8
+ };
@@ -0,0 +1 @@
1
+ export default function findReactFiber(element: HTMLElement): any;
@@ -0,0 +1 @@
1
+ export default function getComponentName(fiber: any): string | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/react-ufo",
3
- "version": "4.0.3",
3
+ "version": "4.1.0",
4
4
  "description": "Parts of React UFO that are publicly available",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -35,6 +35,7 @@
35
35
  "./create-payload": "./src/create-payload/index.ts",
36
36
  "./create-post-interaction-log-payload": "./src/create-post-interaction-log-payload/index.ts",
37
37
  "./custom-data": "./src/custom-data/index.ts",
38
+ "./custom-cohort-data": "./src/custom-cohort-data/index.ts",
38
39
  "./custom-mark": "./src/custom-mark/index.ts",
39
40
  "./custom-spans": "./src/custom-spans/index.ts",
40
41
  "./custom-timings": "./src/custom-timings/index.ts",
@@ -1,15 +0,0 @@
1
- import type { UFOIgnoreHoldsReason } from '../../../../ignore-holds';
2
- import type { VCObserverEntryType } from '../../types';
3
- export declare function checkWithinComponentAndExtractChildProps<T = string>(node: HTMLElement, targetComponentName: string, childComponentConfig?: {
4
- componentName: string;
5
- propName: string;
6
- extractValue?: (props: any) => T;
7
- }): {
8
- isWithin: boolean;
9
- childProp?: T;
10
- };
11
- export declare function checkThirdPartySegmentWithIgnoreReason(node: HTMLElement): {
12
- isWithinThirdPartySegment: boolean;
13
- ignoredReason?: UFOIgnoreHoldsReason;
14
- };
15
- export declare function createMutationTypeWithIgnoredReason(reason: UFOIgnoreHoldsReason): VCObserverEntryType;
@@ -1,15 +0,0 @@
1
- import type { UFOIgnoreHoldsReason } from '../../../../ignore-holds';
2
- import type { VCObserverEntryType } from '../../types';
3
- export declare function checkWithinComponentAndExtractChildProps<T = string>(node: HTMLElement, targetComponentName: string, childComponentConfig?: {
4
- componentName: string;
5
- propName: string;
6
- extractValue?: (props: any) => T;
7
- }): {
8
- isWithin: boolean;
9
- childProp?: T;
10
- };
11
- export declare function checkThirdPartySegmentWithIgnoreReason(node: HTMLElement): {
12
- isWithinThirdPartySegment: boolean;
13
- ignoredReason?: UFOIgnoreHoldsReason;
14
- };
15
- export declare function createMutationTypeWithIgnoredReason(reason: UFOIgnoreHoldsReason): VCObserverEntryType;