Skip to content

Commit

Permalink
with error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
mabaasit committed Sep 20, 2024
1 parent 49cfe1d commit f032ff1
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions packages/compass-collection/src/components/collection-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,54 @@ type CollectionTabProps = Omit<CollectionTabOptions, 'tabId'> &
ConnectionTabConnectedProps &
ConnectionTabExpectedProps;

function WithErrorBoundary({
children,
name,
type,
}: {
children: React.ReactNode;
name: string;
type: 'content' | 'header';
}) {
const { log, mongoLogId } = useLogger('COMPASS-COLLECTION-TAB-UI');
return (
<ErrorBoundary
key={name}
onError={(error: Error, errorInfo: unknown) => {
log.error(
mongoLogId(1001000107),
'Collection Workspace',
'Rendering collection tab failed',
{ name, type, error: error.stack, errorInfo }
);
}}
>
{children}
</ErrorBoundary>
);
}

function useCollectionTabs(props: CollectionMetadata) {
const pluginTabs = useCollectionSubTabs();
const { log, mongoLogId } = useLogger('COMPASS-COLLECTION-TAB-UI');
return pluginTabs.map(({ name, Content, Provider, Header }) => {
// `pluginTabs` never change in runtime so it's safe to call the hook here
// eslint-disable-next-line react-hooks/rules-of-hooks
Provider.useActivate(props);
return {
name,
content: (
<ErrorBoundary
key={name}
onError={(error: Error, errorInfo: unknown) => {
log.error(
mongoLogId(1001000107),
'Collection Workspace',
'Rendering collection tab failed',
{ name, error: error.stack, errorInfo }
);
}}
>
<WithErrorBoundary name={name} type="content">
<Provider {...props}>
<Content {...props} />
</Provider>
</ErrorBoundary>
</WithErrorBoundary>
),
title: (
<Provider {...props}>
<Header />
</Provider>
<WithErrorBoundary name={name} type="header">
<Provider {...props}>
<Header />
</Provider>
</WithErrorBoundary>
),
};
});
Expand Down

0 comments on commit f032ff1

Please sign in to comment.