// SpotlightTooltip.js import PropTypes from 'prop-types' import { Descriptions, Card, Flex, Typography, Skeleton, Spin } from 'antd' import { LoadingOutlined } from '@ant-design/icons' import { useEffect, useState, useContext, useCallback } from 'react' import { AuthContext } from '../context/AuthContext' import ObjectProperty from './ObjectProperty' import InfoCircleIcon from '../../Icons/InfoCircleIcon' import { ApiServerContext } from '../context/ApiServerContext' import merge from 'lodash/merge' import { getModelByName } from '../../../database/ObjectModels' const { Text } = Typography const SpotlightTooltip = ({ query, type }) => { const [spotlightData, setSpotlightData] = useState(null) const [loading, setLoading] = useState(true) const [initialized, setInitialized] = useState(false) const { token } = useContext(AuthContext) const { fetchSpotlightData, subscribeToObjectUpdates, connected } = useContext(ApiServerContext) // Get the model for this type const model = getModelByName(type) const modelProperties = model.properties || [] // Update event handler const updateObjectEventHandler = useCallback((value) => { setSpotlightData((prev) => merge({}, prev, value)) }, []) const fetchSpotlight = useCallback(async () => { setLoading(true) const data = await fetchSpotlightData(query) setSpotlightData(data) setLoading(false) }, [query, fetchSpotlightData]) useEffect(() => { if (token != null && initialized == false) { fetchSpotlight() setInitialized(true) } }, [token, fetchSpotlight, initialized]) // Subscribe to object updates when component mounts useEffect(() => { if (spotlightData?._id && type && connected && token) { const objectUpdatesUnsubscribe = subscribeToObjectUpdates( spotlightData._id, type, updateObjectEventHandler ) return () => { if (objectUpdatesUnsubscribe) objectUpdatesUnsubscribe() } } }, [ spotlightData?._id, type, subscribeToObjectUpdates, connected, token, updateObjectEventHandler ]) if (!spotlightData && !loading) { return ( No spotlight data. ) } // Helper to get nested property value const getNestedValue = (obj, path) => { return path.split('.').reduce((current, key) => current?.[key], obj) } return (
} spinning={loading}> {loading ? ( <> } > } > } > ) : ( modelProperties .filter((prop) => { const value = getNestedValue(spotlightData, prop.name) return value !== undefined && value !== null && value !== '' }) .map((prop) => { const value = getNestedValue(spotlightData, prop.name) return ( ) }) )}
) } SpotlightTooltip.propTypes = { query: PropTypes.string.isRequired, type: PropTypes.string.isRequired } export default SpotlightTooltip