|
1 | 1 | import type { DocAndNode } from '@spyglassmc/core' |
| 2 | +import { useErrorBoundary } from 'preact/hooks' |
2 | 3 | import { useDocAndNode } from '../../contexts/Spyglass.jsx' |
3 | 4 | import { useVersion } from '../../contexts/Version.jsx' |
4 | 5 | import { checkVersion } from '../../services/index.js' |
5 | 6 | import { safeJsonParse } from '../../Utils.js' |
| 7 | +import { ErrorPanel } from '../ErrorPanel.jsx' |
6 | 8 | import { BiomeSourcePreview, BlockStatePreview, DecoratorPreview, DensityFunctionPreview, LootTablePreview, ModelPreview, NoisePreview, NoiseSettingsPreview, RecipePreview, StructureSetPreview } from '../previews/index.js' |
7 | 9 |
|
8 | 10 | export const HasPreview = ['loot_table', 'recipe', 'dimension', 'worldgen/density_function', 'worldgen/noise', 'worldgen/noise_settings', 'worldgen/configured_feature', 'worldgen/placed_feature', 'worldgen/structure_set', 'block_definition', 'docAndNode'] |
9 | 11 |
|
10 | 12 | type PreviewPanelProps = { |
11 | | - docAndNode: DocAndNode | undefined, |
12 | 13 | id: string, |
| 14 | + docAndNode: DocAndNode | undefined, |
13 | 15 | shown: boolean, |
14 | | - onError: (message: string) => unknown, |
15 | 16 | } |
16 | | -export function PreviewPanel({ docAndNode: original, id, shown }: PreviewPanelProps) { |
17 | | - const { version } = useVersion() |
18 | | - |
| 17 | +export function PreviewPanel({ id, docAndNode: original, shown }: PreviewPanelProps) { |
19 | 18 | if (!original) return <></> |
20 | 19 |
|
21 | 20 | const docAndNode = useDocAndNode(original) |
22 | 21 |
|
| 22 | + const [error, dismissError] = useErrorBoundary() |
| 23 | + |
| 24 | + if (error) { |
| 25 | + const previewError = new Error(`Preview error: ${error.message}`) |
| 26 | + if (error.stack) { |
| 27 | + previewError.stack = error.stack |
| 28 | + } |
| 29 | + return <ErrorPanel error={previewError} onDismiss={dismissError} /> |
| 30 | + } |
| 31 | + |
| 32 | + return <div> |
| 33 | + <PreviewContent key={id} id={id} docAndNode={docAndNode} shown={shown} /> |
| 34 | + </div> |
| 35 | +} |
| 36 | + |
| 37 | +type PreviewContentProps = { |
| 38 | + docAndNode: DocAndNode, |
| 39 | + id: string, |
| 40 | + shown: boolean, |
| 41 | +} |
| 42 | +export function PreviewContent({ id, docAndNode, shown }: PreviewContentProps) { |
| 43 | + const { version } = useVersion() |
| 44 | + |
23 | 45 | if (id === 'loot_table') { |
24 | 46 | return <LootTablePreview {...{ docAndNode, shown }} /> |
25 | 47 | } |
|
0 commit comments