From 87a619817399fde31a4f5036c263fd2b1368e8f1 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Sat, 29 Nov 2025 01:25:13 +0000 Subject: [PATCH] Now uses ApiContext --- .../Dashboard/context/SpotlightContext.jsx | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/components/Dashboard/context/SpotlightContext.jsx b/src/components/Dashboard/context/SpotlightContext.jsx index 27cd346..3a47528 100644 --- a/src/components/Dashboard/context/SpotlightContext.jsx +++ b/src/components/Dashboard/context/SpotlightContext.jsx @@ -14,7 +14,8 @@ import { useEffect, useState, useRef, - createElement + createElement, + useContext } from 'react' import axios from 'axios' import { LoadingOutlined } from '@ant-design/icons' @@ -29,6 +30,8 @@ import { getModelByPrefix } from '../../../database/ObjectModels' import InfoCircleIcon from '../../Icons/InfoCircleIcon' +import { ApiServerContext } from './ApiServerContext' +import { AuthContext } from './AuthContext' const SpotlightContext = createContext() @@ -41,6 +44,8 @@ const SpotlightProvider = ({ children }) => { const [listData, setListData] = useState([]) const [messageApi, contextHolder] = message.useMessage() const [inputPrefix, setInputPrefix] = useState({ prefix: '', mode: null }) + const { fetchSpotlightData } = useContext(ApiServerContext) + const { token } = useContext(AuthContext) // Refs for throttling/debouncing const lastFetchTime = useRef(0) @@ -116,7 +121,7 @@ const SpotlightProvider = ({ children }) => { setLoading(true) setListData([]) - let response + let data // Check if we have a prefix with ? mode (filter mode) if (inputPrefix && inputPrefix.mode === '?') { @@ -128,37 +133,34 @@ const SpotlightProvider = ({ children }) => { // Parse the query parameters const params = new URLSearchParams(queryParams) - response = await axios.get(`${config.backendUrl}/spotlight/${prefix}`, { - params: params, - headers: { - Accept: 'application/json' - }, - withCredentials: true - }) - } else { - // For other modes (:, ^), use the original behavior - response = await axios.get( - `${config.backendUrl}/spotlight/${encodeURIComponent(searchQuery.trim())}`, + // For ? mode, use axios directly with query params + const response = await axios.get( + `${config.backendUrl}/spotlight/${prefix}`, { + params: params, headers: { - Accept: 'application/json' - }, - withCredentials: true + Accept: 'application/json', + Authorization: `Bearer ${token}` + } } ) + data = response.data + } else { + // For other modes (:, ^), use fetchSpotlightData from ApiServerContext + data = await fetchSpotlightData(searchQuery.trim()) } setLoading(false) // If the query contains a prefix mode character, and the response is an object, wrap it in an array if ( /[:?^]/.test(searchQuery) && - response.data && - !Array.isArray(response.data) && - typeof response.data === 'object' + data && + !Array.isArray(data) && + typeof data === 'object' ) { - setListData([response.data]) + setListData([data]) } else { - setListData(response.data) + setListData(data) } // Check if there's a pending query after this fetch completes @@ -464,11 +466,22 @@ const SpotlightProvider = ({ children }) => { align='center' justify='space-between' > - + {Icon ? : null} {item.name ? ( - + {item.name} ) : null}