Skip to content

Commit b920151

Browse files
committed
fix rag indices getting & cleanups
1 parent b445a8f commit b920151

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

src/client/App.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import React, { createContext, useContext, useEffect, useRef, useState } from 'react'
2-
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom'
3-
import { SnackbarProvider } from 'notistack'
4-
import { initShibbolethPinger } from 'unfuck-spa-shibboleth-session'
1+
import { Box, Button, CssBaseline, Snackbar } from '@mui/material'
52
import { ThemeProvider } from '@mui/material/styles'
3+
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3'
64
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
75
import { fi } from 'date-fns/locale'
8-
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3'
9-
import { Box, Button, CssBaseline, Snackbar } from '@mui/material'
10-
import { AppContext } from './contexts/AppContext'
6+
import { SnackbarProvider } from 'notistack'
7+
import React, { useEffect, useRef } from 'react'
8+
import { Outlet, useLocation, useParams } from 'react-router-dom'
9+
import { initShibbolethPinger } from 'unfuck-spa-shibboleth-session'
1110
import { PUBLIC_URL } from '../config'
12-
import type { User } from './types'
13-
import useTheme from './theme'
14-
import NavBar from './components/NavBar'
11+
import { Feedback } from './components/Feedback'
1512
import Footer from './components/Footer'
16-
import useCurrentUser from './hooks/useCurrentUser'
13+
import NavBar from './components/NavBar'
14+
import { AppContext } from './contexts/AppContext'
1715
import { EmbeddedProvider, useIsEmbedded } from './contexts/EmbeddedContext'
18-
import { Feedback } from './components/Feedback'
16+
import useCurrentUser from './hooks/useCurrentUser'
1917
import { AnalyticsProvider } from './stores/analytics'
20-
import { useTranslation } from 'react-i18next'
21-
import { useUpdateUrlLang } from './hooks/useUpdateUrlLang.tsx'
18+
import useTheme from './theme'
19+
import type { User } from './types'
2220

2321
const hasAccess = (user: User | null | undefined, courseId?: string) => {
2422
if (!user) return false
@@ -73,7 +71,7 @@ const AdminLoggedInAsBanner = () => {
7371
}
7472

7573
const App = () => {
76-
const urlUpdater = useUpdateUrlLang() //DONT REMOVE, the hook creates 2 useEffects to keep the url param synced with user language changes
74+
useUpdateUrlLang()
7775
const theme = useTheme()
7876
const { courseId } = useParams()
7977
const location = useLocation()

src/client/components/ChatV2/ChatV2.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getLanguageValue } from '../../../shared/utils'
1010
import useCourse from '../../hooks/useCourse'
1111
import useInfoTexts from '../../hooks/useInfoTexts'
1212
import useLocalStorageState from '../../hooks/useLocalStorageState'
13-
import { useRagIndices } from '../../hooks/useRagIndices'
13+
import { useCourseRagIndices } from '../../hooks/useRagIndices'
1414
import useRetryTimeout from '../../hooks/useRetryTimeout'
1515
import useUserStatus from '../../hooks/useUserStatus'
1616
import type { Message } from '../../types'
@@ -59,7 +59,7 @@ export const ChatV2 = () => {
5959
const isMobile = useMediaQuery(theme.breakpoints.down('md'))
6060

6161
const { data: course } = useCourse(courseId)
62-
const { ragIndices } = useRagIndices(courseId)
62+
const { ragIndices } = useCourseRagIndices(course?.id)
6363
const { infoTexts } = useInfoTexts()
6464

6565
const { userStatus, isLoading: statusLoading, refetch: refetchStatus } = useUserStatus(courseId)

src/client/hooks/useRagIndices.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@ import { useQuery } from '@tanstack/react-query'
22
import type { RagIndexAttributes } from '../../shared/types'
33
import apiClient from '../util/apiClient'
44

5-
export const useRagIndices = (chatInstanceId?: string, includeExtras?: boolean) => {
5+
export const useRagIndices = (includeExtras?: boolean) => {
66
const { data: ragIndices, ...rest } = useQuery<RagIndexAttributes[]>({
7-
queryKey: chatInstanceId ? ['ragIndices', chatInstanceId] : ['ragIndices'],
7+
queryKey: ['ragIndices'],
88
queryFn: async () => {
99
const response = await apiClient.get(`/rag/indices`, {
1010
params: {
11-
chatInstanceId: chatInstanceId ? chatInstanceId : undefined,
11+
includeExtras: includeExtras ? includeExtras : false,
12+
},
13+
})
14+
return response.data
15+
},
16+
})
17+
18+
return { ragIndices, ...rest }
19+
}
20+
21+
export const useCourseRagIndices = (chatInstanceId?: string, includeExtras?: boolean) => {
22+
const { data: ragIndices, ...rest } = useQuery<RagIndexAttributes[]>({
23+
enabled: !!chatInstanceId,
24+
queryKey: ['ragIndices', chatInstanceId],
25+
queryFn: async () => {
26+
const response = await apiClient.get(`/rag/indices`, {
27+
params: {
28+
chatInstanceId,
1229
includeExtras: includeExtras ? includeExtras : false,
1330
},
1431
})

src/client/hooks/useUpdateUrlLang.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { useTranslation } from 'react-i18next'
2-
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom'
2+
import { useLocation, useSearchParams } from 'react-router-dom'
33
import useCurrentUser from '../hooks/useCurrentUser'
44
import { useState, useEffect } from 'react'
55

66
export const useUpdateUrlLang = () => {
7-
const location = useLocation()
8-
const navigate = useNavigate()
97
const languages = ['fi', 'sv', 'en']
10-
const { t, i18n } = useTranslation()
11-
const { user, isLoading } = useCurrentUser()
8+
const { i18n } = useTranslation()
9+
const { user } = useCurrentUser()
1210
const [lang, setLanguageState] = useState(localStorage.getItem('lang'))
1311
const [params, setParams] = useSearchParams()
1412
const langParam = params.get('lang')
@@ -28,10 +26,9 @@ export const useUpdateUrlLang = () => {
2826
// so lets use the local storage (example: see how admin page)
2927
setLang(updatedLangFromLocal)
3028
}
31-
}, [location.pathname])
29+
}, [])
3230

3331
useEffect(() => {
34-
console.log('lang changed!')
3532
if (i18n.language !== localStorage.getItem('lang')) {
3633
setLang(i18n.language)
3734
}

0 commit comments

Comments
 (0)