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

feat: changed loading logic for schema, refactored splash page #2355

Merged
merged 1 commit into from
Oct 17, 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
79 changes: 55 additions & 24 deletions src/components/App/Splash/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable react/display-name */
import { LinearProgress } from '@mui/material'
import { useCallback, useEffect, useState } from 'react'
import { useEffect, useState } from 'react'
import styled from 'styled-components'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { getAboutData, getStats } from '~/network/fetchSourcesData'
import { getAboutData, getSchemaAll, getStats } from '~/network/fetchSourcesData'
import { useAppStore } from '~/stores/useAppStore'
import { useDataStore } from '~/stores/useDataStore'
import { useSchemaStore } from '~/stores/useSchemaStore'
import { colors, formatSplashMessage, formatStatsResponse } from '~/utils'
import { AnimatedTextContent } from './animated'
import { initialMessageData, Message } from './constants'
Expand All @@ -16,43 +17,73 @@ export const Splash = () => {
const [message, setMessage] = useState<Message>(initialMessageData)
const [progress, setProgress] = useState(0)
const { appMetaData, setAppMetaData } = useAppStore((s) => s)
const { stats, setStats, isFetching, setSeedQuestions } = useDataStore((s) => s)
const { stats, setStats, setSeedQuestions } = useDataStore((s) => s)
const { schemas, setSchemas } = useSchemaStore((s) => s)
const [isLoading, setIsLoading] = useState(false)

const fetchData = useCallback(async () => {
try {
if (!appMetaData) {
const aboutResponse = await getAboutData()
// Individual useEffect hooks for each data requirement
useEffect(() => {
const fetchData = async () => {
setIsLoading(true)

try {
if (!appMetaData) {
const aboutResponse = await getAboutData()

setAppMetaData(aboutResponse)
setAppMetaData(aboutResponse)

if (aboutResponse.seed_questions) {
setSeedQuestions(aboutResponse.seed_questions)
if (aboutResponse.seed_questions) {
setSeedQuestions(aboutResponse.seed_questions)
}
}
}

if (!stats) {
const statsResponse = await getStats()
if (!stats) {
const statsResponse = await getStats()
const updatedStats = formatStatsResponse(statsResponse)

const updatedStats = formatStatsResponse(statsResponse)
setStats(updatedStats)

setStats(updatedStats)
const messageData = formatSplashMessage(statsResponse)

const messageData = formatSplashMessage(statsResponse)
setMessage(messageData)
}

setMessage(messageData)
if (!schemas.length) {
const response = await getSchemaAll()

setSchemas(response.schemas.filter((schema) => !schema.is_deleted))
}
} finally {
setIsLoading(false) // Ensure fetching flag is reset
}
} catch {
setProgress(100)
}
}, [appMetaData, setAppMetaData, setStats, stats, setSeedQuestions])

if (!appMetaData && !stats && !isLoading) {
fetchData() // Only fetch if there's no appMetaData and it's not already fetching
}
}, [appMetaData, isLoading, schemas.length, setAppMetaData, setSchemas, setSeedQuestions, setStats, stats])

useEffect(() => {
fetchData()
const fetchSchemas = async () => {
try {
const response = await getSchemaAll()

setSchemas(response.schemas.filter((schema) => !schema.is_deleted))
} catch (err) {
console.error(err)
}
}

if (!schemas.length) {
fetchSchemas() // Only fetch if schemas is empty and it's not already fetching
}
}, [schemas.length, setSchemas])

// Progress and interval management
useEffect(() => {
let intervalId: NodeJS.Timer

if (!isFetching && message && appMetaData) {
// increase progress from 0 to 50% after all data is fetched
if (message && appMetaData) {
setProgress((prev) => (!prev ? 50 : prev))

intervalId = setInterval(() => {
Expand All @@ -65,7 +96,7 @@ export const Splash = () => {
clearInterval(intervalId)
}
}
}, [appMetaData, fetchData, isFetching, message, stats])
}, [message, appMetaData])

return (
<SplashWrapper>
Expand Down
Loading