fix: missing media files when renaming + saving the project
This commit is contained in:
+52
-31
@@ -2,58 +2,79 @@ import { KGProjectStorage, DuplicateEntryError } from '../core/io/KGProjectStora
|
||||
import { KGCore } from '../core/KGCore';
|
||||
|
||||
/**
|
||||
* Save project utility function
|
||||
* Handles saving the current project with proper error handling and user confirmation
|
||||
* @param projectName - The name of the project to save
|
||||
* @param setStatus - Function to update the status message
|
||||
* @returns Promise<boolean> - Returns true if save was successful, false otherwise
|
||||
* Save project utility function.
|
||||
* Detects renames (savedProjectName !== projectName) and migrates the OPFS folder,
|
||||
* including media files. Duplicate name conflicts during rename auto-resolve with
|
||||
* the {name} (1), {name} (2), ... pattern.
|
||||
*
|
||||
* @param projectName Current in-memory project name
|
||||
* @param savedProjectName OPFS folder name the project was last saved under
|
||||
* @param setStatus Function to update the status message
|
||||
* @param onSaveSuccess Called with the final saved name on success
|
||||
*/
|
||||
export const saveProject = async (
|
||||
projectName: string,
|
||||
setStatus: (status: string) => void
|
||||
savedProjectName: string,
|
||||
setStatus: (status: string) => void,
|
||||
onSaveSuccess: (finalName: string) => void,
|
||||
): Promise<boolean> => {
|
||||
const storage = KGProjectStorage.getInstance();
|
||||
const isRename = savedProjectName !== projectName;
|
||||
|
||||
if (isRename) {
|
||||
// Determine the target name, resolving conflicts automatically
|
||||
let targetName = projectName;
|
||||
if (await storage.exists(projectName)) {
|
||||
targetName = await storage.resolveUniqueName(projectName);
|
||||
}
|
||||
|
||||
try {
|
||||
await storage.saveWithRename(
|
||||
savedProjectName,
|
||||
targetName,
|
||||
KGCore.instance().getCurrentProject(),
|
||||
);
|
||||
|
||||
const statusMsg =
|
||||
targetName !== projectName
|
||||
? `Project renamed to "${targetName}" and saved`
|
||||
: `Project "${targetName}" has been saved`;
|
||||
setStatus(statusMsg);
|
||||
onSaveSuccess(targetName);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error saving renamed project:', error);
|
||||
window.alert(`An error occurred while saving: ${error}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Same name — existing overwrite logic
|
||||
try {
|
||||
await storage.save(
|
||||
projectName,
|
||||
KGCore.instance().getCurrentProject(),
|
||||
false,
|
||||
);
|
||||
|
||||
await storage.save(projectName, KGCore.instance().getCurrentProject(), false);
|
||||
setStatus(`Project "${projectName}" has been saved`);
|
||||
console.log("project saved successfully");
|
||||
onSaveSuccess(projectName);
|
||||
return true;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error saving project:", error);
|
||||
|
||||
if (error instanceof DuplicateEntryError) {
|
||||
const confirmed = window.confirm(`Project "${projectName}" already exists. Do you want to overwrite it?`);
|
||||
|
||||
const confirmed = window.confirm(
|
||||
`Project "${projectName}" already exists. Do you want to overwrite it?`,
|
||||
);
|
||||
if (confirmed) {
|
||||
try {
|
||||
await storage.save(
|
||||
projectName,
|
||||
KGCore.instance().getCurrentProject(),
|
||||
true,
|
||||
);
|
||||
|
||||
await storage.save(projectName, KGCore.instance().getCurrentProject(), true);
|
||||
setStatus(`Project "${projectName}" has been saved`);
|
||||
console.log("project saved successfully after overwrite");
|
||||
onSaveSuccess(projectName);
|
||||
return true;
|
||||
|
||||
} catch (overwriteError) {
|
||||
console.error("Error overwriting project:", overwriteError);
|
||||
console.error('Error overwriting project:', overwriteError);
|
||||
window.alert(`An error occurred while overwriting the project: ${overwriteError}`);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// User cancelled the overwrite
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
console.error("Error saving project:", error);
|
||||
console.error('Error saving project:', error);
|
||||
window.alert(`An unknown error ${error} occurred. Please try again.`);
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user