@atlaskit/media-card 77.2.3 → 77.4.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.
- package/CHANGELOG.md +17 -0
- package/dist/cjs/card/card.js +1 -1
- package/dist/cjs/card/getCardPreview/cache.js +1 -1
- package/dist/cjs/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/cjs/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/cjs/card/v2/cardV2.js +18 -1037
- package/dist/cjs/card/v2/externalImageCard.js +299 -0
- package/dist/cjs/card/v2/fileCard.js +1001 -0
- package/dist/cjs/errors.js +33 -55
- package/dist/cjs/inline/loader.js +1 -1
- package/dist/cjs/utils/ufoExperiences.js +1 -1
- package/dist/cjs/utils/useCurrentValueRef.js +12 -0
- package/dist/cjs/utils/usePrevious.js +14 -0
- package/dist/es2019/card/card.js +1 -1
- package/dist/es2019/card/getCardPreview/cache.js +2 -0
- package/dist/es2019/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/es2019/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/es2019/card/v2/cardV2.js +17 -1021
- package/dist/es2019/card/v2/externalImageCard.js +261 -0
- package/dist/es2019/card/v2/fileCard.js +881 -0
- package/dist/es2019/errors.js +1 -17
- package/dist/es2019/inline/loader.js +1 -1
- package/dist/es2019/utils/ufoExperiences.js +1 -1
- package/dist/es2019/utils/useCurrentValueRef.js +6 -0
- package/dist/es2019/utils/usePrevious.js +8 -0
- package/dist/esm/card/card.js +1 -1
- package/dist/esm/card/getCardPreview/cache.js +2 -0
- package/dist/esm/card/media-card-analytics-error-boundary.js +1 -1
- package/dist/esm/card/ui/imageRenderer/imageRenderer.js +6 -3
- package/dist/esm/card/v2/cardV2.js +19 -1036
- package/dist/esm/card/v2/externalImageCard.js +289 -0
- package/dist/esm/card/v2/fileCard.js +991 -0
- package/dist/esm/errors.js +31 -55
- package/dist/esm/inline/loader.js +1 -1
- package/dist/esm/utils/ufoExperiences.js +1 -1
- package/dist/esm/utils/useCurrentValueRef.js +6 -0
- package/dist/esm/utils/usePrevious.js +8 -0
- package/dist/types/card/v2/cardV2.d.ts +4 -60
- package/dist/types/card/v2/externalImageCard.d.ts +14 -0
- package/dist/types/card/v2/fileCard.d.ts +19 -0
- package/dist/types/errors.d.ts +1 -7
- package/dist/types/utils/useCurrentValueRef.d.ts +2 -0
- package/dist/types/utils/usePrevious.d.ts +1 -0
- package/dist/types-ts4.5/card/v2/cardV2.d.ts +4 -60
- package/dist/types-ts4.5/card/v2/externalImageCard.d.ts +14 -0
- package/dist/types-ts4.5/card/v2/fileCard.d.ts +19 -0
- package/dist/types-ts4.5/errors.d.ts +1 -7
- package/dist/types-ts4.5/utils/useCurrentValueRef.d.ts +2 -0
- package/dist/types-ts4.5/utils/usePrevious.d.ts +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,991 @@
|
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
4
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
5
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
6
|
+
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; }
|
|
7
|
+
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; }
|
|
8
|
+
import DownloadIcon from '@atlaskit/icon/glyph/download';
|
|
9
|
+
import { RECENTS_COLLECTION, addFileAttrsToUrl, globalMediaEventEmitter, imageResizeModeToFileImageMode, isFileIdentifier, isImageRepresentationReady } from '@atlaskit/media-client';
|
|
10
|
+
import { MediaFileStateError, useFileState } from '@atlaskit/media-client-react';
|
|
11
|
+
import { getMediaTypeFromMimeType, getRandomHex, isMimeTypeSupportedByBrowser } from '@atlaskit/media-common';
|
|
12
|
+
import { getOrientation } from '@atlaskit/media-ui';
|
|
13
|
+
import { MediaViewer } from '@atlaskit/media-viewer';
|
|
14
|
+
import React, { Suspense, useEffect, useMemo, useRef, useState } from 'react';
|
|
15
|
+
import ReactDOM from 'react-dom';
|
|
16
|
+
import { ImageLoadError, LocalPreviewError, MediaCardError, ensureMediaCardError, isLocalPreviewError, isUnsupportedLocalPreviewError } from '../../errors';
|
|
17
|
+
import { extractErrorInfo } from '../../utils/analytics';
|
|
18
|
+
import { isBigger } from '../../utils/dimensionComparer';
|
|
19
|
+
import getDocument from '../../utils/document';
|
|
20
|
+
import { generateUniqueId } from '../../utils/generateUniqueId';
|
|
21
|
+
import { getRequestedDimensions } from '../../utils/getDataURIDimension';
|
|
22
|
+
import { getMediaCardCursor } from '../../utils/getMediaCardCursor';
|
|
23
|
+
import { getSSRData, StoreSSRDataScript } from '../../utils/globalScope';
|
|
24
|
+
import { getFileDetails } from '../../utils/metadata';
|
|
25
|
+
import { abortUfoExperience, completeUfoExperience, startUfoExperience } from '../../utils/ufoExperiences';
|
|
26
|
+
import { useCurrentValueRef } from '../../utils/useCurrentValueRef';
|
|
27
|
+
import { usePrevious } from '../../utils/usePrevious';
|
|
28
|
+
import { videoIsPlayable } from '../../utils/videoIsPlayable';
|
|
29
|
+
import { takeSnapshot } from '../../utils/videoSnapshot';
|
|
30
|
+
import { ViewportDetector } from '../../utils/viewportDetector';
|
|
31
|
+
import { fireCommencedEvent, fireCopiedEvent, fireNonCriticalErrorEvent, fireOperationalEvent, fireScreenEvent } from '../cardAnalytics';
|
|
32
|
+
import { fetchAndCacheRemotePreview, getCardPreviewFromCache, getSSRCardPreview, isLocalPreview, isSSRClientPreview, isSSRDataPreview, isSSRPreview, removeCardPreviewFromCache, shouldResolvePreview } from '../getCardPreview';
|
|
33
|
+
import cardPreviewCache from '../getCardPreview/cache';
|
|
34
|
+
import { CardViewV2 } from './cardViewV2';
|
|
35
|
+
import { InlinePlayerLazyV2 } from './inlinePlayerLazyV2';
|
|
36
|
+
export var FileCard = function FileCard(_ref) {
|
|
37
|
+
var _ssrReliabilityRef$cu;
|
|
38
|
+
var _ref$appearance = _ref.appearance,
|
|
39
|
+
appearance = _ref$appearance === void 0 ? 'auto' : _ref$appearance,
|
|
40
|
+
_ref$resizeMode = _ref.resizeMode,
|
|
41
|
+
resizeMode = _ref$resizeMode === void 0 ? 'crop' : _ref$resizeMode,
|
|
42
|
+
_ref$isLazy = _ref.isLazy,
|
|
43
|
+
isLazy = _ref$isLazy === void 0 ? true : _ref$isLazy,
|
|
44
|
+
_ref$disableOverlay = _ref.disableOverlay,
|
|
45
|
+
disableOverlay = _ref$disableOverlay === void 0 ? false : _ref$disableOverlay,
|
|
46
|
+
_ref$featureFlags = _ref.featureFlags,
|
|
47
|
+
featureFlags = _ref$featureFlags === void 0 ? {} : _ref$featureFlags,
|
|
48
|
+
identifier = _ref.identifier,
|
|
49
|
+
ssr = _ref.ssr,
|
|
50
|
+
mediaClient = _ref.mediaClient,
|
|
51
|
+
dimensions = _ref.dimensions,
|
|
52
|
+
originalDimensions = _ref.originalDimensions,
|
|
53
|
+
contextId = _ref.contextId,
|
|
54
|
+
alt = _ref.alt,
|
|
55
|
+
actions = _ref.actions,
|
|
56
|
+
shouldEnableDownloadButton = _ref.shouldEnableDownloadButton,
|
|
57
|
+
useInlinePlayer = _ref.useInlinePlayer,
|
|
58
|
+
shouldOpenMediaViewer = _ref.shouldOpenMediaViewer,
|
|
59
|
+
onFullscreenChange = _ref.onFullscreenChange,
|
|
60
|
+
selectable = _ref.selectable,
|
|
61
|
+
selected = _ref.selected,
|
|
62
|
+
testId = _ref.testId,
|
|
63
|
+
titleBoxBgColor = _ref.titleBoxBgColor,
|
|
64
|
+
titleBoxIcon = _ref.titleBoxIcon,
|
|
65
|
+
shouldHideTooltip = _ref.shouldHideTooltip,
|
|
66
|
+
mediaViewerItems = _ref.mediaViewerItems,
|
|
67
|
+
onClick = _ref.onClick,
|
|
68
|
+
onMouseEnter = _ref.onMouseEnter,
|
|
69
|
+
createAnalyticsEvent = _ref.createAnalyticsEvent;
|
|
70
|
+
//----------------------------------------------------------------//
|
|
71
|
+
//---------------- State Initializer Functions -------------------//
|
|
72
|
+
//----------------------------------------------------------------//
|
|
73
|
+
|
|
74
|
+
var _useFileState = useFileState(identifier.id, {
|
|
75
|
+
collectionName: identifier.collectionName,
|
|
76
|
+
occurrenceKey: identifier.occurrenceKey
|
|
77
|
+
}),
|
|
78
|
+
fileState = _useFileState.fileState;
|
|
79
|
+
var prevFileState = usePrevious(fileState);
|
|
80
|
+
var fileStateValue = useMemo(function () {
|
|
81
|
+
if (fileState && (fileState === null || fileState === void 0 ? void 0 : fileState.status) !== 'error') {
|
|
82
|
+
return fileState;
|
|
83
|
+
}
|
|
84
|
+
return prevFileState;
|
|
85
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
86
|
+
}, [fileState]);
|
|
87
|
+
var ssrDataRef = useRef();
|
|
88
|
+
var ssrReliabilityRef = useRef({
|
|
89
|
+
server: {
|
|
90
|
+
status: 'unknown'
|
|
91
|
+
},
|
|
92
|
+
client: {
|
|
93
|
+
status: 'unknown'
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
var _useState = useState(null),
|
|
97
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
98
|
+
cardElement = _useState2[0],
|
|
99
|
+
setCardElement = _useState2[1];
|
|
100
|
+
var imageURLParams = useMemo(function () {
|
|
101
|
+
return _objectSpread(_objectSpread({
|
|
102
|
+
collection: identifier.collectionName,
|
|
103
|
+
mode: resizeMode === 'stretchy-fit' ? 'full-fit' : resizeMode
|
|
104
|
+
}, getRequestedDimensions({
|
|
105
|
+
dimensions: dimensions,
|
|
106
|
+
element: cardElement
|
|
107
|
+
})), {}, {
|
|
108
|
+
allowAnimated: true
|
|
109
|
+
});
|
|
110
|
+
}, [cardElement, dimensions, identifier.collectionName, resizeMode]);
|
|
111
|
+
var mediaBlobUrlAttrs = useMemo(function () {
|
|
112
|
+
var id = identifier.id,
|
|
113
|
+
collection = identifier.collectionName;
|
|
114
|
+
var _getFileDetails = getFileDetails(identifier, fileStateValue),
|
|
115
|
+
mimeType = _getFileDetails.mimeType,
|
|
116
|
+
name = _getFileDetails.name,
|
|
117
|
+
size = _getFileDetails.size;
|
|
118
|
+
return contextId ? _objectSpread(_objectSpread({
|
|
119
|
+
id: id,
|
|
120
|
+
collection: collection,
|
|
121
|
+
contextId: contextId,
|
|
122
|
+
mimeType: mimeType,
|
|
123
|
+
name: name,
|
|
124
|
+
size: size
|
|
125
|
+
}, originalDimensions || getRequestedDimensions({
|
|
126
|
+
dimensions: dimensions,
|
|
127
|
+
element: cardElement
|
|
128
|
+
})), {}, {
|
|
129
|
+
alt: alt
|
|
130
|
+
}) : undefined;
|
|
131
|
+
}, [alt, cardElement, contextId, dimensions, fileStateValue, identifier, originalDimensions]);
|
|
132
|
+
var getSSRPreview = function getSSRPreview(ssr, identifier, mediaClient) {
|
|
133
|
+
var _ssrDataRef$current, _ssrDataRef$current2;
|
|
134
|
+
ssrDataRef.current = getSSRData(identifier);
|
|
135
|
+
if ((_ssrDataRef$current = ssrDataRef.current) !== null && _ssrDataRef$current !== void 0 && _ssrDataRef$current.error) {
|
|
136
|
+
ssrReliabilityRef.current.server = _objectSpread({
|
|
137
|
+
status: 'fail'
|
|
138
|
+
}, ssrDataRef.current.error);
|
|
139
|
+
}
|
|
140
|
+
if (!((_ssrDataRef$current2 = ssrDataRef.current) !== null && _ssrDataRef$current2 !== void 0 && _ssrDataRef$current2.dataURI)) {
|
|
141
|
+
try {
|
|
142
|
+
return getSSRCardPreview(ssr, mediaClient, identifier.id, imageURLParams, mediaBlobUrlAttrs);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
ssrReliabilityRef.current[ssr] = _objectSpread({
|
|
145
|
+
status: 'fail'
|
|
146
|
+
}, extractErrorInfo(e));
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
return {
|
|
150
|
+
dataURI: ssrDataRef.current.dataURI,
|
|
151
|
+
source: 'ssr-data'
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var cardPreviewInitializer = function cardPreviewInitializer() {
|
|
156
|
+
var cardPreview;
|
|
157
|
+
var id = identifier.id;
|
|
158
|
+
var fileImageMode = imageResizeModeToFileImageMode(resizeMode);
|
|
159
|
+
cardPreview = getCardPreviewFromCache(id, fileImageMode);
|
|
160
|
+
if (!cardPreview && ssr) {
|
|
161
|
+
cardPreview = getSSRPreview(ssr, identifier, mediaClient);
|
|
162
|
+
}
|
|
163
|
+
return cardPreview;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
//----------------------------------------------------------------//
|
|
167
|
+
//------------ State, Refs & Initial Values ----------------------//
|
|
168
|
+
//----------------------------------------------------------------//
|
|
169
|
+
|
|
170
|
+
var internalOccurrenceKey = useMemo(function () {
|
|
171
|
+
return generateUniqueId();
|
|
172
|
+
}, []);
|
|
173
|
+
var timeElapsedTillCommenced = useMemo(function () {
|
|
174
|
+
return performance.now();
|
|
175
|
+
}, []);
|
|
176
|
+
var fileStateFlagsRef = useRef({
|
|
177
|
+
wasStatusUploading: false,
|
|
178
|
+
wasStatusProcessing: false
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Generate unique traceId for file
|
|
182
|
+
var traceContext = useMemo(function () {
|
|
183
|
+
return {
|
|
184
|
+
traceId: getRandomHex(8)
|
|
185
|
+
};
|
|
186
|
+
}, []);
|
|
187
|
+
var _useState3 = useState('loading'),
|
|
188
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
189
|
+
status = _useState4[0],
|
|
190
|
+
setStatus = _useState4[1];
|
|
191
|
+
useEffect(function () {
|
|
192
|
+
setStatus('loading');
|
|
193
|
+
}, [identifier]);
|
|
194
|
+
var _useState5 = useState(cardPreviewInitializer),
|
|
195
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
196
|
+
cardPreview = _useState6[0],
|
|
197
|
+
setCardPreview = _useState6[1];
|
|
198
|
+
|
|
199
|
+
// If cardPreview is available from local cache or external, `isCardVisible`
|
|
200
|
+
// should be true to avoid flickers during re-mount of the component
|
|
201
|
+
// should not be visible for SSR preview, otherwise we'll fire the metadata fetch from
|
|
202
|
+
// outside the viewport
|
|
203
|
+
var _useState7 = useState(function () {
|
|
204
|
+
return !isLazy || !!cardPreview && !isSSRPreview(cardPreview);
|
|
205
|
+
}),
|
|
206
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
207
|
+
isCardVisible = _useState8[0],
|
|
208
|
+
setIsCardVisible = _useState8[1];
|
|
209
|
+
var _useState9 = useState(false),
|
|
210
|
+
_useState10 = _slicedToArray(_useState9, 2),
|
|
211
|
+
isPlayingFile = _useState10[0],
|
|
212
|
+
setIsPlayingFile = _useState10[1];
|
|
213
|
+
var _useState11 = useState(false),
|
|
214
|
+
_useState12 = _slicedToArray(_useState11, 2),
|
|
215
|
+
shouldAutoplay = _useState12[0],
|
|
216
|
+
setShouldAutoplay = _useState12[1];
|
|
217
|
+
var _useState13 = useState(false),
|
|
218
|
+
_useState14 = _slicedToArray(_useState13, 2),
|
|
219
|
+
isBannedLocalPreview = _useState14[0],
|
|
220
|
+
setIsBannedLocalPreview = _useState14[1];
|
|
221
|
+
var _useState15 = useState(false),
|
|
222
|
+
_useState16 = _slicedToArray(_useState15, 2),
|
|
223
|
+
previewDidRender = _useState16[0],
|
|
224
|
+
setPreviewDidRender = _useState16[1];
|
|
225
|
+
var _useState17 = useState(),
|
|
226
|
+
_useState18 = _slicedToArray(_useState17, 2),
|
|
227
|
+
error = _useState18[0],
|
|
228
|
+
setError = _useState18[1];
|
|
229
|
+
var wasResolvedUpfrontPreviewRef = useRef(false);
|
|
230
|
+
var _useState19 = useState(null),
|
|
231
|
+
_useState20 = _slicedToArray(_useState19, 2),
|
|
232
|
+
mediaViewerSelectedItem = _useState20[0],
|
|
233
|
+
setMediaViewerSelectedItem = _useState20[1];
|
|
234
|
+
var uploadProgressRef = useRef();
|
|
235
|
+
var metadata = useMemo(function () {
|
|
236
|
+
var getProcessingStatusFromFileState = function getProcessingStatusFromFileState(status) {
|
|
237
|
+
switch (status) {
|
|
238
|
+
case 'processed':
|
|
239
|
+
return 'succeeded';
|
|
240
|
+
case 'processing':
|
|
241
|
+
return 'running';
|
|
242
|
+
case 'failed-processing':
|
|
243
|
+
return 'failed';
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
if (fileStateValue && (fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status) !== 'error') {
|
|
247
|
+
return {
|
|
248
|
+
id: fileStateValue.id,
|
|
249
|
+
name: fileStateValue.name,
|
|
250
|
+
size: fileStateValue.size,
|
|
251
|
+
mimeType: fileStateValue.mimeType,
|
|
252
|
+
createdAt: fileStateValue.createdAt,
|
|
253
|
+
mediaType: fileStateValue.mediaType,
|
|
254
|
+
processingStatus: getProcessingStatusFromFileState(fileStateValue.status)
|
|
255
|
+
};
|
|
256
|
+
} else {
|
|
257
|
+
return {
|
|
258
|
+
id: identifier.id
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}, [fileStateValue, identifier.id]);
|
|
262
|
+
var fileAttributes = useMemo(function () {
|
|
263
|
+
return {
|
|
264
|
+
fileMediatype: metadata.mediaType,
|
|
265
|
+
fileMimetype: metadata.mimeType,
|
|
266
|
+
fileId: metadata.id,
|
|
267
|
+
fileSize: metadata.size,
|
|
268
|
+
fileStatus: fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status
|
|
269
|
+
};
|
|
270
|
+
}, [fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status, metadata.id, metadata.mediaType, metadata.mimeType, metadata.size]);
|
|
271
|
+
var computedActions = useMemo(function () {
|
|
272
|
+
if (status === 'failed-processing' || shouldEnableDownloadButton) {
|
|
273
|
+
var downloadAction = {
|
|
274
|
+
label: 'Download',
|
|
275
|
+
icon: /*#__PURE__*/React.createElement(DownloadIcon, {
|
|
276
|
+
label: "Download"
|
|
277
|
+
}),
|
|
278
|
+
handler: function handler() {
|
|
279
|
+
return mediaClient.file.downloadBinary(identifier.id, metadata.name, identifier.collectionName);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
return [downloadAction].concat(_toConsumableArray(actions !== null && actions !== void 0 ? actions : []));
|
|
283
|
+
} else {
|
|
284
|
+
return actions;
|
|
285
|
+
}
|
|
286
|
+
}, [actions, identifier.collectionName, identifier.id, mediaClient.file, metadata.name, shouldEnableDownloadButton, status]);
|
|
287
|
+
|
|
288
|
+
//----------------------------------------------------------------//
|
|
289
|
+
//---------------------- Analytics ------------------------------//
|
|
290
|
+
//----------------------------------------------------------------//
|
|
291
|
+
|
|
292
|
+
var fireOperationalEventRef = useCurrentValueRef(function () {
|
|
293
|
+
var timeElapsedTillEvent = performance.now();
|
|
294
|
+
var durationSinceCommenced = timeElapsedTillEvent - timeElapsedTillCommenced;
|
|
295
|
+
var performanceAttributes = {
|
|
296
|
+
overall: {
|
|
297
|
+
durationSincePageStart: timeElapsedTillEvent,
|
|
298
|
+
durationSinceCommenced: durationSinceCommenced
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
createAnalyticsEvent && fireOperationalEvent(createAnalyticsEvent, status, fileAttributes, performanceAttributes, ssrReliabilityRef.current, error, traceContext, fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.metadataTraceContext);
|
|
302
|
+
completeUfoExperience(internalOccurrenceKey, status, fileAttributes, fileStateFlagsRef.current, ssrReliabilityRef.current, error);
|
|
303
|
+
});
|
|
304
|
+
var fireNonCriticalErrorEventRef = useCurrentValueRef(function (error) {
|
|
305
|
+
createAnalyticsEvent && fireNonCriticalErrorEvent(createAnalyticsEvent, status, fileAttributes, ssrReliabilityRef.current, error, traceContext, fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.metadataTraceContext);
|
|
306
|
+
});
|
|
307
|
+
var fireScreenEventRef = useCurrentValueRef(function () {
|
|
308
|
+
createAnalyticsEvent && fireScreenEvent(createAnalyticsEvent, fileAttributes);
|
|
309
|
+
});
|
|
310
|
+
var fireCommencedEventRef = useCurrentValueRef(function () {
|
|
311
|
+
createAnalyticsEvent && fireCommencedEvent(createAnalyticsEvent, fileAttributes, {
|
|
312
|
+
overall: {
|
|
313
|
+
durationSincePageStart: timeElapsedTillCommenced
|
|
314
|
+
}
|
|
315
|
+
}, traceContext);
|
|
316
|
+
startUfoExperience(internalOccurrenceKey);
|
|
317
|
+
});
|
|
318
|
+
var fireAbortedEventRef = useCurrentValueRef(function () {
|
|
319
|
+
// UFO won't abort if it's already in a final state (succeeded, failed, aborted, etc)
|
|
320
|
+
abortUfoExperience(internalOccurrenceKey, {
|
|
321
|
+
fileAttributes: fileAttributes,
|
|
322
|
+
fileStateFlags: fileStateFlagsRef === null || fileStateFlagsRef === void 0 ? void 0 : fileStateFlagsRef.current,
|
|
323
|
+
ssrReliability: ssrReliabilityRef === null || ssrReliabilityRef === void 0 ? void 0 : ssrReliabilityRef.current
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
//----------------------------------------------------------------//
|
|
328
|
+
//---------------------- Callbacks & Handlers -------------------//
|
|
329
|
+
//----------------------------------------------------------------//
|
|
330
|
+
|
|
331
|
+
var onImageError = function onImageError(newCardPreview) {
|
|
332
|
+
if (newCardPreview) {
|
|
333
|
+
var failedSSRObject = _objectSpread({
|
|
334
|
+
status: 'fail'
|
|
335
|
+
}, extractErrorInfo(new ImageLoadError(newCardPreview.source)));
|
|
336
|
+
if (isSSRClientPreview(newCardPreview)) {
|
|
337
|
+
ssrReliabilityRef.current.client = failedSSRObject;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/*
|
|
341
|
+
If the cardPreview failed and it comes from server (global scope / ssrData), it means that we have reused it in client and the error counts for both: server & client.
|
|
342
|
+
*/
|
|
343
|
+
|
|
344
|
+
if (isSSRDataPreview(newCardPreview)) {
|
|
345
|
+
ssrReliabilityRef.current.server = failedSSRObject;
|
|
346
|
+
ssrReliabilityRef.current.client = failedSSRObject;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// If the dataURI has been replaced, we can dismiss this error
|
|
351
|
+
if ((newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.dataURI) !== (cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.dataURI)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
var error = new ImageLoadError(newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.source);
|
|
355
|
+
var isLocal = newCardPreview && isLocalPreview(newCardPreview);
|
|
356
|
+
var isSSR = newCardPreview && (isSSRClientPreview(newCardPreview) || isSSRDataPreview(newCardPreview));
|
|
357
|
+
if (isLocal || isSSR) {
|
|
358
|
+
if (isLocal) {
|
|
359
|
+
setIsBannedLocalPreview(true);
|
|
360
|
+
fireNonCriticalErrorEventRef.current && fireNonCriticalErrorEventRef.current(error);
|
|
361
|
+
}
|
|
362
|
+
var fileImageMode = imageResizeModeToFileImageMode(resizeMode);
|
|
363
|
+
isFileIdentifier(identifier) && removeCardPreviewFromCache(identifier.id, fileImageMode);
|
|
364
|
+
setCardPreview(undefined);
|
|
365
|
+
} else {
|
|
366
|
+
if (!['complete', 'error', 'failed-processing'].includes(status)) {
|
|
367
|
+
setStatus('error');
|
|
368
|
+
setError(error);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
var onImageLoad = function onImageLoad(newCardPreview) {
|
|
373
|
+
if (newCardPreview) {
|
|
374
|
+
if (isSSRClientPreview(newCardPreview) && ssrReliabilityRef.current.client.status === 'unknown') {
|
|
375
|
+
ssrReliabilityRef.current.client = {
|
|
376
|
+
status: 'success'
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/*
|
|
381
|
+
If the image loads successfully and it comes from server (global scope / ssrData), it means that we have reused it in client and the success counts for both: server & client.
|
|
382
|
+
*/
|
|
383
|
+
|
|
384
|
+
if (isSSRDataPreview(newCardPreview) && ssrReliabilityRef.current.server.status === 'unknown') {
|
|
385
|
+
ssrReliabilityRef.current.server = {
|
|
386
|
+
status: 'success'
|
|
387
|
+
};
|
|
388
|
+
ssrReliabilityRef.current.client = {
|
|
389
|
+
status: 'success'
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// If the dataURI has been replaced, we can dismiss this callback
|
|
395
|
+
if ((newCardPreview === null || newCardPreview === void 0 ? void 0 : newCardPreview.dataURI) !== (cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.dataURI)) {
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
setPreviewDidRender(true);
|
|
399
|
+
};
|
|
400
|
+
var onCardClick = function onCardClick(event, analyticsEvent) {
|
|
401
|
+
if (onClick) {
|
|
402
|
+
var cardEvent = {
|
|
403
|
+
event: event,
|
|
404
|
+
mediaItemDetails: metadata
|
|
405
|
+
};
|
|
406
|
+
onClick(cardEvent, analyticsEvent);
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
var onCardViewClick = function onCardViewClick(event, analyticsEvent) {
|
|
410
|
+
onCardClick(event, analyticsEvent);
|
|
411
|
+
if (!metadata) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
var isVideo = metadata && metadata.mediaType === 'video';
|
|
415
|
+
if (useInlinePlayer && isVideo && !!cardPreview) {
|
|
416
|
+
setIsPlayingFile(true);
|
|
417
|
+
setShouldAutoplay(true);
|
|
418
|
+
} else if (shouldOpenMediaViewer) {
|
|
419
|
+
setMediaViewerSelectedItem({
|
|
420
|
+
id: identifier.id,
|
|
421
|
+
mediaItemType: 'file',
|
|
422
|
+
collectionName: identifier.collectionName,
|
|
423
|
+
occurrenceKey: identifier.occurrenceKey
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
//----------------------------------------------------------------//
|
|
429
|
+
//---------------------- Helper Functions -----------------------//
|
|
430
|
+
//----------------------------------------------------------------//
|
|
431
|
+
|
|
432
|
+
var fetchRemotePreviewRef = useCurrentValueRef(function (identifier) {
|
|
433
|
+
return fetchAndCacheRemotePreview(mediaClient, identifier.id, dimensions !== null && dimensions !== void 0 ? dimensions : {}, imageURLParams, mediaBlobUrlAttrs);
|
|
434
|
+
});
|
|
435
|
+
var resolvePreviewRef = useCurrentValueRef( /*#__PURE__*/function () {
|
|
436
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(identifier, fileState) {
|
|
437
|
+
var filePreview, isRemotePreviewReady, mode, cachedPreview, dimensionsAreBigger, localPreview, value, resolvedFilePreview, _value, type, mediaType, orientation, _dataURI, _dataURI2, preview, source, dataURI, remotePreview, wrappedError;
|
|
438
|
+
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
439
|
+
while (1) switch (_context.prev = _context.next) {
|
|
440
|
+
case 0:
|
|
441
|
+
filePreview = isBannedLocalPreview ? undefined : fileState.status !== 'error' && 'mimeType' in fileState && isMimeTypeSupportedByBrowser(fileState.mimeType) ? fileState.preview : undefined;
|
|
442
|
+
isRemotePreviewReady = isImageRepresentationReady(fileState);
|
|
443
|
+
_context.prev = 2;
|
|
444
|
+
mode = imageURLParams.mode;
|
|
445
|
+
cachedPreview = cardPreviewCache.get(identifier.id, mode);
|
|
446
|
+
dimensionsAreBigger = isBigger(cachedPreview === null || cachedPreview === void 0 ? void 0 : cachedPreview.dimensions, dimensions);
|
|
447
|
+
if (!(cachedPreview && !dimensionsAreBigger)) {
|
|
448
|
+
_context.next = 8;
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
return _context.abrupt("return", cachedPreview);
|
|
452
|
+
case 8:
|
|
453
|
+
_context.prev = 8;
|
|
454
|
+
if (!filePreview) {
|
|
455
|
+
_context.next = 74;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
_context.prev = 10;
|
|
459
|
+
_context.next = 13;
|
|
460
|
+
return filePreview;
|
|
461
|
+
case 13:
|
|
462
|
+
resolvedFilePreview = _context.sent;
|
|
463
|
+
value = resolvedFilePreview.value;
|
|
464
|
+
_context.next = 20;
|
|
465
|
+
break;
|
|
466
|
+
case 17:
|
|
467
|
+
_context.prev = 17;
|
|
468
|
+
_context.t0 = _context["catch"](10);
|
|
469
|
+
throw new LocalPreviewError('local-preview-rejected', _context.t0 instanceof Error ? _context.t0 : undefined);
|
|
470
|
+
case 20:
|
|
471
|
+
if (!(typeof value === 'string')) {
|
|
472
|
+
_context.next = 24;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
localPreview = {
|
|
476
|
+
dataURI: value,
|
|
477
|
+
orientation: 1,
|
|
478
|
+
source: 'local'
|
|
479
|
+
};
|
|
480
|
+
_context.next = 57;
|
|
481
|
+
break;
|
|
482
|
+
case 24:
|
|
483
|
+
if (!(value instanceof Blob)) {
|
|
484
|
+
_context.next = 56;
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
_value = value, type = _value.type;
|
|
488
|
+
mediaType = getMediaTypeFromMimeType(type);
|
|
489
|
+
_context.t1 = mediaType;
|
|
490
|
+
_context.next = _context.t1 === 'image' ? 30 : _context.t1 === 'video' ? 42 : 53;
|
|
491
|
+
break;
|
|
492
|
+
case 30:
|
|
493
|
+
_context.prev = 30;
|
|
494
|
+
_context.next = 33;
|
|
495
|
+
return getOrientation(value);
|
|
496
|
+
case 33:
|
|
497
|
+
orientation = _context.sent;
|
|
498
|
+
_dataURI = URL.createObjectURL(value);
|
|
499
|
+
localPreview = {
|
|
500
|
+
dataURI: _dataURI,
|
|
501
|
+
orientation: orientation,
|
|
502
|
+
source: 'local'
|
|
503
|
+
};
|
|
504
|
+
_context.next = 41;
|
|
505
|
+
break;
|
|
506
|
+
case 38:
|
|
507
|
+
_context.prev = 38;
|
|
508
|
+
_context.t2 = _context["catch"](30);
|
|
509
|
+
throw new LocalPreviewError('local-preview-image', _context.t2 instanceof Error ? _context.t2 : undefined);
|
|
510
|
+
case 41:
|
|
511
|
+
return _context.abrupt("break", 54);
|
|
512
|
+
case 42:
|
|
513
|
+
_context.prev = 42;
|
|
514
|
+
_context.next = 45;
|
|
515
|
+
return takeSnapshot(value);
|
|
516
|
+
case 45:
|
|
517
|
+
_dataURI2 = _context.sent;
|
|
518
|
+
localPreview = {
|
|
519
|
+
dataURI: _dataURI2,
|
|
520
|
+
orientation: 1,
|
|
521
|
+
source: 'local'
|
|
522
|
+
};
|
|
523
|
+
_context.next = 52;
|
|
524
|
+
break;
|
|
525
|
+
case 49:
|
|
526
|
+
_context.prev = 49;
|
|
527
|
+
_context.t3 = _context["catch"](42);
|
|
528
|
+
throw new LocalPreviewError('local-preview-video', _context.t3 instanceof Error ? _context.t3 : undefined);
|
|
529
|
+
case 52:
|
|
530
|
+
return _context.abrupt("break", 54);
|
|
531
|
+
case 53:
|
|
532
|
+
throw new LocalPreviewError('local-preview-unsupported');
|
|
533
|
+
case 54:
|
|
534
|
+
_context.next = 57;
|
|
535
|
+
break;
|
|
536
|
+
case 56:
|
|
537
|
+
throw new LocalPreviewError('local-preview-unsupported');
|
|
538
|
+
case 57:
|
|
539
|
+
preview = _objectSpread(_objectSpread({}, localPreview), {}, {
|
|
540
|
+
dimensions: dimensions
|
|
541
|
+
});
|
|
542
|
+
_context.t4 = preview.source;
|
|
543
|
+
_context.next = _context.t4 === 'local' ? 61 : _context.t4 === 'remote' ? 63 : _context.t4 === 'ssr-server' ? 65 : _context.t4 === 'ssr-client' ? 67 : 69;
|
|
544
|
+
break;
|
|
545
|
+
case 61:
|
|
546
|
+
source = 'cache-local';
|
|
547
|
+
return _context.abrupt("break", 70);
|
|
548
|
+
case 63:
|
|
549
|
+
source = 'cache-remote';
|
|
550
|
+
return _context.abrupt("break", 70);
|
|
551
|
+
case 65:
|
|
552
|
+
source = 'cache-ssr-server';
|
|
553
|
+
return _context.abrupt("break", 70);
|
|
554
|
+
case 67:
|
|
555
|
+
source = 'cache-ssr-client';
|
|
556
|
+
return _context.abrupt("break", 70);
|
|
557
|
+
case 69:
|
|
558
|
+
source = preview.source;
|
|
559
|
+
case 70:
|
|
560
|
+
// We want to embed some meta context into dataURI for Copy/Paste to work.
|
|
561
|
+
dataURI = mediaBlobUrlAttrs ? addFileAttrsToUrl(preview.dataURI, mediaBlobUrlAttrs) : preview.dataURI; // We store new cardPreview into cache
|
|
562
|
+
cardPreviewCache.set(identifier.id, mode, _objectSpread(_objectSpread({}, preview), {}, {
|
|
563
|
+
source: source,
|
|
564
|
+
dataURI: dataURI
|
|
565
|
+
}));
|
|
566
|
+
setCardPreview(_objectSpread(_objectSpread({}, preview), {}, {
|
|
567
|
+
dataURI: dataURI
|
|
568
|
+
}));
|
|
569
|
+
return _context.abrupt("return");
|
|
570
|
+
case 74:
|
|
571
|
+
_context.next = 81;
|
|
572
|
+
break;
|
|
573
|
+
case 76:
|
|
574
|
+
_context.prev = 76;
|
|
575
|
+
_context.t5 = _context["catch"](8);
|
|
576
|
+
/**
|
|
577
|
+
* We report the error if:
|
|
578
|
+
* - local preview is supported and fails
|
|
579
|
+
* - local preview is unsupported and remote preview is NOT READY
|
|
580
|
+
* i.e. the function was called for "no reason".
|
|
581
|
+
* We DON'T report the error if:
|
|
582
|
+
* - local preview is unsupported and remote preview IS READY
|
|
583
|
+
* i.e. local preview is available and not supported,
|
|
584
|
+
* but we are after the remote preview instead.
|
|
585
|
+
*/
|
|
586
|
+
if (!isUnsupportedLocalPreviewError(_context.t5) || isUnsupportedLocalPreviewError(_context.t5) && !isRemotePreviewReady) {
|
|
587
|
+
fireNonCriticalErrorEventRef.current && fireNonCriticalErrorEventRef.current(_context.t5);
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* No matter the reason why the local preview failed, we break the process
|
|
591
|
+
* if there is no remote preview available
|
|
592
|
+
*/
|
|
593
|
+
if (isRemotePreviewReady) {
|
|
594
|
+
_context.next = 81;
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
throw _context.t5;
|
|
598
|
+
case 81:
|
|
599
|
+
if (isRemotePreviewReady) {
|
|
600
|
+
_context.next = 83;
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
throw new MediaCardError('remote-preview-not-ready');
|
|
604
|
+
case 83:
|
|
605
|
+
_context.next = 85;
|
|
606
|
+
return fetchAndCacheRemotePreview(mediaClient, identifier.id, dimensions !== null && dimensions !== void 0 ? dimensions : {}, imageURLParams, mediaBlobUrlAttrs, traceContext);
|
|
607
|
+
case 85:
|
|
608
|
+
remotePreview = _context.sent;
|
|
609
|
+
setCardPreview(remotePreview);
|
|
610
|
+
return _context.abrupt("return");
|
|
611
|
+
case 90:
|
|
612
|
+
_context.prev = 90;
|
|
613
|
+
_context.t6 = _context["catch"](2);
|
|
614
|
+
wrappedError = ensureMediaCardError('preview-fetch', _context.t6); // If remote preview fails, we set status 'error'
|
|
615
|
+
// If local preview fails (i.e, no remote preview available),
|
|
616
|
+
// we can stay in the same status until there is a remote preview available
|
|
617
|
+
// If it's any other error we set status 'error'
|
|
618
|
+
if (isLocalPreviewError(wrappedError)) {
|
|
619
|
+
// This error should already been logged inside the getCardPreview. No need to log it here.
|
|
620
|
+
setIsBannedLocalPreview(true);
|
|
621
|
+
} else {
|
|
622
|
+
if (!['complete', 'error', 'failed-processing'].includes(status)) {
|
|
623
|
+
setStatus('error');
|
|
624
|
+
setError(wrappedError);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
case 94:
|
|
628
|
+
case "end":
|
|
629
|
+
return _context.stop();
|
|
630
|
+
}
|
|
631
|
+
}, _callee, null, [[2, 90], [8, 76], [10, 17], [30, 38], [42, 49]]);
|
|
632
|
+
}));
|
|
633
|
+
return function (_x, _x2) {
|
|
634
|
+
return _ref2.apply(this, arguments);
|
|
635
|
+
};
|
|
636
|
+
}());
|
|
637
|
+
|
|
638
|
+
//----------------------------------------------------------------//
|
|
639
|
+
//------------ resolveUpfrontPreview useEffect -------------------//
|
|
640
|
+
//----------------------------------------------------------------//
|
|
641
|
+
var prevCardPreview = usePrevious(cardPreview);
|
|
642
|
+
var dimensionsRef = useCurrentValueRef(dimensions);
|
|
643
|
+
useEffect(function () {
|
|
644
|
+
var resolveUpfrontPreview = /*#__PURE__*/function () {
|
|
645
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(identifier) {
|
|
646
|
+
var requestedDimensions, newCardPreview, areValidRequestedDimensions;
|
|
647
|
+
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
648
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
649
|
+
case 0:
|
|
650
|
+
// We block any possible future call to this method regardless of the outcome (success or fail)
|
|
651
|
+
// If it fails, the normal preview fetch should occur after the file state is fetched anyways
|
|
652
|
+
wasResolvedUpfrontPreviewRef.current = true;
|
|
653
|
+
_context2.prev = 1;
|
|
654
|
+
requestedDimensions = _objectSpread({}, dimensions);
|
|
655
|
+
_context2.next = 5;
|
|
656
|
+
return fetchRemotePreviewRef.current(identifier);
|
|
657
|
+
case 5:
|
|
658
|
+
newCardPreview = _context2.sent;
|
|
659
|
+
areValidRequestedDimensions = !isBigger(requestedDimensions, dimensionsRef.current); // If there are new and bigger dimensions in the props, and the upfront preview is still resolving,
|
|
660
|
+
// the fetched preview is no longer valid, and thus, we dismiss it
|
|
661
|
+
if (areValidRequestedDimensions) {
|
|
662
|
+
setCardPreview(newCardPreview);
|
|
663
|
+
}
|
|
664
|
+
_context2.next = 12;
|
|
665
|
+
break;
|
|
666
|
+
case 10:
|
|
667
|
+
_context2.prev = 10;
|
|
668
|
+
_context2.t0 = _context2["catch"](1);
|
|
669
|
+
case 12:
|
|
670
|
+
case "end":
|
|
671
|
+
return _context2.stop();
|
|
672
|
+
}
|
|
673
|
+
}, _callee2, null, [[1, 10]]);
|
|
674
|
+
}));
|
|
675
|
+
return function resolveUpfrontPreview(_x3) {
|
|
676
|
+
return _ref3.apply(this, arguments);
|
|
677
|
+
};
|
|
678
|
+
}();
|
|
679
|
+
var hadSSRCardPreview = ssr === 'client' && !!prevCardPreview && isSSRClientPreview(prevCardPreview);
|
|
680
|
+
if ((isCardVisible || hadSSRCardPreview) && !cardPreview && !wasResolvedUpfrontPreviewRef.current) {
|
|
681
|
+
resolveUpfrontPreview(identifier);
|
|
682
|
+
}
|
|
683
|
+
}, [cardPreview, dimensions, dimensionsRef, fetchRemotePreviewRef, identifier, isCardVisible, prevCardPreview, ssr]);
|
|
684
|
+
|
|
685
|
+
//----------------------------------------------------------------//
|
|
686
|
+
//------------------------ handle fireCopiedEvent --------------//
|
|
687
|
+
//----------------------------------------------------------------//
|
|
688
|
+
|
|
689
|
+
useEffect(function () {
|
|
690
|
+
var _getDocument;
|
|
691
|
+
var fireCopiedCardEvent = function fireCopiedCardEvent() {
|
|
692
|
+
cardElement && createAnalyticsEvent && fireCopiedEvent(createAnalyticsEvent, metadata.id, cardElement);
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
// we add a listener for each of the cards on the page
|
|
696
|
+
// and then check if the triggered listener is from the card
|
|
697
|
+
// that contains a div in current window.getSelection()
|
|
698
|
+
// won't work in IE11
|
|
699
|
+
(_getDocument = getDocument()) === null || _getDocument === void 0 || _getDocument.addEventListener('copy', fireCopiedCardEvent);
|
|
700
|
+
return function () {
|
|
701
|
+
var _getDocument2;
|
|
702
|
+
(_getDocument2 = getDocument()) === null || _getDocument2 === void 0 || _getDocument2.removeEventListener('copy', fireCopiedCardEvent);
|
|
703
|
+
};
|
|
704
|
+
}, [cardElement, createAnalyticsEvent, metadata.id]);
|
|
705
|
+
|
|
706
|
+
//----------------------------------------------------------------//
|
|
707
|
+
//----------------- update status flags --------------------------//
|
|
708
|
+
//----------------------------------------------------------------//
|
|
709
|
+
|
|
710
|
+
useEffect(function () {
|
|
711
|
+
if ((fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status) === 'processing') {
|
|
712
|
+
fileStateFlagsRef.current.wasStatusProcessing = true;
|
|
713
|
+
} else if ((fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status) === 'uploading') {
|
|
714
|
+
fileStateFlagsRef.current.wasStatusUploading = true;
|
|
715
|
+
}
|
|
716
|
+
}, [fileStateValue === null || fileStateValue === void 0 ? void 0 : fileStateValue.status]);
|
|
717
|
+
|
|
718
|
+
//----------------------------------------------------------------//
|
|
719
|
+
//---------------- fetch and resolve card preview ----------------//
|
|
720
|
+
//----------------------------------------------------------------//
|
|
721
|
+
|
|
722
|
+
var prevDimensions = usePrevious(dimensions);
|
|
723
|
+
var prevIsCardVisible = usePrevious(isCardVisible);
|
|
724
|
+
var prevStatus = usePrevious(status);
|
|
725
|
+
useEffect(function () {
|
|
726
|
+
if (prevStatus !== undefined && status !== prevStatus) {
|
|
727
|
+
fireOperationalEventRef.current();
|
|
728
|
+
}
|
|
729
|
+
}, [fireOperationalEventRef, prevStatus, status]);
|
|
730
|
+
useEffect(function () {
|
|
731
|
+
var _ssrDataRef$current3;
|
|
732
|
+
/**
|
|
733
|
+
* Variable turnedVisible should only be true when media card
|
|
734
|
+
* was invisible in the previous state and is visible in the current one
|
|
735
|
+
*
|
|
736
|
+
* prevIsCardVisible | isCardVisible | turnedVisible
|
|
737
|
+
* ----------------------------------------------------
|
|
738
|
+
* false | false | false
|
|
739
|
+
* false | true | true
|
|
740
|
+
* true | true | false
|
|
741
|
+
* true | false | false (unreachable case)
|
|
742
|
+
* ----------------------------------------------------
|
|
743
|
+
*/
|
|
744
|
+
|
|
745
|
+
var turnedVisible = !prevIsCardVisible && isCardVisible;
|
|
746
|
+
if (turnedVisible) {
|
|
747
|
+
fireCommencedEventRef.current();
|
|
748
|
+
}
|
|
749
|
+
if (cardPreview && turnedVisible && isSSRDataPreview(cardPreview) && isBigger((_ssrDataRef$current3 = ssrDataRef.current) === null || _ssrDataRef$current3 === void 0 ? void 0 : _ssrDataRef$current3.dimensions, dimensions)) {
|
|
750
|
+
// If dimensions from Server have changed and are bigger,
|
|
751
|
+
// we need to refetch
|
|
752
|
+
// refetchSRRPreview
|
|
753
|
+
fetchRemotePreviewRef.current(identifier).then(setCardPreview).catch(function (e) {
|
|
754
|
+
var wrappedError = ensureMediaCardError('remote-preview-fetch-ssr', e, true);
|
|
755
|
+
fireNonCriticalErrorEventRef.current(wrappedError);
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
if (fileStateValue && shouldResolvePreview({
|
|
759
|
+
status: status,
|
|
760
|
+
fileState: fileStateValue,
|
|
761
|
+
prevDimensions: prevDimensions,
|
|
762
|
+
dimensions: dimensions,
|
|
763
|
+
hasCardPreview: !!cardPreview,
|
|
764
|
+
isBannedLocalPreview: isBannedLocalPreview,
|
|
765
|
+
wasResolvedUpfrontPreview: wasResolvedUpfrontPreviewRef.current
|
|
766
|
+
})) {
|
|
767
|
+
resolvePreviewRef.current(identifier, fileStateValue);
|
|
768
|
+
}
|
|
769
|
+
if (turnedVisible && ssr && !!cardPreview && isSSRClientPreview(cardPreview)) {
|
|
770
|
+
// Since the SSR preview brings the token in the query params,
|
|
771
|
+
// We need to fetch the remote preview to be able to cache it,
|
|
772
|
+
fetchRemotePreviewRef.current(identifier).catch(function () {
|
|
773
|
+
// No need to log this error.
|
|
774
|
+
// If preview fails, it will be refetched later
|
|
775
|
+
//TODO: test this catch
|
|
776
|
+
// https://product-fabric.atlassian.net/browse/MEX-1071
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
}, [cardPreview, dimensions, fetchRemotePreviewRef, fileStateValue, fireCommencedEventRef, fireNonCriticalErrorEventRef, identifier, isBannedLocalPreview, isCardVisible, prevDimensions, prevIsCardVisible, resolvePreviewRef, ssr, status]);
|
|
780
|
+
|
|
781
|
+
//----------------------------------------------------------------//
|
|
782
|
+
//----------------- set complete status --------------------------//
|
|
783
|
+
//----------------------------------------------------------------//
|
|
784
|
+
|
|
785
|
+
useEffect(function () {
|
|
786
|
+
if (previewDidRender &&
|
|
787
|
+
// We should't complete if status is uploading
|
|
788
|
+
['loading-preview', 'processing'].includes(status)) {
|
|
789
|
+
setStatus('complete');
|
|
790
|
+
// TODO MEX-788: add test for "do not remove the card preview when unsubscribing".
|
|
791
|
+
setIsBannedLocalPreview(false);
|
|
792
|
+
}
|
|
793
|
+
}, [previewDidRender, status]);
|
|
794
|
+
|
|
795
|
+
//----------------------------------------------------------------//
|
|
796
|
+
//----------------- set isPlayingFile state ----------------------//
|
|
797
|
+
//----------------------------------------------------------------//
|
|
798
|
+
|
|
799
|
+
useEffect(function () {
|
|
800
|
+
var isVideo = fileAttributes.fileMediatype === 'video';
|
|
801
|
+
var _getFileDetails2 = getFileDetails(identifier, fileStateValue),
|
|
802
|
+
mimeType = _getFileDetails2.mimeType;
|
|
803
|
+
var isVideoPlayable = videoIsPlayable(isBannedLocalPreview, fileStateValue, mimeType);
|
|
804
|
+
if (isVideo && !isPlayingFile && disableOverlay && useInlinePlayer && isVideoPlayable) {
|
|
805
|
+
setIsPlayingFile(true);
|
|
806
|
+
}
|
|
807
|
+
}, [disableOverlay, fileAttributes.fileMediatype, fileStateValue, identifier, isBannedLocalPreview, isPlayingFile, useInlinePlayer]);
|
|
808
|
+
|
|
809
|
+
//----------------------------------------------------------------//
|
|
810
|
+
//----------------- fireScreenEvent ------------------------------//
|
|
811
|
+
//----------------------------------------------------------------//
|
|
812
|
+
|
|
813
|
+
useEffect(function () {
|
|
814
|
+
if (prevStatus !== undefined && status !== prevStatus) {
|
|
815
|
+
if (status === 'complete' || fileAttributes.fileMediatype === 'video' && !!cardPreview && status === 'processing') {
|
|
816
|
+
fireScreenEventRef.current();
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}, [status, prevStatus, fileAttributes, cardPreview, fireScreenEventRef]);
|
|
820
|
+
|
|
821
|
+
//----------------------------------------------------------------//
|
|
822
|
+
//----------------- abort UFO experience -------------------------//
|
|
823
|
+
//----------------------------------------------------------------//
|
|
824
|
+
|
|
825
|
+
useEffect(function () {
|
|
826
|
+
return function () {
|
|
827
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
828
|
+
fireAbortedEventRef.current();
|
|
829
|
+
};
|
|
830
|
+
}, [fireAbortedEventRef]);
|
|
831
|
+
|
|
832
|
+
//----------------------------------------------------------------//
|
|
833
|
+
//------------------ Subscribe to file state ---------------------//
|
|
834
|
+
//----------------------------------------------------------------//
|
|
835
|
+
|
|
836
|
+
var updateFileStateRef = useCurrentValueRef(function () {
|
|
837
|
+
if (fileState) {
|
|
838
|
+
// do not update the card status if the status is final
|
|
839
|
+
if (['complete', 'error', 'failed-processing'].includes(status)) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
if (fileState.status !== 'error') {
|
|
843
|
+
var mediaType = 'mediaType' in fileState ? fileState.mediaType : undefined;
|
|
844
|
+
var isPreviewable = !!mediaType && ['audio', 'video', 'image', 'doc'].indexOf(mediaType) > -1;
|
|
845
|
+
var isPreviewableFileState = !!fileState.preview;
|
|
846
|
+
var isSupportedLocalPreview = mediaType === 'image' || mediaType === 'video';
|
|
847
|
+
var hasLocalPreview = !isBannedLocalPreview && isPreviewableFileState && isSupportedLocalPreview && !!fileState.mimeType && isMimeTypeSupportedByBrowser(fileState.mimeType);
|
|
848
|
+
var hasRemotePreview = isImageRepresentationReady(fileState);
|
|
849
|
+
var hasPreview = hasLocalPreview || hasRemotePreview;
|
|
850
|
+
var newStatus;
|
|
851
|
+
switch (fileState.status) {
|
|
852
|
+
case 'uploading':
|
|
853
|
+
case 'failed-processing':
|
|
854
|
+
case 'processing':
|
|
855
|
+
newStatus = fileState.status;
|
|
856
|
+
break;
|
|
857
|
+
case 'processed':
|
|
858
|
+
if (!isPreviewable || !hasPreview) {
|
|
859
|
+
newStatus = 'complete';
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
newStatus = 'loading-preview';
|
|
863
|
+
break;
|
|
864
|
+
default:
|
|
865
|
+
newStatus = 'loading';
|
|
866
|
+
}
|
|
867
|
+
var newProgress = newStatus === 'uploading' && fileState.status === 'uploading' ? fileState.progress : 1;
|
|
868
|
+
setStatus(newStatus);
|
|
869
|
+
uploadProgressRef.current = newProgress;
|
|
870
|
+
} else {
|
|
871
|
+
var e = new MediaFileStateError(fileState.id, fileState.reason, fileState.message, fileState.details);
|
|
872
|
+
var errorReason = status === 'uploading' ? 'upload' : 'metadata-fetch';
|
|
873
|
+
setError(new MediaCardError(errorReason, e));
|
|
874
|
+
setStatus('error');
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
});
|
|
878
|
+
useEffect(function () {
|
|
879
|
+
updateFileStateRef.current();
|
|
880
|
+
}, [fileState, updateFileStateRef]);
|
|
881
|
+
|
|
882
|
+
//----------------------------------------------------------------//
|
|
883
|
+
//---------------------- Render Card Function --------------------//
|
|
884
|
+
//----------------------------------------------------------------//
|
|
885
|
+
|
|
886
|
+
var renderCard = function renderCard() {
|
|
887
|
+
var withCallbacks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
888
|
+
var cardStatusOverride = arguments.length > 1 ? arguments[1] : undefined;
|
|
889
|
+
var izLazyOverride = arguments.length > 2 ? arguments[2] : undefined;
|
|
890
|
+
var mediaItemType = identifier.mediaItemType;
|
|
891
|
+
var isLazyWithOverride = izLazyOverride === undefined ? isLazy : izLazyOverride;
|
|
892
|
+
|
|
893
|
+
// Card can be artificially turned visible before entering the viewport
|
|
894
|
+
// For example, when we have the image in cache
|
|
895
|
+
var nativeLazyLoad = isLazyWithOverride && !isCardVisible;
|
|
896
|
+
// Force Media Image to always display img for SSR
|
|
897
|
+
var forceSyncDisplay = !!ssr;
|
|
898
|
+
var mediaCardCursor = getMediaCardCursor(!!useInlinePlayer, !!shouldOpenMediaViewer, status === 'error' || status === 'failed-processing', !!cardPreview, metadata.mediaType);
|
|
899
|
+
var card = /*#__PURE__*/React.createElement(CardViewV2, {
|
|
900
|
+
status: cardStatusOverride || status,
|
|
901
|
+
error: error,
|
|
902
|
+
mediaItemType: mediaItemType,
|
|
903
|
+
metadata: metadata,
|
|
904
|
+
cardPreview: cardPreview,
|
|
905
|
+
alt: alt,
|
|
906
|
+
appearance: appearance,
|
|
907
|
+
resizeMode: resizeMode,
|
|
908
|
+
dimensions: dimensions,
|
|
909
|
+
actions: computedActions,
|
|
910
|
+
selectable: selectable,
|
|
911
|
+
selected: selected,
|
|
912
|
+
onClick: withCallbacks ? onCardViewClick : undefined,
|
|
913
|
+
onMouseEnter: withCallbacks ? function (event) {
|
|
914
|
+
onMouseEnter === null || onMouseEnter === void 0 || onMouseEnter({
|
|
915
|
+
event: event,
|
|
916
|
+
mediaItemDetails: metadata
|
|
917
|
+
});
|
|
918
|
+
} : undefined,
|
|
919
|
+
disableOverlay: disableOverlay,
|
|
920
|
+
progress: uploadProgressRef.current,
|
|
921
|
+
onDisplayImage: withCallbacks ? function () {
|
|
922
|
+
var payloadPart = {
|
|
923
|
+
fileId: identifier.id,
|
|
924
|
+
isUserCollection: identifier.collectionName === RECENTS_COLLECTION
|
|
925
|
+
};
|
|
926
|
+
globalMediaEventEmitter.emit('media-viewed', _objectSpread({
|
|
927
|
+
viewingLevel: 'minimal'
|
|
928
|
+
}, payloadPart));
|
|
929
|
+
} : undefined,
|
|
930
|
+
innerRef: setCardElement,
|
|
931
|
+
testId: testId,
|
|
932
|
+
featureFlags: featureFlags,
|
|
933
|
+
titleBoxBgColor: titleBoxBgColor,
|
|
934
|
+
titleBoxIcon: titleBoxIcon,
|
|
935
|
+
onImageError: withCallbacks ? onImageError : undefined,
|
|
936
|
+
onImageLoad: withCallbacks ? onImageLoad : undefined,
|
|
937
|
+
nativeLazyLoad: nativeLazyLoad,
|
|
938
|
+
forceSyncDisplay: forceSyncDisplay,
|
|
939
|
+
mediaCardCursor: mediaCardCursor,
|
|
940
|
+
shouldHideTooltip: shouldHideTooltip
|
|
941
|
+
});
|
|
942
|
+
return isLazyWithOverride ? /*#__PURE__*/React.createElement(ViewportDetector, {
|
|
943
|
+
cardEl: cardElement,
|
|
944
|
+
onVisible: function onVisible() {
|
|
945
|
+
setIsCardVisible(true);
|
|
946
|
+
}
|
|
947
|
+
}, card) : card;
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
//----------------------------------------------------------------//
|
|
951
|
+
//-------------------------- RENDER ------------------------------//
|
|
952
|
+
//----------------------------------------------------------------//
|
|
953
|
+
|
|
954
|
+
var inlinePlayerFallback = renderCard(false, 'loading', false);
|
|
955
|
+
var collectionName = identifier.collectionName || '';
|
|
956
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, isPlayingFile ? /*#__PURE__*/React.createElement(Suspense, {
|
|
957
|
+
fallback: inlinePlayerFallback
|
|
958
|
+
}, /*#__PURE__*/React.createElement(InlinePlayerLazyV2, {
|
|
959
|
+
dimensions: dimensions,
|
|
960
|
+
originalDimensions: originalDimensions,
|
|
961
|
+
identifier: identifier,
|
|
962
|
+
autoplay: !!shouldAutoplay,
|
|
963
|
+
onFullscreenChange: onFullscreenChange,
|
|
964
|
+
onError: function onError() {
|
|
965
|
+
setIsPlayingFile(false);
|
|
966
|
+
},
|
|
967
|
+
onClick: onCardClick,
|
|
968
|
+
selected: selected,
|
|
969
|
+
ref: setCardElement,
|
|
970
|
+
testId: testId,
|
|
971
|
+
cardPreview: cardPreview
|
|
972
|
+
})) : renderCard(), mediaViewerSelectedItem ? /*#__PURE__*/ReactDOM.createPortal( /*#__PURE__*/React.createElement(MediaViewer, {
|
|
973
|
+
collectionName: collectionName,
|
|
974
|
+
items: mediaViewerItems || [],
|
|
975
|
+
mediaClientConfig: mediaClient.config,
|
|
976
|
+
selectedItem: mediaViewerSelectedItem,
|
|
977
|
+
onClose: function onClose() {
|
|
978
|
+
setMediaViewerSelectedItem(null);
|
|
979
|
+
},
|
|
980
|
+
contextId: contextId,
|
|
981
|
+
featureFlags: featureFlags
|
|
982
|
+
}), document.body) : null, ssr === 'server' && /*#__PURE__*/React.createElement(StoreSSRDataScript, {
|
|
983
|
+
identifier: identifier,
|
|
984
|
+
dataURI: cardPreview === null || cardPreview === void 0 ? void 0 : cardPreview.dataURI,
|
|
985
|
+
dimensions: getRequestedDimensions({
|
|
986
|
+
dimensions: dimensions,
|
|
987
|
+
element: cardElement
|
|
988
|
+
}),
|
|
989
|
+
error: ((_ssrReliabilityRef$cu = ssrReliabilityRef.current.server) === null || _ssrReliabilityRef$cu === void 0 ? void 0 : _ssrReliabilityRef$cu.status) === 'fail' ? ssrReliabilityRef.current.server : undefined
|
|
990
|
+
}));
|
|
991
|
+
};
|