Make Spyglass a singleton object

This commit is contained in:
Misode
2024-10-20 20:23:17 +02:00
parent d248732469
commit 9cb7f7297c
5 changed files with 40 additions and 52 deletions

View File

@@ -22,7 +22,7 @@ export function ErrorPanel({ error, prefix, reportable, onDismiss, body: body_,
const [stack, setStack] = useState<string | undefined>(undefined)
const gen = getGenerator(getCurrentUrl())
const source = gen ? spyglass?.getFile(spyglass.getUnsavedFileUri(gen)).doc?.getText() : undefined
const source = gen ? spyglass?.getFileContents(spyglass.getUnsavedFileUri(version, gen)) : undefined
const name = (prefix ?? '') + (error instanceof Error ? error.message : error)
useEffect(() => {

View File

@@ -21,7 +21,7 @@ interface Props {
export function SchemaGenerator({ gen, allowedVersions }: Props) {
const { locale } = useLocale()
const { version, changeVersion, changeTargetVersion } = useVersion()
const { spyglass, spyglassLoading } = useSpyglass()
const { spyglass } = useSpyglass()
const { projects, project, file, updateProject, updateFile, closeFile } = useProject()
const [error, setError] = useState<Error | string | null>(null)
const [errorBoundary, errorRetry] = useErrorBoundary()
@@ -34,17 +34,14 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
const uri = useMemo(() => {
// TODO: return different uri when project file is open
return spyglass?.getUnsavedFileUri(gen)
}, [spyglass, gen.id])
return spyglass?.getUnsavedFileUri(version, gen)
}, [spyglass, version, gen])
const [currentPreset, setCurrentPreset] = useSearchParam('preset')
const [sharedSnippetId, setSharedSnippetId] = useSearchParam(SHARE_KEY)
const ignoreChange = useRef(false)
const { value: docAndNode } = useAsync(async () => {
if (spyglassLoading || !spyglass || !uri) {
return AsyncCancel
}
let data: unknown = undefined
if (currentPreset && sharedSnippetId) {
setSharedSnippetId(undefined)
@@ -86,10 +83,10 @@ export function SchemaGenerator({ gen, allowedVersions }: Props) {
data = file.data
}
// TODO: if data is undefined, set to generator's default
const docAndNode = await spyglass.setFileContents(uri, data ? JSON.stringify(data) : undefined)
const docAndNode = await spyglass.setFileContents(version, uri, data ? JSON.stringify(data) : undefined)
Analytics.setGenerator(gen.id)
return docAndNode
}, [gen.id, version, sharedSnippetId, currentPreset, project.name, file?.id, spyglass, spyglassLoading])
}, [gen.id, version, sharedSnippetId, currentPreset, project.name, file?.id, spyglass])
const { doc } = docAndNode ?? {}

View File

@@ -1,7 +1,7 @@
import type { DocAndNode } from '@spyglassmc/core'
import { fileUtil } from '@spyglassmc/core'
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
import { useLocale } from '../../contexts/index.js'
import { useLocale, useVersion } from '../../contexts/index.js'
import { useDocAndNode } from '../../contexts/Spyglass.jsx'
import { useLocalStorage } from '../../hooks/index.js'
import { getSourceFormats, getSourceIndent, getSourceIndents, parseSource, sortData, stringifySource } from '../../services/index.js'
@@ -28,6 +28,7 @@ type SourcePanelProps = {
}
export function SourcePanel({ spyglass, docAndNode, doCopy, doDownload, doImport, copySuccess, onError }: SourcePanelProps) {
const { locale } = useLocale()
const { version } = useVersion()
const [indent, setIndent] = useState(Store.getIndent())
const [format, setFormat] = useState(Store.getFormat())
const [sort, setSort] = useLocalStorage('misode_output_sort', 'schema')
@@ -77,7 +78,7 @@ export function SourcePanel({ spyglass, docAndNode, doCopy, doDownload, doImport
if (!spyglass || !docAndNode) return
try {
const data = await parseSource(value, format)
await spyglass.setFileContents(docAndNode.doc.uri, JSON.stringify(data))
await spyglass.setFileContents(version, docAndNode.doc.uri, JSON.stringify(data))
} catch (e) {
if (e instanceof Error) {
e.message = `Error importing: ${e.message}`
@@ -88,7 +89,7 @@ export function SourcePanel({ spyglass, docAndNode, doCopy, doDownload, doImport
console.error(e)
}
}
}, [spyglass, docAndNode, text, indent, format, sort, highlighting])
}, [spyglass, version, docAndNode, text, indent, format, sort, highlighting])
useEffect(() => {
if (highlighting) {