'use client' import type { SceneGraph } from '@pascal-app/editor' import { useRouter } from 'next/navigation' import { useCallback, useState } from 'react' import { withPascalBasePath } from '@/lib/pascal-base-path' const EMPTY_GRAPH: SceneGraph = { nodes: {}, rootNodeIds: [], } interface SaveButtonProps { sceneId: string name: string version: number getGraph: () => SceneGraph | null } /** * Creates a new empty scene and navigates the user to it. */ export function CreateSceneButton({ label = '新建场景' }: { label?: string } = {}) { const router = useRouter() const [isCreating, setIsCreating] = useState(false) const [error, setError] = useState(null) const handleCreate = useCallback(async () => { setIsCreating(true) setError(null) try { const response = await fetch(withPascalBasePath('/api/scenes'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: '未命名场景', graph: EMPTY_GRAPH }), }) if (!response.ok) { setError(`创建场景失败 (${response.status})`) return } const meta = (await response.json()) as { id: string } router.push(`/scene/${meta.id}`) } catch (err) { setError(err instanceof Error ? err.message : '创建场景失败') } finally { setIsCreating(false) } }, [router]) return (
{error && {error}}
) } /** * Save + Save-as buttons that call the scenes API directly. * Used for UIs that want explicit save controls outside of the Editor's * built-in autosave plumbing. */ export function SaveButton({ sceneId, name, version, getGraph }: SaveButtonProps) { const router = useRouter() const [isSaving, setIsSaving] = useState(false) const [status, setStatus] = useState(null) const handleSave = useCallback(async () => { const graph = getGraph() if (!graph) { setStatus('没有可保存的场景') return } setIsSaving(true) setStatus(null) try { const response = await fetch(withPascalBasePath(`/api/scenes/${sceneId}`), { method: 'PUT', headers: { 'Content-Type': 'application/json', 'If-Match': String(version), }, body: JSON.stringify({ name, graph }), }) if (response.status === 409) { setStatus('保存冲突,请刷新后继续') return } if (!response.ok) { setStatus(`保存失败 (${response.status})`) return } setStatus('已保存') } catch (error) { setStatus(error instanceof Error ? error.message : '保存失败') } finally { setIsSaving(false) } }, [getGraph, name, sceneId, version]) const handleSaveAs = useCallback(async () => { const graph = getGraph() if (!graph) { setStatus('没有可保存的场景') return } const newName = typeof window !== 'undefined' ? window.prompt('请输入新场景名称', name) : null if (!newName) return setIsSaving(true) setStatus(null) try { const response = await fetch(withPascalBasePath('/api/scenes'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newName, graph }), }) if (!response.ok) { setStatus(`另存为失败 (${response.status})`) return } const meta = (await response.json()) as { id: string } router.push(`/scene/${meta.id}`) } catch (error) { setStatus(error instanceof Error ? error.message : '另存为失败') } finally { setIsSaving(false) } }, [getGraph, name, router]) return (
{status && {status}}
) }