From dbe054c4d33ed6f79f0b4b762a640d520e13db7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Fri, 27 Sep 2024 19:45:08 +0300 Subject: [PATCH] feat: added check for running project-id --- src/components/AddContentModal/index.tsx | 11 ++++++++-- src/components/App/index.tsx | 28 ++++++++++++------------ src/stores/useDataStore/index.ts | 5 +++++ src/types/index.ts | 1 + 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/components/AddContentModal/index.tsx b/src/components/AddContentModal/index.tsx index 6b09504a0..a56230997 100644 --- a/src/components/AddContentModal/index.tsx +++ b/src/components/AddContentModal/index.tsx @@ -15,6 +15,7 @@ import { YOUTUBE_CHANNEL, } from '~/constants' import { api } from '~/network/api' +import { useDataStore } from '~/stores/useDataStore' import { useModal } from '~/stores/useModalStore' import { useUserStore } from '~/stores/useUserStore' import { sphinxBridge } from '~/testSphinxBridge' @@ -39,6 +40,7 @@ const handleSubmitForm = async ( data: FieldValues, sourceType: string, setBudget: (value: number | null) => void, + setRunningProjectId: (value: string) => void, ): Promise => { const endPoint = isSource(sourceType) ? 'radar' : 'add_node' @@ -111,6 +113,10 @@ const handleSubmitForm = async ( Authorization: lsatToken, }) + if (res.data.project_id) { + setRunningProjectId(res.data.project_id) + } + if (res.error) { const { message } = res.error @@ -122,7 +128,7 @@ const handleSubmitForm = async ( if (err.status === 402) { await payLsat(setBudget) await updateBudget(setBudget) - await handleSubmitForm(data, sourceType, setBudget) + await handleSubmitForm(data, sourceType, setBudget, setRunningProjectId) } else { let errorMessage = NODE_ADD_ERROR @@ -147,6 +153,7 @@ export const AddContentModal = () => { const [currentStep, setCurrentStep] = useState(0) const { close, visible } = useModal('addContent') const { setBudget } = useUserStore((s) => s) + const { setRunningProjectId } = useDataStore((s) => s) const form = useForm({ mode: 'onChange' }) const { watch, setValue, reset } = form const [loading, setLoading] = useState(false) @@ -198,7 +205,7 @@ export const AddContentModal = () => { setLoading(true) try { - await handleSubmitForm(data, type, setBudget) + await handleSubmitForm(data, type, setBudget, setRunningProjectId) SuccessNotify('Content Added') handleClose() // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index d665a4ed3..aa49c6d42 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -69,7 +69,8 @@ export const App = () => { const setTeachMeAnswer = useTeachStore((s) => s.setTeachMeAnswer) - const { fetchData, setCategoryFilter, setAbortRequests, addNewNode, splashDataLoading } = useDataStore((s) => s) + const { fetchData, setCategoryFilter, setAbortRequests, addNewNode, splashDataLoading, runningProjectId } = + useDataStore((s) => s) const { setAiSummaryAnswer, getKeyExist, aiRefId } = useAiSummaryStore((s) => s) @@ -238,18 +239,16 @@ export const App = () => { ]) useEffect(() => { - const ws = new WebSocket('wss://staging.stakwork.com/cable?channel=ProjectLogChannel') - - ws.onopen = () => { - console.log('WebSocket connection established') + if (!runningProjectId) { + return } - ws.onmessage = (event) => { - console.log('Message from server:', event.data) + const ws = new WebSocket('wss://jobs.stakwork.com/cable?channel=ProjectLogChannel') + ws.onopen = () => { let id = 'a' - id = '51998781' + id = runningProjectId const command = { command: 'subscribe', @@ -260,6 +259,12 @@ export const App = () => { ws.send(JSON.stringify(command)) console.log('Subscription command sent:', command) + console.log('WebSocket connection established') + } + + ws.onmessage = (event) => { + console.log('Message from server:', event.data) + // Handle the message from the server here } @@ -270,12 +275,7 @@ export const App = () => { ws.onclose = () => { console.log('WebSocket connection closed') } - - // Cleanup when the component is unmounted - return () => { - // ws.close() - } - }, []) + }, [runningProjectId]) useEffect(() => { if (!splashDataLoading) { diff --git a/src/stores/useDataStore/index.ts b/src/stores/useDataStore/index.ts index 26f980ff0..78c1d7937 100644 --- a/src/stores/useDataStore/index.ts +++ b/src/stores/useDataStore/index.ts @@ -45,6 +45,7 @@ export type DataStore = { stats: TStats | null nodeTypes: string[] seedQuestions: string[] | null + runningProjectId: string setTrendingTopics: (trendingTopics: Trending[]) => void setDataNew: (data: GraphData) => void @@ -62,6 +63,7 @@ export type DataStore = { setSources: (sources: Sources[] | null) => void setQueuedSources: (sources: Sources[] | null) => void setIsFetching: (_: boolean) => void + setRunningProjectId: (runningProjectId: string) => void setHideNodeDetails: (_: boolean) => void addNewNode: (data: FetchDataResponse) => void updateNode: (updatedNode: NodeExtended) => void @@ -129,6 +131,7 @@ const defaultData: Omit< abortRequest: false, dataNew: null, seedQuestions: null, + runningProjectId: '', } let abortController: AbortController | null = null @@ -342,6 +345,8 @@ export const useDataStore = create()( removeNode: (id) => { console.log(id) }, + + setRunningProjectId: (runningProjectId) => set({ runningProjectId }), setAbortRequests: (abortRequest) => set({ abortRequest }), })), ) diff --git a/src/types/index.ts b/src/types/index.ts index 5ce944daa..18e314bf2 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -226,6 +226,7 @@ export type SubmitErrRes = { error?: { message?: string } data: { ref_id: string + project_id?: string } }