|
1 | | -import { Fragment, h } from 'preact'; |
| 1 | +import { h } from 'preact'; |
2 | 2 | import { WidgetConfigContext, WidgetVisibilityProvider } from './widget-config.provider.js'; |
3 | 3 | import { useContext } from 'preact/hooks'; |
4 | | -import { Customizer, CustomizerMenuPositionedFixed } from '../customizer/components/Customizer'; |
5 | | -import { useEnv } from '../../../../shared/components/EnvironmentProvider.js'; |
6 | | -import { DebugCustomized } from '../telemetry/Debug.js'; |
| 4 | +import { Customizer, CustomizerMenuPositionedFixed } from '../customizer/components/Customizer.js'; |
| 5 | +import { useMessaging } from '../types.js'; |
| 6 | +import { ErrorBoundary } from '../../../../shared/components/ErrorBoundary.js'; |
| 7 | +import { Fallback } from '../../../../shared/components/Fallback/Fallback.jsx'; |
| 8 | +import { Centered } from '../components/Layout.js'; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * @param {string} id |
@@ -37,25 +39,53 @@ export async function widgetEntryPoint(id) { |
37 | 39 |
|
38 | 40 | export function WidgetList() { |
39 | 41 | const { widgets, widgetConfigItems, entryPoints } = useContext(WidgetConfigContext); |
40 | | - const { env } = useEnv(); |
| 42 | + const messaging = useMessaging(); |
| 43 | + |
| 44 | + /** |
| 45 | + * @param {any} error |
| 46 | + * @param {string} id |
| 47 | + */ |
| 48 | + const didCatch = (error, id) => { |
| 49 | + const message = error?.message || error?.error || 'unknown'; |
| 50 | + const composed = `Widget '${id}' threw an exception: ` + message; |
| 51 | + messaging.reportPageException({ message: composed }); |
| 52 | + }; |
41 | 53 |
|
42 | 54 | return ( |
43 | 55 | <div> |
44 | 56 | {widgets.map((widget, index) => { |
45 | 57 | const matchingConfig = widgetConfigItems.find((item) => item.id === widget.id); |
46 | 58 | const matchingEntryPoint = entryPoints[widget.id]; |
| 59 | + /** |
| 60 | + * If there's no config, it means the user does not control the visibility of the elements in question. |
| 61 | + */ |
47 | 62 | if (!matchingConfig) { |
48 | | - return <Fragment key={widget.id}>{matchingEntryPoint.factory?.()}</Fragment>; |
| 63 | + return ( |
| 64 | + <ErrorBoundary key={widget.id} didCatch={(error) => didCatch(error, widget.id)} fallback={null}> |
| 65 | + {matchingEntryPoint.factory?.()} |
| 66 | + </ErrorBoundary> |
| 67 | + ); |
49 | 68 | } |
| 69 | + |
| 70 | + /** |
| 71 | + * This section is for elements that the user controls the visibility of |
| 72 | + */ |
50 | 73 | return ( |
51 | | - <Fragment key={widget.id}> |
52 | | - <WidgetVisibilityProvider visibility={matchingConfig.visibility} id={matchingConfig.id} index={index}> |
| 74 | + <WidgetVisibilityProvider key={widget.id} visibility={matchingConfig.visibility} id={matchingConfig.id} index={index}> |
| 75 | + <ErrorBoundary |
| 76 | + key={widget.id} |
| 77 | + didCatch={(error) => didCatch(error, widget.id)} |
| 78 | + fallback={ |
| 79 | + <Centered> |
| 80 | + <Fallback showDetails={true}>Widget id: {matchingConfig.id}</Fallback> |
| 81 | + </Centered> |
| 82 | + } |
| 83 | + > |
53 | 84 | {matchingEntryPoint.factory?.()} |
54 | | - </WidgetVisibilityProvider> |
55 | | - </Fragment> |
| 85 | + </ErrorBoundary> |
| 86 | + </WidgetVisibilityProvider> |
56 | 87 | ); |
57 | 88 | })} |
58 | | - {env === 'development' && <DebugCustomized index={widgets.length} />} |
59 | 89 | <CustomizerMenuPositionedFixed> |
60 | 90 | <Customizer /> |
61 | 91 | </CustomizerMenuPositionedFixed> |
|
0 commit comments