Skip to content

Commit 4690f6f

Browse files
committed
persist recently closed files on browser refresh
1 parent 26bd462 commit 4690f6f

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/packages/frontend/project/open-files.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ The point of this code here is ensure that these objects stay in sync properly.
3030

3131
import { List, Map } from "immutable";
3232
import { close } from "@cocalc/util/misc";
33+
import {
34+
local_storage,
35+
local_storage_delete,
36+
} from "@cocalc/frontend/editor-local-storage";
3337
import { ProjectActions } from "../project_actions";
3438
import { ProjectStore } from "../project_store";
3539

@@ -38,6 +42,7 @@ type OpenFilesOrderType = List<string>;
3842
type ClosedFilesType = List<string>;
3943

4044
const MAX_JUST_CLOSED_FILES = 50;
45+
const JUST_CLOSED_STORAGE_FILE = "__just_closed_files__";
4146

4247
export class OpenFiles {
4348
private actions: ProjectActions;
@@ -48,6 +53,7 @@ export class OpenFiles {
4853
const store = actions.get_store();
4954
if (store == null) throw Error("store must be defined");
5055
this.store = store;
56+
this.restoreClosedFiles();
5157
}
5258

5359
public close(): void {
@@ -64,10 +70,46 @@ export class OpenFiles {
6470
if (open_files_order != null) x.open_files_order = open_files_order;
6571
if (just_closed_files != null) {
6672
x.just_closed_files = just_closed_files;
73+
this.persistClosedFiles(just_closed_files);
6774
}
6875
this.actions.setState(x);
6976
}
7077

78+
private restoreClosedFiles(): void {
79+
const current = this.store.get("just_closed_files");
80+
if (current != null && current.size > 0) return;
81+
const stored = local_storage(
82+
this.actions.project_id,
83+
JUST_CLOSED_STORAGE_FILE,
84+
"list",
85+
);
86+
if (!Array.isArray(stored) || stored.length == 0) return;
87+
const list = List(stored.filter((path) => typeof path === "string")).slice(
88+
-MAX_JUST_CLOSED_FILES,
89+
);
90+
if (list.size == 0) return;
91+
this.actions.setState({ just_closed_files: list });
92+
}
93+
94+
private persistClosedFiles(just_closed_files: ClosedFilesType): void {
95+
const stored = just_closed_files.toArray();
96+
97+
if (stored.length == 0) {
98+
local_storage_delete(
99+
this.actions.project_id,
100+
JUST_CLOSED_STORAGE_FILE,
101+
"list",
102+
);
103+
return;
104+
}
105+
local_storage(
106+
this.actions.project_id,
107+
JUST_CLOSED_STORAGE_FILE,
108+
"list",
109+
stored,
110+
);
111+
}
112+
71113
public close_all(): void {
72114
this.setState(Map({}), List([]));
73115
}
@@ -129,6 +171,10 @@ export class OpenFiles {
129171
}
130172
}
131173

174+
public set_closed_files(just_closed_files: ClosedFilesType): void {
175+
this.setState(undefined, undefined, just_closed_files);
176+
}
177+
132178
public get(path: string, key: string): any {
133179
return this.store.getIn(["open_files", path, key]);
134180
}

src/packages/frontend/project_actions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,9 +2866,11 @@ export class ProjectActions extends Actions<ProjectStoreState> {
28662866
}
28672867

28682868
public clear_just_closed_files() {
2869-
this.setState({
2870-
just_closed_files: List([]),
2871-
});
2869+
if (this.open_files != null) {
2870+
this.open_files.set_closed_files(List([]));
2871+
return;
2872+
}
2873+
this.setState({ just_closed_files: List([]) });
28722874
}
28732875

28742876
showComputeServers = () => {};

0 commit comments

Comments
 (0)