Skip to content

Commit

Permalink
Enforce origin on cross-document message in Visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Jul 25, 2024
1 parent 6280e20 commit 7ea0985
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 178 deletions.
3 changes: 2 additions & 1 deletion viz/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"rules": {
"eqeqeq": "error",
"no-unused-vars": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "error"
}
}
92 changes: 92 additions & 0 deletions viz/app/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Button, ErrorMessage } from "@viz/app/components/Components";
import React, { useState } from "react";

interface ErrorBoundaryState {
error: unknown;
hasError: boolean;
}

interface ErrorBoundaryProps {
children: React.ReactNode;
errorMessage: string;
onRetryClick: (errorMessage: string) => void;
}

export class ErrorBoundary extends React.Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false, error: null };
}

static getDerivedStateFromError() {
// Update state so the next render will show the fallback UI.
return { hasError: true };
}

componentDidCatch(error: unknown) {
this.setState({ hasError: true, error });
}

render() {
if (this.state.hasError) {
let error: Error;
if (this.state.error instanceof Error) {
error = this.state.error;
} else {
error = new Error("Unknown error.");
}

return (
<RenderError
error={error}
onRetryClick={this.props.onRetryClick}
message={this.props.errorMessage}
/>
);
}

return <>{this.props.children}</>;
}
}

// This is the component to render when an error occurs.
export function RenderError({
error,
message,
onRetryClick,
}: {
error: Error;
message: string;
onRetryClick: (errorMessage: string) => void;
}) {
const [showDetails, setShowDetails] = useState(false);

return (
<div className="flex w-full flex-col items-center justify-center gap-4">
<ErrorMessage title="Error">
<>
{message}
<div className="mt-2">
<button
onClick={() => setShowDetails(!showDetails)}
className="text-pink-700 underline focus:outline-none"
>
{showDetails ? "Hide details" : "Show details"}
</button>
{showDetails && (
<div className="mt-2 p-2 bg-pink-50 rounded">
<strong>Error message:</strong> {error.message}
</div>
)}
</div>
</>
</ErrorMessage>
<div>
<Button label="Retry" onClick={() => onRetryClick(error.message)} />
</div>
</div>
);
}
Loading

0 comments on commit 7ea0985

Please sign in to comment.