@atlaskit/media-card 71.0.0 → 72.0.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 (110) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/errors.js +56 -5
  3. package/dist/cjs/files/cardImageView/index.js +53 -104
  4. package/dist/cjs/files/index.js +0 -6
  5. package/dist/cjs/root/card/cardAnalytics.js +3 -15
  6. package/dist/cjs/root/card/cardSSRView.js +4 -2
  7. package/dist/cjs/root/card/cardState.js +50 -0
  8. package/dist/cjs/root/card/getCardPreview/cache.js +5 -0
  9. package/dist/cjs/root/card/getCardPreview/filePreviewStatus.js +50 -0
  10. package/dist/cjs/root/card/getCardPreview/helpers.js +13 -21
  11. package/dist/cjs/root/card/getCardPreview/index.js +91 -94
  12. package/dist/cjs/root/card/getCardStatus.js +7 -1
  13. package/dist/cjs/root/card/index.js +271 -266
  14. package/dist/cjs/root/cardView.js +96 -60
  15. package/dist/cjs/root/inline/loader.js +2 -1
  16. package/dist/cjs/root/inline/mediaInlineCard.js +4 -3
  17. package/dist/cjs/root/ui/iconMessage/index.js +12 -5
  18. package/dist/cjs/root/ui/imageRenderer/imageRenderer.js +30 -124
  19. package/dist/cjs/root/ui/titleBox/failedTitleBox.js +7 -3
  20. package/dist/cjs/utils/analytics.js +6 -44
  21. package/dist/cjs/utils/dimensionComparer.js +1 -1
  22. package/dist/cjs/utils/metadata.js +11 -3
  23. package/dist/cjs/utils/objectURLCache.js +6 -0
  24. package/dist/cjs/utils/shouldDisplayImageThumbnail.js +1 -1
  25. package/dist/cjs/version.json +1 -1
  26. package/dist/es2019/errors.js +34 -2
  27. package/dist/es2019/files/cardImageView/index.js +8 -61
  28. package/dist/es2019/files/index.js +1 -1
  29. package/dist/es2019/root/card/cardAnalytics.js +3 -14
  30. package/dist/es2019/root/card/cardSSRView.js +3 -2
  31. package/dist/es2019/root/card/cardState.js +26 -0
  32. package/dist/es2019/root/card/getCardPreview/cache.js +5 -0
  33. package/dist/es2019/root/card/getCardPreview/filePreviewStatus.js +35 -0
  34. package/dist/es2019/root/card/getCardPreview/helpers.js +2 -12
  35. package/dist/es2019/root/card/getCardPreview/index.js +71 -74
  36. package/dist/es2019/root/card/getCardStatus.js +1 -0
  37. package/dist/es2019/root/card/index.js +193 -171
  38. package/dist/es2019/root/cardView.js +100 -61
  39. package/dist/es2019/root/inline/loader.js +2 -1
  40. package/dist/es2019/root/inline/mediaInlineCard.js +4 -3
  41. package/dist/es2019/root/ui/iconMessage/index.js +5 -3
  42. package/dist/es2019/root/ui/imageRenderer/imageRenderer.js +21 -88
  43. package/dist/es2019/root/ui/titleBox/failedTitleBox.js +5 -3
  44. package/dist/es2019/utils/analytics.js +5 -34
  45. package/dist/es2019/utils/dimensionComparer.js +1 -1
  46. package/dist/es2019/utils/metadata.js +12 -4
  47. package/dist/es2019/utils/objectURLCache.js +5 -0
  48. package/dist/es2019/utils/shouldDisplayImageThumbnail.js +1 -1
  49. package/dist/es2019/version.json +1 -1
  50. package/dist/esm/errors.js +42 -1
  51. package/dist/esm/files/cardImageView/index.js +51 -102
  52. package/dist/esm/files/index.js +1 -1
  53. package/dist/esm/root/card/cardAnalytics.js +3 -15
  54. package/dist/esm/root/card/cardSSRView.js +3 -2
  55. package/dist/esm/root/card/cardState.js +32 -0
  56. package/dist/esm/root/card/getCardPreview/cache.js +6 -0
  57. package/dist/esm/root/card/getCardPreview/filePreviewStatus.js +35 -0
  58. package/dist/esm/root/card/getCardPreview/helpers.js +13 -21
  59. package/dist/esm/root/card/getCardPreview/index.js +79 -88
  60. package/dist/esm/root/card/getCardStatus.js +3 -0
  61. package/dist/esm/root/card/index.js +278 -268
  62. package/dist/esm/root/cardView.js +98 -61
  63. package/dist/esm/root/inline/loader.js +2 -1
  64. package/dist/esm/root/inline/mediaInlineCard.js +4 -3
  65. package/dist/esm/root/ui/iconMessage/index.js +7 -3
  66. package/dist/esm/root/ui/imageRenderer/imageRenderer.js +23 -118
  67. package/dist/esm/root/ui/titleBox/failedTitleBox.js +6 -3
  68. package/dist/esm/utils/analytics.js +5 -36
  69. package/dist/esm/utils/dimensionComparer.js +1 -1
  70. package/dist/esm/utils/metadata.js +12 -4
  71. package/dist/esm/utils/objectURLCache.js +6 -0
  72. package/dist/esm/utils/shouldDisplayImageThumbnail.js +1 -1
  73. package/dist/esm/version.json +1 -1
  74. package/dist/types/errors.d.ts +9 -1
  75. package/dist/types/files/cardImageView/index.d.ts +4 -13
  76. package/dist/types/files/cardImageView/styled.d.ts +1 -1
  77. package/dist/types/files/index.d.ts +1 -1
  78. package/dist/types/index.d.ts +4 -3
  79. package/dist/types/root/card/cardAnalytics.d.ts +1 -6
  80. package/dist/types/root/card/cardSSRView.d.ts +1 -1
  81. package/dist/types/root/card/cardState.d.ts +5 -0
  82. package/dist/types/root/card/getCardPreview/cache.d.ts +3 -1
  83. package/dist/types/root/card/getCardPreview/filePreviewStatus.d.ts +5 -0
  84. package/dist/types/root/card/getCardPreview/helpers.d.ts +3 -4
  85. package/dist/types/root/card/getCardPreview/index.d.ts +17 -13
  86. package/dist/types/root/card/getCardStatus.d.ts +1 -0
  87. package/dist/types/root/card/index.d.ts +11 -10
  88. package/dist/types/root/cardView.d.ts +11 -8
  89. package/dist/types/root/inlinePlayer.d.ts +1 -1
  90. package/dist/types/root/ui/iconMessage/index.d.ts +2 -1
  91. package/dist/types/root/ui/imageRenderer/imageRenderer.d.ts +2 -13
  92. package/dist/types/root/ui/styledSSR.d.ts +1 -1
  93. package/dist/types/root/ui/titleBox/failedTitleBox.d.ts +2 -0
  94. package/dist/types/styles/mixins.d.ts +1 -1
  95. package/dist/types/types.d.ts +7 -1
  96. package/dist/types/utils/analytics.d.ts +6 -15
  97. package/dist/types/utils/cardDimensions.d.ts +4 -4
  98. package/dist/types/utils/dimensionComparer.d.ts +1 -1
  99. package/dist/types/utils/metadata.d.ts +2 -2
  100. package/dist/types/utils/objectURLCache.d.ts +2 -1
  101. package/example-helpers/index.tsx +21 -0
  102. package/package.json +6 -5
  103. package/dist/cjs/root/card/getCardPreview/types.js +0 -5
  104. package/dist/cjs/utils/fileAttributesContext.js +0 -40
  105. package/dist/es2019/root/card/getCardPreview/types.js +0 -1
  106. package/dist/es2019/utils/fileAttributesContext.js +0 -19
  107. package/dist/esm/root/card/getCardPreview/types.js +0 -1
  108. package/dist/esm/utils/fileAttributesContext.js +0 -18
  109. package/dist/types/root/card/getCardPreview/types.d.ts +0 -5
  110. package/dist/types/utils/fileAttributesContext.d.ts +0 -10
