Skip to content

Commit

Permalink
fix: tab open incosistency
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulyadav-57 committed Sep 18, 2024
1 parent 2fc5e49 commit 8b32a17
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/components/workspace/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ const Editor: FC<Props> = ({ className = '' }) => {
}
};

const updateFileSaveCounter = () => {
setSaveFileCounter((prev) => prev + 1);
};

useEffect(() => {
async function loadMonacoEditor() {
const monaco = await import('monaco-editor');
Expand Down Expand Up @@ -115,9 +119,7 @@ const Editor: FC<Props> = ({ className = '' }) => {

useEffect(() => {
if (!isLoaded) return;
EventEmitter.on('SAVE_FILE', () => {
setSaveFileCounter((prev) => prev + 1);
});
EventEmitter.on('SAVE_FILE', updateFileSaveCounter);

// If file is changed e.g. in case of build process then force update in editor
EventEmitter.on('FORCE_UPDATE_FILE', (filePath: string) => {
Expand All @@ -132,7 +134,7 @@ const Editor: FC<Props> = ({ className = '' }) => {
});
});
return () => {
EventEmitter.off('SAVE_FILE');
EventEmitter.off('SAVE_FILE', updateFileSaveCounter);
EventEmitter.off('FORCE_UPDATE_FILE');
};
}, [isLoaded]);
Expand Down
5 changes: 4 additions & 1 deletion src/components/workspace/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const Tabs: FC = () => {

useEffect(() => {
EventEmitter.on('FILE_SAVED', onFileSave);
}, []);
return () => {
EventEmitter.off('FILE_SAVED', onFileSave);
};
}, [updateFileDirty]);

if (fileTab.items.length === 0) {
return <></>;
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/fileTabs.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fileSystem from '@/lib/fs';
import { IDEContext, IFileTab } from '@/state/IDE.context';
import EventEmitter from '@/utility/eventEmitter';
import cloneDeep from 'lodash.clonedeep';
import { useContext } from 'react';

const useFileTab = () => {
Expand Down Expand Up @@ -39,7 +40,7 @@ const useFileTab = () => {
...parsedSetting,
};
}
setFileTab(parsedSetting.tab);
setFileTab(cloneDeep(parsedSetting.tab));

await fileSystem.writeFile(
settingPath,
Expand Down Expand Up @@ -102,12 +103,11 @@ const useFileTab = () => {
updatedTab = { items: updatedItems, active: newActiveTab };
}

setFileTab(updatedTab);
syncTabSettings(updatedTab);
};

const updateFileDirty = (filePath: string, isDirty: boolean) => {
const updatedItems = fileTab.items.map((item) => {
const updatedItems = cloneDeep(fileTab).items.map((item) => {
if (item.path === filePath) {
return { ...item, isDirty: isDirty };
}
Expand Down

0 comments on commit 8b32a17

Please sign in to comment.