import React from "react"; import { createProject } from "@pien-studio/editor-core"; import { upsertProject } from "@pien-studio/storage"; type Translator = (key: string, params?: Record) => string; type UseEditorProjectLifecycleOptions = { projectId: string; hydrate: () => void; loadProjectById: (projectId: string) => Promise; setProject: (project: ReturnType) => void; t: Translator; }; export function useEditorProjectLifecycle(options: UseEditorProjectLifecycleOptions) { const { projectId, hydrate, loadProjectById, setProject, t } = options; const initializedProjectId = React.useRef(null); React.useEffect(() => { hydrate(); }, [hydrate]); React.useEffect(() => { if (initializedProjectId.current === projectId) return; initializedProjectId.current = projectId; if (projectId === "new") { const nextProject = createProject(t("home.untitledProject")); setProject(nextProject); void upsertProject(nextProject); return; } void loadProjectById(projectId); }, [loadProjectById, projectId, setProject, t]); }