Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 08ebaa4

Browse files
committedJul 9, 2024·
feat: add onGetFolderAndFileCounts for folder and file coutns
1 parent 05853c0 commit 08ebaa4

File tree

7 files changed

+137
-13
lines changed

7 files changed

+137
-13
lines changed
 

‎client/components/home-screen/create-project.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,11 @@ export default function CreateProjectBtn() {
6969
}
7070

7171
function checkProjectNameAvailability(value: string) {
72-
console.log("value", value);
73-
7472
const isAlreadyExits = userCreatedProjectsList.some(
7573
(project) =>
7674
project.projectName.trim().toLowerCase() === value.trim().toLowerCase()
7775
);
7876

79-
console.log("result", isAlreadyExits);
80-
8177
return isAlreadyExits;
8278
}
8379

‎client/components/home-screen/login-socket.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
"use client";
22

33
import useSocket from "@/hooks/useSocket";
4+
import { SOCKET_ENUMS } from "@/lib/constants";
5+
import { FileFolderCounts } from "@/types/project";
46
import { useEffect } from "react";
7+
import { useStore } from "../store/useStore";
58

69
export default function LoginSocket() {
710
// initialize socket connection to server
811
const socket = useSocket();
12+
const { userCreatedProjectsList, updateCreatingProjectItem } = useStore(
13+
(state) => state
14+
);
15+
16+
const onGetFolderAndFileCounts = ({
17+
folderFileCounts,
18+
}: {
19+
folderFileCounts: { projectId: string; counts: FileFolderCounts }[];
20+
}) => {
21+
console.log("got ", folderFileCounts);
22+
};
923

1024
useEffect(() => {
1125
if (!socket) return;
26+
27+
if (socket) {
28+
socket.emit(SOCKET_ENUMS.GET_FOLDER_AND_FILE_COUNTS, {
29+
projectIds: userCreatedProjectsList.map((project) => project.projectId),
30+
});
31+
32+
socket.on(
33+
SOCKET_ENUMS.GET_FOLDER_AND_FILE_COUNTS,
34+
onGetFolderAndFileCounts
35+
);
36+
}
37+
38+
return () => {
39+
socket?.off(
40+
SOCKET_ENUMS.GET_FOLDER_AND_FILE_COUNTS,
41+
onGetFolderAndFileCounts
42+
);
43+
};
1244
}, [socket]);
1345

1446
return null;

‎client/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const SOCKET_ENUMS = {
1212

1313
// file
1414
FILE_CONTENT_CHANGED: "file-content-changed",
15+
GET_FOLDER_AND_FILE_COUNTS: "get-folder-file-counts",
1516

1617
// project
1718
CREATE_PROJECT: "create-project",

‎server/socket/listeners/files-listener.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { SocketType } from "../../types/socket";
22
import { SOCKET_ENUMS } from "../../utils/constants";
3-
import { updateFileContentToProject } from "../../utils/project-structure-utils";
3+
import {
4+
getFoldersAndFilesCount,
5+
updateFileContentToProject,
6+
} from "../../utils/project-structure-utils";
47
import { userProjects, userSockets } from "../socket";
58

69
const { FILE_CONTENT_CHANGED } = SOCKET_ENUMS;
10+
const { GET_FOLDER_AND_FILE_COUNTS } = SOCKET_ENUMS;
711

812
const onFileContentChanged = ({
913
socket,
@@ -41,4 +45,35 @@ const onFileContentChanged = ({
4145
});
4246
};
4347

44-
export { onFileContentChanged };
48+
const onGetFolderAndFileCounts = ({
49+
socket,
50+
projectIds,
51+
}: {
52+
socket: SocketType;
53+
projectIds: string[];
54+
}) => {
55+
if (!projectIds) return;
56+
57+
let folderFileCounts: {}[] = [];
58+
projectIds.forEach((projectId) => {
59+
// check if the project with this projectIds exits or not
60+
if (!userProjects[projectId]) return;
61+
62+
// now calculate counts of folders and files
63+
const { status, count } = getFoldersAndFilesCount(
64+
userProjects[projectId].structure
65+
);
66+
67+
if (!status) return;
68+
69+
// add them into array
70+
folderFileCounts.push({ projectId: projectId, count: count });
71+
});
72+
73+
// console.log("folderFileCounts", folderFileCounts);
74+
75+
// give back the counts to user
76+
socket.emit(GET_FOLDER_AND_FILE_COUNTS, { folderFileCounts });
77+
};
78+
79+
export { onFileContentChanged, onGetFolderAndFileCounts };

‎server/socket/socket.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { IoType, SocketType, UserSockets } from "../types/socket";
33
import { UserProjects } from "../types/project";
44
import { onDisconnect, onLogin } from "./listeners/users-listener";
55
import { onNewMessageSend } from "./listeners/chat-listener";
6-
import { onFileContentChanged } from "./listeners/files-listener";
6+
import {
7+
onFileContentChanged,
8+
onGetFolderAndFileCounts,
9+
} from "./listeners/files-listener";
710
import {
811
onProjectItemCreated,
912
onProjectItemDeleted,
@@ -21,6 +24,7 @@ const {
2124
LOGIN,
2225
DISCONNECT,
2326
FILE_CONTENT_CHANGED,
27+
GET_FOLDER_AND_FILE_COUNTS,
2428
CREATE_PROJECT,
2529
DELETE_PROJECT,
2630
JOIN_PROJECT,
@@ -33,7 +37,51 @@ const {
3337
} = SOCKET_ENUMS;
3438

3539
let userSockets: UserSockets = {};
36-
let userProjects: UserProjects = {};
40+
let userProjects: UserProjects = {
41+
project1: {
42+
owner: "",
43+
projectId: "project1",
44+
projectName: "project1",
45+
joinedUsers: [],
46+
structure: {
47+
id: ":root",
48+
name: "root",
49+
type: "folder",
50+
files: [
51+
{
52+
id: "file1",
53+
name: "file2",
54+
type: "file",
55+
},
56+
],
57+
subFolders: [],
58+
},
59+
},
60+
project2: {
61+
owner: "",
62+
projectId: "project2",
63+
projectName: "project2",
64+
joinedUsers: [],
65+
structure: {
66+
id: ":root",
67+
name: "root",
68+
type: "folder",
69+
files: [
70+
{
71+
id: "file1",
72+
name: "file1",
73+
type: "file",
74+
},
75+
{
76+
id: "file2",
77+
name: "file2",
78+
type: "file",
79+
},
80+
],
81+
subFolders: [],
82+
},
83+
},
84+
};
3785

3886
const ioListener = (socket: SocketType, io: IoType) => {
3987
const createSocketHandler = (eventName: string, handler: Function) => {
@@ -80,6 +128,9 @@ const ioListener = (socket: SocketType, io: IoType) => {
80128
// on file content changes in project
81129
createSocketHandler(FILE_CONTENT_CHANGED, onFileContentChanged);
82130

131+
// to give folder and files counts of projects
132+
createSocketHandler(GET_FOLDER_AND_FILE_COUNTS, onGetFolderAndFileCounts);
133+
83134
// chat
84135
createSocketHandler(SEND_MESSAGE, onNewMessageSend);
85136
};

‎server/utils/constants.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const SOCKET_ENUMS = {
1212

1313
// file
1414
FILE_CONTENT_CHANGED: "file-content-changed",
15+
GET_FOLDER_AND_FILE_COUNTS: "get-folder-file-counts",
1516

1617
// project
1718
CREATE_PROJECT: "create-project",
@@ -31,6 +32,12 @@ export const DEFAULT_PROJECT_STRUCTURE: ProjectStructure = {
3132
id: ":root",
3233
name: "root",
3334
type: "folder",
34-
files: [],
35+
files: [
36+
{
37+
id: "index.ts",
38+
name: "index.ts",
39+
type: "file",
40+
},
41+
],
3542
subFolders: [],
3643
};

‎server/utils/project-structure-utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,21 @@ const getFoldersAndFilesCount = (project: ProjectStructure) => {
206206
let foldersCount = 0;
207207

208208
const countFolderAndFiles = (folder: FolderInterface): boolean => {
209-
if (folder.id !== ":root") foldersCount++;
209+
console.log("folder", folder.id);
210+
if (folder?.id !== ":root") foldersCount++;
210211

211-
if (folder.files) {
212+
if (folder?.files) {
212213
filesCount += folder.files.length;
213214
}
214215

215-
if (!folder.subFolders) return false;
216+
if (!folder?.subFolders) return false;
216217
for (const subFolder of folder.subFolders) {
217218
if (countFolderAndFiles(subFolder)) {
218219
return true;
219220
}
220221
}
221-
return false;
222+
223+
return true;
222224
};
223225

224226
const success = countFolderAndFiles(project);

0 commit comments

Comments
 (0)
Please sign in to comment.