@@ -5,22 +5,20 @@ import { version as packageVersion, name as packageName } from '../../version.js
5
5
  import { withAnalyticsEvents } from '@atlaskit/analytics-next';
6
6
  import { withMediaAnalyticsContext } from '@atlaskit/media-common';
7
7
  import DownloadIcon from '@atlaskit/icon/glyph/download';
8
- import { addFileAttrsToUrl, globalMediaEventEmitter, isDifferentIdentifier, isFileIdentifier, isErrorFileState, RECENTS_COLLECTION, isImageRepresentationReady, isExternalImageIdentifier } from '@atlaskit/media-client';
8
+ import { globalMediaEventEmitter, isDifferentIdentifier, isFileIdentifier, RECENTS_COLLECTION, isImageRepresentationReady, isExternalImageIdentifier, imageResizeModeToFileImageMode } from '@atlaskit/media-client';
9
9
  import { MediaViewer } from '@atlaskit/media-viewer';
10
10
  import { IntlProvider, intlShape } from 'react-intl';
11
11
  import { CardView } from '../cardView';
12
12
  import { ViewportDetector } from '../../utils/viewportDetector';
13
13
  import { getRequestedDimensions } from '../../utils/getDataURIDimension';
14
- import { shouldGetCardPreview, extractFilePreviewStatus, getCardPreview, getCardPreviewFromCache, getFilePreviewFromFileState } from './getCardPreview';
14
+ import { getCardPreview, getCardPreviewFromCache, removeCardPreviewFromCache, getFilePreviewFromFileState, shouldResolvePreview } from './getCardPreview';
15
15
  import { getFileDetails } from '../../utils/metadata';
