// SpotlightTooltip.js import PropTypes from 'prop-types' import { message, Descriptions, Card, Flex, Typography, Skeleton, Spin, Badge } from 'antd' import { LoadingOutlined, ExportOutlined } from '@ant-design/icons' import React, { useEffect, useState, useContext, useCallback } from 'react' import axios from 'axios' import { AuthContext } from '../context/AuthContext' import config from '../../../config' import IdText from './IdText' import TimeDisplay from './TimeDisplay' import { Tag } from 'antd' import InfoCircleIcon from '../../Icons/InfoCircleIcon' import PrinterState from './PrinterState' import JobState from './JobState' import FilamentStockState from './FilamentStockState' import SubJobState from './SubJobState' const { Text, Link } = Typography const SpotlightTooltip = ({ query, type }) => { const [spotlightData, setSpotlightData] = useState(null) const [loading, setLoading] = useState(true) const [messageApi] = message.useMessage() const { authenticated } = useContext(AuthContext) const fetchSpotlightData = useCallback(async () => { if (!authenticated) { return } setLoading(true) try { const response = await axios.get( `${config.backendUrl}/spotlight/${query}`, { headers: { Accept: 'application/json' }, withCredentials: true // Important for including cookies } ) setSpotlightData(response.data) setLoading(false) } catch (error) { setLoading(false) if (error.response) { messageApi.error( `Error fetching spotlight data: ${error.response.status}` ) } else { messageApi.error( 'An unexpected error occurred. Please try again later.' ) } } }, [authenticated, messageApi, query]) useEffect(() => { if (authenticated) { fetchSpotlightData() } }, [authenticated, fetchSpotlightData]) if (!spotlightData && !loading) { return ( No spotlight data. ) } // Helper to render value nicely const renderValue = (key, value) => { if (key === '_id' || key === 'id') { return ( ) } if (key === 'state') { if (type === 'printer') { return ( ) } if (type === 'job') { return ( ) } if (type === 'subjob') { return ( ) } if (type === 'filamentstock') { return ( ) } } if (key === 'tags' && Array.isArray(value)) { if (value.length == 0) { return n/a } return value.map((tag) => ( {tag} )) } if (key === 'email') { return ( {value + ' '} ) } if (key === 'color') { return } if ( typeof value === 'string' && value.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/) ) { // Format ISO date strings return ( ) } if (Array.isArray(value)) { return value.join(', ') } if (typeof value === 'object' && value !== null) { // For nested objects, show JSON string return JSON.stringify(value) } if (value == '' || value.length == 0) { return n/a } if (value != null) { return {value.toString()} } return n/a } // Map of property names to user-friendly labels const LABEL_MAP = { name: 'Name', state: 'State', tags: 'Tags', email: 'Email', updatedAt: 'Updated At', _id: 'ID' // Add more mappings as needed } return (
} spinning={loading}> {loading ? ( <> } > } > } > ) : ( Object.entries(spotlightData).map(([key, value]) => value !== undefined && value !== null && value !== '' ? ( {renderValue( key, key === 'state' && value.type ? value.type : value )} ) : null ) )}
) } SpotlightTooltip.propTypes = { query: PropTypes.string.isRequired, type: PropTypes.string.isRequired } export default SpotlightTooltip