@atlaskit/link-picker 1.8.2 → 1.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/analytics.codegen.js +1 -1
  3. package/dist/cjs/{ui/link-picker → common/utils}/url.js +0 -0
  4. package/dist/cjs/controllers/use-fix-height/index.js +27 -0
  5. package/dist/cjs/services/use-plugins/index.js +48 -90
  6. package/dist/cjs/services/use-plugins/reducer.js +64 -0
  7. package/dist/cjs/services/use-plugins/utils.js +134 -0
  8. package/dist/cjs/services/use-search-query/index.js +1 -1
  9. package/dist/cjs/services/with-link-picker-analytics/index.js +33 -0
  10. package/dist/cjs/ui/link-picker/form-footer/index.js +76 -0
  11. package/dist/cjs/ui/link-picker/form-footer/styled.js +17 -0
  12. package/dist/cjs/ui/link-picker/index.js +33 -75
  13. package/dist/cjs/ui/link-picker/link-search-error/index.js +47 -6
  14. package/dist/cjs/ui/link-picker/link-search-list/index.js +89 -20
  15. package/dist/cjs/ui/link-picker/link-search-list/link-search-no-results/index.js +54 -0
  16. package/dist/cjs/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/index.js +0 -0
  17. package/dist/cjs/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/styled.js +0 -0
  18. package/dist/cjs/ui/link-picker/link-search-list/link-search-no-results/styled.js +17 -0
  19. package/dist/cjs/ui/link-picker/link-search-list/styled.js +13 -6
  20. package/dist/cjs/ui/link-picker/messages.js +0 -50
  21. package/dist/cjs/ui/link-picker/styled.js +16 -11
  22. package/dist/cjs/ui/link-picker/text-input/styled.js +1 -1
  23. package/dist/cjs/version.json +1 -1
  24. package/dist/es2019/analytics.codegen.js +1 -1
  25. package/dist/es2019/{ui/link-picker → common/utils}/url.js +0 -0
  26. package/dist/es2019/controllers/use-fix-height/index.js +19 -0
  27. package/dist/es2019/services/use-plugins/index.js +35 -57
  28. package/dist/es2019/services/use-plugins/reducer.js +37 -0
  29. package/dist/es2019/services/use-plugins/utils.js +59 -0
  30. package/dist/es2019/services/use-search-query/index.js +1 -1
  31. package/dist/es2019/services/with-link-picker-analytics/index.js +15 -0
  32. package/dist/es2019/ui/link-picker/form-footer/index.js +51 -0
  33. package/dist/es2019/ui/link-picker/form-footer/styled.js +5 -0
  34. package/dist/es2019/ui/link-picker/index.js +28 -69
  35. package/dist/es2019/ui/link-picker/link-search-error/index.js +40 -4
  36. package/dist/es2019/ui/link-picker/link-search-list/index.js +60 -14
  37. package/dist/es2019/ui/link-picker/link-search-list/link-search-no-results/index.js +35 -0
  38. package/dist/es2019/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/index.js +0 -0
  39. package/dist/es2019/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/styled.js +0 -0
  40. package/dist/es2019/ui/link-picker/link-search-list/link-search-no-results/styled.js +6 -0
  41. package/dist/es2019/ui/link-picker/link-search-list/styled.js +23 -2
  42. package/dist/es2019/ui/link-picker/messages.js +0 -50
  43. package/dist/es2019/ui/link-picker/styled.js +20 -15
  44. package/dist/es2019/ui/link-picker/text-input/styled.js +3 -1
  45. package/dist/es2019/version.json +1 -1
  46. package/dist/esm/analytics.codegen.js +1 -1
  47. package/dist/esm/{ui/link-picker → common/utils}/url.js +0 -0
  48. package/dist/esm/controllers/use-fix-height/index.js +19 -0
  49. package/dist/esm/services/use-plugins/index.js +49 -94
  50. package/dist/esm/services/use-plugins/reducer.js +43 -0
  51. package/dist/esm/services/use-plugins/utils.js +111 -0
  52. package/dist/esm/services/use-search-query/index.js +1 -1
  53. package/dist/esm/services/with-link-picker-analytics/index.js +17 -0
  54. package/dist/esm/ui/link-picker/form-footer/index.js +53 -0
  55. package/dist/esm/ui/link-picker/form-footer/styled.js +6 -0
  56. package/dist/esm/ui/link-picker/index.js +29 -74
  57. package/dist/esm/ui/link-picker/link-search-error/index.js +41 -5
  58. package/dist/esm/ui/link-picker/link-search-list/index.js +67 -13
  59. package/dist/esm/ui/link-picker/link-search-list/link-search-no-results/index.js +37 -0
  60. package/dist/esm/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/index.js +0 -0
  61. package/dist/esm/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/styled.js +0 -0
  62. package/dist/esm/ui/link-picker/link-search-list/link-search-no-results/styled.js +6 -0
  63. package/dist/esm/ui/link-picker/link-search-list/styled.js +11 -5
  64. package/dist/esm/ui/link-picker/messages.js +0 -50
  65. package/dist/esm/ui/link-picker/styled.js +14 -11
  66. package/dist/esm/ui/link-picker/text-input/styled.js +1 -1
  67. package/dist/esm/version.json +1 -1
  68. package/dist/types/analytics.codegen.d.ts +7 -4
  69. package/dist/types/{ui/link-picker → common/utils}/url.d.ts +0 -0
  70. package/dist/types/controllers/use-fix-height/index.d.ts +8 -0
  71. package/dist/types/services/use-plugins/reducer.d.ts +30 -0
  72. package/dist/types/services/use-plugins/utils.d.ts +18 -0
  73. package/dist/types/services/with-link-picker-analytics/index.d.ts +4 -0
  74. package/dist/types/ui/link-picker/form-footer/index.d.ts +13 -0
  75. package/dist/types/ui/link-picker/form-footer/styled.d.ts +1 -0
  76. package/dist/types/ui/link-picker/index.d.ts +19 -19
  77. package/dist/types/ui/link-picker/link-search-error/index.d.ts +19 -3
  78. package/dist/types/ui/link-picker/link-search-list/index.d.ts +30 -5
  79. package/dist/types/ui/link-picker/link-search-list/link-search-no-results/index.d.ts +19 -0
  80. package/dist/types/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/index.d.ts +0 -0
  81. package/dist/types/ui/link-picker/{link-search-no-results → link-search-list/link-search-no-results}/no-results-svg/styled.d.ts +0 -0
  82. package/dist/types/ui/link-picker/link-search-list/link-search-no-results/styled.d.ts +1 -0
  83. package/dist/types/ui/link-picker/link-search-list/styled.d.ts +1 -0
  84. package/dist/types/ui/link-picker/messages.d.ts +0 -50
  85. package/dist/types/ui/link-picker/styled.d.ts +5 -1
  86. package/package.json +12 -12
  87. package/report.api.md +2 -0
  88. package/dist/cjs/ui/link-picker/link-search-no-results/index.js +0 -27
  89. package/dist/es2019/ui/link-picker/link-search-no-results/index.js +0 -10
  90. package/dist/esm/ui/link-picker/link-search-no-results/index.js +0 -14
  91. package/dist/types/ui/link-picker/link-search-no-results/index.d.ts +0 -4