16
- import { isBigger } from '../../utils/dimensionComparer';
17
- import { getCardStatus } from './getCardStatus';
18
16
  import { InlinePlayer } from '../inlinePlayer';
19
17
  import { getFileAttributes } from '../../utils/analytics';
20
- import { FileAttributesProvider } from '../../utils/fileAttributesContext';
21
- import { isRemotePreviewError, MediaCardError, ensureMediaCardError } from '../../errors';
18
+ import { isLocalPreviewError, MediaCardError, ensureMediaCardError, ImageLoadError } from '../../errors';
22
19
  import { fireOperationalEvent, fireCommencedEvent, relevantFeatureFlagNames, fireCopiedEvent, fireScreenEvent } from './cardAnalytics';
23
20
  import getDocument from '../../utils/document';
21
+ import { getCardStateFromFileState, createStateUpdater } from './cardState';
24
22
  export class CardBase extends Component {
25
23
  // We initialise timeElapsedTillCommenced
26
24
  // to avoid extra branching on a possibly undefined value.
@@ -31,75 +29,136 @@ export class CardBase extends Component {
31
29
 
32
30
  _defineProperty(this, "timeElapsedTillCommenced", performance.now());
33
31
 
34
- _defineProperty(this, "shouldRefetchImage", (current, next) => {
35
- if (!current || !next) {
36
- return false;
37
- }
38
-
39
- return isBigger(current, next);
40
- });
41
-
42
- _defineProperty(this, "isLatestCardStatusUpdate", cardStatusUpdateTimestamp => !this.lastCardStatusUpdateTimestamp || this.lastCardStatusUpdateTimestamp <= cardStatusUpdateTimestamp);
43
-
44
- _defineProperty(this, "onLocalPreviewError", error => {// TODO: track local preview success rate
45
- // https://product-fabric.atlassian.net/browse/MEX-774
46
- });
32
+ _defineProperty(this, "getImageURLParams", ({
33
+ collectionName: collection
34
+ }) => ({
35
+ collection,
36
+ mode: imageResizeModeToFileImageMode(this.props.resizeMode),
37
+ ...this.requestedDimensions,
38
+ allowAnimated: true
39
+ }));
47
40
 
48
- _defineProperty(this, "createAddContextToDataURI", (fileId, fileState, {
49
- width,
50
- height
51
- }, collectionName) => dataURI => {
41
+ _defineProperty(this, "getMediaBlobUrlAttrs", (identifier, fileState) => {
52
42
  const {
43
+ id,
44
+ collectionName: collection
45
+ } = identifier;
46
+ const {
47
+ originalDimensions,
53
48
  contextId,
54
49
  alt
55
50
  } = this.props;
56
-
57
- if (!contextId) {
58
- return dataURI;
59
- }
60
-
61
- const metadata = getFileDetails(fileState);
62
- return addFileAttrsToUrl(dataURI, {
63
- id: fileId,
64
- collection: collectionName,
51
+ const {
52
+ mimeType,
53
+ name,
54
+ size
55
+ } = getFileDetails(identifier, fileState);
56
+ return contextId ? {
57
+ id,
58
+ collection,
65
59
  contextId,
66
- mimeType: metadata.mimeType,
67
- name: metadata.name,
68
- size: metadata.size,
69
- width,
70
- height,
60
+ mimeType,
61
+ name,
62
+ size,
63
+ ...(originalDimensions || this.requestedDimensions),
71
64
  alt
72
- });
65
+ } : undefined;
73
66
  });
74
67
 
75
- _defineProperty(this, "getCardPreviewParams", (id, collectionName, fileState) => {
68
+ _defineProperty(this, "getCardPreviewParams", (identifier, fileState) => {
69
+ const {
70
+ isBannedLocalPreview
71
+ } = this.state;
72
+ const {
73
+ id
74
+ } = identifier;
76
75
  const {
77
76
  dimensions = {},
78
- originalDimensions,
79
- resizeMode,
80
77
  mediaClient
81
78
  } = this.props;
82
- const {
83
- cardRef
84
- } = this.state;
85
- const requestedDimensions = getRequestedDimensions({
86
- dimensions,
87
- element: cardRef
88
- });
89
79
  return {
90
80
  mediaClient,
91
81
  id,
92
- collectionName,
93
82
  dimensions,
94
- resizeMode,
95
- requestedDimensions,
96
- onLocalPreviewError: this.onLocalPreviewError,
97
- filePreview: getFilePreviewFromFileState(fileState),
83
+ onLocalPreviewError: this.fireLocalPreviewErrorEvent,
84
+ filePreview: isBannedLocalPreview ? undefined : getFilePreviewFromFileState(fileState),
98
85
  isRemotePreviewReady: isImageRepresentationReady(fileState),
99
- addContextToDataURI: this.createAddContextToDataURI(id, fileState, originalDimensions || requestedDimensions, collectionName)
86
+ imageUrlParams: this.getImageURLParams(identifier),
87
+ mediaBlobUrlAttrs: this.getMediaBlobUrlAttrs(identifier, fileState)
88
+ };
89
+ });
90
+
91
+ _defineProperty(this, "resolvePreview", async (identifier, fileState) => {
92
+ try {
93
+ const params = this.getCardPreviewParams(identifier, fileState);
94
+ const cardPreview = await getCardPreview(params);
95
+ this.safeSetState({
96
+ cardPreview
97
+ });
98
+ } catch (e) {
99
+ const wrappedError = ensureMediaCardError('preview-fetch', e); // If remote preview fails, we set status 'error'
100
+ // If local preview fails (i.e, no remote preview available),
101
+ // we can stay in the same status until there is a remote preview available
102
+ // If it's any other error we set status 'error'
103
+
104
+ if (isLocalPreviewError(wrappedError)) {
105
+ this.safeSetState({
106
+ isBannedLocalPreview: true
107
+ });
108
+ } else {
109
+ this.safeSetState({
110
+ status: 'error',
111
+ error: wrappedError
112
+ });
113
+ }
114
+ }
115
+ });
116
+
117
+ _defineProperty(this, "getPerformanceAttributes", () => {
118
+ const {
119
+ timeElapsedTillCommenced
120
+ } = this;
121
+ const timeElapsedTillEvent = performance.now();
122
+ const durationSinceCommenced = timeElapsedTillCommenced && timeElapsedTillEvent - timeElapsedTillCommenced;
123
+ return {
124
+ overall: {
125
+ durationSincePageStart: timeElapsedTillEvent,
126
+ durationSinceCommenced
127
+ }
100
128
  };
101
129
  });
102
130
 
131
+ _defineProperty(this, "onImageError", () => {
132
+ const {
133
+ cardPreview
134
+ } = this.state;
135
+ const error = new ImageLoadError(cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.source);
136
+
137
+ if (cardPreview !== null && cardPreview !== void 0 && cardPreview.source && ['local', 'cache-local'].includes(cardPreview.source)) {
138
+ const {
139
+ identifier,
140
+ dimensions = {}
141
+ } = this.props;
142
+ isFileIdentifier(identifier) && removeCardPreviewFromCache(identifier.id, dimensions);
143
+ this.safeSetState({
144
+ cardPreview: undefined,
145
+ isBannedLocalPreview: true
146
+ });
147
+ this.fireLocalPreviewErrorEvent(error);
148
+ } else {
149
+ this.safeSetState({
150
+ status: 'error',
151
+ error
152
+ });
153
+ }
154
+ });
155
+
156
+ _defineProperty(this, "onImageLoad", () => {
157
+ this.safeSetState({
158
+ previewDidRender: true
159
+ });
160
+ });
161
+
103
162
  _defineProperty(this, "fireCopiedEvent", () => {
104
163
  const {
105
164
  createAnalyticsEvent
@@ -117,9 +176,17 @@ export class CardBase extends Component {
117
176
  createAnalyticsEvent && fireScreenEvent(createAnalyticsEvent, this.fileAttributes);
118
177
  });
119
178
 
179
+ _defineProperty(this, "fireLocalPreviewErrorEvent", error => {// TODO: track local preview success rate
180
+ // https://product-fabric.atlassian.net/browse/MEX-774
181
+ });
182
+
120
183
  _defineProperty(this, "safeSetState", state => {
121
184
  if (this.hasBeenMounted) {
122
- this.setState(state);
185
+ // If it's setting the status, we need to use a state updater function,
186
+ // which ensures that no non-final status overrides a final status.
187
+ // If no status is set, we don't need a sate updater
188
+ const updater = !!state.status ? createStateUpdater(state) : state;
189
+ this.setState(updater);
123
190
  }
124
191
  });
125
192
 
@@ -129,8 +196,9 @@ export class CardBase extends Component {
129
196
  }
130
197
 
131
198
  if (this.hasBeenMounted) {
199
+ // TODO MEX-788: add test for "do not remove the card preview when unsubscribing".
132
200
  this.setState({
133
- cardPreview: undefined
201
+ isBannedLocalPreview: false
134
202
  });
135
203
  }
136
204
  });
@@ -311,8 +379,7 @@ export class CardBase extends Component {
311
379
  cardRef
312
380
  } = this.state;
313
381
  const {
314
- metadata,
315
- timeElapsedTillCommenced
382
+ metadata
316
383
  } = this;
317
384
  const {
318
385
  onCardViewClick,
@@ -322,7 +389,7 @@ export class CardBase extends Component {
322
389
  } = this;
323
390
  const card = /*#__PURE__*/React.createElement(CardView, {
324
391
  status: status,
325
- error: error && error.secondaryError,
392
+ error: error,
326
393
  mediaItemType: mediaItemType,
327
394
  metadata: metadata,
328
395
  dataURI: dataURI,
@@ -344,7 +411,8 @@ export class CardBase extends Component {
344
411
  featureFlags: featureFlags,
345
412
  titleBoxBgColor: titleBoxBgColor,
346
413
  titleBoxIcon: titleBoxIcon,
347
- timeElapsedTillCommenced: timeElapsedTillCommenced
414
+ onImageError: this.onImageError,
415
+ onImageLoad: this.onImageLoad
348
416
  });
349
417
  return isLazy ? /*#__PURE__*/React.createElement(ViewportDetector, {
350
418
  targetRef: cardRef,
@@ -408,7 +476,7 @@ export class CardBase extends Component {
408
476
  _cardPreview = getCardPreviewFromCache(id, _dimensions);
409
477
  } else if (isExternalImageIdentifier(_identifier)) {
410
478
  this.fireCommencedEvent();
411
- _status = 'complete';
479
+ _status = 'loading-preview';
412
480
  const {
413
481
  dataURI
414
482
  } = _identifier;
@@ -417,11 +485,8 @@ export class CardBase extends Component {
417
485
  orientation: 1,
418
486
  source: 'external'
419
487
  };
420
- }
421
- /**
422
- * If cardPreview is available from local cache, `isCardVisible`
423
- * should be true to avoid flickers during re-mount of the component
424
- */
488
+ } // If cardPreview is available from local cache, `isCardVisible`
489
+ // should be true to avoid flickers during re-mount of the component
425
490
 
426
491
 
427
492
  const isCardVisible = _cardPreview ? true : !this.props.isLazy;
@@ -430,7 +495,9 @@ export class CardBase extends Component {
430
495
  isCardVisible,
431
496
  isPlayingFile: false,
432
497
  cardPreview: _cardPreview,
433
- cardRef: null
498
+ cardRef: null,
499
+ isBannedLocalPreview: false,
500
+ previewDidRender: false
434
501
  };
435
502
  }
436
503
 
@@ -466,14 +533,20 @@ export class CardBase extends Component {
466
533
  const {
467
534
  mediaClient,
468
535
  identifier,
469
- dimensions
536
+ dimensions,
537
+ featureFlags
470
538
  } = this.props;
471
539
  const {
472
540
  isCardVisible,
473
- cardPreview
541
+ cardPreview,
542
+ status,
543
+ fileState,
544
+ isBannedLocalPreview,
545
+ previewDidRender
474
546
  } = this.state;
475
547
  const isDifferent = isDifferentIdentifier(prevIdentifier, identifier);
476
548
  const turnedVisible = !prevIsCardVisible && isCardVisible;
549
+ const isNewMediaClient = prevMediaClient !== mediaClient;
477
550
 
478
551
  if (isExternalImageIdentifier(identifier) && isDifferent) {
479
552
  this.fireCommencedEvent();
@@ -481,7 +554,7 @@ export class CardBase extends Component {
481
554
  dataURI
482
555
  } = identifier;
483
556
  this.setState({
484
- status: 'complete',
557
+ status: 'loading-preview',
485
558
  cardPreview: {
486
559
  dataURI,
487
560
  orientation: 1,
@@ -491,8 +564,7 @@ export class CardBase extends Component {
491
564
  });
492
565
  }
493
566
 
494
- if (isFileIdentifier(identifier) && (turnedVisible || prevMediaClient !== mediaClient || isDifferent || // TODO: should not resubscribe on resize. Only refetch
495
- this.shouldRefetchImage(prevDimensions, dimensions))) {
567
+ if (isFileIdentifier(identifier) && (turnedVisible || !!this.subscription && (isNewMediaClient || isDifferent))) {
496
568
  this.updateStateForIdentifier(identifier);
497
569
  }
498
570
 
@@ -503,6 +575,26 @@ export class CardBase extends Component {
503
575
  this.fireScreenEvent();
504
576
  }
505
577
  }
578
+
579
+ if (isFileIdentifier(identifier) && fileState && shouldResolvePreview({
580
+ status,
581
+ fileState,
582
+ dimensions,
583
+ prevDimensions,
584
+ featureFlags,
585
+ hasCardPreview: !!cardPreview,
586
+ isBannedLocalPreview
587
+ })) {
588
+ this.resolvePreview(identifier, fileState);
589
+ }
590
+
591
+ if (previewDidRender && // We should't complete if status is uploading
592
+ ['loading', 'loading-preview', 'processing'].includes(status)) {
593
+ this.safeSetState({
594
+ status: 'complete'
595
+ });
596
+ this.unsubscribe();
597
+ }
506
598
  }
507
599
 
508
600
  componentWillUnmount() {
@@ -520,6 +612,9 @@ export class CardBase extends Component {
520
612
  const {
521
613
  mediaClient
522
614
  } = this.props;
615
+ const {
616
+ isBannedLocalPreview
617
+ } = this.state;
523
618
  const {
524
619
  id,
525
620
  collectionName,
@@ -530,126 +625,58 @@ export class CardBase extends Component {
530
625
  collectionName,
531
626
  occurrenceKey
532
627
  }).subscribe({
533
- next: async fileState => {
534
- this.lastFileState = fileState;
535
- const thisCardStatusUpdateTimestamp = (performance || Date).now();
536
- const filePreviewStatus = extractFilePreviewStatus(fileState, this.props.featureFlags);
537
- let status = getCardStatus(fileState.status, filePreviewStatus);
538
- this.safeSetState({
539
- fileState
540
- });
541
- let cardPreview;
542
- let error;
543
-
544
- if (shouldGetCardPreview(status, filePreviewStatus)) {
545
- try {
546
- cardPreview = await getCardPreview(this.getCardPreviewParams(id, collectionName, fileState));
547
-
548
- if (['loading-preview', 'processing'].includes(status)) {
549
- status = 'complete';
550
- }
551
- } catch (e) {
552
- const wrappedError = ensureMediaCardError('preview-fetch', e); // If remote preview fails, we set status 'error'
553
- // If the local preview fails (i.e, no remote preview available),
554
- // we can stay in the same status until there is a remote preview available
555
-
556
- if (isRemotePreviewError(wrappedError)) {
557
- status = 'error';
558
- error = wrappedError;
559
- }
560
- }
561
- }
562
-
563
- if (this.isLatestCardStatusUpdate(thisCardStatusUpdateTimestamp)) {
564
- // These status and progress must not override values representing more recent FileState
565
-
566
- /* next() start some await() delay in next() status & progress update
567
- * ------- ------------------ ------------------------
568
- * |----[1]FileState:uploading------>| |
569
- * | | |
570
- * |----[2]FileState:uploading------>| |
571
- * | | |
572
- * | |----[2]FileState:uploading------>| Update status to `uploading`
573
- * |----[3]FileState:processing----->| |
574
- * | |----[3]FileState:processing----->| Update status to `complete`
575
- * | | |
576
- * | |----[1]FileState:uploading------>| We do not want to update status to `uploading` again!
577
- *
578
- */
579
- if (status === 'error' && isErrorFileState(fileState) && !error) {
580
- error = new MediaCardError('error-file-state', new Error(fileState.message));
581
- }
582
-
583
- this.safeSetState({
584
- status,
585
- cardPreview,
586
- progress: status === 'uploading' && fileState.status === 'uploading' ? fileState.progress : 1,
587
- error: status === 'error' && error ? error : undefined
588
- });
589
- this.lastCardStatusUpdateTimestamp = thisCardStatusUpdateTimestamp;
590
- }
628
+ next: fileState => {
629
+ const {
630
+ featureFlags
631
+ } = this.props;
632
+ const newState = getCardStateFromFileState(fileState, isBannedLocalPreview, featureFlags);
633
+ this.safeSetState(newState);
591
634
  },
592
635
  error: e => {
593
- // If file state subscription decides that the card is complete
594
- // and later there is an error, we won't change the card's status.
595
- if (this.state.status === 'complete') {
596
- return;
597
- }
598
-
599
- const errorReason = this.fileAttributes.fileStatus === 'uploading' ? 'upload' : 'metadata-fetch';
636
+ const errorReason = this.state.status === 'uploading' ? 'upload' : 'metadata-fetch';
600
637
  const error = new MediaCardError(errorReason, e);
601
638
  this.safeSetState({
602
639
  error,
603
640
  status: 'error'
604
641
  });
605
- this.lastCardStatusUpdateTimestamp = (performance || Date).now();
606
642
  }
607
643
  });
608
644
  }
609
645
 
610
- get metadata() {
646
+ get requestedDimensions() {
611
647
  const {
612
- identifier
648
+ dimensions
613
649
  } = this.props;
614
- return isFileIdentifier(identifier) ? this.state.fileState ? getFileDetails(this.state.fileState) : {
615
- id: identifier.id
616
- } : {
617
- id: identifier.mediaItemType,
618
- name: identifier.name || identifier.dataURI,
619
- mediaType: 'image'
620
- };
650
+ const {
651
+ cardRef: element
652
+ } = this.state;
653
+ return getRequestedDimensions({
654
+ dimensions,
655
+ element
656
+ });
657
+ }
658
+
659
+ get metadata() {
660
+ var _this$state;
661
+
662
+ return getFileDetails(this.props.identifier, (_this$state = this.state) === null || _this$state === void 0 ? void 0 : _this$state.fileState);
621
663
  }
622
664
 
623
665
  get fileAttributes() {
624
- var _this$lastFileState;
666
+ var _this$state2, _this$state2$fileStat;
625
667
 
626
- return getFileAttributes(this.metadata, (_this$lastFileState = this.lastFileState) === null || _this$lastFileState === void 0 ? void 0 : _this$lastFileState.status);
668
+ return getFileAttributes(this.metadata, (_this$state2 = this.state) === null || _this$state2 === void 0 ? void 0 : (_this$state2$fileStat = _this$state2.fileState) === null || _this$state2$fileStat === void 0 ? void 0 : _this$state2$fileStat.status);
627
669
  }
628
670
 
629
671
  fireOperationalEvent() {
630
- const {
631
- timeElapsedTillCommenced
632
- } = this;
633
672
  const {
634
673
  status,
635
- cardPreview,
636
674
  error
637
675
  } = this.state;
638
676
  const {
639
677
  createAnalyticsEvent
640
678
  } = this.props;
641
- const timeElapsedTillEvent = performance.now();
642
- const durationSinceCommenced = timeElapsedTillCommenced && timeElapsedTillEvent - timeElapsedTillCommenced;
643
- const performanceAttributes = {
644
- overall: {
645
- durationSincePageStart: timeElapsedTillEvent,
646
- durationSinceCommenced
647
- }
648
- };
649
- createAnalyticsEvent && fireOperationalEvent(createAnalyticsEvent, status, this.fileAttributes, performanceAttributes, {
650
- cardPreview,
651
- error
652
- });
679
+ createAnalyticsEvent && fireOperationalEvent(createAnalyticsEvent, status, this.fileAttributes, this.getPerformanceAttributes(), error);
653
680
  }
654
681
 
655
682
  fireCommencedEvent() {
@@ -692,19 +719,14 @@ export class CardBase extends Component {
692
719
  }
693
720
 
694
721
  render() {
695
- var _this$lastFileState2;
696
-
697
722
  const {
698
723
  isPlayingFile,
699
724
  mediaViewerSelectedItem
700
725
  } = this.state;
701
726
  const innerContent = /*#__PURE__*/React.createElement(React.Fragment, null, isPlayingFile ? this.renderInlinePlayer() : this.renderCard(), mediaViewerSelectedItem ? this.renderMediaViewer() : null);
702
- const content = this.context.intl ? innerContent : /*#__PURE__*/React.createElement(IntlProvider, {
727
+ return this.context.intl ? innerContent : /*#__PURE__*/React.createElement(IntlProvider, {
703
728
  locale: "en"
704
729
  }, innerContent);
705
- return /*#__PURE__*/React.createElement(FileAttributesProvider, {
706
- data: getFileAttributes(this.metadata, (_this$lastFileState2 = this.lastFileState) === null || _this$lastFileState2 === void 0 ? void 0 : _this$lastFileState2.status)
707
- }, content);
708
730
  }
709
731
 
710
732
  }