Now uses ApiContext

This commit is contained in:
Tom Butcher 2025-11-29 01:25:13 +00:00
parent df85d99aaf
commit 87a6198173

View File

@ -14,7 +14,8 @@ import {
useEffect, useEffect,
useState, useState,
useRef, useRef,
createElement createElement,
useContext
} from 'react' } from 'react'
import axios from 'axios' import axios from 'axios'
import { LoadingOutlined } from '@ant-design/icons' import { LoadingOutlined } from '@ant-design/icons'
@ -29,6 +30,8 @@ import {
getModelByPrefix getModelByPrefix
} from '../../../database/ObjectModels' } from '../../../database/ObjectModels'
import InfoCircleIcon from '../../Icons/InfoCircleIcon' import InfoCircleIcon from '../../Icons/InfoCircleIcon'
import { ApiServerContext } from './ApiServerContext'
import { AuthContext } from './AuthContext'
const SpotlightContext = createContext() const SpotlightContext = createContext()
@ -41,6 +44,8 @@ const SpotlightProvider = ({ children }) => {
const [listData, setListData] = useState([]) const [listData, setListData] = useState([])
const [messageApi, contextHolder] = message.useMessage() const [messageApi, contextHolder] = message.useMessage()
const [inputPrefix, setInputPrefix] = useState({ prefix: '', mode: null }) const [inputPrefix, setInputPrefix] = useState({ prefix: '', mode: null })
const { fetchSpotlightData } = useContext(ApiServerContext)
const { token } = useContext(AuthContext)
// Refs for throttling/debouncing // Refs for throttling/debouncing
const lastFetchTime = useRef(0) const lastFetchTime = useRef(0)
@ -116,7 +121,7 @@ const SpotlightProvider = ({ children }) => {
setLoading(true) setLoading(true)
setListData([]) setListData([])
let response let data
// Check if we have a prefix with ? mode (filter mode) // Check if we have a prefix with ? mode (filter mode)
if (inputPrefix && inputPrefix.mode === '?') { if (inputPrefix && inputPrefix.mode === '?') {
@ -128,37 +133,34 @@ const SpotlightProvider = ({ children }) => {
// Parse the query parameters // Parse the query parameters
const params = new URLSearchParams(queryParams) const params = new URLSearchParams(queryParams)
response = await axios.get(`${config.backendUrl}/spotlight/${prefix}`, { // For ? mode, use axios directly with query params
const response = await axios.get(
`${config.backendUrl}/spotlight/${prefix}`,
{
params: params, params: params,
headers: { headers: {
Accept: 'application/json' Accept: 'application/json',
}, Authorization: `Bearer ${token}`
withCredentials: true }
})
} else {
// For other modes (:, ^), use the original behavior
response = await axios.get(
`${config.backendUrl}/spotlight/${encodeURIComponent(searchQuery.trim())}`,
{
headers: {
Accept: 'application/json'
},
withCredentials: true
} }
) )
data = response.data
} else {
// For other modes (:, ^), use fetchSpotlightData from ApiServerContext
data = await fetchSpotlightData(searchQuery.trim())
} }
setLoading(false) setLoading(false)
// If the query contains a prefix mode character, and the response is an object, wrap it in an array // If the query contains a prefix mode character, and the response is an object, wrap it in an array
if ( if (
/[:?^]/.test(searchQuery) && /[:?^]/.test(searchQuery) &&
response.data && data &&
!Array.isArray(response.data) && !Array.isArray(data) &&
typeof response.data === 'object' typeof data === 'object'
) { ) {
setListData([response.data]) setListData([data])
} else { } else {
setListData(response.data) setListData(data)
} }
// Check if there's a pending query after this fetch completes // Check if there's a pending query after this fetch completes
@ -464,11 +466,22 @@ const SpotlightProvider = ({ children }) => {
align='center' align='center'
justify='space-between' justify='space-between'
> >
<Flex gap={'small'} align='center'> <Flex
gap={'small'}
align='center'
style={{ flexGrow: 1 }}
>
{Icon ? <Icon style={{ fontSize: '20px' }} /> : null} {Icon ? <Icon style={{ fontSize: '20px' }} /> : null}
{item.name ? ( {item.name ? (
<Text ellipsis style={{ maxWidth: 170 }}> <Text
ellipsis
style={{
maxWidth: 170,
flexGrow: 1,
width: '100%'
}}
>
{item.name} {item.name}
</Text> </Text>
) : null} ) : null}