Skip to content

Commit

Permalink
Merge pull request #180 from reorproject/save-reliability
Browse files Browse the repository at this point in the history
await save and add state to deal with file path changes
  • Loading branch information
samlhuillier committed Apr 3, 2024
2 parents 1900cc9 + d278ca5 commit 7b32eb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reor-project",
"version": "0.1.58",
"version": "0.1.59",
"productName": "Reor",
"main": "dist-electron/main/index.js",
"description": "An AI note-taking app that runs models locally.",
Expand Down
19 changes: 14 additions & 5 deletions src/components/File/hooks/use-file-by-filepath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const useFileByFilepath = () => {
const [noteToBeRenamed, setNoteToBeRenamed] = useState<string>("");
const [fileDirToBeRenamed, setFileDirToBeRenamed] = useState<string>("");
const [navigationHistory, setNavigationHistory] = useState<string[]>([]);
const [currentlyChangingFilePath, setCurrentlyChangingFilePath] =
useState(false);

const setFileNodeToBeRenamed = async (filePath: string) => {
const isDirectory = await window.files.isDirectory(filePath);
Expand Down Expand Up @@ -72,13 +74,18 @@ export const useFileByFilepath = () => {
const [debouncedEditor] = useDebounce(editor?.state.doc.content, 4000);

useEffect(() => {
if (debouncedEditor) {
saveEditorContentToPath(editor, currentlyOpenedFilePath, false);
if (debouncedEditor && !currentlyChangingFilePath) {
saveEditorContentToPath(editor, currentlyOpenedFilePath);
}
}, [debouncedEditor, currentlyOpenedFilePath, editor]);
}, [
debouncedEditor,
currentlyOpenedFilePath,
editor,
currentlyChangingFilePath,
]);

const saveCurrentlyOpenedFile = async () => {
saveEditorContentToPath(editor, currentlyOpenedFilePath);
await saveEditorContentToPath(editor, currentlyOpenedFilePath);
};

const saveEditorContentToPath = async (
Expand Down Expand Up @@ -106,11 +113,13 @@ export const useFileByFilepath = () => {
};

const openFileByPath = async (newFilePath: string) => {
saveEditorContentToPath(editor, currentlyOpenedFilePath, true);
setCurrentlyChangingFilePath(true);
await saveEditorContentToPath(editor, currentlyOpenedFilePath, true);
const fileContent = (await window.files.readFile(newFilePath)) ?? "";
setCurrentlyOpenedFilePath(newFilePath);

editor?.commands.setContent(fileContent);
setCurrentlyChangingFilePath(false);
};

// delete file depending on file path returned by the listener
Expand Down

0 comments on commit 7b32eb7

Please sign in to comment.