1
1
import { h , render } from 'preact' ;
2
2
import { EnvironmentProvider , UpdateEnvironment } from '../../../shared/components/EnvironmentProvider.js' ;
3
3
4
- import { App } from './components/App.jsx' ;
4
+ import { App , AppLevelErrorBoundaryFallback } from './components/App.jsx' ;
5
5
import { Components } from './components/Components.jsx' ;
6
6
7
7
import enStrings from '../public/locales/en/history.json' ;
@@ -14,6 +14,7 @@ import { HistoryServiceProvider } from './global/Providers/HistoryServiceProvide
14
14
import { Settings } from './Settings.js' ;
15
15
import { SelectionProvider } from './global/Providers/SelectionProvider.js' ;
16
16
import { QueryProvider } from './global/Providers/QueryProvider.js' ;
17
+ import { InlineErrorBoundary } from '../../../shared/components/InlineErrorBoundary.js' ;
17
18
18
19
/**
19
20
* @param {Element } root
@@ -45,47 +46,54 @@ export async function init(root, messaging, baseEnvironment) {
45
46
. withDebounce ( baseEnvironment . urlParams . get ( 'debounce' ) )
46
47
. withUrlDebounce ( baseEnvironment . urlParams . get ( 'urlDebounce' ) ) ;
47
48
48
- console . log ( 'initialSetup' , init ) ;
49
- console . log ( 'environment' , environment ) ;
50
- console . log ( 'settings' , settings ) ;
49
+ if ( ! window . __playwright_01 ) {
50
+ console . log ( 'initialSetup' , init ) ;
51
+ console . log ( 'environment' , environment ) ;
52
+ console . log ( 'settings' , settings ) ;
53
+ }
51
54
52
- const strings =
53
- environment . locale === 'en'
54
- ? enStrings
55
- : await fetch ( `./locales/${ environment . locale } /history.json` )
56
- . then ( ( resp ) => {
57
- if ( ! resp . ok ) {
58
- throw new Error ( 'did not give a result' ) ;
59
- }
60
- return resp . json ( ) ;
61
- } )
62
- . catch ( ( e ) => {
63
- console . error ( 'Could not load locale' , environment . locale , e ) ;
64
- return enStrings ;
65
- } ) ;
55
+ /**
56
+ * @param {string } message
57
+ */
58
+ const didCatchInit = ( message ) => {
59
+ messaging . reportInitException ( { message } ) ;
60
+ } ;
66
61
62
+ const strings = await getStrings ( environment ) ;
67
63
const service = new HistoryService ( messaging ) ;
68
64
const query = paramsToQuery ( environment . urlParams , 'initial' ) ;
69
- const initial = await service . getInitial ( query ) ;
65
+ const initial = await fetchInitial ( query , service , didCatchInit ) ;
70
66
71
67
if ( environment . display === 'app' ) {
72
68
render (
73
- < EnvironmentProvider debugState = { environment . debugState } injectName = { environment . injectName } willThrow = { environment . willThrow } >
74
- < UpdateEnvironment search = { window . location . search } />
75
- < TranslationProvider translationObject = { strings } fallback = { enStrings } textLength = { environment . textLength } >
76
- < MessagingContext . Provider value = { messaging } >
77
- < SettingsContext . Provider value = { settings } >
78
- < QueryProvider query = { query . query } >
79
- < HistoryServiceProvider service = { service } initial = { initial } >
80
- < SelectionProvider >
81
- < App />
82
- </ SelectionProvider >
83
- </ HistoryServiceProvider >
84
- </ QueryProvider >
85
- </ SettingsContext . Provider >
86
- </ MessagingContext . Provider >
87
- </ TranslationProvider >
88
- </ EnvironmentProvider > ,
69
+ < InlineErrorBoundary
70
+ messaging = { messaging }
71
+ context = { 'History view application' }
72
+ fallback = { ( message ) => {
73
+ return < AppLevelErrorBoundaryFallback > { message } </ AppLevelErrorBoundaryFallback > ;
74
+ } }
75
+ >
76
+ < EnvironmentProvider
77
+ debugState = { environment . debugState }
78
+ injectName = { environment . injectName }
79
+ willThrow = { environment . willThrow }
80
+ >
81
+ < UpdateEnvironment search = { window . location . search } />
82
+ < TranslationProvider translationObject = { strings } fallback = { enStrings } textLength = { environment . textLength } >
83
+ < MessagingContext . Provider value = { messaging } >
84
+ < SettingsContext . Provider value = { settings } >
85
+ < QueryProvider query = { query . query } >
86
+ < HistoryServiceProvider service = { service } initial = { initial } >
87
+ < SelectionProvider >
88
+ < App />
89
+ </ SelectionProvider >
90
+ </ HistoryServiceProvider >
91
+ </ QueryProvider >
92
+ </ SettingsContext . Provider >
93
+ </ MessagingContext . Provider >
94
+ </ TranslationProvider >
95
+ </ EnvironmentProvider >
96
+ </ InlineErrorBoundary > ,
89
97
root ,
90
98
) ;
91
99
} else if ( environment . display === 'components' ) {
@@ -99,3 +107,42 @@ export async function init(root, messaging, baseEnvironment) {
99
107
) ;
100
108
}
101
109
}
110
+
111
+ /**
112
+ * @param {import('../types/history.js').HistoryQuery } query
113
+ * @param {HistoryService } service
114
+ * @param {(message: string) => void } didCatch
115
+ * @returns {Promise<import('./history.service.js').InitialServiceData> }
116
+ */
117
+ async function fetchInitial ( query , service , didCatch ) {
118
+ try {
119
+ return await service . getInitial ( query ) ;
120
+ } catch ( e ) {
121
+ console . error ( e ) ;
122
+ didCatch ( e . message || String ( e ) ) ;
123
+ return {
124
+ ranges : {
125
+ ranges : [ { id : 'all' , count : 0 } ] ,
126
+ } ,
127
+ query : {
128
+ info : { query : { term : '' } , finished : true } ,
129
+ results : [ ] ,
130
+ lastQueryParams : null ,
131
+ } ,
132
+ } ;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * @param {import("../../../shared/environment").Environment } environment
138
+ */
139
+ async function getStrings ( environment ) {
140
+ return environment . locale === 'en'
141
+ ? enStrings
142
+ : await fetch ( `./locales/${ environment . locale } /new-tab.json` )
143
+ . then ( ( x ) => x . json ( ) )
144
+ . catch ( ( e ) => {
145
+ console . error ( 'Could not load locale' , environment . locale , e ) ;
146
+ return enStrings ;
147
+ } ) ;
148
+ }
0 commit comments