Skip to content

Commit 4d92592

Browse files
committed
Remove unused files state
1 parent 035e1ec commit 4d92592

File tree

1 file changed

+18
-82
lines changed

1 file changed

+18
-82
lines changed

packages/web/src/components/apps/use-files.tsx

+18-82
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import React, {
2-
createContext,
3-
useCallback,
4-
useContext,
5-
useEffect,
6-
useReducer,
7-
useRef,
8-
useState,
9-
} from 'react';
1+
import React, { createContext, useCallback, useContext, useReducer, useRef, useState } from 'react';
102

11-
import type {
12-
FilePayloadType,
13-
FileType,
14-
DirEntryType,
15-
FileEntryType,
16-
AppType,
17-
} from '@srcbook/shared';
3+
import type { FileType, DirEntryType, FileEntryType, AppType } from '@srcbook/shared';
184
import { AppChannel } from '@/clients/websocket';
195
import {
206
createFile as doCreateFile,
@@ -35,23 +21,7 @@ import {
3521
updateFileNode,
3622
} from './lib/file-tree';
3723

38-
function removeCachedFiles(
39-
files: Record<string, FileType>,
40-
path: string,
41-
): Record<string, FileType> {
42-
const result: Record<string, FileType> = {};
43-
44-
for (const key of Object.keys(files)) {
45-
if (!key.startsWith(path)) {
46-
result[key] = files[key]!;
47-
}
48-
}
49-
50-
return result;
51-
}
52-
5324
export interface FilesContextValue {
54-
files: FileType[];
5525
fileTree: DirEntryType;
5626
openFile: (entry: FileEntryType) => void;
5727
createFile: (dirname: string, basename: string, source?: string) => void;
@@ -85,25 +55,14 @@ export function FilesProvider({ app, channel, rootDirEntries, children }: Provid
8555
//
8656
const [, forceComponentRerender] = useReducer((x) => x + 1, 0);
8757

88-
const filesRef = useRef<Record<string, FileType>>({});
8958
const fileTreeRef = useRef<DirEntryType>(sortTree(rootDirEntries));
9059
const openedDirectoriesRef = useRef<Set<string>>(new Set());
9160

9261
const [openedFile, setOpenedFile] = useState<FileType | null>(null);
9362

94-
useEffect(() => {
95-
function onFile(payload: FilePayloadType) {
96-
filesRef.current[payload.file.path] = payload.file;
97-
forceComponentRerender();
98-
}
99-
channel.on('file', onFile);
100-
return () => channel.off('file', onFile);
101-
}, [channel, forceComponentRerender]);
102-
10363
const openFile = useCallback(
10464
async (entry: FileEntryType) => {
10565
const { data: file } = await loadFile(app.id, entry.path);
106-
filesRef.current[file.path] = file;
10766
setOpenedFile(file);
10867
},
10968
[app.id],
@@ -123,8 +82,8 @@ export function FilesProvider({ app, channel, rootDirEntries, children }: Provid
12382
const updateFile = useCallback(
12483
(file: FileType, attrs: Partial<FileType>) => {
12584
const updatedFile: FileType = { ...file, ...attrs };
126-
filesRef.current[file.path] = updatedFile;
12785
channel.push('file:updated', { file: updatedFile });
86+
setOpenedFile(updatedFile);
12887
forceComponentRerender();
12988
},
13089
[channel],
@@ -133,7 +92,6 @@ export function FilesProvider({ app, channel, rootDirEntries, children }: Provid
13392
const deleteFile = useCallback(
13493
async (entry: FileEntryType) => {
13594
await doDeleteFile(app.id, entry.path);
136-
delete filesRef.current[entry.path];
13795
setOpenedFile((openedFile) => {
13896
if (openedFile && openedFile.path === entry.path) {
13997
return null;
@@ -143,28 +101,19 @@ export function FilesProvider({ app, channel, rootDirEntries, children }: Provid
143101
fileTreeRef.current = deleteNode(fileTreeRef.current, entry.path);
144102
forceComponentRerender(); // required
145103
},
146-
[app.id],
104+
[app.id, openedFile],
147105
);
148106

149107
const renameFile = useCallback(
150108
async (entry: FileEntryType, name: string) => {
151109
const { data: newEntry } = await doRenameFile(app.id, entry.path, name);
152-
const oldFile = filesRef.current[entry.path];
153-
if (oldFile) {
154-
const newFile = { ...oldFile, path: newEntry.path, name: newEntry.name };
155-
delete filesRef.current[oldFile.path];
156-
filesRef.current[newFile.path] = newFile;
157-
setOpenedFile((openedFile) => {
158-
if (openedFile && openedFile.path === oldFile.path) {
159-
return newFile;
160-
}
161-
return openedFile;
162-
});
110+
if (openedFile && openedFile.path === entry.path) {
111+
setOpenedFile({ ...openedFile, path: newEntry.path, name: newEntry.name });
163112
}
164113
fileTreeRef.current = updateFileNode(fileTreeRef.current, entry, newEntry);
165114
forceComponentRerender(); // required
166115
},
167-
[app.id],
116+
[app.id, openedFile],
168117
);
169118

170119
const isFolderOpen = useCallback((entry: DirEntryType) => {
@@ -212,50 +161,37 @@ export function FilesProvider({ app, channel, rootDirEntries, children }: Provid
212161
const deleteFolder = useCallback(
213162
async (entry: DirEntryType) => {
214163
await deleteDirectory(app.id, entry.path);
215-
removeCachedFiles(filesRef.current, entry.path);
216-
setOpenedFile((openedFile) => {
217-
if (openedFile && openedFile.path.startsWith(entry.path)) {
218-
return null;
219-
}
220-
return openedFile;
221-
});
164+
if (openedFile && openedFile.path.startsWith(entry.path)) {
165+
setOpenedFile(null);
166+
}
222167
openedDirectoriesRef.current.delete(entry.path);
223168
fileTreeRef.current = deleteNode(fileTreeRef.current, entry.path);
224169
forceComponentRerender(); // required
225170
},
226-
[app.id],
171+
[app.id, openedFile],
227172
);
228173

229174
const renameFolder = useCallback(
230175
async (entry: DirEntryType, name: string) => {
231176
const { data: newEntry } = await renameDirectory(app.id, entry.path, name);
232-
// const oldFile = filesRef.current[entry.path];
233-
// if (oldFile) {
234-
// const newFile = { ...oldFile, path: newEntry.path, name: newEntry.name };
235-
// delete filesRef.current[oldFile.path];
236-
// filesRef.current[newFile.path] = newFile;
237-
// setOpenedFile((openedFile) => {
238-
// if (openedFile && openedFile.path === oldFile.path) {
239-
// return newFile;
240-
// }
241-
// return openedFile;
242-
// });
243-
// }
177+
178+
if (openedFile && openedFile.path.startsWith(entry.path)) {
179+
setOpenedFile({ ...openedFile, path: openedFile.path.replace(entry.path, newEntry.path) });
180+
}
181+
244182
if (openedDirectoriesRef.current.has(entry.path)) {
245183
openedDirectoriesRef.current.delete(entry.path);
246184
openedDirectoriesRef.current.add(newEntry.path);
247185
}
248186

249187
fileTreeRef.current = renameDirNode(fileTreeRef.current, entry, newEntry);
188+
250189
forceComponentRerender(); // required
251190
},
252-
[app.id],
191+
[app.id, openedFile],
253192
);
254193

255-
const files = Object.values(filesRef.current);
256-
257194
const context: FilesContextValue = {
258-
files,
259195
fileTree: fileTreeRef.current,
260196
openFile,
261197
renameFile,

0 commit comments

Comments
 (0)