-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add fallback UI for webgl init failure
- Loading branch information
1 parent
ec7f4c1
commit e3ee05e
Showing
9 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from "./lib/Badge/index"; | ||
export * from "./lib/SectionContainer/index"; | ||
export * from "./lib/Anchor/index"; | ||
export * from "./lib/ErrorBoundary/index"; | ||
export * from "./lib/WebGLFallback/index"; | ||
export * from "./lib/types"; |
40 changes: 40 additions & 0 deletions
40
packages/shared-components/lib/ErrorBoundary/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Component, type ErrorInfo, type ReactNode } from "react"; | ||
|
||
interface Props { | ||
children?: ReactNode; | ||
fallback: (error: Error) => ReactNode; | ||
} | ||
|
||
interface State { | ||
error: Error | undefined; | ||
} | ||
|
||
class ErrorBoundary extends Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { error: undefined }; | ||
} | ||
|
||
public state: State = { | ||
error: undefined, | ||
}; | ||
|
||
public static getDerivedStateFromError(error: Error): State { | ||
// Update state so the next render will show the fallback UI. | ||
return { error }; | ||
} | ||
|
||
public componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
console.error("Uncaught error:", error, errorInfo); | ||
} | ||
|
||
public render() { | ||
if (this.state.error) { | ||
return this.props.fallback(this.state.error); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as ErrorBoundary } from "./ErrorBoundary"; |
35 changes: 35 additions & 0 deletions
35
packages/shared-components/lib/WebGLFallback/WebGLFallback.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ReactNode } from "react"; | ||
|
||
function isWebglSupported() { | ||
if (window.WebGLRenderingContext) { | ||
const canvas = document.createElement("canvas"); | ||
try { | ||
// Note that { failIfMajorPerformanceCaveat: true } can be passed as a second argument | ||
// to canvas.getContext(), causing the check to fail if hardware rendering is not available. See | ||
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext | ||
// for more details. | ||
const context = | ||
canvas.getContext("webgl2") || canvas.getContext("webgl"); | ||
if (context && typeof context.getParameter == "function") { | ||
return true; | ||
} | ||
} catch (e) { | ||
// WebGL is supported, but disabled | ||
} | ||
return false; | ||
} | ||
// WebGL not supported | ||
return false; | ||
} | ||
|
||
// webgl doesnt exist on server. so this can be a client side only component. | ||
export default function WebGLFallback({ | ||
fallback, | ||
children, | ||
}: { | ||
fallback: ReactNode; | ||
children: ReactNode; | ||
}) { | ||
if (isWebglSupported()) return children; | ||
else return fallback; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as WebGLFallback } from "./WebGLFallback"; |