Skip to content

Commit

Permalink
fix: fix graph consistency, chunked nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rassl committed Sep 30, 2024
1 parent da6c7b9 commit 1efab89
Showing 1 changed file with 33 additions and 10 deletions.
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 @@ import {
AiSummaryQuestionsResponse,
AiSummarySourcesResponse,
ExtractedEntitiesResponse,
FetchDataResponse,
} from '~/types'
import { colors } from '~/utils/colors'
import { updateBudget } from '~/utils/setBudget'
Expand Down Expand Up @@ -57,6 +58,8 @@ export const App = () => {
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 @@ export const App = () => {
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 @@ export const App = () => {
[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

0 comments on commit 1efab89

Please sign in to comment.