Skip to content

Commit 1a6a0a4

Browse files
authored
Merge pull request #10 from code-kern-ai/websocket-change
Prep for rework including useWebsocket hook
2 parents 7e19b01 + f0e38ce commit 1a6a0a4

File tree

39 files changed

+255
-499
lines changed

39 files changed

+255
-499
lines changed

src/components/models-download/ModelsDownload.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import LoadingIcon from "../shared/loading/LoadingIcon";
1111
import { openModal, setModalStates } from "@/src/reduxStore/states/modal";
1212
import { ModalEnum } from "@/src/types/shared/modal";
1313
import { selectIsManaged } from "@/src/reduxStore/states/general";
14-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
1514
import { CurrentPage } from "@/src/types/shared/general";
1615
import { timer } from "rxjs";
17-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
1816
import { TOOLTIPS_DICT } from "@/src/util/tooltip-constants";
1917
import AddModelDownloadModal from "./AddModelDownloadModal";
2018
import DeleteModelDownloadModal from "./DeleteModelDownloadModal";
19+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
2120

2221
export default function ModelsDownload() {
2322
const router = useRouter();
@@ -28,14 +27,8 @@ export default function ModelsDownload() {
2827

2928
const [refetchModelsDownload] = useLazyQuery(GET_MODEL_PROVIDER_INFO, { fetchPolicy: 'network-only', nextFetchPolicy: 'cache-first' });
3029

31-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.MODELS_DOWNLOAD]), []);
32-
3330
useEffect(() => {
3431
refetchModels();
35-
WebSocketsService.subscribeToNotification(CurrentPage.MODELS_DOWNLOAD, {
36-
whitelist: ['model_provider_download'],
37-
func: handleWebsocketNotification
38-
});
3932
}, []);
4033

4134
function refetchModels() {
@@ -53,9 +46,7 @@ export default function ModelsDownload() {
5346
}
5447
}, []);
5548

56-
useEffect(() => {
57-
WebSocketsService.updateFunctionPointer(null, CurrentPage.MODELS_DOWNLOAD, handleWebsocketNotification)
58-
}, [handleWebsocketNotification]);
49+
useWebsocket(CurrentPage.MODELS_DOWNLOAD, handleWebsocketNotification);
5950

6051
return (<div className="p-4 bg-gray-100 flex-1 flex flex-col h-[calc(100vh-4rem)] overflow-y-auto">
6152
<div className="flex flex-row items-center">

src/components/projects/ButtonsContainer.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import { useCallback, useEffect, useState } from "react";
99
import { useDispatch, useSelector } from "react-redux"
1010
import ModalUpload from "../shared/upload/ModalUpload";
1111
import SampleProjectsDropdown from "./SampleProjectsDropdown";
12-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
13-
import { CurrentPage } from "@/src/types/shared/general";
14-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
12+
import { CurrentPage, CurrentPageSubKey } from "@/src/types/shared/general";
13+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
1514

1615
const BASE_OPTIONS = { reloadOnFinish: false, deleteProjectOnFail: true, closeModalOnClick: true, isModal: true, navigateToProject: true, showBadPasswordMsg: null };
1716

@@ -24,15 +23,6 @@ export default function ButtonsContainer() {
2423
const [uploadOptions, setUploadOptions] = useState<UploadOptions>(BASE_OPTIONS);
2524
const [showBadPasswordMsg, setShowBadPasswordMsg] = useState(false);
2625

27-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.PROJECTS]), []);
28-
29-
useEffect(() => {
30-
WebSocketsService.subscribeToNotification(CurrentPage.PROJECTS, {
31-
whitelist: ['bad_password'],
32-
func: handleWebsocketNotification
33-
});
34-
}, []);
35-
3626
useEffect(() => {
3727
setUploadOptions({ ...BASE_OPTIONS, showBadPasswordMsg: showBadPasswordMsg });
3828
}, [showBadPasswordMsg]);
@@ -43,9 +33,7 @@ export default function ButtonsContainer() {
4333
}
4434
}, []);
4535

46-
useEffect(() => {
47-
WebSocketsService.updateFunctionPointer(null, CurrentPage.PROJECTS, handleWebsocketNotification)
48-
}, [handleWebsocketNotification]);
36+
useWebsocket(CurrentPage.PROJECTS, handleWebsocketNotification, null, CurrentPageSubKey.BUTTONS_CONTAINER);
4937

