Repository navigation
website: Reset playground previews after edit-caused errors #4255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+733
−43
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
75364aa
website: Reset playground previews after edit-caused render errors
claude 139eecb
website: Simplify playground reset plumbing
claude 348af31
website: Cover no-op paths of usePreviewReset
claude ba42041
website: Only auto-reset playground errors that follow an edit
claude 75c619b
website: End a playground retry once other code renders cleanly
claude 34cbf66
website: Restore playground fixture data with the store; drop pending…
claude bf0260d
website: Install react-live at the root so Playground tests can use i…
claude a02a31b
website: Test the playground snapshot drops optimistic updates and ke…
claude 7ea162c
Merge master into claude/project-thread-hlsp96
claude f1ac12f
website: Label playground compile vs runtime errors in a redesigned e…
claude e2d926e
website: Bold the name of plain Error messages in the playground erro…
claude 65644ef
website: Simplify the playground error card
claude 78ad1c8
website: Cover the playground error card's healthy signal, plain thro…
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,69 @@ | ||
| import React, { type ReactNode } from 'react'; | ||
|
|
||
| import styles from './styles.module.css'; | ||
|
|
||
| export type ErrorKind = 'compile' | 'runtime' | 'network'; | ||
|
|
||
| /** Each kind's label and icon paths (24×24, stroked) */ | ||
| const KINDS: Record<ErrorKind, { label: string; icon: string[] }> = { | ||
| // warning triangle | ||
| compile: { | ||
| label: 'Compile error', | ||
| icon: ['M12 3.5 2.5 20h19L12 3.5Z', 'M12 10v4.5', 'M12 17.5h.01'], | ||
| }, | ||
| // octagon with × | ||
| runtime: { | ||
| label: 'Runtime error', | ||
| icon: [ | ||
| 'M8 2.5h8l5.5 5.5v8L16 21.5H8L2.5 16V8L8 2.5Z', | ||
| 'm9 9 6 6M15 9l-6 6', | ||
| ], | ||
| }, | ||
| // broken link | ||
| network: { | ||
| label: 'Network error', | ||
| icon: [ | ||
| 'M10.5 13.5a4 4 0 0 0 5.66 0l2.84-2.84a4 4 0 0 0-5.66-5.66L12 6.34', | ||
| 'M13.5 10.5a4 4 0 0 0-5.66 0L5 13.34A4 4 0 0 0 10.66 19L12 17.66', | ||
| 'M4 4l16 16', | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| /** Card framing a preview failure: what stage failed (compile, runtime, network) and the message */ | ||
| export default function ErrorPanel({ | ||
| kind, | ||
| children, | ||
| action, | ||
| }: { | ||
| kind: ErrorKind; | ||
| children: ReactNode; | ||
| action?: ReactNode; | ||
| }) { | ||
| const { label, icon } = KINDS[kind]; | ||
| return ( | ||
| <div className={styles.previewError} data-kind={kind}> | ||
| <div className={styles.errorKind}> | ||
| <svg | ||
| className={styles.errorIcon} | ||
| viewBox="0 0 24 24" | ||
| width="1em" | ||
| height="1em" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth="2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| aria-hidden="true" | ||
| > | ||
| {icon.map(d => ( | ||
| <path key={d} d={d} /> | ||
| ))} | ||
| </svg> | ||
| {label} | ||
| </div> | ||
| {children} | ||
| {action} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
137 changes: 137 additions & 0 deletions
137
website/src/components/Playground/preview/PreviewError.tsx
This file contains hidden or 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,137 @@ | ||
| import { useController } from '@data-client/react'; | ||
| import clsx from 'clsx'; | ||
| import React, { useContext, useEffect } from 'react'; | ||
| import { LiveContext } from 'react-live'; | ||
|
|
||
| import type { PreviewSnapshot } from './usePreviewReset'; | ||
| import ErrorPanel from '../ErrorPanel'; | ||
| import styles from '../styles.module.css'; | ||
|
|
||
| /** react-live's error, plus a reset when it came from rendering (a fresh store may fix it). | ||
| * | ||
| * react-live sets `newCode` only once code reaches render, so compile and | ||
| * evaluation errors leave it at older code. | ||
| */ | ||
| export default function PreviewError({ | ||
| onReset, | ||
| onRenderError, | ||
| onHealthy, | ||
| }: PreviewErrorProps) { | ||
| const { error, code, newCode } = useContext(LiveContext); | ||
| const controller = useController(); | ||
| const rendered = newCode === code; | ||
| const isRenderError = !!error && rendered; | ||
| const isHealthy = !error && rendered; | ||
|
|
||
| useEffect(() => { | ||
| if (!isRenderError) return; | ||
| onRenderError(code, { | ||
| // The old store's in-flight requests die with it, so drop their optimistic updates | ||
| state: { ...controller.getState(), optimistic: [] }, | ||
| // MockResolver's controller holds the simulated server's data | ||
| interceptorData: (controller as { interceptorData?: unknown }) | ||
| .interceptorData, | ||
| }); | ||
| }, [isRenderError, code, onRenderError, controller]); | ||
| // react-live commits a throwing render once before reporting its error, so | ||
| // "healthy" means no error for a while. | ||
| useEffect(() => { | ||
| if (!isHealthy) return; | ||
| const timer = setTimeout(() => onHealthy(code), HEALTHY_AFTER_MS); | ||
| return () => clearTimeout(timer); | ||
| }, [isHealthy, code, onHealthy]); | ||
|
|
||
| if (!error) return null; | ||
| const { name, message, location } = splitError(error); | ||
| // react-live reports transform failures (and a missing `render()`) as SyntaxErrors | ||
| const kind = !isRenderError && name === 'SyntaxError' ? 'compile' : 'runtime'; | ||
|
ntucker marked this conversation as resolved.
|
||
| return ( | ||
| <ErrorPanel | ||
| kind={kind} | ||
| action={ | ||
| isRenderError ? | ||
| <button | ||
| type="button" | ||
| className={styles.errorAction} | ||
| onClick={onReset} | ||
| > | ||
| <ResetIcon /> Reset preview | ||
| </button> | ||
| : null | ||
| } | ||
| > | ||
| {/* Text content stays exactly `error`; the parts are only styled */} | ||
| <pre className={styles.playgroundError}> | ||
| {name ? | ||
| <strong className={styles.errorName}>{name}</strong> | ||
| : null} | ||
| {message} | ||
| {location ? | ||
| <span className={styles.errorLocation}>{location}</span> | ||
| : null} | ||
| </pre> | ||
| </ErrorPanel> | ||
| ); | ||
| } | ||
|
|
||
| const HEALTHY_AFTER_MS = 1000; | ||
|
|
||
| export interface PreviewErrorProps { | ||
| onReset: () => void; | ||
| /** Called with the code whose render threw and the preview it threw in */ | ||
| onRenderError: (code: string, snapshot: PreviewSnapshot) => void; | ||
| /** Called once the current code has rendered without error for a while */ | ||
| onHealthy: (code: string) => void; | ||
| } | ||
|
|
||
| /** "TypeError: msg (3:12)" → name "TypeError", message ": msg ", location "(3:12)" */ | ||
| function splitError(error: string) { | ||
| const named = /^((?:[A-Z][A-Za-z]*)?Error)((?::[\s\S]*)?)$/.exec(error); | ||
| let name = ''; | ||
| let message = error; | ||
| if (named) { | ||
| name = named[1]; | ||
| message = named[2]; | ||
| } | ||
| let location = ''; | ||
| // sucrase appends the position in the concatenated document | ||
| const located = /^([\s\S]*?\s)(\(\d+:\d+\))$/.exec(message); | ||
| if (located) { | ||
| message = located[1]; | ||
| location = located[2]; | ||
| } | ||
| return { name, message, location }; | ||
| } | ||
|
|
||
| export function ResetButton({ onClick }: { onClick: () => void }) { | ||
| return ( | ||
| <button | ||
| type="button" | ||
| className={clsx('clean-btn', styles.resetButton)} | ||
| title="Reset preview" | ||
| aria-label="Reset preview" | ||
| onClick={onClick} | ||
| > | ||
| <ResetIcon /> | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| function ResetIcon() { | ||
| return ( | ||
| <svg | ||
| viewBox="0 0 24 24" | ||
| width="1em" | ||
| height="1em" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth="2.25" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| aria-hidden="true" | ||
| > | ||
| <path d="M3 12a9 9 0 1 0 2.64-6.36L3 8.3" /> | ||
| <path d="M3 3v5.3h5.3" /> | ||
| </svg> | ||
| ); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.