Skip to content

Commit e783b34

Browse files
committed
update close tab pop up logic
1 parent ec5c14a commit e783b34

5 files changed

Lines changed: 41 additions & 10 deletions

File tree

src/GraphArea.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function Graph({
4444
useEffect(() => active && instance && instance.setCurStatus(), [active && instance]);
4545
useEffect(() => {
4646
if (active && instance) dispatcher({ type: T.SET_CUR_INSTANCE, payload: instance });
47-
}, [active && instance]);
47+
if (instance) dispatcher({ type: T.SET_GRAPH_INSTANCE, payload: { graphID, instance } });
48+
}, [active, instance, graphID, dispatcher]);
4849

4950
useEffect(() => {
5051
if (ref.current) {

src/component/TabBar.jsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ const TabBar = ({ superState, dispatcher }) => {
1414
const [confirmOpen, setConfirmOpen] = useState(false);
1515
const [tabToClose, setTabToClose] = useState(null);
1616

17+
const closeTab = (i) => {
18+
localStorageManager.remove(superState.graphs[i] ? superState.graphs[i].graphID : null);
19+
dispatcher({ type: T.REMOVE_GRAPH, payload: i });
20+
if (!superState.curGraphIndex && superState.graphs.length === 1) {
21+
dispatcher({ type: T.SET_CUR_INSTANCE, payload: null });
22+
dispatcher({ type: T.SET_CUR_INDEX, payload: -1 });
23+
}
24+
};
25+
1726
const handleRequestCloseTab = (i, e) => {
1827
e.stopPropagation();
19-
setTabToClose(i);
20-
setConfirmOpen(true);
28+
const graph = superState.graphs[i];
29+
if (graph && graph.instance && !graph.instance.isSaved) {
30+
setTabToClose(i);
31+
setConfirmOpen(true);
32+
} else {
33+
closeTab(i);
34+
}
2135
};
2236

2337
const handleConfirmClose = () => {
24-
const i = tabToClose;
38+
closeTab(tabToClose);
2539
setConfirmOpen(false);
2640
setTabToClose(null);
27-
localStorageManager.remove(superState.graphs[i] ? superState.graphs[i].graphID : null);
28-
dispatcher({ type: T.REMOVE_GRAPH, payload: i });
29-
if (!superState.curGraphIndex && superState.graphs.length === 1) {
30-
dispatcher({ type: T.SET_CUR_INSTANCE, payload: null });
31-
dispatcher({ type: T.SET_CUR_INDEX, payload: -1 });
32-
}
3341
};
3442

3543
const handleCancelClose = () => {

src/graph-builder/graph-core/5-load-save.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class GraphLoadSave extends GraphUndoRedo {
1313
constructor(...args) {
1414
super(...args);
1515
this.autoSaveIntervalId = null;
16+
this.lastSavedActionIndex = 0;
17+
}
18+
19+
get isSaved() {
20+
return this.curActionIndex === this.lastSavedActionIndex;
1621
}
1722

1823
registerEvents() {
@@ -132,6 +137,7 @@ class GraphLoadSave extends GraphUndoRedo {
132137
fileHandle: handle,
133138
}]);
134139
this.dispatcher({ type: T.SET_FILE_STATE, payload: fS });
140+
this.lastSavedActionIndex = this.curActionIndex;
135141
toast.success('File saved Successfully');
136142
} catch (error) {
137143
// AbortError is silently ignored (user cancelled)
@@ -140,6 +146,7 @@ class GraphLoadSave extends GraphUndoRedo {
140146
// eslint-disable-next-line no-alert
141147
const fileName = prompt('Filename:');
142148
saveAs(blob, `${fileName || `${this.getName()}-concore`}.graphml`);
149+
this.lastSavedActionIndex = this.curActionIndex;
143150
toast.success('File saved Successfully');
144151
}
145152
}
@@ -172,6 +179,7 @@ class GraphLoadSave extends GraphUndoRedo {
172179
const stream = await handle.createWritable();
173180
await stream.write(blob);
174181
await stream.close();
182+
this.lastSavedActionIndex = this.curActionIndex;
175183
toast.success('File saved Successfully');
176184
} catch (error) {
177185
// AbortError is silently ignored (user cancelled)
@@ -213,6 +221,7 @@ class GraphLoadSave extends GraphUndoRedo {
213221
graphMLParser(graphML).then((graphObject) => {
214222
localStorageManager.save(this.id, graphObject);
215223
this.loadGraphFromLocalStorage();
224+
this.lastSavedActionIndex = this.curActionIndex;
216225
});
217226
}
218227

@@ -225,6 +234,10 @@ class GraphLoadSave extends GraphUndoRedo {
225234
const graphContent = localStorageManager.get(this.id);
226235
if (!graphContent) return false;
227236
this.loadJson(graphContent);
237+
// If loaded from localStorage, assume UNSAVED unless actions are 0.
238+
// Actually, loadJson sets curActionIndex based on history.
239+
// If we want to support recovering unsaved work, we should leave lastSavedActionIndex as 0 (unless graph is empty and curActionIndex is 0).
240+
if (this.curActionIndex === 0) this.lastSavedActionIndex = 0;
228241
return true;
229242
}
230243

src/reducer/actionType.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const actionType = {
4444
SET_FUNCTIONS: 'SET_FUNCTIONS',
4545
SET_LOGS: 'SET_LOGS',
4646
SET_LOGS_MESSAGE: 'SET_LOGS_MESSAGE',
47+
SET_GRAPH_INSTANCE: 'SET_GRAPH_INSTANCE',
4748
};
4849

4950
export default zealit(actionType);

src/reducer/reducer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ const reducer = (state, action) => {
243243
return { ...newState };
244244
}
245245

246+
case T.SET_GRAPH_INSTANCE: {
247+
const newState = { ...state };
248+
newState.graphs = newState.graphs.map((g) => (
249+
g.graphID === action.payload.graphID ? { ...g, instance: action.payload.instance } : g
250+
));
251+
return { ...newState };
252+
}
253+
246254
default:
247255
return state;
248256
}

0 commit comments

Comments
 (0)