Skip to content

Commit

Permalink
Merge branch 'master' into document-recursion-error-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AceTheCreator authored Dec 9, 2024
2 parents 74c82b0 + baa8a96 commit f62e6c1
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 75 deletions.
5 changes: 3 additions & 2 deletions library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/react-component",
"version": "2.4.3",
"version": "2.5.0",
"private": false,
"description": "A React component for AsyncAPI specification.",
"repository": {
Expand Down Expand Up @@ -71,11 +71,12 @@
"@asyncapi/avro-schema-parser": "^3.0.24",
"@asyncapi/openapi-schema-parser": "^3.0.24",
"@asyncapi/parser": "^3.3.0",
"@asyncapi/protobuf-schema-parser": "^3.2.14",
"@asyncapi/protobuf-schema-parser": "^3.3.0",
"highlight.js": "^10.7.2",
"isomorphic-dompurify": "^2.14.0",
"marked": "^4.0.14",
"openapi-sampler": "^1.2.1",
"react-error-boundary": "^4.1.2",
"use-resize-observer": "^9.1.0"
},
"peerDependencies": {
Expand Down
39 changes: 39 additions & 0 deletions library/src/containers/ApplicationErrorHandler/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { useEffect, useState } from 'react';
import { ReactNode } from 'react';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
import { ErrorObject } from '../../types';
import { Error } from '../Error/Error';

interface Props {
children: ReactNode;
}

function fallbackRender({ error }: FallbackProps) {
const ErrorObject: ErrorObject = {
title: 'Something went wrong',
type: 'application-error',
validationErrors: [
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
title: error?.message,
},
],
};
return <Error error={ErrorObject} />;
}

const AsyncApiErrorBoundary = ({ children }: Props) => {
const [key, setKey] = useState(0);

useEffect(() => {
setKey((prevKey) => prevKey + 1);
}, [children]);

return (
<ErrorBoundary key={key} fallbackRender={fallbackRender}>
{children}
</ErrorBoundary>
);
};

export default AsyncApiErrorBoundary;
38 changes: 18 additions & 20 deletions library/src/containers/AsyncApi/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ import { Servers } from '../Servers/Servers';
import { Operations } from '../Operations/Operations';
import { Messages } from '../Messages/Messages';
import { Schemas } from '../Schemas/Schemas';
import { Error } from '../Error/Error';

import { ConfigInterface } from '../../config';
import { SpecificationContext, ConfigContext } from '../../contexts';
import { ErrorObject } from '../../types';
import AsyncApiErrorBoundary from '../ApplicationErrorHandler/ErrorBoundary';

interface Props {
asyncapi: AsyncAPIDocumentInterface;
config: ConfigInterface;
error?: ErrorObject;
}

const AsyncApiLayout: React.FunctionComponent<Props> = ({
asyncapi,
config,
error = null,
}) => {
const [observerClassName, setObserverClassName] = useState('container:xl');

Expand All @@ -48,24 +45,25 @@ const AsyncApiLayout: React.FunctionComponent<Props> = ({
<ConfigContext.Provider value={config}>
<SpecificationContext.Provider value={asyncapi}>
<section className="aui-root">
<div
className={`${observerClassName} relative md:flex bg-white leading-normal`}
id={config.schemaID ?? undefined}
ref={ref}
>
{configShow.sidebar && <Sidebar />}
<div className="panel--center relative py-8 flex-1">
<div className="relative z-10">
{configShow.errors && error && <Error error={error} />}
{configShow.info && <Info />}
{configShow.servers && <Servers />}
{configShow.operations && <Operations />}
{configShow.messages && <Messages />}
{configShow.schemas && <Schemas />}
<AsyncApiErrorBoundary>
<div
className={`${observerClassName} relative md:flex bg-white leading-normal`}
id={config.schemaID ?? undefined}
ref={ref}
>
{configShow.sidebar && <Sidebar />}
<div className="panel--center relative py-8 flex-1">
<div className="relative z-10">
{configShow.info && <Info />}
{configShow.servers && <Servers />}
{configShow.operations && <Operations />}
{configShow.messages && <Messages />}
{configShow.schemas && <Schemas />}
</div>
<div className="panel--right absolute top-0 right-0 h-full bg-gray-800" />
</div>
<div className="panel--right absolute top-0 right-0 h-full bg-gray-800" />
</div>
</div>
</AsyncApiErrorBoundary>
</section>
</SpecificationContext.Provider>
</ConfigContext.Provider>
Expand Down
8 changes: 1 addition & 7 deletions library/src/containers/AsyncApi/Standalone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {
);
}

return (
<AsyncApiLayout
asyncapi={asyncapi}
config={concatenatedConfig}
error={error}
/>
);
return <AsyncApiLayout asyncapi={asyncapi} config={concatenatedConfig} />;
}

private updateState(schema: PropsSchema) {
Expand Down
5 changes: 4 additions & 1 deletion library/src/containers/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const renderErrors = (errors: ValidationError[]): React.ReactNode => {
}
return (
<div key={index} className="flex gap-2">
<span>{`line ${singleError?.location?.startLine + singleError?.location?.startOffset}:`}</span>
{(singleError?.location?.startLine ??
singleError?.location?.startOffset) && (
<span>{`line ${singleError?.location?.startLine + singleError?.location?.startOffset}:`}</span>
)}
<code className="whitespace-pre-wrap break-all ml-2">
{singleError.title}
</code>
Expand Down
2 changes: 1 addition & 1 deletion library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface MessageExample {

export interface ValidationError {
title: string;
location: {
location?: {
jsonPointer: string;
startLine: number;
startColumn: number;
Expand Down
Loading

0 comments on commit f62e6c1

Please sign in to comment.