5038
return (
5139
user && user.role === UserRole.ENGINEER ? (<div>

src/components/projects/ProjectsList.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { selectInactiveOrganization, selectIsDemo, selectIsManaged, selectUser, setComments } from "@/src/reduxStore/states/general"
22
import { selectAllProjects, setAllProjects } from "@/src/reduxStore/states/project";
3-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
43
import { GET_OVERVIEW_STATS, GET_PROJECT_LIST } from "@/src/services/gql/queries/projects";
54
import { Project, ProjectStatistics } from "@/src/types/components/projects/projects-list";
65
import { CurrentPage } from "@/src/types/shared/general";
@@ -15,14 +14,12 @@ import { GET_CAN_CREATE_LOCAL_ORG } from "@/src/services/gql/queries/organizatio
1514
import { ADD_USER_TO_ORGANIZATION, CREATE_ORGANIZATION } from "@/src/services/gql/mutations/organizations";
1615
import style from "@/src/styles/components/projects/projects-list.module.css";
1716
import { useRouter } from "next/router";
18-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
1917
import AdminDeleteProjectModal from "./AdminDeleteProjectModal";
2018
import { setAllAttributes, setAllEmbeddings, setLabelingTasksAll } from "@/src/reduxStore/states/pages/settings";
2119
import { setOverviewFilters } from "@/src/reduxStore/states/tmp";
2220
import { setDataSlices, setFullSearchStore, setSearchGroupsStore } from "@/src/reduxStore/states/pages/data-browser";
23-
import { closeModal } from "@/src/reduxStore/states/modal";
24-
import { ModalEnum } from "@/src/types/shared/modal";
2521
import { SearchGroup } from "@/submodules/javascript-functions/enums/enums";
22+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
2623

2724
export default function ProjectsList() {
2825
const router = useRouter();
@@ -44,13 +41,7 @@ export default function ProjectsList() {
4441
const [createOrgMut] = useMutation(CREATE_ORGANIZATION);
4542
const [addUserToOrgMut] = useMutation(ADD_USER_TO_ORGANIZATION);
4643

47-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.PROJECTS]), []);
48-
4944
useEffect(() => {
50-
WebSocketsService.subscribeToNotification(CurrentPage.PROJECTS, {
51-
whitelist: ['project_created', 'project_deleted', 'project_update', 'file_upload'],
52-
func: handleWebsocketNotification
53-
});
5445
dispatch(setLabelingTasksAll(null));
5546
dispatch(setOverviewFilters(null));
5647
dispatch(setSearchGroupsStore(null));
@@ -119,9 +110,7 @@ export default function ProjectsList() {
119110
}
120111
}, []);
121112

122-
useEffect(() => {
123-
WebSocketsService.updateFunctionPointer(null, CurrentPage.PROJECTS, handleWebsocketNotification)
124-
}, [handleWebsocketNotification]);
113+
useWebsocket(CurrentPage.PROJECTS, handleWebsocketNotification);
125114

126115
return (
127116
<div>

src/components/projects/projectId/admin/ProjectAdmin.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { useDispatch, useSelector } from "react-redux";
1313
import NewPersonalToken from "./NewPersonalTokenModal";
1414
import DeletePersonalToken from "./DeletePersonalTokenModal";
1515
import { CurrentPage } from "@/src/types/shared/general";
16-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
1716
import { useRouter } from "next/router";
1817
import { selectIsAdmin } from "@/src/reduxStore/states/general";
19-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
18+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
2019

2120
export default function ProjectAdmin() {
2221
const router = useRouter();
@@ -29,8 +28,6 @@ export default function ProjectAdmin() {
2928

3029
const [refetchAccessTokens] = useLazyQuery(GET_ALL_PERSONAL_ACCESS_TOKENS, { fetchPolicy: "network-only" });
3130

32-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.ADMIN_PAGE]), []);
33-
3431
useEffect(() => {
3532
if (!projectId) return;
3633
refetchAccessTokensAndProcess();
@@ -41,11 +38,6 @@ export default function ProjectAdmin() {
4138
return;
4239
}
4340

44-
WebSocketsService.subscribeToNotification(CurrentPage.ADMIN_PAGE, {
45-
projectId: projectId,
46-
whitelist: ['pat'],
47-
func: handleWebsocketNotification
48-
});
4941
}, [projectId]);
5042

5143
function refetchAccessTokensAndProcess() {
@@ -64,10 +56,7 @@ export default function ProjectAdmin() {
6456
}
6557
}, [accessTokens]);
6658

