Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix graph consistency, chunked nodes #2266

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions src/components/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Leva } from 'leva'
import { lazy, Suspense, useCallback, useEffect } from 'react'
import { lazy, Suspense, useCallback, useEffect, useRef } from 'react'
import { FormProvider, useForm } from 'react-hook-form'
import { useSearchParams } from 'react-router-dom'
import 'react-toastify/dist/ReactToastify.css'
Expand All @@ -23,6 +23,7 @@
AiSummaryQuestionsResponse,
AiSummarySourcesResponse,
ExtractedEntitiesResponse,
FetchDataResponse,
} from '~/types'
import { colors } from '~/utils/colors'
import { updateBudget } from '~/utils/setBudget'
Expand Down Expand Up @@ -57,6 +58,8 @@
const [searchParams] = useSearchParams()
const query = searchParams.get('q')
const { setBudget, setNodeCount } = useUserStore((s) => s)
const queueRef = useRef<FetchDataResponse | null>(null)
const timerRef = useRef<NodeJS.Timeout | null>(null)

const {
setSidebarOpen,
Expand Down Expand Up @@ -134,6 +137,35 @@
setNodeCount('INCREMENT')
}, [setNodeCount])

const handleNewNodeCreated = useCallback(
(data: FetchDataResponse) => {
if (!queueRef.current) {
queueRef.current = { nodes: [], edges: [] }
}

if (data.edges) {
queueRef.current.edges.push(...data.edges)
}

if (data.nodes) {
queueRef.current.nodes.push(...data.nodes)
}

if (timerRef.current) {
clearTimeout(timerRef.current)
}

timerRef.current = setTimeout(() => {
// Combine all queued data into a single update
const batchedData = { ...queueRef.current }

queueRef.current = { nodes: [], edges: [] } // Reset the queue
addNewNode(batchedData) // Call the original addNewNode function with batched data
}, 2000) // Adjust delay as necessary
},
[addNewNode],
)

const handleAiSummaryAnswer = useCallback(
(data: AiSummaryAnswerResponse) => {
if (data.ref_id) {
Expand Down Expand Up @@ -173,15 +205,6 @@
[setAiSummaryAnswer],
)

const handleNewNodeCreated = useCallback(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(data: any) => {
// Use the data recieved to create graph in realtime
addNewNode(data)
},
[addNewNode],
)

const handleExtractedEntities = useCallback(
(data: ExtractedEntitiesResponse) => {
if (data.question && getKeyExist(aiRefId)) {
Expand Down Expand Up @@ -268,7 +291,7 @@
}

ws.onmessage = (event) => {
console.log('Message from server:', event.data)

Check warning on line 294 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 294 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement

const data = JSON.parse(event.data)

Expand All @@ -283,7 +306,7 @@

setRunningProjectMessages(message)

console.log(message)

Check warning on line 309 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 309 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement
}

// Handle the message from the server here
Expand All @@ -294,7 +317,7 @@
}

ws.onclose = () => {
console.log('WebSocket connection closed')

Check warning on line 320 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / craco-build-run

Unexpected console statement

Check warning on line 320 in src/components/App/index.tsx

View workflow job for this annotation

GitHub Actions / eslint-run

Unexpected console statement
}
}, [runningProjectId, setRunningProjectMessages])

Expand Down
Loading