|
1 | | -import * as fos from "@fiftyone/state"; |
2 | | -import { getFieldsWithEmbeddedDocType } from "@fiftyone/utilities"; |
3 | | -import React, { useMemo } from "react"; |
4 | | -import { useRecoilValue } from "recoil"; |
5 | | -import { CustomErrorBoundary } from "./CustomErrorBoundary"; |
6 | | -import { RerunReactRenderer } from "./RerunReactRenderer"; |
| 1 | +import React, { Component, ReactNode, Suspense, useRef } from "react"; |
| 2 | +import { RerunViewerReact } from "./RerunReactRenderer"; |
7 | 3 |
|
8 | 4 | export const RerunFileDescriptor = { |
9 | 5 | EMBEDDED_DOC_TYPE: "fiftyone.utils.rerun.RrdFile", |
10 | 6 | }; |
11 | 7 |
|
12 | | -type RerunFieldDescriptor = { |
13 | | - _cls: "RrdFile"; |
14 | | - filepath: string; |
15 | | - version: string; |
16 | | -}; |
17 | | - |
18 | | -export const RerunViewer = React.memo(() => { |
19 | | - const currentSample = useRecoilValue(fos.modalSample); |
20 | | - |
21 | | - const schema = useRecoilValue( |
22 | | - fos.fieldSchema({ space: fos.State.SPACE.SAMPLE }) |
23 | | - ); |
24 | | - |
25 | | - const rerunFieldPath = useMemo( |
26 | | - () => |
27 | | - getFieldsWithEmbeddedDocType( |
28 | | - schema, |
29 | | - RerunFileDescriptor.EMBEDDED_DOC_TYPE |
30 | | - ).at(0)?.path, |
31 | | - [schema] |
32 | | - ); |
| 8 | +interface ErrorBoundaryState { |
| 9 | + hasError: boolean; |
| 10 | + error?: Error; |
| 11 | +} |
33 | 12 |
|
34 | | - const rrdParams = useMemo(() => { |
35 | | - if (!rerunFieldPath || !currentSample.urls) { |
36 | | - return undefined; |
37 | | - } |
| 13 | +export class RerunErrorBoundary extends Component< |
| 14 | + { children: ReactNode; action?: () => void }, |
| 15 | + ErrorBoundaryState |
| 16 | +> { |
| 17 | + constructor(props: { children: ReactNode }) { |
| 18 | + super(props); |
| 19 | + this.state = { hasError: false }; |
| 20 | + } |
38 | 21 |
|
39 | | - const filePathAndVersion = currentSample?.sample?.[ |
40 | | - rerunFieldPath |
41 | | - ] as unknown as RerunFieldDescriptor; |
| 22 | + static getDerivedStateFromError(error: Error): ErrorBoundaryState { |
| 23 | + return { hasError: true, error }; |
| 24 | + } |
42 | 25 |
|
43 | | - const urlsStandardized = fos.getStandardizedUrls(currentSample.urls); |
| 26 | + componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { |
| 27 | + console.error("Rerun viewer error:", error, errorInfo); |
| 28 | + } |
44 | 29 |
|
45 | | - const rrdFilePath = urlsStandardized[`${rerunFieldPath}.filepath`]; |
| 30 | + resetError = () => { |
| 31 | + this.setState({ hasError: false, error: undefined }); |
| 32 | + }; |
46 | 33 |
|
47 | | - const url = fos.getSampleSrc(rrdFilePath); |
48 | | - return { |
49 | | - url, |
50 | | - version: filePathAndVersion?.version, |
51 | | - }; |
52 | | - }, [currentSample, rerunFieldPath]); |
| 34 | + render() { |
| 35 | + if (this.state.hasError) { |
| 36 | + return ( |
| 37 | + <div |
| 38 | + style={{ |
| 39 | + display: "flex", |
| 40 | + flexDirection: "column", |
| 41 | + alignItems: "center", |
| 42 | + justifyContent: "center", |
| 43 | + minHeight: "100vh", |
| 44 | + padding: "40px 20px", |
| 45 | + }} |
| 46 | + > |
| 47 | + <div |
| 48 | + style={{ |
| 49 | + maxWidth: "500px", |
| 50 | + width: "100%", |
| 51 | + padding: "32px 24px", |
| 52 | + textAlign: "center", |
| 53 | + color: "#d32f2f", |
| 54 | + fontSize: "14px", |
| 55 | + border: "1px solid #d32f2f", |
| 56 | + borderRadius: "12px", |
| 57 | + backgroundColor: "#ffebee", |
| 58 | + backdropFilter: "blur(10px)", |
| 59 | + position: "relative", |
| 60 | + overflow: "hidden", |
| 61 | + }} |
| 62 | + > |
| 63 | + <div |
| 64 | + style={{ |
| 65 | + position: "absolute", |
| 66 | + top: 0, |
| 67 | + left: 0, |
| 68 | + right: 0, |
| 69 | + height: "4px", |
| 70 | + backgroundColor: "#d32f2f", |
| 71 | + }} |
| 72 | + /> |
| 73 | + <div |
| 74 | + style={{ |
| 75 | + fontWeight: "600", |
| 76 | + fontSize: "18px", |
| 77 | + marginBottom: "12px", |
| 78 | + color: "#b71c1c", |
| 79 | + }} |
| 80 | + > |
| 81 | + Rerun viewer error |
| 82 | + </div> |
| 83 | + <div |
| 84 | + style={{ |
| 85 | + fontSize: "14px", |
| 86 | + wordBreak: "break-word", |
| 87 | + lineHeight: "1.5", |
| 88 | + marginBottom: "20px", |
| 89 | + color: "#c62828", |
| 90 | + }} |
| 91 | + > |
| 92 | + {this.state.error?.message || "An unknown error occurred"} |
| 93 | + </div> |
| 94 | + {this.props.action ? ( |
| 95 | + <button |
| 96 | + onClick={() => { |
| 97 | + this.setState({ hasError: false }); |
| 98 | + this.props.action(); |
| 99 | + }} |
| 100 | + style={{ |
| 101 | + padding: "10px 24px", |
| 102 | + backgroundColor: "#d32f2f", |
| 103 | + color: "white", |
| 104 | + border: "none", |
| 105 | + borderRadius: "8px", |
| 106 | + fontSize: "14px", |
| 107 | + fontWeight: "500", |
| 108 | + cursor: "pointer", |
| 109 | + transition: "all 0.2s ease", |
| 110 | + }} |
| 111 | + > |
| 112 | + Retry |
| 113 | + </button> |
| 114 | + ) : ( |
| 115 | + <button |
| 116 | + onClick={() => { |
| 117 | + this.setState({ hasError: false }); |
| 118 | + }} |
| 119 | + style={{ |
| 120 | + padding: "10px 24px", |
| 121 | + backgroundColor: "#d32f2f", |
| 122 | + color: "white", |
| 123 | + border: "none", |
| 124 | + borderRadius: "8px", |
| 125 | + fontSize: "14px", |
| 126 | + fontWeight: "500", |
| 127 | + cursor: "pointer", |
| 128 | + transition: "all 0.2s ease", |
| 129 | + }} |
| 130 | + > |
| 131 | + Attempt Reload |
| 132 | + </button> |
| 133 | + )} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
53 | 138 |
|
54 | | - if (!rrdParams) { |
55 | | - return <div>Loading...</div>; |
| 139 | + return this.props.children; |
56 | 140 | } |
| 141 | +} |
| 142 | + |
| 143 | +export const RerunViewer = React.memo(() => { |
| 144 | + const errorBoundaryRef = useRef<RerunErrorBoundary>(null); |
57 | 145 |
|
58 | 146 | return ( |
59 | | - <CustomErrorBoundary> |
60 | | - <RerunReactRenderer url={rrdParams.url} version={rrdParams.version} /> |
61 | | - </CustomErrorBoundary> |
| 147 | + <Suspense |
| 148 | + fallback={ |
| 149 | + <div |
| 150 | + style={{ |
| 151 | + padding: "20px", |
| 152 | + textAlign: "center", |
| 153 | + color: "#666", |
| 154 | + fontSize: "14px", |
| 155 | + }} |
| 156 | + > |
| 157 | + Loading Rerun viewer... |
| 158 | + </div> |
| 159 | + } |
| 160 | + > |
| 161 | + <RerunErrorBoundary ref={errorBoundaryRef}> |
| 162 | + <RerunViewerReact /> |
| 163 | + </RerunErrorBoundary> |
| 164 | + </Suspense> |
62 | 165 | ); |
63 | 166 | }); |
0 commit comments