67-
useEffect(() => {
68-
if (!projectId) return;
69-
WebSocketsService.updateFunctionPointer(projectId, CurrentPage.ADMIN_PAGE, handleWebsocketNotification)
70-
}, [handleWebsocketNotification, projectId]);
59+
useWebsocket(CurrentPage.ADMIN_PAGE, handleWebsocketNotification, projectId);
7160

7261
return (<>
7362
{accessTokens && <div className="p-4 bg-gray-100 h-full overflow-y-auto flex-1 flex flex-col">

src/components/projects/projectId/attributes/attributeId/AttributeCalculations.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ import DangerZone from "@/src/components/shared/danger-zone/DangerZone";
2323
import { DangerZoneEnum } from "@/src/types/shared/danger-zone";
2424
import ContainerLogs from "@/src/components/shared/logs/ContainerLogs";
2525
import LoadingIcon from "@/src/components/shared/loading/LoadingIcon";
26-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
2726
import { debounceTime, distinctUntilChanged, fromEvent, timer } from "rxjs";
28-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
2927
import { TOOLTIPS_DICT } from "@/src/util/tooltip-constants";
30-
import { selectAllUsers, setBricksIntegrator, setComments } from "@/src/reduxStore/states/general";
28+
import { selectAllUsers, setComments } from "@/src/reduxStore/states/general";
3129
import { REQUEST_COMMENTS } from "@/src/services/gql/queries/projects";
3230
import { CommentDataManager } from "@/src/util/classes/comments";
3331
import { CommentType } from "@/src/types/shared/comments";
3432
import BricksIntegrator from "@/src/components/shared/bricks-integrator/BricksIntegrator";
3533
import { AttributeCodeLookup } from "@/src/util/classes/attribute-calculation";
3634
import Dropdown2 from "@/submodules/react-components/components/Dropdown2";
35+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
3736
import { postProcessLabelingTasks, postProcessLabelingTasksSchema } from "@/src/util/components/projects/projectId/settings/labeling-tasks-helper";
3837

3938
const EDITOR_OPTIONS = { theme: 'vs-light', language: 'python', readOnly: false };
@@ -68,8 +67,6 @@ export default function AttributeCalculation() {
6867
const [refetchComments] = useLazyQuery(REQUEST_COMMENTS, { fetchPolicy: "no-cache" });
6968
const [refetchLabelingTasksByProjectId] = useLazyQuery(GET_LABELING_TASKS_BY_PROJECT_ID, { fetchPolicy: "network-only" });
7069

71-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.ATTRIBUTE_CALCULATION]), []);
72-
7370
useEffect(() => {
7471
if (!currentAttribute) return;
7572
if (isInitial == null) setIsInitial(AttributeCodeLookup.isCodeStillTemplate(currentAttribute.sourceCode, currentAttribute.dataType))
@@ -92,11 +89,6 @@ export default function AttributeCalculation() {
9289
}
9390
refetchLabelingTasksAndProcess();
9491
checkProjectTokenization();
95-
WebSocketsService.subscribeToNotification(CurrentPage.ATTRIBUTE_CALCULATION, {
96-
projectId: projectId,
97-
whitelist: ['attributes_updated', 'calculate_attribute', 'tokenization', 'knowledge_base_updated', 'knowledge_base_deleted', 'knowledge_base_created'],
98-
func: handleWebsocketNotification
99-
});
10092
}, [projectId, attributes, currentAttribute]);
10193

10294
useEffect(() => {
@@ -298,10 +290,7 @@ export default function AttributeCalculation() {
298290
}
299291
}, [projectId, currentAttribute]);
300292

301-
useEffect(() => {
302-
if (!projectId) return;
303-
WebSocketsService.updateFunctionPointer(projectId, CurrentPage.ATTRIBUTE_CALCULATION, handleWebsocketNotification)
304-
}, [handleWebsocketNotification, projectId]);
293+
useWebsocket(CurrentPage.ATTRIBUTE_CALCULATION, handleWebsocketNotification, projectId);
305294

