diff --git a/src/hooks/useCreateProjectFromTemplate.tsx b/src/hooks/useCreateProjectFromTemplate.tsx index fbef9e6fb..7d6086070 100644 --- a/src/hooks/useCreateProjectFromTemplate.tsx +++ b/src/hooks/useCreateProjectFromTemplate.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useState } from "react"; import yaml from "js-yaml"; import { useTranslation } from "react-i18next"; @@ -20,36 +20,7 @@ export const useCreateProjectFromTemplate = () => { const { findTemplateByAssetDirectory, getTemplateStorage } = useTemplatesStore(); const [isCreating, setIsCreating] = useState(false); - const [projectId, setProjectId] = useState(null); - const [templateFiles, setTemplateFiles] = useState>(); - - const { saveAllFiles } = useFileOperations(projectId || ""); - - useEffect(() => { - const getAndSaveFiles = async () => { - if (!projectId || !templateFiles) return; - - await saveAllFiles( - Object.fromEntries( - Object.entries(templateFiles).map(([path, content]) => [ - path, - new Uint8Array(new TextEncoder().encode(content)), - ]) - ) - ); - - addToast({ - message: tActions("projectCreatedSuccessfully"), - type: "success", - }); - - getProjectsList(); - navigate(`/projects/${projectId}`); - }; - - getAndSaveFiles(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [projectId, templateFiles]); + const { saveAllFiles } = useFileOperations(""); const createProjectFromTemplate = async (template: TemplateMetadata, projectName?: string) => { try { @@ -100,8 +71,23 @@ export const useCreateProjectFromTemplate = () => { }) ); - setProjectId(newProjectId); - setTemplateFiles(files); + await saveAllFiles( + Object.fromEntries( + Object.entries(files).map(([path, content]) => [ + path, + new Uint8Array(new TextEncoder().encode(content)), + ]) + ), + newProjectId + ); + + addToast({ + message: tActions("projectCreatedSuccessfully"), + type: "success", + }); + + getProjectsList(); + navigate(`/projects/${newProjectId}`); } catch (error) { addToast({ message: tActions("projectCreationFailed"), diff --git a/src/hooks/useFileOperations.ts b/src/hooks/useFileOperations.ts index 2c55155af..ef9e58f19 100644 --- a/src/hooks/useFileOperations.ts +++ b/src/hooks/useFileOperations.ts @@ -48,14 +48,15 @@ export function useFileOperations(projectId: string) { ); const saveAllFiles = useCallback( - async (files: Record) => { + async (files: Record, newProjectId?: string) => { + const affectedProjectId = newProjectId || projectId; const filesArray = Object.entries(files).map(([name, content]) => ({ name, content: new TextEncoder().encode(content.toString()), })); - await dbService.put(projectId, filesArray); - await ProjectsService.setResources(projectId, files); + await dbService.put(affectedProjectId, filesArray); + await ProjectsService.setResources(affectedProjectId, files); }, [projectId] );