@atlaskit/smart-card 27.4.1 → 27.4.2
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 +8 -0
- package/dist/cjs/state/hooks/use-resolve/index.js +15 -120
- package/dist/cjs/state/hooks/use-response/index.js +132 -0
- package/dist/cjs/utils/analytics/analytics.js +1 -1
- package/dist/cjs/view/LinkUrl/index.js +1 -1
- package/dist/es2019/state/hooks/use-resolve/index.js +9 -112
- package/dist/es2019/state/hooks/use-response/index.js +126 -0
- package/dist/es2019/utils/analytics/analytics.js +1 -1
- package/dist/es2019/view/LinkUrl/index.js +1 -1
- package/dist/esm/state/hooks/use-resolve/index.js +13 -118
- package/dist/esm/state/hooks/use-response/index.js +126 -0
- package/dist/esm/utils/analytics/analytics.js +1 -1
- package/dist/esm/view/LinkUrl/index.js +1 -1
- package/dist/types/state/hooks/use-response/index.d.ts +7 -0
- package/dist/types-ts4.5/state/hooks/use-response/index.d.ts +7 -0
- package/package.json +3 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @atlaskit/smart-card
|
|
2
2
|
|
|
3
|
+
## 27.4.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#105240](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/105240)
|
|
8
|
+
[`d54017d7086f`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/d54017d7086f) -
|
|
9
|
+
EDM-9896: separate useResponse hook from useResolveHook.
|
|
10
|
+
|
|
3
11
|
## 27.4.1
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
|
@@ -8,133 +8,28 @@ exports.default = void 0;
|
|
|
8
8
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
9
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
10
10
|
var _react = require("react");
|
|
11
|
-
var _reactDom = require("react-dom");
|
|
12
11
|
var _analytics = require("../../analytics");
|
|
13
|
-
var
|
|
14
|
-
var _constants = require("../../actions/constants");
|
|
15
|
-
var _jsonld = require("../../../utils/jsonld");
|
|
16
|
-
var _constants2 = require("../../../constants");
|
|
12
|
+
var _constants = require("../../../constants");
|
|
17
13
|
var _linkProvider = require("@atlaskit/link-provider");
|
|
14
|
+
var _useResponse2 = _interopRequireDefault(require("../use-response"));
|
|
18
15
|
var useResolve = function useResolve() {
|
|
19
16
|
// Request JSON-LD data for the card from ORS, if it has extended
|
|
20
17
|
// its cache lifespan OR there is no data for it currently. Once the data
|
|
21
|
-
// has come back asynchronously,
|
|
18
|
+
// has come back asynchronously, call the useResponse callback to
|
|
19
|
+
// dispatch the resolved action for the card.
|
|
22
20
|
var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
|
|
23
21
|
store = _useSmartLinkContext.store,
|
|
24
|
-
connections = _useSmartLinkContext.connections
|
|
25
|
-
|
|
26
|
-
var
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var setMetadataStatus = (0, _react.useCallback)(function (url, metadataStatus) {
|
|
30
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_UPDATE_METADATA_STATUS, {
|
|
31
|
-
url: url
|
|
32
|
-
}, undefined, undefined, metadataStatus));
|
|
33
|
-
}, [dispatch]);
|
|
34
|
-
var handleResolvedLinkError = (0, _react.useCallback)(function (url, error, response, isMetadataRequest) {
|
|
35
|
-
var _ref = getState()[url] || {
|
|
36
|
-
status: _constants2.SmartLinkStatus.Pending,
|
|
37
|
-
details: undefined
|
|
38
|
-
},
|
|
39
|
-
details = _ref.details;
|
|
40
|
-
var hasData = !!(details && details.data);
|
|
41
|
-
|
|
42
|
-
// If metadata request then set metadata status, return and do not alter link status
|
|
43
|
-
if (isMetadataRequest) {
|
|
44
|
-
setMetadataStatus(url, 'errored');
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
if (error.kind === 'fatal') {
|
|
48
|
-
// If there's no previous data in the store for this URL, then bail
|
|
49
|
-
// out and let the editor handle fallbacks (returns to a blue link).
|
|
50
|
-
if (!hasData && status !== 'resolved') {
|
|
51
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR, {
|
|
52
|
-
url: url
|
|
53
|
-
}, details, error));
|
|
54
|
-
throw error;
|
|
55
|
-
}
|
|
56
|
-
// If we already have resolved data for this URL in the store, then
|
|
57
|
-
// simply fallback to the previous data.
|
|
58
|
-
if (hasData) {
|
|
59
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
60
|
-
url: url
|
|
61
|
-
}, details));
|
|
62
|
-
}
|
|
63
|
-
// Handle AuthErrors (user did not have access to resource) -
|
|
64
|
-
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
65
|
-
} else if (error.kind === 'auth') {
|
|
66
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
67
|
-
url: url
|
|
68
|
-
}, (0, _jsonld.getUnauthorizedJsonLd)()));
|
|
69
|
-
} else {
|
|
70
|
-
if (error.kind === 'fallback') {
|
|
71
|
-
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
72
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR_FALLBACK, {
|
|
73
|
-
url: url
|
|
74
|
-
}, response, error));
|
|
75
|
-
} else {
|
|
76
|
-
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
77
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR, {
|
|
78
|
-
url: url
|
|
79
|
-
}, undefined, error));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}, [getState, setMetadataStatus, dispatch]);
|
|
83
|
-
var handleResolvedLinkSuccess = (0, _react.useCallback)(function (resourceUrl, response, isReloading, isMetadataRequest) {
|
|
84
|
-
//if a link resolves normally, metadata will also always be resolved
|
|
85
|
-
setMetadataStatus(resourceUrl, 'resolved');
|
|
86
|
-
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
87
|
-
if (isReloading) {
|
|
88
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RELOADING, {
|
|
89
|
-
url: resourceUrl
|
|
90
|
-
}, response));
|
|
91
|
-
} else {
|
|
92
|
-
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
93
|
-
url: resourceUrl
|
|
94
|
-
}, response, undefined, undefined, isMetadataRequest));
|
|
95
|
-
}
|
|
96
|
-
}, [setMetadataStatus, dispatch]);
|
|
97
|
-
var handleResolvedLinkResponse = (0, _react.useCallback)(function (resourceUrl, response) {
|
|
98
|
-
var isReloading = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
99
|
-
var isMetadataRequest = arguments.length > 3 ? arguments[3] : undefined;
|
|
100
|
-
var hostname = new URL(resourceUrl).hostname;
|
|
101
|
-
var nextStatus = response ? (0, _linkingCommon.getStatus)(response) : 'fatal';
|
|
102
|
-
|
|
103
|
-
// If we require authorization & do not have an authFlow available,
|
|
104
|
-
// throw an error and render as a normal blue link.
|
|
105
|
-
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
106
|
-
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('fallback', hostname, _constants.ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Handle any other errors
|
|
111
|
-
if (nextStatus === 'fatal') {
|
|
112
|
-
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('fatal', hostname, _constants.ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Handle errors of any other kind in the metadata request response
|
|
117
|
-
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
118
|
-
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('error', hostname, _constants.ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Using unstable_batchedUpdates because this store update happens async and trigers a rerender
|
|
124
|
-
* more info:
|
|
125
|
-
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
126
|
-
* https://react-redux.js.org/api/batch
|
|
127
|
-
*/
|
|
128
|
-
(0, _reactDom.unstable_batchedUpdates)(function () {
|
|
129
|
-
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
130
|
-
});
|
|
131
|
-
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
22
|
+
connections = _useSmartLinkContext.connections;
|
|
23
|
+
var getState = store.getState;
|
|
24
|
+
var _useResponse = (0, _useResponse2.default)(),
|
|
25
|
+
handleResolvedLinkResponse = _useResponse.handleResolvedLinkResponse,
|
|
26
|
+
handleResolvedLinkError = _useResponse.handleResolvedLinkError;
|
|
132
27
|
return (0, _react.useCallback)( /*#__PURE__*/function () {
|
|
133
|
-
var
|
|
28
|
+
var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(url) {
|
|
134
29
|
var isReloading,
|
|
135
30
|
isMetadataRequest,
|
|
136
31
|
id,
|
|
137
|
-
|
|
32
|
+
_ref2,
|
|
138
33
|
details,
|
|
139
34
|
hasData,
|
|
140
35
|
_args = arguments;
|
|
@@ -144,10 +39,10 @@ var useResolve = function useResolve() {
|
|
|
144
39
|
isReloading = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
|
|
145
40
|
isMetadataRequest = _args.length > 2 && _args[2] !== undefined ? _args[2] : false;
|
|
146
41
|
id = _args.length > 3 && _args[3] !== undefined ? _args[3] : '';
|
|
147
|
-
|
|
148
|
-
status:
|
|
42
|
+
_ref2 = getState()[url] || {
|
|
43
|
+
status: _constants.SmartLinkStatus.Pending,
|
|
149
44
|
details: undefined
|
|
150
|
-
}, details =
|
|
45
|
+
}, details = _ref2.details;
|
|
151
46
|
hasData = !!(details && details.data);
|
|
152
47
|
if (!(isReloading || !hasData || isMetadataRequest)) {
|
|
153
48
|
_context.next = 9;
|
|
@@ -169,7 +64,7 @@ var useResolve = function useResolve() {
|
|
|
169
64
|
}, _callee);
|
|
170
65
|
}));
|
|
171
66
|
return function (_x) {
|
|
172
|
-
return
|
|
67
|
+
return _ref.apply(this, arguments);
|
|
173
68
|
};
|
|
174
69
|
}(), [connections.client, getState, handleResolvedLinkError, handleResolvedLinkResponse]);
|
|
175
70
|
};
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _linkingCommon = require("@atlaskit/linking-common");
|
|
9
|
+
var _constants = require("../../../constants");
|
|
10
|
+
var _jsonld = require("../../../utils/jsonld");
|
|
11
|
+
var _constants2 = require("../../actions/constants");
|
|
12
|
+
var _reactDom = require("react-dom");
|
|
13
|
+
var _linkProvider = require("@atlaskit/link-provider");
|
|
14
|
+
var useResponse = function useResponse() {
|
|
15
|
+
// Takes in the JSON-LD response and dispatch the resolved action for the card.
|
|
16
|
+
// It will dispatch a successful or error action
|
|
17
|
+
var _useSmartLinkContext = (0, _linkProvider.useSmartLinkContext)(),
|
|
18
|
+
store = _useSmartLinkContext.store,
|
|
19
|
+
config = _useSmartLinkContext.config;
|
|
20
|
+
var getState = store.getState,
|
|
21
|
+
dispatch = store.dispatch;
|
|
22
|
+
var hasAuthFlowSupported = config.authFlow !== 'disabled';
|
|
23
|
+
var setMetadataStatus = (0, _react.useCallback)(function (url, metadataStatus) {
|
|
24
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_UPDATE_METADATA_STATUS, {
|
|
25
|
+
url: url
|
|
26
|
+
}, undefined, undefined, metadataStatus));
|
|
27
|
+
}, [dispatch]);
|
|
28
|
+
var handleResolvedLinkError = (0, _react.useCallback)(function (url, error, response, isMetadataRequest) {
|
|
29
|
+
var _ref = getState()[url] || {
|
|
30
|
+
status: _constants.SmartLinkStatus.Pending,
|
|
31
|
+
details: undefined
|
|
32
|
+
},
|
|
33
|
+
details = _ref.details;
|
|
34
|
+
var hasData = !!(details && details.data);
|
|
35
|
+
// If metadata request then set metadata status, return and do not alter link status
|
|
36
|
+
if (isMetadataRequest) {
|
|
37
|
+
setMetadataStatus(url, 'errored');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (error.kind === 'fatal') {
|
|
41
|
+
// If there's no previous data in the store for this URL, then bail
|
|
42
|
+
// out and let the editor handle fallbacks (returns to a blue link).
|
|
43
|
+
if (!hasData && status !== 'resolved') {
|
|
44
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR, {
|
|
45
|
+
url: url
|
|
46
|
+
}, details, error));
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
// If we already have resolved data for this URL in the store, then
|
|
50
|
+
// simply fallback to the previous data.
|
|
51
|
+
if (hasData) {
|
|
52
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
53
|
+
url: url
|
|
54
|
+
}, details));
|
|
55
|
+
}
|
|
56
|
+
// Handle AuthErrors (user did not have access to resource) -
|
|
57
|
+
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
58
|
+
} else if (error.kind === 'auth') {
|
|
59
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
60
|
+
url: url
|
|
61
|
+
}, (0, _jsonld.getUnauthorizedJsonLd)()));
|
|
62
|
+
} else {
|
|
63
|
+
if (error.kind === 'fallback') {
|
|
64
|
+
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
65
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR_FALLBACK, {
|
|
66
|
+
url: url
|
|
67
|
+
}, response, error));
|
|
68
|
+
} else {
|
|
69
|
+
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
70
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_ERROR, {
|
|
71
|
+
url: url
|
|
72
|
+
}, undefined, error));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}, [getState, setMetadataStatus, dispatch]);
|
|
76
|
+
var handleResolvedLinkSuccess = (0, _react.useCallback)(function (resourceUrl, response, isReloading, isMetadataRequest) {
|
|
77
|
+
//if a link resolves normally, metadata will also always be resolved
|
|
78
|
+
setMetadataStatus(resourceUrl, 'resolved');
|
|
79
|
+
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
80
|
+
if (isReloading) {
|
|
81
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RELOADING, {
|
|
82
|
+
url: resourceUrl
|
|
83
|
+
}, response));
|
|
84
|
+
} else {
|
|
85
|
+
dispatch((0, _linkingCommon.cardAction)(_linkingCommon.ACTION_RESOLVED, {
|
|
86
|
+
url: resourceUrl
|
|
87
|
+
}, response, undefined, undefined, isMetadataRequest));
|
|
88
|
+
}
|
|
89
|
+
}, [setMetadataStatus, dispatch]);
|
|
90
|
+
var handleResolvedLinkResponse = (0, _react.useCallback)(function (resourceUrl, response) {
|
|
91
|
+
var isReloading = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
92
|
+
var isMetadataRequest = arguments.length > 3 ? arguments[3] : undefined;
|
|
93
|
+
var hostname = new URL(resourceUrl).hostname;
|
|
94
|
+
var nextStatus = response ? (0, _linkingCommon.getStatus)(response) : 'fatal';
|
|
95
|
+
|
|
96
|
+
// If we require authorization & do not have an authFlow available,
|
|
97
|
+
// throw an error and render as a normal blue link.
|
|
98
|
+
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
99
|
+
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('fallback', hostname, _constants2.ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Handle any other errors
|
|
104
|
+
if (nextStatus === 'fatal') {
|
|
105
|
+
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('fatal', hostname, _constants2.ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Handle errors of any other kind in the metadata request response
|
|
110
|
+
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
111
|
+
handleResolvedLinkError(resourceUrl, new _linkingCommon.APIError('error', hostname, _constants2.ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Using unstable_batchedUpdates because this store update happens async and triggers a rerender
|
|
117
|
+
* more info:
|
|
118
|
+
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
119
|
+
* https://react-redux.js.org/api/batch
|
|
120
|
+
*/
|
|
121
|
+
(0, _reactDom.unstable_batchedUpdates)(function () {
|
|
122
|
+
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
123
|
+
});
|
|
124
|
+
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
125
|
+
return (0, _react.useMemo)(function () {
|
|
126
|
+
return {
|
|
127
|
+
handleResolvedLinkError: handleResolvedLinkError,
|
|
128
|
+
handleResolvedLinkResponse: handleResolvedLinkResponse
|
|
129
|
+
};
|
|
130
|
+
}, [handleResolvedLinkError, handleResolvedLinkResponse]);
|
|
131
|
+
};
|
|
132
|
+
var _default = exports.default = useResponse;
|
|
@@ -22,7 +22,7 @@ var ANALYTICS_CHANNEL = exports.ANALYTICS_CHANNEL = 'media';
|
|
|
22
22
|
var context = exports.context = {
|
|
23
23
|
componentName: 'smart-cards',
|
|
24
24
|
packageName: "@atlaskit/smart-card",
|
|
25
|
-
packageVersion: "27.4.
|
|
25
|
+
packageVersion: "27.4.2"
|
|
26
26
|
};
|
|
27
27
|
var TrackQuickActionType = exports.TrackQuickActionType = /*#__PURE__*/function (TrackQuickActionType) {
|
|
28
28
|
TrackQuickActionType["StatusUpdate"] = "StatusUpdate";
|
|
@@ -17,7 +17,7 @@ var _excluded = ["href", "children", "checkSafety", "onClick", "testId"],
|
|
|
17
17
|
_excluded2 = ["isLinkSafe", "showSafetyWarningModal"];
|
|
18
18
|
var PACKAGE_DATA = {
|
|
19
19
|
packageName: "@atlaskit/smart-card",
|
|
20
|
-
packageVersion: "27.4.
|
|
20
|
+
packageVersion: "27.4.2",
|
|
21
21
|
componentName: 'linkUrl'
|
|
22
22
|
};
|
|
23
23
|
var Link = (0, _click.withLinkClickedEvent)('a');
|
|
@@ -1,127 +1,24 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
|
-
import { unstable_batchedUpdates } from 'react-dom';
|
|
3
2
|
import { addMetadataToExperience } from '../../analytics';
|
|
4
|
-
import { ACTION_ERROR, ACTION_ERROR_FALLBACK, ACTION_RELOADING, ACTION_RESOLVED, ACTION_UPDATE_METADATA_STATUS, APIError, cardAction, getStatus } from '@atlaskit/linking-common';
|
|
5
|
-
import { ERROR_MESSAGE_FATAL, ERROR_MESSAGE_METADATA, ERROR_MESSAGE_OAUTH } from '../../actions/constants';
|
|
6
|
-
import { getUnauthorizedJsonLd } from '../../../utils/jsonld';
|
|
7
3
|
import { SmartLinkStatus } from '../../../constants';
|
|
8
4
|
import { useSmartLinkContext } from '@atlaskit/link-provider';
|
|
5
|
+
import useResponse from '../use-response';
|
|
9
6
|
const useResolve = () => {
|
|
10
7
|
// Request JSON-LD data for the card from ORS, if it has extended
|
|
11
8
|
// its cache lifespan OR there is no data for it currently. Once the data
|
|
12
|
-
// has come back asynchronously,
|
|
9
|
+
// has come back asynchronously, call the useResponse callback to
|
|
10
|
+
// dispatch the resolved action for the card.
|
|
13
11
|
const {
|
|
14
12
|
store,
|
|
15
|
-
connections
|
|
16
|
-
config
|
|
13
|
+
connections
|
|
17
14
|
} = useSmartLinkContext();
|
|
18
15
|
const {
|
|
19
|
-
getState
|
|
20
|
-
dispatch
|
|
16
|
+
getState
|
|
21
17
|
} = store;
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}, undefined, undefined, metadataStatus));
|
|
27
|
-
}, [dispatch]);
|
|
28
|
-
const handleResolvedLinkError = useCallback((url, error, response, isMetadataRequest) => {
|
|
29
|
-
const {
|
|
30
|
-
details
|
|
31
|
-
} = getState()[url] || {
|
|
32
|
-
status: SmartLinkStatus.Pending,
|
|
33
|
-
details: undefined
|
|
34
|
-
};
|
|
35
|
-
const hasData = !!(details && details.data);
|
|
36
|
-
|
|
37
|
-
// If metadata request then set metadata status, return and do not alter link status
|
|
38
|
-
if (isMetadataRequest) {
|
|
39
|
-
setMetadataStatus(url, 'errored');
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (error.kind === 'fatal') {
|
|
43
|
-
// If there's no previous data in the store for this URL, then bail
|
|
44
|
-
// out and let the editor handle fallbacks (returns to a blue link).
|
|
45
|
-
if (!hasData && status !== 'resolved') {
|
|
46
|
-
dispatch(cardAction(ACTION_ERROR, {
|
|
47
|
-
url
|
|
48
|
-
}, details, error));
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
// If we already have resolved data for this URL in the store, then
|
|
52
|
-
// simply fallback to the previous data.
|
|
53
|
-
if (hasData) {
|
|
54
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
55
|
-
url
|
|
56
|
-
}, details));
|
|
57
|
-
}
|
|
58
|
-
// Handle AuthErrors (user did not have access to resource) -
|
|
59
|
-
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
60
|
-
} else if (error.kind === 'auth') {
|
|
61
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
62
|
-
url
|
|
63
|
-
}, getUnauthorizedJsonLd()));
|
|
64
|
-
} else {
|
|
65
|
-
if (error.kind === 'fallback') {
|
|
66
|
-
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
67
|
-
dispatch(cardAction(ACTION_ERROR_FALLBACK, {
|
|
68
|
-
url
|
|
69
|
-
}, response, error));
|
|
70
|
-
} else {
|
|
71
|
-
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
72
|
-
dispatch(cardAction(ACTION_ERROR, {
|
|
73
|
-
url
|
|
74
|
-
}, undefined, error));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}, [getState, setMetadataStatus, dispatch]);
|
|
78
|
-
const handleResolvedLinkSuccess = useCallback((resourceUrl, response, isReloading, isMetadataRequest) => {
|
|
79
|
-
//if a link resolves normally, metadata will also always be resolved
|
|
80
|
-
setMetadataStatus(resourceUrl, 'resolved');
|
|
81
|
-
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
82
|
-
if (isReloading) {
|
|
83
|
-
dispatch(cardAction(ACTION_RELOADING, {
|
|
84
|
-
url: resourceUrl
|
|
85
|
-
}, response));
|
|
86
|
-
} else {
|
|
87
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
88
|
-
url: resourceUrl
|
|
89
|
-
}, response, undefined, undefined, isMetadataRequest));
|
|
90
|
-
}
|
|
91
|
-
}, [setMetadataStatus, dispatch]);
|
|
92
|
-
const handleResolvedLinkResponse = useCallback((resourceUrl, response, isReloading = false, isMetadataRequest) => {
|
|
93
|
-
const hostname = new URL(resourceUrl).hostname;
|
|
94
|
-
const nextStatus = response ? getStatus(response) : 'fatal';
|
|
95
|
-
|
|
96
|
-
// If we require authorization & do not have an authFlow available,
|
|
97
|
-
// throw an error and render as a normal blue link.
|
|
98
|
-
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
99
|
-
handleResolvedLinkError(resourceUrl, new APIError('fallback', hostname, ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Handle any other errors
|
|
104
|
-
if (nextStatus === 'fatal') {
|
|
105
|
-
handleResolvedLinkError(resourceUrl, new APIError('fatal', hostname, ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Handle errors of any other kind in the metadata request response
|
|
110
|
-
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
111
|
-
handleResolvedLinkError(resourceUrl, new APIError('error', hostname, ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Using unstable_batchedUpdates because this store update happens async and trigers a rerender
|
|
117
|
-
* more info:
|
|
118
|
-
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
119
|
-
* https://react-redux.js.org/api/batch
|
|
120
|
-
*/
|
|
121
|
-
unstable_batchedUpdates(() => {
|
|
122
|
-
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
123
|
-
});
|
|
124
|
-
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
18
|
+
const {
|
|
19
|
+
handleResolvedLinkResponse,
|
|
20
|
+
handleResolvedLinkError
|
|
21
|
+
} = useResponse();
|
|
125
22
|
return useCallback(async (url, isReloading = false, isMetadataRequest = false, id = '') => {
|
|
126
23
|
const {
|
|
127
24
|
details
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useCallback, useMemo } from 'react';
|
|
2
|
+
import { ACTION_ERROR, ACTION_ERROR_FALLBACK, ACTION_RELOADING, ACTION_RESOLVED, ACTION_UPDATE_METADATA_STATUS, APIError, cardAction, getStatus } from '@atlaskit/linking-common';
|
|
3
|
+
import { SmartLinkStatus } from '../../../constants';
|
|
4
|
+
import { getUnauthorizedJsonLd } from '../../../utils/jsonld';
|
|
5
|
+
import { ERROR_MESSAGE_FATAL, ERROR_MESSAGE_METADATA, ERROR_MESSAGE_OAUTH } from '../../actions/constants';
|
|
6
|
+
import { unstable_batchedUpdates } from 'react-dom';
|
|
7
|
+
import { useSmartLinkContext } from '@atlaskit/link-provider';
|
|
8
|
+
const useResponse = () => {
|
|
9
|
+
// Takes in the JSON-LD response and dispatch the resolved action for the card.
|
|
10
|
+
// It will dispatch a successful or error action
|
|
11
|
+
const {
|
|
12
|
+
store,
|
|
13
|
+
config
|
|
14
|
+
} = useSmartLinkContext();
|
|
15
|
+
const {
|
|
16
|
+
getState,
|
|
17
|
+
dispatch
|
|
18
|
+
} = store;
|
|
19
|
+
const hasAuthFlowSupported = config.authFlow !== 'disabled';
|
|
20
|
+
const setMetadataStatus = useCallback((url, metadataStatus) => {
|
|
21
|
+
dispatch(cardAction(ACTION_UPDATE_METADATA_STATUS, {
|
|
22
|
+
url
|
|
23
|
+
}, undefined, undefined, metadataStatus));
|
|
24
|
+
}, [dispatch]);
|
|
25
|
+
const handleResolvedLinkError = useCallback((url, error, response, isMetadataRequest) => {
|
|
26
|
+
const {
|
|
27
|
+
details
|
|
28
|
+
} = getState()[url] || {
|
|
29
|
+
status: SmartLinkStatus.Pending,
|
|
30
|
+
details: undefined
|
|
31
|
+
};
|
|
32
|
+
const hasData = !!(details && details.data);
|
|
33
|
+
// If metadata request then set metadata status, return and do not alter link status
|
|
34
|
+
if (isMetadataRequest) {
|
|
35
|
+
setMetadataStatus(url, 'errored');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (error.kind === 'fatal') {
|
|
39
|
+
// If there's no previous data in the store for this URL, then bail
|
|
40
|
+
// out and let the editor handle fallbacks (returns to a blue link).
|
|
41
|
+
if (!hasData && status !== 'resolved') {
|
|
42
|
+
dispatch(cardAction(ACTION_ERROR, {
|
|
43
|
+
url
|
|
44
|
+
}, details, error));
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
// If we already have resolved data for this URL in the store, then
|
|
48
|
+
// simply fallback to the previous data.
|
|
49
|
+
if (hasData) {
|
|
50
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
51
|
+
url
|
|
52
|
+
}, details));
|
|
53
|
+
}
|
|
54
|
+
// Handle AuthErrors (user did not have access to resource) -
|
|
55
|
+
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
56
|
+
} else if (error.kind === 'auth') {
|
|
57
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
58
|
+
url
|
|
59
|
+
}, getUnauthorizedJsonLd()));
|
|
60
|
+
} else {
|
|
61
|
+
if (error.kind === 'fallback') {
|
|
62
|
+
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
63
|
+
dispatch(cardAction(ACTION_ERROR_FALLBACK, {
|
|
64
|
+
url
|
|
65
|
+
}, response, error));
|
|
66
|
+
} else {
|
|
67
|
+
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
68
|
+
dispatch(cardAction(ACTION_ERROR, {
|
|
69
|
+
url
|
|
70
|
+
}, undefined, error));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}, [getState, setMetadataStatus, dispatch]);
|
|
74
|
+
const handleResolvedLinkSuccess = useCallback((resourceUrl, response, isReloading, isMetadataRequest) => {
|
|
75
|
+
//if a link resolves normally, metadata will also always be resolved
|
|
76
|
+
setMetadataStatus(resourceUrl, 'resolved');
|
|
77
|
+
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
78
|
+
if (isReloading) {
|
|
79
|
+
dispatch(cardAction(ACTION_RELOADING, {
|
|
80
|
+
url: resourceUrl
|
|
81
|
+
}, response));
|
|
82
|
+
} else {
|
|
83
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
84
|
+
url: resourceUrl
|
|
85
|
+
}, response, undefined, undefined, isMetadataRequest));
|
|
86
|
+
}
|
|
87
|
+
}, [setMetadataStatus, dispatch]);
|
|
88
|
+
const handleResolvedLinkResponse = useCallback((resourceUrl, response, isReloading = false, isMetadataRequest) => {
|
|
89
|
+
const hostname = new URL(resourceUrl).hostname;
|
|
90
|
+
const nextStatus = response ? getStatus(response) : 'fatal';
|
|
91
|
+
|
|
92
|
+
// If we require authorization & do not have an authFlow available,
|
|
93
|
+
// throw an error and render as a normal blue link.
|
|
94
|
+
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
95
|
+
handleResolvedLinkError(resourceUrl, new APIError('fallback', hostname, ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Handle any other errors
|
|
100
|
+
if (nextStatus === 'fatal') {
|
|
101
|
+
handleResolvedLinkError(resourceUrl, new APIError('fatal', hostname, ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Handle errors of any other kind in the metadata request response
|
|
106
|
+
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
107
|
+
handleResolvedLinkError(resourceUrl, new APIError('error', hostname, ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Using unstable_batchedUpdates because this store update happens async and triggers a rerender
|
|
113
|
+
* more info:
|
|
114
|
+
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
115
|
+
* https://react-redux.js.org/api/batch
|
|
116
|
+
*/
|
|
117
|
+
unstable_batchedUpdates(() => {
|
|
118
|
+
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
119
|
+
});
|
|
120
|
+
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
121
|
+
return useMemo(() => ({
|
|
122
|
+
handleResolvedLinkError,
|
|
123
|
+
handleResolvedLinkResponse
|
|
124
|
+
}), [handleResolvedLinkError, handleResolvedLinkResponse]);
|
|
125
|
+
};
|
|
126
|
+
export default useResponse;
|
|
@@ -4,7 +4,7 @@ export const ANALYTICS_CHANNEL = 'media';
|
|
|
4
4
|
export const context = {
|
|
5
5
|
componentName: 'smart-cards',
|
|
6
6
|
packageName: "@atlaskit/smart-card",
|
|
7
|
-
packageVersion: "27.4.
|
|
7
|
+
packageVersion: "27.4.2"
|
|
8
8
|
};
|
|
9
9
|
export let TrackQuickActionType = /*#__PURE__*/function (TrackQuickActionType) {
|
|
10
10
|
TrackQuickActionType["StatusUpdate"] = "StatusUpdate";
|
|
@@ -7,7 +7,7 @@ import { useLinkWarningModal } from './LinkWarningModal/hooks/use-link-warning-m
|
|
|
7
7
|
import LinkWarningModal from './LinkWarningModal';
|
|
8
8
|
const PACKAGE_DATA = {
|
|
9
9
|
packageName: "@atlaskit/smart-card",
|
|
10
|
-
packageVersion: "27.4.
|
|
10
|
+
packageVersion: "27.4.2",
|
|
11
11
|
componentName: 'linkUrl'
|
|
12
12
|
};
|
|
13
13
|
const Link = withLinkClickedEvent('a');
|
|
@@ -1,133 +1,28 @@
|
|
|
1
1
|
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
2
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
3
3
|
import { useCallback } from 'react';
|
|
4
|
-
import { unstable_batchedUpdates } from 'react-dom';
|
|
5
4
|
import { addMetadataToExperience } from '../../analytics';
|
|
6
|
-
import { ACTION_ERROR, ACTION_ERROR_FALLBACK, ACTION_RELOADING, ACTION_RESOLVED, ACTION_UPDATE_METADATA_STATUS, APIError, cardAction, getStatus } from '@atlaskit/linking-common';
|
|
7
|
-
import { ERROR_MESSAGE_FATAL, ERROR_MESSAGE_METADATA, ERROR_MESSAGE_OAUTH } from '../../actions/constants';
|
|
8
|
-
import { getUnauthorizedJsonLd } from '../../../utils/jsonld';
|
|
9
5
|
import { SmartLinkStatus } from '../../../constants';
|
|
10
6
|
import { useSmartLinkContext } from '@atlaskit/link-provider';
|
|
7
|
+
import useResponse from '../use-response';
|
|
11
8
|
var useResolve = function useResolve() {
|
|
12
9
|
// Request JSON-LD data for the card from ORS, if it has extended
|
|
13
10
|
// its cache lifespan OR there is no data for it currently. Once the data
|
|
14
|
-
// has come back asynchronously,
|
|
11
|
+
// has come back asynchronously, call the useResponse callback to
|
|
12
|
+
// dispatch the resolved action for the card.
|
|
15
13
|
var _useSmartLinkContext = useSmartLinkContext(),
|
|
16
14
|
store = _useSmartLinkContext.store,
|
|
17
|
-
connections = _useSmartLinkContext.connections
|
|
18
|
-
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var setMetadataStatus = useCallback(function (url, metadataStatus) {
|
|
23
|
-
dispatch(cardAction(ACTION_UPDATE_METADATA_STATUS, {
|
|
24
|
-
url: url
|
|
25
|
-
}, undefined, undefined, metadataStatus));
|
|
26
|
-
}, [dispatch]);
|
|
27
|
-
var handleResolvedLinkError = useCallback(function (url, error, response, isMetadataRequest) {
|
|
28
|
-
var _ref = getState()[url] || {
|
|
29
|
-
status: SmartLinkStatus.Pending,
|
|
30
|
-
details: undefined
|
|
31
|
-
},
|
|
32
|
-
details = _ref.details;
|
|
33
|
-
var hasData = !!(details && details.data);
|
|
34
|
-
|
|
35
|
-
// If metadata request then set metadata status, return and do not alter link status
|
|
36
|
-
if (isMetadataRequest) {
|
|
37
|
-
setMetadataStatus(url, 'errored');
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (error.kind === 'fatal') {
|
|
41
|
-
// If there's no previous data in the store for this URL, then bail
|
|
42
|
-
// out and let the editor handle fallbacks (returns to a blue link).
|
|
43
|
-
if (!hasData && status !== 'resolved') {
|
|
44
|
-
dispatch(cardAction(ACTION_ERROR, {
|
|
45
|
-
url: url
|
|
46
|
-
}, details, error));
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
// If we already have resolved data for this URL in the store, then
|
|
50
|
-
// simply fallback to the previous data.
|
|
51
|
-
if (hasData) {
|
|
52
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
53
|
-
url: url
|
|
54
|
-
}, details));
|
|
55
|
-
}
|
|
56
|
-
// Handle AuthErrors (user did not have access to resource) -
|
|
57
|
-
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
58
|
-
} else if (error.kind === 'auth') {
|
|
59
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
60
|
-
url: url
|
|
61
|
-
}, getUnauthorizedJsonLd()));
|
|
62
|
-
} else {
|
|
63
|
-
if (error.kind === 'fallback') {
|
|
64
|
-
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
65
|
-
dispatch(cardAction(ACTION_ERROR_FALLBACK, {
|
|
66
|
-
url: url
|
|
67
|
-
}, response, error));
|
|
68
|
-
} else {
|
|
69
|
-
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
70
|
-
dispatch(cardAction(ACTION_ERROR, {
|
|
71
|
-
url: url
|
|
72
|
-
}, undefined, error));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}, [getState, setMetadataStatus, dispatch]);
|
|
76
|
-
var handleResolvedLinkSuccess = useCallback(function (resourceUrl, response, isReloading, isMetadataRequest) {
|
|
77
|
-
//if a link resolves normally, metadata will also always be resolved
|
|
78
|
-
setMetadataStatus(resourceUrl, 'resolved');
|
|
79
|
-
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
80
|
-
if (isReloading) {
|
|
81
|
-
dispatch(cardAction(ACTION_RELOADING, {
|
|
82
|
-
url: resourceUrl
|
|
83
|
-
}, response));
|
|
84
|
-
} else {
|
|
85
|
-
dispatch(cardAction(ACTION_RESOLVED, {
|
|
86
|
-
url: resourceUrl
|
|
87
|
-
}, response, undefined, undefined, isMetadataRequest));
|
|
88
|
-
}
|
|
89
|
-
}, [setMetadataStatus, dispatch]);
|
|
90
|
-
var handleResolvedLinkResponse = useCallback(function (resourceUrl, response) {
|
|
91
|
-
var isReloading = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
92
|
-
var isMetadataRequest = arguments.length > 3 ? arguments[3] : undefined;
|
|
93
|
-
var hostname = new URL(resourceUrl).hostname;
|
|
94
|
-
var nextStatus = response ? getStatus(response) : 'fatal';
|
|
95
|
-
|
|
96
|
-
// If we require authorization & do not have an authFlow available,
|
|
97
|
-
// throw an error and render as a normal blue link.
|
|
98
|
-
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
99
|
-
handleResolvedLinkError(resourceUrl, new APIError('fallback', hostname, ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Handle any other errors
|
|
104
|
-
if (nextStatus === 'fatal') {
|
|
105
|
-
handleResolvedLinkError(resourceUrl, new APIError('fatal', hostname, ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// Handle errors of any other kind in the metadata request response
|
|
110
|
-
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
111
|
-
handleResolvedLinkError(resourceUrl, new APIError('error', hostname, ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Using unstable_batchedUpdates because this store update happens async and trigers a rerender
|
|
117
|
-
* more info:
|
|
118
|
-
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
119
|
-
* https://react-redux.js.org/api/batch
|
|
120
|
-
*/
|
|
121
|
-
unstable_batchedUpdates(function () {
|
|
122
|
-
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
123
|
-
});
|
|
124
|
-
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
15
|
+
connections = _useSmartLinkContext.connections;
|
|
16
|
+
var getState = store.getState;
|
|
17
|
+
var _useResponse = useResponse(),
|
|
18
|
+
handleResolvedLinkResponse = _useResponse.handleResolvedLinkResponse,
|
|
19
|
+
handleResolvedLinkError = _useResponse.handleResolvedLinkError;
|
|
125
20
|
return useCallback( /*#__PURE__*/function () {
|
|
126
|
-
var
|
|
21
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
|
|
127
22
|
var isReloading,
|
|
128
23
|
isMetadataRequest,
|
|
129
24
|
id,
|
|
130
|
-
|
|
25
|
+
_ref2,
|
|
131
26
|
details,
|
|
132
27
|
hasData,
|
|
133
28
|
_args = arguments;
|
|
@@ -137,10 +32,10 @@ var useResolve = function useResolve() {
|
|
|
137
32
|
isReloading = _args.length > 1 && _args[1] !== undefined ? _args[1] : false;
|
|
138
33
|
isMetadataRequest = _args.length > 2 && _args[2] !== undefined ? _args[2] : false;
|
|
139
34
|
id = _args.length > 3 && _args[3] !== undefined ? _args[3] : '';
|
|
140
|
-
|
|
35
|
+
_ref2 = getState()[url] || {
|
|
141
36
|
status: SmartLinkStatus.Pending,
|
|
142
37
|
details: undefined
|
|
143
|
-
}, details =
|
|
38
|
+
}, details = _ref2.details;
|
|
144
39
|
hasData = !!(details && details.data);
|
|
145
40
|
if (!(isReloading || !hasData || isMetadataRequest)) {
|
|
146
41
|
_context.next = 9;
|
|
@@ -162,7 +57,7 @@ var useResolve = function useResolve() {
|
|
|
162
57
|
}, _callee);
|
|
163
58
|
}));
|
|
164
59
|
return function (_x) {
|
|
165
|
-
return
|
|
60
|
+
return _ref.apply(this, arguments);
|
|
166
61
|
};
|
|
167
62
|
}(), [connections.client, getState, handleResolvedLinkError, handleResolvedLinkResponse]);
|
|
168
63
|
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useCallback, useMemo } from 'react';
|
|
2
|
+
import { ACTION_ERROR, ACTION_ERROR_FALLBACK, ACTION_RELOADING, ACTION_RESOLVED, ACTION_UPDATE_METADATA_STATUS, APIError, cardAction, getStatus } from '@atlaskit/linking-common';
|
|
3
|
+
import { SmartLinkStatus } from '../../../constants';
|
|
4
|
+
import { getUnauthorizedJsonLd } from '../../../utils/jsonld';
|
|
5
|
+
import { ERROR_MESSAGE_FATAL, ERROR_MESSAGE_METADATA, ERROR_MESSAGE_OAUTH } from '../../actions/constants';
|
|
6
|
+
import { unstable_batchedUpdates } from 'react-dom';
|
|
7
|
+
import { useSmartLinkContext } from '@atlaskit/link-provider';
|
|
8
|
+
var useResponse = function useResponse() {
|
|
9
|
+
// Takes in the JSON-LD response and dispatch the resolved action for the card.
|
|
10
|
+
// It will dispatch a successful or error action
|
|
11
|
+
var _useSmartLinkContext = useSmartLinkContext(),
|
|
12
|
+
store = _useSmartLinkContext.store,
|
|
13
|
+
config = _useSmartLinkContext.config;
|
|
14
|
+
var getState = store.getState,
|
|
15
|
+
dispatch = store.dispatch;
|
|
16
|
+
var hasAuthFlowSupported = config.authFlow !== 'disabled';
|
|
17
|
+
var setMetadataStatus = useCallback(function (url, metadataStatus) {
|
|
18
|
+
dispatch(cardAction(ACTION_UPDATE_METADATA_STATUS, {
|
|
19
|
+
url: url
|
|
20
|
+
}, undefined, undefined, metadataStatus));
|
|
21
|
+
}, [dispatch]);
|
|
22
|
+
var handleResolvedLinkError = useCallback(function (url, error, response, isMetadataRequest) {
|
|
23
|
+
var _ref = getState()[url] || {
|
|
24
|
+
status: SmartLinkStatus.Pending,
|
|
25
|
+
details: undefined
|
|
26
|
+
},
|
|
27
|
+
details = _ref.details;
|
|
28
|
+
var hasData = !!(details && details.data);
|
|
29
|
+
// If metadata request then set metadata status, return and do not alter link status
|
|
30
|
+
if (isMetadataRequest) {
|
|
31
|
+
setMetadataStatus(url, 'errored');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (error.kind === 'fatal') {
|
|
35
|
+
// If there's no previous data in the store for this URL, then bail
|
|
36
|
+
// out and let the editor handle fallbacks (returns to a blue link).
|
|
37
|
+
if (!hasData && status !== 'resolved') {
|
|
38
|
+
dispatch(cardAction(ACTION_ERROR, {
|
|
39
|
+
url: url
|
|
40
|
+
}, details, error));
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
// If we already have resolved data for this URL in the store, then
|
|
44
|
+
// simply fallback to the previous data.
|
|
45
|
+
if (hasData) {
|
|
46
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
47
|
+
url: url
|
|
48
|
+
}, details));
|
|
49
|
+
}
|
|
50
|
+
// Handle AuthErrors (user did not have access to resource) -
|
|
51
|
+
// Missing AAID in ASAP claims, or missing UserContext, or 403 from downstream
|
|
52
|
+
} else if (error.kind === 'auth') {
|
|
53
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
54
|
+
url: url
|
|
55
|
+
}, getUnauthorizedJsonLd()));
|
|
56
|
+
} else {
|
|
57
|
+
if (error.kind === 'fallback') {
|
|
58
|
+
// Fallback to blue link with smart link formatting. Not part of reliability.
|
|
59
|
+
dispatch(cardAction(ACTION_ERROR_FALLBACK, {
|
|
60
|
+
url: url
|
|
61
|
+
}, response, error));
|
|
62
|
+
} else {
|
|
63
|
+
// Fallback to blue link with smart link formatting. Part of reliability.
|
|
64
|
+
dispatch(cardAction(ACTION_ERROR, {
|
|
65
|
+
url: url
|
|
66
|
+
}, undefined, error));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}, [getState, setMetadataStatus, dispatch]);
|
|
70
|
+
var handleResolvedLinkSuccess = useCallback(function (resourceUrl, response, isReloading, isMetadataRequest) {
|
|
71
|
+
//if a link resolves normally, metadata will also always be resolved
|
|
72
|
+
setMetadataStatus(resourceUrl, 'resolved');
|
|
73
|
+
// Dispatch Analytics and resolved card action - including unauthorized states.
|
|
74
|
+
if (isReloading) {
|
|
75
|
+
dispatch(cardAction(ACTION_RELOADING, {
|
|
76
|
+
url: resourceUrl
|
|
77
|
+
}, response));
|
|
78
|
+
} else {
|
|
79
|
+
dispatch(cardAction(ACTION_RESOLVED, {
|
|
80
|
+
url: resourceUrl
|
|
81
|
+
}, response, undefined, undefined, isMetadataRequest));
|
|
82
|
+
}
|
|
83
|
+
}, [setMetadataStatus, dispatch]);
|
|
84
|
+
var handleResolvedLinkResponse = useCallback(function (resourceUrl, response) {
|
|
85
|
+
var isReloading = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
86
|
+
var isMetadataRequest = arguments.length > 3 ? arguments[3] : undefined;
|
|
87
|
+
var hostname = new URL(resourceUrl).hostname;
|
|
88
|
+
var nextStatus = response ? getStatus(response) : 'fatal';
|
|
89
|
+
|
|
90
|
+
// If we require authorization & do not have an authFlow available,
|
|
91
|
+
// throw an error and render as a normal blue link.
|
|
92
|
+
if ((nextStatus === 'unauthorized' || nextStatus === 'forbidden') && !hasAuthFlowSupported) {
|
|
93
|
+
handleResolvedLinkError(resourceUrl, new APIError('fallback', hostname, ERROR_MESSAGE_OAUTH), response, isMetadataRequest);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Handle any other errors
|
|
98
|
+
if (nextStatus === 'fatal') {
|
|
99
|
+
handleResolvedLinkError(resourceUrl, new APIError('fatal', hostname, ERROR_MESSAGE_FATAL), undefined, isMetadataRequest);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Handle errors of any other kind in the metadata request response
|
|
104
|
+
if (isMetadataRequest && nextStatus !== 'resolved') {
|
|
105
|
+
handleResolvedLinkError(resourceUrl, new APIError('error', hostname, ERROR_MESSAGE_METADATA), response, isMetadataRequest);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Using unstable_batchedUpdates because this store update happens async and triggers a rerender
|
|
111
|
+
* more info:
|
|
112
|
+
* https://github.com/facebook/react/blob/v18.2.0/packages/use-sync-external-store/src/useSyncExternalStoreShimClient.js#L113
|
|
113
|
+
* https://react-redux.js.org/api/batch
|
|
114
|
+
*/
|
|
115
|
+
unstable_batchedUpdates(function () {
|
|
116
|
+
handleResolvedLinkSuccess(resourceUrl, response, isReloading, isMetadataRequest);
|
|
117
|
+
});
|
|
118
|
+
}, [handleResolvedLinkError, handleResolvedLinkSuccess, hasAuthFlowSupported]);
|
|
119
|
+
return useMemo(function () {
|
|
120
|
+
return {
|
|
121
|
+
handleResolvedLinkError: handleResolvedLinkError,
|
|
122
|
+
handleResolvedLinkResponse: handleResolvedLinkResponse
|
|
123
|
+
};
|
|
124
|
+
}, [handleResolvedLinkError, handleResolvedLinkResponse]);
|
|
125
|
+
};
|
|
126
|
+
export default useResponse;
|
|
@@ -15,7 +15,7 @@ export var ANALYTICS_CHANNEL = 'media';
|
|
|
15
15
|
export var context = {
|
|
16
16
|
componentName: 'smart-cards',
|
|
17
17
|
packageName: "@atlaskit/smart-card",
|
|
18
|
-
packageVersion: "27.4.
|
|
18
|
+
packageVersion: "27.4.2"
|
|
19
19
|
};
|
|
20
20
|
export var TrackQuickActionType = /*#__PURE__*/function (TrackQuickActionType) {
|
|
21
21
|
TrackQuickActionType["StatusUpdate"] = "StatusUpdate";
|
|
@@ -10,7 +10,7 @@ import { useLinkWarningModal } from './LinkWarningModal/hooks/use-link-warning-m
|
|
|
10
10
|
import LinkWarningModal from './LinkWarningModal';
|
|
11
11
|
var PACKAGE_DATA = {
|
|
12
12
|
packageName: "@atlaskit/smart-card",
|
|
13
|
-
packageVersion: "27.4.
|
|
13
|
+
packageVersion: "27.4.2",
|
|
14
14
|
componentName: 'linkUrl'
|
|
15
15
|
};
|
|
16
16
|
var Link = withLinkClickedEvent('a');
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { APIError } from '@atlaskit/linking-common';
|
|
2
|
+
import { type JsonLd } from 'json-ld-types';
|
|
3
|
+
declare const useResponse: () => {
|
|
4
|
+
handleResolvedLinkError: (url: string, error: APIError, response?: JsonLd.Response, isMetadataRequest?: boolean) => void;
|
|
5
|
+
handleResolvedLinkResponse: (resourceUrl: string, response: JsonLd.Response, isReloading?: any, isMetadataRequest?: boolean) => void;
|
|
6
|
+
};
|
|
7
|
+
export default useResponse;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { APIError } from '@atlaskit/linking-common';
|
|
2
|
+
import { type JsonLd } from 'json-ld-types';
|
|
3
|
+
declare const useResponse: () => {
|
|
4
|
+
handleResolvedLinkError: (url: string, error: APIError, response?: JsonLd.Response, isMetadataRequest?: boolean) => void;
|
|
5
|
+
handleResolvedLinkResponse: (resourceUrl: string, response: JsonLd.Response, isReloading?: any, isMetadataRequest?: boolean) => void;
|
|
6
|
+
};
|
|
7
|
+
export default useResponse;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaskit/smart-card",
|
|
3
|
-
"version": "27.4.
|
|
3
|
+
"version": "27.4.2",
|
|
4
4
|
"description": "Smart card component",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"@atlaskit/outbound-auth-flow-client": "^3.4.0",
|
|
56
56
|
"@atlaskit/platform-feature-flags": "^0.2.1",
|
|
57
57
|
"@atlaskit/popup": "^1.17.0",
|
|
58
|
-
"@atlaskit/primitives": "^6.
|
|
58
|
+
"@atlaskit/primitives": "^6.4.0",
|
|
59
59
|
"@atlaskit/section-message": "^6.5.0",
|
|
60
60
|
"@atlaskit/select": "^17.10.0",
|
|
61
61
|
"@atlaskit/spinner": "^16.1.0",
|
|
@@ -97,7 +97,6 @@
|
|
|
97
97
|
"@atlaskit/ssr": "*",
|
|
98
98
|
"@atlaskit/visual-regression": "*",
|
|
99
99
|
"@atlassian/analytics-tooling": "*",
|
|
100
|
-
"@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
|
|
101
100
|
"@atlassian/feature-flags-test-utils": "^0.2.0",
|
|
102
101
|
"@testing-library/dom": "^8.17.1",
|
|
103
102
|
"@testing-library/jest-dom": "^5.16.5",
|
|
@@ -192,6 +191,5 @@
|
|
|
192
191
|
"platform.linking-platform.smart-card.enable-view-related-urls-action": {
|
|
193
192
|
"type": "boolean"
|
|
194
193
|
}
|
|
195
|
-
}
|
|
196
|
-
"prettier": "@atlassian/atlassian-frontend-prettier-config-1.0.1"
|
|
194
|
+
}
|
|
197
195
|
}
|