306295
return (projectId && <div className="bg-white p-4 pb-16 overflow-y-auto h-screen" style={{ width: 'calc(100vw - 75px)' }} onScroll={(e: any) => onScrollEvent(e)}>
307296
{currentAttribute && <div>

src/components/projects/projectId/data-browser/DataBrowser.tsx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@ import { GET_ORGANIZATION_USERS_WITH_COUNT } from "@/src/services/gql/queries/or
1313
import { selectAllUsers, selectUser, setComments } from "@/src/reduxStore/states/general";
1414
import DataBrowserRecords from "./DataBrowserRecords";
1515
import { postProcessingEmbeddings } from "@/src/util/components/projects/projectId/settings/embeddings-helper";
16-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
1716
import { CurrentPage } from "@/src/types/shared/general";
1817
import { CommentType } from "@/src/types/shared/comments";
1918
import { CommentDataManager } from "@/src/util/classes/comments";
2019
import { REQUEST_COMMENTS } from "@/src/services/gql/queries/projects";
21-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
22-
import { useRouter } from "next/router";
20+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
2321

2422
const SEARCH_REQUEST = { offset: 0, limit: 20 };
2523

2624
export default function DataBrowser() {
2725
const dispatch = useDispatch();
28-
const router = useRouter();
2926

3027
const projectId = useSelector(selectProjectId);
3128
const users = useSelector(selectAllUsers);
@@ -45,8 +42,6 @@ export default function DataBrowser() {
4542
const [refetchComments] = useLazyQuery(REQUEST_COMMENTS, { fetchPolicy: "no-cache" });
4643
const [refetchUniqueValues] = useLazyQuery(GET_UNIQUE_VALUES_BY_ATTRIBUTES, { fetchPolicy: "no-cache" });
4744

48-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.DATA_BROWSER]), []);
49-
5045
useEffect(() => {
5146
if (!projectId) return;
5247
if (!users || !user) return;
@@ -56,11 +51,6 @@ export default function DataBrowser() {
5651
refetchUsersCountAndProcess();
5752
refetchEmbeddingsAndPostProcess();
5853
refetchUniqueValuesAndProcess();
59-
WebSocketsService.subscribeToNotification(CurrentPage.DATA_BROWSER, {
60-
projectId: projectId,
61-
whitelist: ['data_slice_created', 'data_slice_updated', 'data_slice_deleted', 'label_created', 'label_deleted', 'labeling_task_deleted', 'labeling_task_updated', 'labeling_task_created', 'information_source_created', 'information_source_updated', 'information_source_deleted', 'attributes_updated', 'calculate_attribute', 'embedding', 'embedding_deleted'],
62-
func: handleWebsocketNotification
63-
});
6454
}, [projectId, users, user]);
6555

6656
useEffect(() => {
@@ -181,10 +171,7 @@ export default function DataBrowser() {
181171
}
182172
}, [projectId]);
183173

184-
useEffect(() => {
185-
if (!projectId) return;
186-
WebSocketsService.updateFunctionPointer(projectId, CurrentPage.DATA_BROWSER, handleWebsocketNotification)
187-
}, [handleWebsocketNotification, projectId]);
174+
useWebsocket(CurrentPage.DATA_BROWSER, handleWebsocketNotification, projectId);
188175

189176
return (<>
190177
{projectId && <div className="flex flex-row h-full">

src/components/projects/projectId/edit-records/EditRecords.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import { IconAlertCircle, IconAlertTriangle, IconAlertTriangleFilled, IconBallpe
1010
import { Tooltip } from "@nextui-org/react";
1111
import { TOOLTIPS_DICT } from "@/src/util/tooltip-constants";
1212
import EditField from "./EditField";
13-
import { WebSocketsService } from "@/src/services/base/web-sockets/WebSocketsService";
1413
import { useRouter } from "next/router";
15-
import { unsubscribeWSOnDestroy } from "@/src/services/base/web-sockets/web-sockets-helper";
1614
import { ModalEnum } from "@/src/types/shared/modal";
1715
import { openModal } from "@/src/reduxStore/states/modal";
16+
import { useWebsocket } from "@/src/services/base/web-sockets/useWebsocket";
1817

1918
export default function EditRecords() {
2019
const dispatch = useDispatch();
@@ -26,8 +25,6 @@ export default function EditRecords() {
2625
const [erdData, setErdData] = useState(createDefaultEditRecordComponentData());
2726
const [alertLastVisible, setAlertLastVisible] = useState<number>(null);
2827

29-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.EDIT_RECORDS]), []);
30-
3128
useEffect(() => {
3229
if (!projectId) return;
3330
if (!sessionData) {
@@ -44,11 +41,6 @@ export default function EditRecords() {
4441
if (!erdDataCopy.modals.hideExplainModal) {
4542
dispatch(openModal(ModalEnum.EXPLAIN_EDIT_RECORDS));
4643
}
47-
WebSocketsService.subscribeToNotification(CurrentPage.EDIT_RECORDS, {
48-
projectId: projectId,
49-
whitelist: ['calculate_attribute'],
50-
func: handleWebsocketNotification
51-
});
5244
}, [sessionData, projectId]);
5345

5446
useEffect(() => {
@@ -77,10 +69,7 @@ export default function EditRecords() {
7769
setAlertLastVisible(Date.now());
7870
}, [alertLastVisible]);
7971

80-
useEffect(() => {
81-
if (!projectId) return;
82-
WebSocketsService.updateFunctionPointer(projectId, CurrentPage.EDIT_RECORDS, handleWebsocketNotification)
83-
}, [handleWebsocketNotification, projectId]);
72+
useWebsocket(CurrentPage.EDIT_RECORDS, handleWebsocketNotification, projectId);
8473

8574
return (<>{projectId && <div className="h-screen bg-white flex flex-col">
8675
<NavBarTopEditRecords erdData={erdData} setErdData={(erdData) => setErdData(erdData)} />

src/components/projects/projectId/heuristics/HeuristicsHeader.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { openModal, selectModal } from '@/src/reduxStore/states/modal';
44
import { selectHeuristicsAll, setHeuristicType } from '@/src/reduxStore/states/pages/heuristics';
55
import { selectLabelingTasksAll } from '@/src/reduxStore/states/pages/settings';
66
import { selectProjectId } from '@/src/reduxStore/states/project';
7-
import { WebSocketsService } from '@/src/services/base/web-sockets/WebSocketsService';
8-
import { unsubscribeWSOnDestroy } from '@/src/services/base/web-sockets/web-sockets-helper';
97
import { CREATE_INFORMATION_SOURCE_PAYLOAD, RUN_ZERO_SHOT_PROJECT, SET_ALL_HEURISTICS, START_WEAK_SUPERVISIONS } from '@/src/services/gql/mutations/heuristics';
108
import { GET_CURRENT_WEAK_SUPERVISION_RUN } from '@/src/services/gql/queries/heuristics';
119
import style from '@/src/styles/components/projects/projectId/heuristics/heuristics.module.css';
@@ -26,6 +24,7 @@ import { useDispatch, useSelector } from 'react-redux';
2624
import LastWeakSupervisionModal from './modals/LastWeakSupervisionModal';
2725
import DeleteHeuristicsModal from './DeleteHeuristicsModal';
2826
import Dropdown2 from '@/submodules/react-components/components/Dropdown2';
27+
import { useWebsocket } from '@/src/services/base/web-sockets/useWebsocket';
2928

3029

3130
export default function HeuristicsHeader(props: HeuristicsHeaderProps) {
@@ -52,17 +51,6 @@ export default function HeuristicsHeader(props: HeuristicsHeaderProps) {
5251
const [createTaskMut] = useMutation(CREATE_INFORMATION_SOURCE_PAYLOAD);
5352
const [runZeroShotMut] = useMutation(RUN_ZERO_SHOT_PROJECT);
5453

55-
useEffect(unsubscribeWSOnDestroy(router, [CurrentPage.HEURISTICS]), []);
56-
57-
useEffect(() => {
58-
if (!projectId) return;
59-
WebSocketsService.subscribeToNotification(CurrentPage.HEURISTICS, {
60-
projectId: projectId,
61-
whitelist: ['weak_supervision_started', 'weak_supervision_finished'],
62-
func: handleWebsocketNotification
63-
});
64-
}, [projectId]);
65-
6654
useEffect(() => {
6755
if (!heuristics) return;
6856
setAreHeuristicsSelected(checkSelectedHeuristics(heuristics, false));
@@ -173,10 +161,7 @@ export default function HeuristicsHeader(props: HeuristicsHeaderProps) {
173161
else if (msgParts[1] == 'weak_supervision_finished') setLoadingIconWS(false);
174162
}, []);
175163

176-
useEffect(() => {
177-
if (!projectId) return;
178-
WebSocketsService.updateFunctionPointer(projectId, CurrentPage.HEURISTICS, handleWebsocketNotification)
179-
}, [handleWebsocketNotification, projectId]);
164+
useWebsocket(CurrentPage.HEURISTICS, handleWebsocketNotification, projectId);
180165

181166
return (
182167
<div className="flex-shrink-0 block xl:flex justify-between items-center">

0 commit comments

Comments
 (0)