@atlaskit/help 4.2.3 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/dist/cjs/analytics.js +1 -1
  3. package/dist/cjs/components/Article/ArticleContent/index.js +1 -1
  4. package/dist/cjs/components/Article/ArticleLoadingFail/index.js +2 -2
  5. package/dist/cjs/components/Article/ArticleLoadingFail/styled.js +1 -1
  6. package/dist/cjs/components/Article/HelpArticle/Loading/index.js +2 -2
  7. package/dist/cjs/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulNoButton.js +2 -2
  8. package/dist/cjs/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulYesButton.js +2 -2
  9. package/dist/cjs/components/Article/HelpArticle/WasHelpfulForm/index.js +2 -2
  10. package/dist/cjs/components/Article/HelpArticle/WasHelpfulForm/styled.js +1 -1
  11. package/dist/cjs/components/Article/WhatsNewArticle/Loading/index.js +2 -2
  12. package/dist/cjs/components/Article/WhatsNewArticle/index.js +2 -2
  13. package/dist/cjs/components/Article/WhatsNewArticle/styled.js +1 -1
  14. package/dist/cjs/components/ArticlesList/ArticlesListItem/index.js +3 -3
  15. package/dist/cjs/components/ArticlesList/ArticlesListItem/styled.js +1 -1
  16. package/dist/cjs/components/ArticlesList/constants.js +1 -1
  17. package/dist/cjs/components/ArticlesList/styled.js +1 -1
  18. package/dist/cjs/components/HelpContent.js +7 -9
  19. package/dist/cjs/components/HelpContentButton/styled.js +1 -1
  20. package/dist/cjs/components/MessagesIntlProvider.js +7 -7
  21. package/dist/cjs/components/RelatedArticles/RelatedArticlesLoading.js +2 -2
  22. package/dist/cjs/components/RelatedArticles/index.js +2 -2
  23. package/dist/cjs/components/RelatedArticles/styled.js +1 -1
  24. package/dist/cjs/components/Search/SearchInput/index.js +2 -2
  25. package/dist/cjs/components/Search/SearchInput/styled.js +1 -1
  26. package/dist/cjs/components/Search/SearchResults/SearchExternalSite.js +2 -2
  27. package/dist/cjs/components/Search/SearchResults/SearchResultsEmpty.js +2 -2
  28. package/dist/cjs/components/Search/SearchResults/SearchResultsError.js +2 -2
  29. package/dist/cjs/components/Search/SearchResults/styled.js +1 -1
  30. package/dist/cjs/components/ShowMoreButton/index.js +2 -2
  31. package/dist/cjs/components/WhatsNew/WhatsNewButton/index.js +2 -2
  32. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/index.js +52 -0
  33. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/styled.js +32 -0
  34. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsError/index.js +2 -2
  35. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/index.js +2 -2
  36. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/styled.js +1 -1
  37. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/styled.js +1 -1
  38. package/dist/cjs/components/WhatsNew/WhatsNewResults/WhatsNewResultsLoading/index.js +2 -2
  39. package/dist/cjs/components/WhatsNew/WhatsNewResults/index.js +33 -11
  40. package/dist/cjs/components/WhatsNew/WhatsNewResults/styled.js +1 -1
  41. package/dist/cjs/components/constants.js +1 -1
  42. package/dist/cjs/components/contexts/headerContext.js +1 -1
  43. package/dist/cjs/components/contexts/helpArticleContext.js +1 -1
  44. package/dist/cjs/components/contexts/homeContext.js +1 -1
  45. package/dist/cjs/components/contexts/navigationContext.js +393 -473
  46. package/dist/cjs/components/contexts/relatedArticlesContext.js +1 -1
  47. package/dist/cjs/components/contexts/searchContext.js +1 -1
  48. package/dist/cjs/components/contexts/whatsNewArticleContext.js +2 -3
  49. package/dist/cjs/components/styled.js +2 -2
  50. package/dist/cjs/i18n/index.js +18 -18
  51. package/dist/cjs/index.js +16 -16
  52. package/dist/cjs/messages.js +23 -3
  53. package/dist/cjs/util/hooks/cancellablePromise.js +1 -1
  54. package/dist/cjs/util/styled.js +1 -1
  55. package/dist/cjs/version.json +1 -1
  56. package/dist/es2019/components/Article/ArticleContent/index.js +1 -1
  57. package/dist/es2019/components/Article/ArticleLoadingFail/index.js +1 -1
  58. package/dist/es2019/components/Article/HelpArticle/Loading/index.js +1 -1
  59. package/dist/es2019/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulNoButton.js +1 -1
  60. package/dist/es2019/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulYesButton.js +1 -1
  61. package/dist/es2019/components/Article/HelpArticle/WasHelpfulForm/index.js +1 -1
  62. package/dist/es2019/components/Article/WhatsNewArticle/Loading/index.js +1 -1
  63. package/dist/es2019/components/Article/WhatsNewArticle/index.js +1 -1
  64. package/dist/es2019/components/ArticlesList/ArticlesListItem/index.js +1 -1
  65. package/dist/es2019/components/HelpContent.js +7 -8
  66. package/dist/es2019/components/MessagesIntlProvider.js +4 -4
  67. package/dist/es2019/components/RelatedArticles/RelatedArticlesLoading.js +1 -1
  68. package/dist/es2019/components/RelatedArticles/index.js +1 -1
  69. package/dist/es2019/components/Search/SearchInput/index.js +1 -1
  70. package/dist/es2019/components/Search/SearchResults/SearchExternalSite.js +1 -1
  71. package/dist/es2019/components/Search/SearchResults/SearchResultsEmpty.js +1 -1
  72. package/dist/es2019/components/Search/SearchResults/SearchResultsError.js +1 -1
  73. package/dist/es2019/components/ShowMoreButton/index.js +1 -1
  74. package/dist/es2019/components/WhatsNew/WhatsNewButton/index.js +1 -1
  75. package/dist/es2019/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/index.js +35 -0
  76. package/dist/es2019/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/styled.js +16 -0
  77. package/dist/es2019/components/WhatsNew/WhatsNewResults/WhatsNewResultsError/index.js +1 -1
  78. package/dist/es2019/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/index.js +1 -1
  79. package/dist/es2019/components/WhatsNew/WhatsNewResults/WhatsNewResultsLoading/index.js +1 -1
  80. package/dist/es2019/components/WhatsNew/WhatsNewResults/index.js +32 -10
  81. package/dist/es2019/components/contexts/navigationContext.js +336 -347
  82. package/dist/es2019/components/contexts/whatsNewArticleContext.js +1 -2
  83. package/dist/es2019/components/styled.js +1 -1
  84. package/dist/es2019/messages.js +22 -2
  85. package/dist/es2019/version.json +1 -1
  86. package/dist/esm/components/Article/ArticleContent/index.js +1 -1
  87. package/dist/esm/components/Article/ArticleLoadingFail/index.js +1 -1
  88. package/dist/esm/components/Article/HelpArticle/Loading/index.js +1 -1
  89. package/dist/esm/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulNoButton.js +1 -1
  90. package/dist/esm/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulYesButton.js +1 -1
  91. package/dist/esm/components/Article/HelpArticle/WasHelpfulForm/index.js +1 -1
  92. package/dist/esm/components/Article/WhatsNewArticle/Loading/index.js +1 -1
  93. package/dist/esm/components/Article/WhatsNewArticle/index.js +1 -1
  94. package/dist/esm/components/ArticlesList/ArticlesListItem/index.js +1 -1
  95. package/dist/esm/components/HelpContent.js +7 -9
  96. package/dist/esm/components/MessagesIntlProvider.js +5 -4
  97. package/dist/esm/components/RelatedArticles/RelatedArticlesLoading.js +1 -1
  98. package/dist/esm/components/RelatedArticles/index.js +1 -1
  99. package/dist/esm/components/Search/SearchInput/index.js +1 -1
  100. package/dist/esm/components/Search/SearchResults/SearchExternalSite.js +1 -1
  101. package/dist/esm/components/Search/SearchResults/SearchResultsEmpty.js +1 -1
  102. package/dist/esm/components/Search/SearchResults/SearchResultsError.js +1 -1
  103. package/dist/esm/components/ShowMoreButton/index.js +1 -1
  104. package/dist/esm/components/WhatsNew/WhatsNewButton/index.js +1 -1
  105. package/dist/esm/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/index.js +32 -0
  106. package/dist/esm/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/styled.js +10 -0
  107. package/dist/esm/components/WhatsNew/WhatsNewResults/WhatsNewResultsError/index.js +1 -1
  108. package/dist/esm/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/index.js +1 -1
  109. package/dist/esm/components/WhatsNew/WhatsNewResults/WhatsNewResultsLoading/index.js +1 -1
  110. package/dist/esm/components/WhatsNew/WhatsNewResults/index.js +31 -10
  111. package/dist/esm/components/contexts/navigationContext.js +395 -474
  112. package/dist/esm/components/contexts/whatsNewArticleContext.js +1 -2
  113. package/dist/esm/components/styled.js +1 -1
  114. package/dist/esm/messages.js +22 -2
  115. package/dist/esm/version.json +1 -1
  116. package/dist/types/analytics.d.ts +1 -1
  117. package/dist/types/assets/ErrorImage.d.ts +1 -0
  118. package/dist/types/assets/NotFoundImage.d.ts +1 -0
  119. package/dist/types/assets/SomethingWrongImage.d.ts +1 -0
  120. package/dist/types/assets/WhatsNewIcoExperiment.d.ts +1 -0
  121. package/dist/types/assets/WhatsNewIconFix.d.ts +1 -0
  122. package/dist/types/assets/WhatsNewIconImprovement.d.ts +1 -0
  123. package/dist/types/assets/WhatsNewIconNew.d.ts +1 -0
  124. package/dist/types/assets/WhatsNewIconRemoved.d.ts +1 -0
  125. package/dist/types/components/Article/ArticleLoadingFail/index.d.ts +4 -4
  126. package/dist/types/components/Article/ArticleLoadingFail/styled.d.ts +2 -2
  127. package/dist/types/components/Article/HelpArticle/Loading/index.d.ts +4 -4
  128. package/dist/types/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulNoButton.d.ts +4 -4
  129. package/dist/types/components/Article/HelpArticle/WasHelpfulForm/WasHelpfulYesButton.d.ts +4 -4
  130. package/dist/types/components/Article/HelpArticle/WasHelpfulForm/index.d.ts +4 -4
  131. package/dist/types/components/Article/HelpArticle/WasHelpfulForm/styled.d.ts +3 -3
  132. package/dist/types/components/Article/WhatsNewArticle/Loading/index.d.ts +4 -4
  133. package/dist/types/components/Article/WhatsNewArticle/index.d.ts +4 -4
  134. package/dist/types/components/Article/WhatsNewArticle/styled.d.ts +6 -6
  135. package/dist/types/components/Article/styled.d.ts +1 -1
  136. package/dist/types/components/ArticlesList/ArticlesListItem/index.d.ts +4 -4
  137. package/dist/types/components/ArticlesList/ArticlesListItem/styled.d.ts +5 -5
  138. package/dist/types/components/ArticlesList/styled.d.ts +2 -2
  139. package/dist/types/components/Help.d.ts +1 -1
  140. package/dist/types/components/MessagesIntlProvider.d.ts +6 -5
  141. package/dist/types/components/RelatedArticles/RelatedArticlesLoading.d.ts +3 -3
  142. package/dist/types/components/RelatedArticles/index.d.ts +7 -3
  143. package/dist/types/components/RelatedArticles/styled.d.ts +3 -3
  144. package/dist/types/components/Search/SearchInput/index.d.ts +4 -4
  145. package/dist/types/components/Search/SearchInput/styled.d.ts +3 -3
  146. package/dist/types/components/Search/SearchResults/SearchExternalSite.d.ts +4 -4
  147. package/dist/types/components/Search/SearchResults/SearchResultsEmpty.d.ts +4 -4
  148. package/dist/types/components/Search/SearchResults/SearchResultsError.d.ts +4 -4
  149. package/dist/types/components/Search/SearchResults/styled.d.ts +5 -5
  150. package/dist/types/components/ShowMoreButton/index.d.ts +4 -4
  151. package/dist/types/components/WhatsNew/WhatsNewButton/index.d.ts +4 -4
  152. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/index.d.ts +10 -0
  153. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsEmpty/styled.d.ts +3 -0
  154. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsError/index.d.ts +4 -4
  155. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsError/styled.d.ts +2 -2
  156. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/index.d.ts +4 -4
  157. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/WhatsNewResultItem/styled.d.ts +3 -3
  158. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsList/styled.d.ts +4 -4
  159. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsLoading/index.d.ts +4 -4
  160. package/dist/types/components/WhatsNew/WhatsNewResults/WhatsNewResultsLoading/styled.d.ts +2 -2
  161. package/dist/types/components/WhatsNew/WhatsNewResults/index.d.ts +4 -4
  162. package/dist/types/components/WhatsNew/WhatsNewResults/styled.d.ts +4 -4
  163. package/dist/types/components/contexts/navigationContext.d.ts +8 -6
  164. package/dist/types/messages.d.ts +20 -0
  165. package/dist/types/util/index.d.ts +1 -0
  166. package/dist/types/util/styled.d.ts +1 -1
  167. package/package.json +5 -5