@@ -1,11 +1,38 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+
1
3
  /** @jsx jsx */
4
+ import { forwardRef, Fragment } from 'react';
2
5
  import { jsx } from '@emotion/react';
6
+ import { defineMessages, FormattedMessage } from 'react-intl-next';
3
7
  import Spinner from '@atlaskit/spinner';
8
+ import VisuallyHidden from '@atlaskit/visually-hidden';
4
9
  import LinkSearchListItem from '../list-item';
5
- import { listContainerStyles, spinnerContainerStyles, listStyles } from './styled';
6
- import { testIds } from '../';
7
-
8
- const LinkSearchList = ({
10
+ import { listContainerStyles, spinnerContainerStyles, listStyles, listTitleStyles } from './styled';
11
+ import LinkSearchNoResults, { testIds as noResultsTestIds } from './link-search-no-results';
12
+ export const messages = defineMessages({
13
+ titleRecentlyViewed: {
14
+ id: 'fabric.linkPicker.listTitle.recentlyViewed',
15
+ defaultMessage: 'Recently Viewed',
16
+ description: 'Describes type of items shown in the list for screen-reader users'
17
+ },
18
+ titleResults: {
19
+ id: 'fabric.linkPicker.listTitle.results',
20
+ defaultMessage: 'Results',
21
+ description: 'Describes type of items shown in the list for screen-reader users'
22
+ },
23
+ searchLinkResults: {
24
+ id: 'fabric.linkPicker.hyperlink.searchLinkResults',
25
+ defaultMessage: '{count, plural, =0 {no results} one {# result} other {# results}} found',
26
+ description: 'Announce search results for screen-reader users.'
27
+ }
28
+ });
29
+ export const testIds = { ...noResultsTestIds,
30
+ resultListTitle: 'link-picker-list-title',
31
+ searchResultItem: 'link-search-list-item',
32
+ searchResultList: 'link-search-list',
33
+ searchResultLoadingIndicator: 'link-picker.results-loading-indicator'
34
+ };
35
+ const LinkSearchList = /*#__PURE__*/forwardRef(({
9
36
  onSelect,
10
37
  onMouseEnter,
11
38
  onMouseLeave,
@@ -16,18 +43,37 @@ const LinkSearchList = ({
16
43
  ariaControls,
17
44
  ariaLabelledBy,
18
45
  role,
19
- id
20
- }) => {
46
+ id,
47
+ hasSearchTerm,
48
+ ...restProps
49
+ }, ref) => {
21
50
  let itemsContent;
22
51
  let loadingContent;
52
+ const linkListTitle = hasSearchTerm ? messages.titleResults : messages.titleRecentlyViewed;
53
+
54
+ if ((items === null || items === void 0 ? void 0 : items.length) === 0) {
55
+ return jsx(LinkSearchNoResults, null);
56
+ }
23
57
 
24
58
  if (items && items.length > 0) {
25
- itemsContent = jsx("ul", {
59
+ itemsContent = jsx(Fragment, null, jsx("div", {
60
+ css: listTitleStyles,
61
+ id: testIds.resultListTitle,
62
+ "data-testid": testIds.resultListTitle
63
+ }, jsx(FormattedMessage, linkListTitle)), jsx(VisuallyHidden, {
64
+ id: "fabric.smartcard.linkpicker.suggested.results"
65
+ }, hasSearchTerm && jsx(FormattedMessage, _extends({}, messages.searchLinkResults, {
66
+ values: {
67
+ count: items.length
68
+ },
69
+ "aria-live": "polite",
70
+ "aria-atomic": "true"
71
+ }))), jsx("ul", {
26
72
  id: id,
27
73
  role: role,
28
74
  css: listStyles,
29
- "aria-controls": ariaControls,
30
- "aria-labelledby": ariaLabelledBy,
75
+ "aria-controls": "fabric.smartcard.linkpicker.suggested.results",
76
+ "aria-labelledby": testIds.resultListTitle,
31
77
  "data-testid": testIds.searchResultList
32
78
  }, items.map((item, index) => jsx(LinkSearchListItem, {
33
79
  id: `${testIds.searchResultItem}-${index}`,
@@ -39,7 +85,7 @@ const LinkSearchList = ({
39
85
  onMouseLeave: onMouseLeave,
40
86
  onSelect: onSelect,
41
87
  key: item.objectId
42
- })));
88
+ }))));
43
89
  }
44
90
 
45
91
  if (isLoading) {
@@ -51,9 +97,9 @@ const LinkSearchList = ({
51
97
  }));
52
98
  }
53
99
 
54
- return jsx("div", {
100
+ return jsx("div", _extends({
101
+ ref: ref,
55
102
  css: listContainerStyles
56
- }, itemsContent, loadingContent);
57
- };
58
-
103
+ }, restProps), itemsContent, loadingContent);
104
+ });
59
105
  export default LinkSearchList;
@@ -0,0 +1,35 @@
1
+ /** @jsx jsx */
2
+ import { jsx } from '@emotion/react';
3
+ import EmptyState from '@atlaskit/empty-state';
4
+ import { useIntl, defineMessages } from 'react-intl-next';
5
+ import NoResultsSVG from './no-results-svg';
6
+ import { emptyStateWrapperStyles } from './styled';
7
+ export const messages = defineMessages({
8
+ noResults: {
9
+ id: 'fabric.linkPicker.search.noResults.heading',
10
+ defaultMessage: 'We couldn’t find anything matching your search.',
11
+ description: 'Heading message shown when a search has no results'
12
+ },
13
+ noResultsDescription: {
14
+ id: 'fabric.linkPicker.search.noResults.description',
15
+ defaultMessage: 'Try again with a different term.',
16
+ description: 'Describes possible action when a search returns no results'
17
+ }
18
+ });
19
+ export const testIds = {
20
+ emptyResultPage: 'link-search-no-results'
21
+ };
22
+
23
+ const NoResults = () => {
24
+ const intl = useIntl();
25
+ return jsx("div", {
26
+ css: emptyStateWrapperStyles
27
+ }, jsx(EmptyState, {
28
+ testId: testIds.emptyResultPage,
29
+ header: intl.formatMessage(messages.noResults),
30
+ description: intl.formatMessage(messages.noResultsDescription),
31
+ renderImage: () => jsx(NoResultsSVG, null)
32
+ }));
33
+ };
34
+
35
+ export default NoResults;
@@ -0,0 +1,6 @@
1
+ import { css } from '@emotion/react';
2
+ export const emptyStateWrapperStyles = css`
3
+ & p {
4
+ margin: 0;
5
+ }
6
+ `;
@@ -1,13 +1,34 @@
1
- import { css } from '@emotion/react';
1
+ import { css } from '@emotion/react'; // AFP-2532 TODO: Fix automatic suppressions below
2
+ // eslint-disable-next-line @atlassian/tangerine/import/entry-points
3
+
4
+ import { typography } from '@atlaskit/theme';
5
+ import { gridSize as getGridSize } from '@atlaskit/theme/constants';
6
+ const gridSize = getGridSize();
2
7
  export const listContainerStyles = css`
3
8
  padding-top: 0;
9
+ min-height: 80px;
10
+ margin-top: ${gridSize * 2}px;
11
+ margin-bottom: ${gridSize * 2}px;
12
+ flex-grow: 1;
13
+ display: flex;
14
+ flex-direction: column;
4
15
  `;
5
16
  export const spinnerContainerStyles = css`
6
17
  text-align: center;
7
18
  min-height: 80px;
8
- margin-top: 30px;
19
+ display: flex;
20
+ align-items: center;
21
+ justify-content: center;
22
+ flex-grow: 1;
9
23
  `;
10
24
  export const listStyles = css`
11
25
  padding: 0;
26
+ margin: 0;
12
27
  list-style: none;
28
+ `;
29
+ export const listTitleStyles = css`
30
+ ${typography.h100()}
31
+ text-transform: uppercase;
32
+ margin-top: 0;
33
+ margin-bottom: ${gridSize / 2}px;
13
34
  `;
@@ -45,11 +45,6 @@ export const messages = defineMessages({
45
45
  defaultMessage: 'Suggestions will appear below as you type into the field',
46
46
  description: 'Describes what the search field does for screen reader users.'
47
47
  },
48
- searchLinkResults: {
49
- id: 'fabric.linkPicker.hyperlink.searchLinkResults',
50
- defaultMessage: '{count, plural, =0 {no results} one {# result} other {# results}} found',
51
- description: 'Announce search results for screen-reader users.'
52
- },
53
48
  linkAriaLabel: {
54
49
  id: 'fabric.linkPicker.hyperlink.linkAriaLabel',
55
50
  defaultMessage: 'Link label',
@@ -69,50 +64,5 @@ export const messages = defineMessages({
69
64
  id: 'fabric.linkPicker.time.ago',
70
65
  defaultMessage: 'ago',
71
66
  description: 'Some time ago'
72
- },
73
- titleRecentlyViewed: {
74
- id: 'fabric.linkPicker.listTitle.recentlyViewed',
75
- defaultMessage: 'Recently Viewed',
76
- description: 'Describes type of items shown in the list for screen-reader users'
77
- },
78
- titleResults: {
79
- id: 'fabric.linkPicker.listTitle.results',
80
- defaultMessage: 'Results',
81
- description: 'Describes type of items shown in the list for screen-reader users'
82
- },
83
- cancelButton: {
84
- id: 'fabric.linkPicker.button.cancel',
85
- defaultMessage: 'Cancel',
86
- description: 'Button to cancel and dismiss the link picker'
87
- },
88
- insertButton: {
89
- id: 'fabric.linkPicker.button.insert',
90
- defaultMessage: 'Insert',
91
- description: 'Button to insert searched or selected link'
92
- },
93
- noResults: {
94
- id: 'fabric.linkPicker.search.noResults.heading',
95
- defaultMessage: 'We couldn’t find anything matching your search.',
96
- description: 'Heading message shown when a search has no results'
97
- },
98
- noResultsDescription: {
99
- id: 'fabric.linkPicker.search.noResults.description',
100
- defaultMessage: 'Try again with a different term.',
101
- description: 'Describes possible action when a search returns no results'
102
- },
103
- saveButton: {
104
- id: 'fabric.linkPicker.button.save',
105
- defaultMessage: 'Save',
106
- description: 'Button to save edited link'
107
- },
108
- searchErrorHeader: {
109
- id: 'fabric.linkPicker.search.error.heading',
110
- defaultMessage: 'We’re having trouble loading data.',
111
- description: 'Heading message shown when a search throws an error'
112
- },
113
- searchErrorDescription: {
114
- id: 'fabric.linkPicker.search.error.description',
115
- defaultMessage: 'Refresh the page, or contact <a>Atlassian Support</a> if this keeps happening.',
116
- description: 'Describes possible actions when search throws an error'
117
67
  }
118
68
  });
@@ -1,15 +1,17 @@
1
1
  import { token } from '@atlaskit/tokens';
2
2
  import { css } from '@emotion/react';
3
- import { N500 } from '@atlaskit/theme/colors'; // AFP-2532 TODO: Fix automatic suppressions below
4
- // eslint-disable-next-line @atlassian/tangerine/import/entry-points
5
-
6
- import { typography } from '@atlaskit/theme';
7
- import { gridSize } from '@atlaskit/theme/constants';
3
+ import { N500 } from '@atlaskit/theme/colors';
4
+ import { gridSize as getGridSize } from '@atlaskit/theme/constants';
8
5
  const LINK_PICKER_WIDTH_IN_PX = 342;
9
- const LINK_PICKER_MIN_HEIGHT_IN_PX = 234;
6
+ const LINK_PICKER_MIN_HEIGHT_IN_PX = 220;
7
+ const gridSize = getGridSize();
8
+ /**
9
+ * Half padding on the top as the form field has a `gridSize()` margin top that cannot be overridden
10
+ */
11
+
10
12
  export const rootContainerStyles = css`
11
13
  width: ${LINK_PICKER_WIDTH_IN_PX}px;
12
- padding: ${gridSize() * 2}px;
14
+ padding: ${gridSize}px ${gridSize * 2}px ${gridSize * 2}px;
13
15
  box-sizing: border-box;
14
16
  line-height: initial;
15
17
  min-height: ${LINK_PICKER_MIN_HEIGHT_IN_PX}px;
@@ -21,16 +23,19 @@ export const rootContainerStylesForLoader = css`
21
23
  justify-content: center;
22
24
  `;
23
25
  export const searchIconStyles = css`
24
- margin-left: ${gridSize() / 2}px;
26
+ margin-left: ${gridSize / 2}px;
25
27
  color: ${token('color.icon', N500)};
26
28
  cursor: default;
27
29
  `;
28
- export const listTitleStyles = css`
29
- ${typography.h100()}
30
- text-transform: uppercase;
31
- margin-bottom: ${gridSize() / 2}px;
32
- `;
33
30
  export const tabsWrapperStyles = css`
34
- margin-left: -${gridSize()}px;
35
- margin-right: -${gridSize()}px;
31
+ margin-top: ${gridSize * 1.5}px;
32
+ margin-left: -${gridSize}px;
33
+ margin-right: -${gridSize}px;
34
+ `;
35
+ export const flexColumnStyles = css`
36
+ display: flex;
37
+ flex-direction: column;
38
+ `;
39
+ export const formFooterMargin = css`
40
+ margin-top: ${gridSize * 2}px;
36
41
  `;
@@ -11,5 +11,7 @@ export const clearTextButtonStyles = css`
11
11
  cursor: pointer;
12
12
  `;
13
13
  export const fieldStyles = css`
14
- margin-bottom: ${gridSize() * 2}px;
14
+ & + & {
15
+ margin-top: ${gridSize() * 2}px;
16
+ }
15
17
  `;
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/link-picker",
3
- "version": "1.8.2",
3
+ "version": "1.8.5",
4
4
  "sideEffects": false
5
5
  }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Generates analytics utilities from the package analytics spec yaml
5
5
  *
6
- * @codegen <<SignedSource::ffa7ec78cc793f941d06cdb25b07dc8a>>
6
+ * @codegen <<SignedSource::115bafba068f36f76da67200ee2d08c4>>
7
7
  * @codegenCommand yarn workspace @atlaskit/link-picker run codegen-analytics
8
8
  */
9
9
  function createEventPayload(eventKey, attributes) {
File without changes
@@ -0,0 +1,19 @@
1
+ import { useLayoutEffect, useRef } from 'react';
2
+
3
+ var useFixHeight = function useFixHeight(shouldFixHeight) {
4
+ var ref = useRef(null);
5
+ var currentHeight = useRef(null);
6
+ useLayoutEffect(function () {
7
+ if (ref.current) {
8
+ currentHeight.current = ref.current.getBoundingClientRect().height;
9
+ }
10
+ });
11
+ return {
12
+ ref: ref,
13
+ style: shouldFixHeight ? {
14
+ minHeight: currentHeight.current || 'auto'
15
+ } : undefined
16
+ };
17
+ };
18
+
19
+ export default useFixHeight;
@@ -1,38 +1,21 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
5
-
6
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
7
-
8
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
-
10
- import { useEffect, useReducer, useMemo, useRef, useState, useCallback } from 'react';
4
+ import { useEffect, useMemo, useState, useCallback } from 'react';
11
5
  import { RECENT_SEARCH_LIST_SIZE } from '../../ui/link-picker';
12
-
13
- function reducer(state, payload) {
14
- return _objectSpread(_objectSpread({}, state), payload);
15
- }
16
-
6
+ import { CancellationError, resolvePluginUpdates } from './utils';
7
+ import { usePluginReducer } from './reducer';
17
8
  export function usePlugins(state, activeTab, plugins) {
18
9
  var _useState = useState(0),
19
10
  _useState2 = _slicedToArray(_useState, 2),
20
11
  retries = _useState2[0],
21
12
  setRetries = _useState2[1];
22
13
 
23
- var _useReducer = useReducer(reducer, {
24
- items: null,
25
- error: null,
26
- isLoading: false
27
- }),
28
- _useReducer2 = _slicedToArray(_useReducer, 2),
29
- pluginState = _useReducer2[0],
30
- dispatch = _useReducer2[1];
14
+ var _usePluginReducer = usePluginReducer(),
15
+ _usePluginReducer2 = _slicedToArray(_usePluginReducer, 2),
16
+ pluginState = _usePluginReducer2[0],
17
+ dispatch = _usePluginReducer2[1];
31
18
 
32
- var items = pluginState.items,
33
- isLoading = pluginState.isLoading,
34
- error = pluginState.error;
35
- var queryVersion = useRef(0);
36
19
  var activePlugin = plugins === null || plugins === void 0 ? void 0 : plugins[activeTab];
37
20
  useEffect(function () {
38
21
  if (!activePlugin) {
@@ -41,107 +24,75 @@ export function usePlugins(state, activeTab, plugins) {
41
24
 
42
25
  if (!state) {
43
26
  dispatch({
44
- items: null,
45
- isLoading: false,
46
- error: null
27
+ type: 'CLEAR'
47
28
  });
48
29
  return;
49
30
  }
50
31
 
51
- var updates = activePlugin.resolve(state);
52
32
  dispatch({
53
- items: null,
54
- isLoading: true,
55
- error: null
33
+ type: 'LOADING'
56
34
  });
57
- var newQuery = ++queryVersion.current;
35
+
36
+ var _resolvePluginUpdates = resolvePluginUpdates(activePlugin, state),
37
+ next = _resolvePluginUpdates.next,
38
+ cancel = _resolvePluginUpdates.cancel;
58
39
 
59
40
  var updateResults = /*#__PURE__*/function () {
60
41
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
61
- var done, value, _result$done, result;
42
+ var _isLoading, _yield$next, value, done;
62
43
 
63
44
  return _regeneratorRuntime.wrap(function _callee$(_context) {
64
45
  while (1) {
65
46
  switch (_context.prev = _context.next) {
66
47
  case 0:
67
48
  _context.prev = 0;
49
+ _isLoading = true;
68
50
 
69
- case 1:
70
- if (!(newQuery === queryVersion.current)) {
71
- _context.next = 23;
72
- break;
73
- }
74
-
75
- done = false;
76
- value = void 0;
77
-
78
- if (!(updates instanceof Promise)) {
79
- _context.next = 11;
80
- break;
81
- }
82
-
83
- done = true;
84
- _context.next = 8;
85
- return updates;
86
-
87
- case 8:
88
- value = _context.sent;
89
- _context.next = 16;
90
- break;
91
-
92
- case 11:
93
- _context.next = 13;
94
- return updates.next();
95
-
96
- case 13:
97
- result = _context.sent;
98
- done = (_result$done = result.done) !== null && _result$done !== void 0 ? _result$done : false;
99
- value = result.value;
100
-
101
- case 16:
102
- if (!(newQuery !== queryVersion.current)) {
103
- _context.next = 18;
51
+ case 2:
52
+ if (!_isLoading) {
53
+ _context.next = 12;
104
54
  break;
105
55
  }
106
56
 
107
- return _context.abrupt("return");
57
+ _context.next = 5;
58
+ return next();
108
59
 
109
- case 18:
60
+ case 5:
61
+ _yield$next = _context.sent;
62
+ value = _yield$next.value;
63
+ done = _yield$next.done;
64
+ _isLoading = !done;
110
65
  dispatch({
111
- items: limit(value.data),
112
- isLoading: !done
66
+ type: 'SUCCESS',
67
+ payload: {
68
+ items: limit(value.data),
69
+ isLoading: !done
70
+ }
113
71
  });
114
-
115
- if (!done) {
116
- _context.next = 21;
117
- break;
118
- }
119
-
120
- return _context.abrupt("return");
121
-
122
- case 21:
123
- _context.next = 1;
72
+ _context.next = 2;
124
73
  break;
125
74
 
126
- case 23:
127
- _context.next = 28;
75
+ case 12:
76
+ _context.next = 17;
128
77
  break;
129
78
 
130
- case 25:
131
- _context.prev = 25;
79
+ case 14:
80
+ _context.prev = 14;
132
81
  _context.t0 = _context["catch"](0);
133
- dispatch({
134
- error: _context.t0,
135
- items: null,
136
- isLoading: false
137
- });
138
82
 
139
- case 28:
83
+ if (!(_context.t0 instanceof CancellationError)) {
84
+ dispatch({
85
+ type: 'ERROR',
86
+ payload: _context.t0
87
+ });
88
+ }
89
+
90
+ case 17:
140
91
  case "end":
141
92
  return _context.stop();
142
93
  }
143
94
  }
144
- }, _callee, null, [[0, 25]]);
95
+ }, _callee, null, [[0, 14]]);
145
96
  }));
146
97
 
147
98
  return function updateResults() {
@@ -150,7 +101,8 @@ export function usePlugins(state, activeTab, plugins) {
150
101
  }();
151
102
 
152
103
  updateResults();
153
- }, [activePlugin, queryVersion, state, retries]);
104
+ return cancel;
105
+ }, [activePlugin, state, retries, dispatch]);
154
106
  var tabs = useMemo(function () {
155
107
  if (!plugins || plugins.length <= 1) {
156
108
  return [];
@@ -169,6 +121,9 @@ export function usePlugins(state, activeTab, plugins) {
169
121
  return ++prev;
170
122
  });
171
123
  }, []);
124
+ var items = pluginState.items,
125
+ isLoading = pluginState.isLoading,
126
+ error = pluginState.error;
172
127
  return {
173
128
  tabs: tabs,
174
129
  items: items,
@@ -0,0 +1,43 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
7
+ import { useReducer } from 'react';
8
+ export var ACTION_CLEAR = 'CLEAR';
9
+ export var ACTION_LOADING = 'LOADING';
10
+ export var ACTION_SUCCESS = 'SUCCESS';
11
+ export var ACTION_ERRORED = 'ERROR';
12
+ export var INITIAL_STATE = {
13
+ items: null,
14
+ error: null,
15
+ isLoading: false
16
+ };
17
+ export function reducer(state, action) {
18
+ switch (action.type) {
19
+ case ACTION_CLEAR:
20
+ return _objectSpread({}, INITIAL_STATE);
21
+
22
+ case ACTION_LOADING:
23
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
24
+ isLoading: true
25
+ });
26
+
27
+ case ACTION_SUCCESS:
28
+ return _objectSpread(_objectSpread(_objectSpread({}, state), action.payload), {}, {
29
+ error: null
30
+ });
31
+
32
+ case ACTION_ERRORED:
33
+ return _objectSpread(_objectSpread({}, INITIAL_STATE), {}, {
34
+ error: action.payload
35
+ });
36
+
37
+ default:
38
+ return state;
39
+ }
40
+ }
41
+ export var usePluginReducer = function usePluginReducer() {
42
+ return useReducer(reducer, INITIAL_STATE);
43
+ };