Skip to content

Commit

Permalink
Fix #418 noise settings preview freezes
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Oct 3, 2023
1 parent f71e44f commit 446025c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/app/components/ErrorPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import { Octicon } from './index.js'

type ErrorPanelProps = {
error: string | Error,
prefix?: string,
reportable?: boolean,
onDismiss?: () => unknown,
body?: string,
children?: ComponentChildren,
}
export function ErrorPanel({ error, reportable, onDismiss, body: body_, children }: ErrorPanelProps) {
export function ErrorPanel({ error, prefix, reportable, onDismiss, body: body_, children }: ErrorPanelProps) {
const { version } = useVersion()
const [stackVisible, setStackVisible] = useState(false)
const [stack, setStack] = useState<string | undefined>(undefined)

const gen = getGenerator(getCurrentUrl())
const source = gen ? Store.getBackup(gen.id) : undefined
const name = (prefix ?? '') + (error instanceof Error ? error.message : error)

useEffect(() => {
if (error instanceof Error) {
Expand All @@ -41,7 +43,8 @@ export function ErrorPanel({ error, reportable, onDismiss, body: body_, children

const url = useMemo(() => {
let url ='https://github.com/misode/misode.github.io/issues/new'
url += `?title=${encodeURIComponent(error instanceof Error ? `${error.name}: ${error.message}` : error.toString())}`
const fullName = (error instanceof Error ? `${error.name}: ` : '') + name
url += `?title=${encodeURIComponent(fullName)}`
let body = ''
body += `## Crash report\n * Page url: \`${location.href}\`\n`
if (gen) {
Expand All @@ -50,7 +53,7 @@ export function ErrorPanel({ error, reportable, onDismiss, body: body_, children
body += ` * Current version: \`${version}\`\n`
body += ` * Latest version: \`${latestVersion}\`\n`
if (error instanceof Error && stack) {
body += `\n### Stack trace\n\`\`\`\n${error.name}: ${error.message}\n${stack}\n\`\`\`\n`
body += `\n### Stack trace\n\`\`\`\n${fullName}\n${stack}\n\`\`\`\n`
}
if (source) {
body += `\n### Generator JSON\n<details>\n<pre>\n${JSON.stringify(source, null, 2)}\n</pre>\n</details>\n`
Expand All @@ -60,12 +63,12 @@ export function ErrorPanel({ error, reportable, onDismiss, body: body_, children
}
url += `&body=${encodeURIComponent(body)}`
return url
}, [error, body_, version, stack, source, gen?.id])
}, [error, name, body_, version, stack, source, gen?.id])

return <div class="error">
{onDismiss && <div class="error-dismiss" onClick={onDismiss}>{Octicon.x}</div>}
<h3>
{error instanceof Error ? error.message : error}
{(prefix ?? '') + (error instanceof Error ? error.message : error)}
{stack && <span onClick={() => setStackVisible(!stackVisible)}>
{Octicon.info}
</span>}
Expand Down
5 changes: 4 additions & 1 deletion src/app/components/previews/Deepslate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as deepslate19 from 'deepslate/worldgen'
import { clamp, computeIfAbsent, computeIfAbsentAsync, deepClone, deepEqual, isObject, square } from '../../Utils.js'
import type { VersionId } from '../../services/index.js'
import { checkVersion, fetchAllPresets, fetchPreset } from '../../services/index.js'
import { clamp, computeIfAbsent, computeIfAbsentAsync, deepClone, deepEqual, isObject, square } from '../../Utils.js'

export type ProjectData = Record<string, Record<string, unknown>>

Expand Down Expand Up @@ -99,6 +99,9 @@ export class Deepslate {
const newCacheState = [settings, `${seed}`, biomeState]
if (!deepEqual(this.cacheState, newCacheState)) {
const noiseSettings = this.createNoiseSettings(settings)
if (noiseSettings.noise.xzSize === 0 || noiseSettings.noise.ySize === 0) {
throw new Error('size_horizontal or size_vertical cannot be zero')
}
const biomeSource = await this.createBiomeSource(noiseSettings, biomeState, seed)
const chunkGenerator = this.isVersion('1.19')
? new this.d.NoiseChunkGenerator(biomeSource, noiseSettings)
Expand Down
14 changes: 9 additions & 5 deletions src/app/components/previews/NoiseSettingsPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { clampedMap } from 'deepslate'
import type { mat3 } from 'gl-matrix'
import { vec2 } from 'gl-matrix'
import { useCallback, useMemo, useRef, useState } from 'preact/hooks'
import { Store } from '../../Store.js'
import { iterateWorld2D, randomSeed } from '../../Utils.js'
import { getProjectData, useLocale, useProject } from '../../contexts/index.js'
import { useAsync } from '../../hooks/index.js'
import { CachedCollections } from '../../services/index.js'
import { Store } from '../../Store.js'
import { iterateWorld2D, randomSeed } from '../../Utils.js'
import { Btn, BtnInput, BtnMenu } from '../index.js'
import { Btn, BtnInput, BtnMenu, ErrorPanel } from '../index.js'
import type { ColormapType } from './Colormap.js'
import { getColormap } from './Colormap.js'
import { ColormapSelector } from './ColormapSelector.jsx'
import { DEEPSLATE } from './Deepslate.js'
import type { PreviewProps } from './index.js'
import { InteractiveCanvas2D } from './InteractiveCanvas2D.jsx'
import type { PreviewProps } from './index.js'

export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) => {
const { locale } = useLocale()
Expand All @@ -24,7 +24,7 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) =>
const [layer, setLayer] = useState('terrain')
const state = JSON.stringify(data)

const { value } = useAsync(async () => {
const { value, error } = useAsync(async () => {
const unwrapped = DataModel.unwrapLists(data)
await DEEPSLATE.loadVersion(version, getProjectData(project))
const biomeSource = { type: 'fixed', biome }
Expand Down Expand Up @@ -88,6 +88,10 @@ export const NoiseSettingsPreview = ({ data, shown, version }: PreviewProps) =>

const allBiomes = useMemo(() => CachedCollections?.get('worldgen/biome') ?? [], [version])

if (error) {
return <ErrorPanel error={error} prefix="Failed to initialize preview: " />
}

return <>
<div class="controls preview-controls">
{focused.map(s => <Btn label={s} class="no-pointer" /> )}
Expand Down
5 changes: 5 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,11 @@ main.has-project {
margin: 10px 0;
}

.error svg {
display: inline;
fill: currentColor;
}

.error h3 span {
margin-left: 8px;
cursor: pointer;
Expand Down

1 comment on commit 446025c

@vercel
Copy link

@vercel vercel bot commented on 446025c Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

misode – ./

misode.vercel.app
misode-git-master-misode.vercel.app
misode-misode.vercel.app

Please sign in to comment.