@@ -1,4 +1,4 @@
1
- import React, { useState, useRef, useEffect, useCallback, useMemo } from 'react';
1
+ import React, { useReducer, useEffect, useCallback, useMemo } from 'react';
2
2
  import { REQUEST_STATE } from '../../model/Requests';
3
3
  import { ARTICLE_TYPE } from '../../model/Help';
4
4
  import { createCtx } from '../../util/hooks/ctx';
@@ -9,10 +9,178 @@ import { useHomeContext } from './homeContext';
9
9
  import { useSearchContext } from './searchContext';
10
10
  import { useHeaderContext } from './headerContext';
11
11
  export const [useNavigationContext, CtxProvider] = createCtx();
12
+
13
+ const getNewHistoryItem = (id, type) => {
14
+ let uid = Math.floor(Math.random() * Math.pow(10, 17));
15
+ const newHistoryItem = {
16
+ uid,
17
+ id,
18
+ type,
19
+ state: REQUEST_STATE.loading
20
+ };
21
+ return newHistoryItem;
22
+ };
23
+ /**
24
+ * Get a simplified version of the history. The items in this array should
25
+ * have only the ID, UID and state === 'reload'
26
+ */
27
+
28
+
29
+ const getSimpleHistory = history => history.map(historyItem => {
30
+ const {
31
+ id,
32
+ uid,
33
+ type
34
+ } = historyItem;
35
+ return {
36
+ id,
37
+ uid,
38
+ state: REQUEST_STATE.reload,
39
+ type
40
+ };
41
+ });
42
+ /**
43
+ * Get the last article in the history
44
+ */
45
+
46
+
47
+ const getCurrentArticle = history => history[history.length - 1];
48
+ /**
49
+ * Get an ArticleItem/WhatsNewArticleItem based on the last article in the history
50
+ */
51
+
52
+
53
+ const getCurrentArticleItemSlim = history => {
54
+ const {
55
+ article,
56
+ type
57
+ } = getCurrentArticle(history);
58
+
59
+ if (article) {
60
+ if (type === ARTICLE_TYPE.HELP_ARTICLE) {
61
+ const {
62
+ body,
63
+ relatedArticles,
64
+ ...articleItemData
65
+ } = article;
66
+ const currentArticleSlimData = articleItemData;
67
+ return currentArticleSlimData;
68
+ } else if (type === ARTICLE_TYPE.WHATS_NEW) {
69
+ const {
70
+ description,
71
+ ...whatsNewArticleItemData
72
+ } = article;
73
+ const currentWhatsNewArticleSlimData = whatsNewArticleItemData;
74
+ return currentWhatsNewArticleSlimData;
75
+ }
76
+ }
77
+ };
78
+
79
+ const getViewForArticleId = articleId => {
80
+ let view = VIEW.DEFAULT_CONTENT;
81
+
82
+ if (articleId.type === ARTICLE_TYPE.HELP_ARTICLE) {
83
+ if (articleId.id) {
84
+ view = VIEW.ARTICLE;
85
+ } else {
86
+ view = VIEW.DEFAULT_CONTENT;
87
+ }
88
+ } else if (articleId.type === ARTICLE_TYPE.WHATS_NEW) {
89
+ if (articleId.id) {
90
+ view = VIEW.WHATS_NEW_ARTICLE;
91
+ } else {
92
+ view = VIEW.WHATS_NEW;
93
+ }
94
+ }
95
+
96
+ return view;
97
+ };
98
+
99
+ const navigationReducer = ({
100
+ articleId: currentArticleId,
101
+ history: currentHistory,
102
+ view: currentView
103
+ }, action) => {
104
+ let newState = {
105
+ articleId: currentArticleId,
106
+ history: currentHistory,
107
+ view: currentView
108
+ };
109
+
110
+ if (action.type === 'newArticle' && action.payload) {
111
+ const {
112
+ payload: newArticleId
113
+ } = action;
114
+ newState = {
115
+ articleId: newArticleId,
116
+ history: [...currentHistory, getNewHistoryItem(newArticleId.id, newArticleId.type)],
117
+ view: getViewForArticleId(newArticleId)
118
+ };
119
+ } else if (action.type === 'updateHistoryItem' && action.payload) {
120
+ const {
121
+ payload: HistoryItemUpdate
122
+ } = action;
123
+ const index = currentHistory.findIndex(historyItemTemp => historyItemTemp.uid === HistoryItemUpdate.uid);
124
+
125
+ if (index !== -1) {
126
+ const newHistory = [...currentHistory];
127
+ newHistory[index] = { ...HistoryItemUpdate
128
+ };
129
+ newState = {
130
+ articleId: currentArticleId,
131
+ history: newHistory,
132
+ view: getViewForArticleId(currentArticleId)
133
+ };
134
+ }
135
+ } else if (action.type === 'removeLastHistoryItem') {
136
+ const newHistory = currentHistory.length > 0 ? [...currentHistory] : [];
137
+
138
+ if (newHistory.length > 0) {
139
+ newHistory.splice(-1);
140
+ }
141
+
142
+ newState = {
143
+ articleId: currentArticleId,
144
+ history: newHistory,
145
+ view: getViewForArticleId(currentArticleId)
146
+ };
147
+ } else if (action.type === 'removeAllHistoryItems') {
148
+ const defaultHistory = [];
149
+ newState = {
150
+ articleId: currentArticleId,
151
+ history: defaultHistory,
152
+ view: VIEW.DEFAULT_CONTENT
153
+ };
154
+ } else if (action.type === 'updateArticleId' && action.payload) {
155
+ const {
156
+ payload: newArticleId
157
+ } = action;
158
+ newState = {
159
+ articleId: newArticleId,
160
+ history: currentHistory,
161
+ view: getViewForArticleId(newArticleId)
162
+ };
163
+ } else if (action.type === 'updateView' && action.payload) {
164
+ const {
165
+ payload: newView
166
+ } = action;
167
+ newState = {
168
+ articleId: currentArticleId,
169
+ history: currentHistory,
170
+ view: newView
171
+ };
172
+ }
173
+
174
+ return newState;
175
+ };
176
+
12
177
  export const NavigationContextProvider = ({
13
- articleId: propsArticleId,
178
+ articleId: propsArticleId = {
179
+ id: '',
180
+ type: ARTICLE_TYPE.HELP_ARTICLE
181
+ },
14
182
  articleIdSetter,
15
- history: propsHistory,
183
+ history: propsHistory = [],
16
184
  historySetter,
17
185
  children
18
186
  }) => {
@@ -28,403 +196,224 @@ export const NavigationContextProvider = ({
28
196
  } = useHomeContext();
29
197
  const {
30
198
  onSearch,
31
- isSearchResultVisible
199
+ isSearchResultVisible,
200
+ searchValue
32
201
  } = useSearchContext();
33
202
  const {
34
203
  onCloseButtonClick
35
204
  } = useHeaderContext();
36
- const [view, setView] = useState(VIEW.DEFAULT_CONTENT);
37
- const articleId = useMemo(() => {
38
- if ((propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.id) === undefined && (propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.type) === undefined) {
39
- return undefined;
205
+ const [{
206
+ articleId: currentArticleId,
207
+ history: currentHistory,
208
+ view: currentView
209
+ }, dispatchNavigationAction] = useReducer(navigationReducer, {
210
+ articleId: propsArticleId,
211
+ history: propsHistory,
212
+ view: VIEW.DEFAULT_CONTENT
213
+ });
214
+ const isOverlayVisible = useMemo(() => {
215
+ return currentView === VIEW.ARTICLE || currentView === VIEW.SEARCH || currentView === VIEW.WHATS_NEW || currentView === VIEW.WHATS_NEW_ARTICLE;
216
+ }, [currentView]);
217
+ const isDefaultContentDefined = useMemo(() => {
218
+ return homeContent !== undefined || homeOptions !== undefined;
219
+ }, [homeContent, homeOptions]);
220
+ const canNavigateBack = useMemo(() => {
221
+ /**
222
+ * - If default content isn't defined and the history only has one article,
223
+ * we should not display the back button
224
+ * - If the prop.article.setArticleId is not defined, we should also hide the back
225
+ * button because we are not able to navigate though the history without it
226
+ */
227
+ if (currentHistory.length === 1 && !isDefaultContentDefined || currentView === VIEW.WHATS_NEW && !isDefaultContentDefined) {
228
+ return false;
40
229
  }
230
+ /**
231
+ * if an overlay is visible return true to display the back buton
232
+ */
41
233
 
42
- return {
43
- id: propsArticleId.id ? propsArticleId.id : '',
44
- type: propsArticleId.type ? propsArticleId.type : ARTICLE_TYPE.HELP_ARTICLE
45
- };
46
- }, [propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.id, propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.type]);
47
- const [history, setHistory] = useState(propsHistory ? propsHistory : []);
48
- const tempHistory = useRef(propsHistory ? propsHistory : []);
49
- const clearHistory = useCallback(() => {
50
- if (tempHistory.current.length > 0) {
51
- if (!articleIdSetter) {
52
- return;
53
- } // Clear History
54
234
 
235
+ return isOverlayVisible;
236
+ }, [currentHistory.length, isDefaultContentDefined, isOverlayVisible, currentView]);
237
+ const fetchArticleData = useCallback(async historyItem => {
238
+ try {
239
+ let article;
55
240
 
56
- tempHistory.current = [];
57
- setHistory(tempHistory.current); // Clear host history using the historySetter
241
+ switch (historyItem.type) {
242
+ case ARTICLE_TYPE.HELP_ARTICLE:
243
+ if (!onGetHelpArticle) {
244
+ throw new Error('onGetHelpArticle prop not defined');
245
+ }
58
246
 
59
- historySetter && historySetter(tempHistory.current); // Set article ID to ''
247
+ article = await onGetHelpArticle({
248
+ id: historyItem.id,
249
+ type: historyItem.type
250
+ });
251
+ break;
60
252
 
61
- articleIdSetter({
62
- id: '',
63
- type: ARTICLE_TYPE.HELP_ARTICLE
64
- });
65
- }
66
- }, [articleIdSetter, historySetter]);
67
- const isDefaultContentDefined = useCallback(() => {
68
- return homeContent !== undefined || homeOptions !== undefined;
69
- }, [homeContent, homeOptions]);
253
+ case ARTICLE_TYPE.WHATS_NEW:
254
+ if (!onGetWhatsNewArticle) {
255
+ throw new Error('onGetWhatsNewArticle prop not defined');
256
+ }
70
257
 
71
- const getNewHistoryItem = (id, type) => {
72
- let uid = Math.floor(Math.random() * Math.pow(10, 17));
73
- const newHistoryItem = {
74
- uid,
75
- id,
76
- type,
77
- state: REQUEST_STATE.loading
78
- };
79
- return newHistoryItem;
80
- };
81
- /**
82
- * Get a simplified version of the history. The items in the history have only
83
- * the ID, UID and state === 'reload'
84
- */
258
+ if (historyItem.id === '') {
259
+ break;
260
+ }
85
261
 
262
+ article = await onGetWhatsNewArticle({
263
+ id: historyItem.id,
264
+ type: historyItem.type
265
+ });
266
+ break;
86
267
 
87
- const getSimpleHistory = useCallback(fullHistory => {
88
- const copyHistory = fullHistory.map(tempHistoryItem => {
89
- const {
90
- id,
91
- uid,
92
- type
93
- } = tempHistoryItem;
94
- return {
95
- id,
96
- uid,
97
- state: REQUEST_STATE.reload,
98
- type
99
- };
100
- });
101
- return copyHistory;
102
- }, []);
103
- const updateView = useCallback(() => {
104
- if (isSearchResultVisible) {
105
- setView(VIEW.SEARCH);
106
- } else if ((articleId === null || articleId === void 0 ? void 0 : articleId.type) === ARTICLE_TYPE.HELP_ARTICLE) {
107
- if (articleId.id) {
108
- setView(VIEW.ARTICLE);
109
- } else {
110
- setView(VIEW.DEFAULT_CONTENT);
111
- }
112
- } else if ((articleId === null || articleId === void 0 ? void 0 : articleId.type) === ARTICLE_TYPE.WHATS_NEW) {
113
- if (articleId.id) {
114
- setView(VIEW.WHATS_NEW_ARTICLE);
115
- } else {
116
- setView(VIEW.WHATS_NEW);
268
+ default:
269
+ throw new Error('onGetHelpArticle prop not defined');
270
+ break;
117
271
  }
118
- }
119
- }, [articleId, isSearchResultVisible]);
120
- const updateHistoryItem = useCallback((historyItem, update) => {
121
- const index = tempHistory.current.findIndex(historyItemTemp => historyItemTemp.uid === historyItem.uid);
122
272
 
123
- if (index !== -1) {
124
- const newHistory = [...tempHistory.current];
125
- newHistory[index] = { ...historyItem,
126
- ...update
273
+ return { ...historyItem,
274
+ ...(article && {
275
+ article
276
+ }),
277
+ state: REQUEST_STATE.done
127
278
  };
128
- tempHistory.current = newHistory;
129
- setHistory(tempHistory.current);
130
- const simpleHistory = getSimpleHistory(tempHistory.current);
131
- historySetter && historySetter(simpleHistory);
132
- }
133
- }, [getSimpleHistory, historySetter]);
134
- const reloadHelpArticle = useCallback(async historyItem => {
135
- let reloadingHelpArticleHistoryItem = historyItem;
136
-
137
- if (onGetHelpArticle) {
138
- // if the historyItem isn't a "Help article", display the error message
139
- if (historyItem.type !== ARTICLE_TYPE.HELP_ARTICLE) {
140
- reloadingHelpArticleHistoryItem = { ...reloadingHelpArticleHistoryItem,
141
- state: REQUEST_STATE.error
142
- };
143
- } else {
144
- reloadingHelpArticleHistoryItem.state = REQUEST_STATE.loading;
145
- updateHistoryItem(reloadingHelpArticleHistoryItem);
146
-
147
- try {
148
- const article = await onGetHelpArticle({
149
- id: reloadingHelpArticleHistoryItem.id,
150
- type: reloadingHelpArticleHistoryItem.type
151
- });
152
- reloadingHelpArticleHistoryItem = { ...reloadingHelpArticleHistoryItem,
153
- state: REQUEST_STATE.done,
154
- article
155
- };
156
- } catch (error) {
157
- reloadingHelpArticleHistoryItem = { ...reloadingHelpArticleHistoryItem,
158
- state: REQUEST_STATE.error
159
- };
160
- }
161
- }
162
- } else {
163
- reloadingHelpArticleHistoryItem = { ...reloadingHelpArticleHistoryItem,
279
+ } catch (error) {
280
+ return { ...historyItem,
164
281
  state: REQUEST_STATE.error
165
282
  };
166
283
  }
284
+ }, [onGetHelpArticle, onGetWhatsNewArticle]);
285
+ const reloadArticle = useCallback(async historyItem => {
286
+ let historyItemToReload = { ...historyItem
287
+ };
167
288
 
168
- updateHistoryItem(reloadingHelpArticleHistoryItem);
169
- }, [onGetHelpArticle, updateHistoryItem]);
170
- const reloadWhatsNewArticle = useCallback(async historyItem => {
171
- let reloadingWhatsNewArticleHistoryItem = historyItem;
172
-
173
- if (onGetWhatsNewArticle) {
174
- if (historyItem.type !== ARTICLE_TYPE.WHATS_NEW) {
175
- // if the historyItem isn't a "What's new article", display the error message
176
- reloadingWhatsNewArticleHistoryItem = { ...reloadingWhatsNewArticleHistoryItem,
177
- state: REQUEST_STATE.error
178
- };
179
- } else {
180
- reloadingWhatsNewArticleHistoryItem.state = REQUEST_STATE.loading;
181
- updateHistoryItem(reloadingWhatsNewArticleHistoryItem);
182
-
183
- try {
184
- const article = await onGetWhatsNewArticle({
185
- id: reloadingWhatsNewArticleHistoryItem.id,
186
- type: reloadingWhatsNewArticleHistoryItem.type
187
- });
188
- reloadingWhatsNewArticleHistoryItem = { ...reloadingWhatsNewArticleHistoryItem,
189
- state: REQUEST_STATE.done,
190
- article
191
- };
192
- } catch (error) {
193
- reloadingWhatsNewArticleHistoryItem = { ...reloadingWhatsNewArticleHistoryItem,
194
- state: REQUEST_STATE.error
195
- };
196
- }
197
- }
289
+ if (historyItem.type === ARTICLE_TYPE.HELP_ARTICLE || historyItem.type === ARTICLE_TYPE.WHATS_NEW) {
290
+ historyItemToReload.state = REQUEST_STATE.loading;
198
291
  } else {
199
- reloadingWhatsNewArticleHistoryItem = { ...reloadingWhatsNewArticleHistoryItem,
200
- state: REQUEST_STATE.error
201
- };
292
+ historyItemToReload.state = REQUEST_STATE.error;
202
293
  }
203
294
 
204
- updateHistoryItem(reloadingWhatsNewArticleHistoryItem);
205
- }, [onGetWhatsNewArticle, updateHistoryItem]);
206
- const fetchArticleData = useCallback(async historyItem => {
207
- if (historyItem.type === ARTICLE_TYPE.HELP_ARTICLE) {
208
- if (onGetHelpArticle) {
209
- try {
210
- const article = await onGetHelpArticle({
211
- id: historyItem.id,
212
- type: historyItem.type
213
- });
214
- updateHistoryItem(historyItem, {
215
- state: REQUEST_STATE.done,
216
- article
217
- });
218
- } catch (error) {
219
- updateHistoryItem(historyItem, {
220
- state: REQUEST_STATE.error
221
- });
222
- }
223
- } else {
224
- updateHistoryItem(historyItem, {
225
- state: REQUEST_STATE.error
226
- });
227
- }
228
- } else if (historyItem.type === ARTICLE_TYPE.WHATS_NEW) {
229
- if (onGetWhatsNewArticle) {
230
- try {
231
- const article = await onGetWhatsNewArticle({
232
- id: historyItem.id,
233
- type: historyItem.type
234
- });
235
- updateHistoryItem(historyItem, {
236
- state: REQUEST_STATE.done,
237
- article
238
- });
239
- } catch (error) {
240
- updateHistoryItem(historyItem, {
241
- state: REQUEST_STATE.error
242
- });
243
- }
244
- } else {
245
- updateHistoryItem(historyItem, {
246
- state: REQUEST_STATE.error
247
- });
248
- }
249
- }
250
- }, [onGetHelpArticle, onGetWhatsNewArticle, updateHistoryItem]);
251
- const getCurrentArticle = useCallback(() => {
252
- const currentArticleItem = tempHistory.current[tempHistory.current.length - 1];
253
- return currentArticleItem;
295
+ dispatchNavigationAction({
296
+ type: 'updateHistoryItem',
297
+ payload: historyItemToReload
298
+ });
254
299
  }, []);
255
- const getCurrentArticleItemData = useCallback(() => {
256
- const {
257
- article,
258
- type
259
- } = getCurrentArticle();
260
-
261
- if (article) {
262
- if (type === ARTICLE_TYPE.HELP_ARTICLE) {
263
- const {
264
- body,
265
- relatedArticles,
266
- ...articleItemData
267
- } = article;
268
- const currentArticleSlimData = articleItemData;
269
- return currentArticleSlimData;
270
- } else if (type === ARTICLE_TYPE.WHATS_NEW) {
271
- const {
272
- description,
273
- ...whatsNewArticleItemData
274
- } = article;
275
- const currentWhatsNewArticleSlimData = whatsNewArticleItemData;
276
- return currentWhatsNewArticleSlimData;
277
- }
278
- }
279
- }, [getCurrentArticle]);
280
- const isOverlayVisible = useCallback(() => {
281
- return view === VIEW.ARTICLE || view === VIEW.SEARCH || view === VIEW.WHATS_NEW || view === VIEW.WHATS_NEW_ARTICLE;
282
- }, [view]);
283
- const canNavigateBack = useCallback(() => {
284
- /**
285
- * - If default content isn't defined and the history only has one article,
286
- * we should not display the back button
287
- * - If the prop.article.setArticleId is not defined, we should also hide the back
288
- * button because we are not able to navigate though the history without it
289
- */
290
- if (tempHistory.current.length === 1 && !isDefaultContentDefined() || view === VIEW.WHATS_NEW && !isDefaultContentDefined()) {
291
- return false;
292
- }
293
- /**
294
- * if an overlay is visible return true to display the back buton
295
- */
296
-
297
-
298
- return isOverlayVisible();
299
- }, [isDefaultContentDefined, isOverlayVisible, view]);
300
300
  const navigateBack = useCallback(async () => {
301
- if (!articleIdSetter) {
302
- return;
303
- }
304
301
  /**
305
302
  * If the user is in the search screen, just clean the search. That will make the search results
306
303
  * overlay disapear and show the last element in the history or (if is defined) the default content
307
304
  * */
308
-
309
-
310
- if (view === VIEW.SEARCH && onSearch) {
311
- onSearch('');
312
- return;
305
+ if (currentView === VIEW.SEARCH && onSearch) {
306
+ if (searchValue !== '') {
307
+ onSearch('');
308
+ return;
309
+ } else {
310
+ dispatchNavigationAction({
311
+ type: 'updateView',
312
+ payload: currentArticleId.type
313
+ });
314
+ return;
315
+ }
313
316
  } // if the history is not empty and ...
314
317
 
315
318
 
316
- if (tempHistory.current.length > 0) {
319
+ if (currentHistory.length > 0) {
317
320
  // the history has more than one article, navigate back through the history
318
- if (tempHistory.current.length > 1) {
321
+ if (currentHistory.length > 1) {
319
322
  // Remove last element
320
- const newHistoryValue = [...tempHistory.current.slice(0, -1)]; // update tempHistory and history
321
-
322
- tempHistory.current = newHistoryValue;
323
- setHistory(tempHistory.current); // Update host history using the historySetter
324
-
325
- historySetter && historySetter(tempHistory.current); // If the state of the current article (last article in the history)
326
- // is 'reload', fetch the article
327
-
328
- const lastHistoryItem = getCurrentArticle();
329
-
330
- if (lastHistoryItem.state === REQUEST_STATE.reload) {
331
- // fetch article data for the new history Item
332
- fetchArticleData(lastHistoryItem);
333
- }
334
- /**
335
- * If the state of the current article (last article in the history)
336
- * is NOT 'reload', it means the article is already loaded, is loading or
337
- * it had an error (which is handled and we display an error messages with
338
- * a "try again" button). So we just need to set the host article ID === to the
339
- * current article ID using the "articleIdSetter" prop function
340
- */
341
-
342
-
343
- if (lastHistoryItem) {
344
- articleIdSetter({
345
- id: lastHistoryItem.id,
346
- type: lastHistoryItem.type
347
- });
348
- }
349
- } else if (tempHistory.current.length === 1) {
323
+ dispatchNavigationAction({
324
+ type: 'removeLastHistoryItem'
325
+ });
326
+ } else if (currentHistory.length === 1) {
350
327
  // but if the history only has one item, clear the history
351
- clearHistory();
328
+ dispatchNavigationAction({
329
+ type: 'removeAllHistoryItems'
330
+ });
352
331
  }
353
332
  }
354
- }, [articleIdSetter, clearHistory, fetchArticleData, getCurrentArticle, historySetter, onSearch, view]);
333
+ }, [currentView, onSearch, currentHistory.length, searchValue, currentArticleId.type]);
355
334
  const onClose = useCallback((event, analyticsEvent) => {
356
335
  if (onCloseButtonClick) {
357
- clearHistory();
336
+ dispatchNavigationAction({
337
+ type: 'removeAllHistoryItems'
338
+ });
358
339
  onCloseButtonClick(event, analyticsEvent);
359
340
  }
360
- }, [clearHistory, onCloseButtonClick]);
341
+ }, [onCloseButtonClick]);
361
342
  useEffect(() => {
362
- if (!articleIdSetter) {
363
- return;
343
+ if (isSearchResultVisible) {
344
+ dispatchNavigationAction({
345
+ type: 'updateView',
346
+ payload: VIEW.SEARCH
347
+ });
364
348
  }
349
+ }, [isSearchResultVisible]);
350
+ useEffect(() => {
351
+ const simpleHistory = getSimpleHistory(currentHistory);
352
+ historySetter && historySetter(simpleHistory);
353
+ }, [historySetter, currentHistory]);
354
+ useEffect(() => {
365
355
  /**
366
- * If the article type is HELP_ARTICLE and the ID is defined we add a new historyItem
367
- *
368
- * If the article type is WHATS_NEW it doesn't matter if the the articleId.id is defined or not, we want
369
- * to add it to the history
370
- */
371
-
372
-
373
- if ((articleId === null || articleId === void 0 ? void 0 : articleId.type) === ARTICLE_TYPE.HELP_ARTICLE && articleId !== null && articleId !== void 0 && articleId.id || (articleId === null || articleId === void 0 ? void 0 : articleId.type) === ARTICLE_TYPE.WHATS_NEW) {
374
- // get the last History Item
375
- const lastHistoryItem = getCurrentArticle(); // If the last history item articleId isn't different to the current articleId don't do anything
376
-
377
- if (lastHistoryItem && lastHistoryItem.id === articleId.id) {
378
- return;
379
- } // Create a new History Item
380
-
381
-
382
- const newHistoryItem = getNewHistoryItem(articleId.id, articleId.type); // add add it to the tempHistory variable
383
-
384
- tempHistory.current = [...tempHistory.current, { ...newHistoryItem
385
- }];
386
- updateHistoryItem(newHistoryItem); // fetch article data for the new history Item
387
-
388
- fetchArticleData(newHistoryItem);
356
+ * If the propsArticleId.id (host articleId) is different from currentArticleId.id (internal articleId)
357
+ * it means the host updated propsArticleId and we need to use this new value to load a new article.
358
+ * */
359
+ if ((propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.id) !== currentArticleId.id || (propsArticleId === null || propsArticleId === void 0 ? void 0 : propsArticleId.type) !== currentArticleId.type) {
360
+ dispatchNavigationAction({
361
+ type: 'newArticle',
362
+ payload: propsArticleId
363
+ });
389
364
  } else {
390
- // articleId is undefined only during the first execution of this effect
391
- if (articleId === undefined) {
392
- const lastHistoryItem = getCurrentArticle();
393
-
394
- if (lastHistoryItem) {
395
- articleIdSetter({
365
+ /**
366
+ * If the propsArticleId.id (host articleId) is equal to currentArticleId.id (internal articleId)
367
+ * and the id from the last history item is different from currentArticleId.id, it means the history
368
+ * changed and we need to update the host articleId (propsArticleId) using 'articleIdSetter' and the local
369
+ * history using the dispatchNavigationAction reducer
370
+ * */
371
+ const lastHistoryItem = currentHistory.length > 0 ? getCurrentArticle(currentHistory) : getNewHistoryItem('', ARTICLE_TYPE.HELP_ARTICLE);
372
+
373
+ if (articleIdSetter && lastHistoryItem && (currentArticleId.id !== lastHistoryItem.id || currentArticleId.type !== lastHistoryItem.type)) {
374
+ dispatchNavigationAction({
375
+ type: 'updateArticleId',
376
+ payload: {
396
377
  id: lastHistoryItem.id,
397
378
  type: lastHistoryItem.type
398
- }); // fetch article data for the new history Item
399
-
400
- fetchArticleData(lastHistoryItem);
401
- }
402
- } else {
403
- // If article ID is empty clear the history
404
- if (!getCurrentArticle()) {
405
- clearHistory();
406
- }
379
+ }
380
+ });
381
+ articleIdSetter({
382
+ id: lastHistoryItem.id,
383
+ type: lastHistoryItem.type
384
+ });
407
385
  }
408
386
  }
409
- }, [articleId, articleIdSetter, clearHistory, fetchArticleData, getCurrentArticle, updateHistoryItem]);
410
- /**
411
- * VIEW effect
412
- * Set the view value based on the values of articleId
413
- */
414
-
387
+ }, [currentArticleId, propsArticleId, currentHistory, articleIdSetter]);
415
388
  useEffect(() => {
416
- updateView();
417
- }, [articleId, updateView]);
389
+ const requestNewArticle = async historyItem => {
390
+ const historyItemUpdate = await fetchArticleData(historyItem);
391
+ dispatchNavigationAction({
392
+ type: 'updateHistoryItem',
393
+ payload: historyItemUpdate
394
+ });
395
+ };
396
+ /**
397
+ * If the last history item state is "loading" or "reload", we need to request the article (fetch article from API)
398
+ */
399
+
400
+
401
+ const lastHistoryItem = getCurrentArticle(currentHistory);
402
+
403
+ if ((lastHistoryItem === null || lastHistoryItem === void 0 ? void 0 : lastHistoryItem.state) === REQUEST_STATE.loading || (lastHistoryItem === null || lastHistoryItem === void 0 ? void 0 : lastHistoryItem.state) === REQUEST_STATE.reload) {
404
+ requestNewArticle(lastHistoryItem);
405
+ }
406
+ }, [currentArticleId, currentHistory, fetchArticleData]);
418
407
  return /*#__PURE__*/React.createElement(CtxProvider, {
419
408
  value: {
420
- view,
421
- articleId,
409
+ view: currentView,
410
+ articleId: currentArticleId,
422
411
  setArticleId: articleIdSetter,
423
- history,
424
- getCurrentArticle,
425
- getCurrentArticleItemData,
426
- reloadHelpArticle: onGetHelpArticle && reloadHelpArticle,
427
- reloadWhatsNewArticle: onGetWhatsNewArticle && reloadWhatsNewArticle,
412
+ history: currentHistory,
413
+ getCurrentArticle: () => getCurrentArticle(currentHistory),
414
+ getCurrentArticleItemData: () => getCurrentArticleItemSlim(currentHistory),
415
+ reloadHelpArticle: onGetHelpArticle && reloadArticle,
416
+ reloadWhatsNewArticle: onGetWhatsNewArticle && reloadArticle,
428
417
  canNavigateBack,
429
418
  navigateBack,
430
419
  isOverlayVisible,