|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Link, useParams } from 'react-router-dom'; |
| 3 | +import { FiArrowLeft, FiRefreshCw } from 'react-icons/fi'; |
| 4 | + |
| 5 | +import { spaceService } from '../../services'; |
| 6 | + |
| 7 | +const truncateJson = (value, limit = 4000) => { |
| 8 | + try { |
| 9 | + const s = JSON.stringify(value, null, 2); |
| 10 | + if (s.length <= limit) return s; |
| 11 | + return s.slice(0, limit - 1) + '…'; |
| 12 | + } catch { |
| 13 | + const s = String(value ?? ''); |
| 14 | + if (s.length <= limit) return s; |
| 15 | + return s.slice(0, limit - 1) + '…'; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +const SpaceApiPage = () => { |
| 20 | + const { apiId } = useParams(); |
| 21 | + |
| 22 | + const config = useMemo(() => { |
| 23 | + const defs = { |
| 24 | + apod: { |
| 25 | + title: 'APOD: Astronomy Picture of the Day', |
| 26 | + description: 'Daily astronomy image and metadata.', |
| 27 | + run: () => spaceService.getApod(), |
| 28 | + }, |
| 29 | + neows: { |
| 30 | + title: 'Asteroids NeoWs', |
| 31 | + description: 'Near Earth Object Web Service summary (feed-based).', |
| 32 | + run: () => spaceService.getNeoSummary(), |
| 33 | + }, |
| 34 | + donki: { |
| 35 | + title: 'DONKI', |
| 36 | + description: 'Space weather notifications summary.', |
| 37 | + run: () => spaceService.getDonkiSummary(), |
| 38 | + }, |
| 39 | + eonet: { |
| 40 | + title: 'EONET', |
| 41 | + description: 'Earth Observatory Natural Event Tracker events.', |
| 42 | + run: () => spaceService.getEonetEvents({ status: 'open', limit: 10 }), |
| 43 | + }, |
| 44 | + epic: { |
| 45 | + title: 'EPIC', |
| 46 | + description: 'Earth Polychromatic Imaging Camera (natural images).', |
| 47 | + run: () => spaceService.getEpicLatest(), |
| 48 | + }, |
| 49 | + exoplanet: { |
| 50 | + title: 'Exoplanet Archive', |
| 51 | + description: 'Sample TAP query results from NASA Exoplanet Archive.', |
| 52 | + run: () => spaceService.getExoplanetSample(), |
| 53 | + }, |
| 54 | + images: { |
| 55 | + title: 'NASA Image and Video Library', |
| 56 | + description: 'Search results from images.nasa.gov API.', |
| 57 | + run: () => spaceService.searchNasaImages({ q: 'nebula', media_type: 'image' }), |
| 58 | + }, |
| 59 | + techport: { |
| 60 | + title: 'Techport', |
| 61 | + description: 'NASA technology projects (summary).', |
| 62 | + run: () => spaceService.getTechportProjects(), |
| 63 | + }, |
| 64 | + techtransfer: { |
| 65 | + title: 'TechTransfer', |
| 66 | + description: 'Patents/software tech transfer search preview.', |
| 67 | + run: () => spaceService.searchTechtransferPatents({ q: 'robot' }), |
| 68 | + }, |
| 69 | + ssd_cneos: { |
| 70 | + title: 'SSD/CNEOS', |
| 71 | + description: 'JPL close-approach data (CAD) summary.', |
| 72 | + run: () => spaceService.getSsdCneosCad({ 'dist_max': '0.05', limit: 10 }), |
| 73 | + }, |
| 74 | + tle: { |
| 75 | + title: 'TLE API', |
| 76 | + description: 'Two-line element set (defaults to ISS / NORAD 25544).', |
| 77 | + run: () => spaceService.getTle({ catnr: 25544 }), |
| 78 | + }, |
| 79 | + gibs: { |
| 80 | + title: 'GIBS', |
| 81 | + description: 'Global Imagery Browse Services (WMTS info).', |
| 82 | + run: () => spaceService.getGibsInfo(), |
| 83 | + }, |
| 84 | + trek_wmts: { |
| 85 | + title: 'Vesta/Moon/Mars Trek WMTS', |
| 86 | + description: 'WMTS integration info for Trek imagery tiles.', |
| 87 | + run: () => spaceService.getTrekWmtsInfo(), |
| 88 | + }, |
| 89 | + insight: { |
| 90 | + title: 'InSight: Mars Weather', |
| 91 | + description: 'Status/info for InSight weather API integration.', |
| 92 | + run: () => spaceService.getInsightInfo(), |
| 93 | + }, |
| 94 | + osdr: { |
| 95 | + title: 'Open Science Data Repository', |
| 96 | + description: 'Status/info for OSDR integration.', |
| 97 | + run: () => spaceService.getOsdrInfo(), |
| 98 | + }, |
| 99 | + ssc: { |
| 100 | + title: 'Satellite Situation Center', |
| 101 | + description: 'Status/info for SSC integration.', |
| 102 | + run: () => spaceService.getSscInfo(), |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + return defs[apiId] || null; |
| 107 | + }, [apiId]); |
| 108 | + |
| 109 | + const [state, setState] = useState({ loading: true, error: null, data: null }); |
| 110 | + |
| 111 | + const load = async () => { |
| 112 | + if (!config) return; |
| 113 | + setState({ loading: true, error: null, data: null }); |
| 114 | + try { |
| 115 | + const res = await config.run(); |
| 116 | + setState({ loading: false, error: null, data: res?.data }); |
| 117 | + } catch (e) { |
| 118 | + const msg = e?.response?.data?.detail || e?.response?.data?.error || e?.message || 'Request failed'; |
| 119 | + setState({ loading: false, error: msg, data: null }); |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + load(); |
| 125 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 126 | + }, [apiId]); |
| 127 | + |
| 128 | + if (!config) { |
| 129 | + return ( |
| 130 | + <div className="space-y-4"> |
| 131 | + <div> |
| 132 | + <h1 className="text-3xl font-bold text-white font-['Poppins']">Unknown API</h1> |
| 133 | + <p className="text-gray-400 mt-2">This Space Lab integration key is not configured.</p> |
| 134 | + </div> |
| 135 | + <Link to="/lab/space" className="inline-flex items-center gap-2 text-[#00E0FF] font-semibold"> |
| 136 | + <FiArrowLeft /> |
| 137 | + Back to Space Lab |
| 138 | + </Link> |
| 139 | + </div> |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + return ( |
| 144 | + <div className="space-y-6"> |
| 145 | + <div className="flex items-start justify-between gap-4"> |
| 146 | + <div> |
| 147 | + <h1 className="text-3xl font-bold text-white font-['Poppins']">{config.title}</h1> |
| 148 | + <p className="text-gray-400 mt-2">{config.description}</p> |
| 149 | + </div> |
| 150 | + <div className="flex items-center gap-3"> |
| 151 | + <Link to="/lab/space" className="px-4 py-2 bg-white/5 border border-white/10 rounded-lg text-white font-semibold hover:bg-white/10"> |
| 152 | + <FiArrowLeft className="inline mr-2" /> |
| 153 | + Space Lab |
| 154 | + </Link> |
| 155 | + <button |
| 156 | + type="button" |
| 157 | + onClick={load} |
| 158 | + className="px-4 py-2 bg-gradient-to-r from-[#1A4FFF] to-[#00E0FF] text-white rounded-lg font-semibold hover:shadow-lg hover:shadow-[#1A4FFF]/50" |
| 159 | + > |
| 160 | + <FiRefreshCw className="inline mr-2" /> |
| 161 | + Refresh |
| 162 | + </button> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + |
| 166 | + {state.error && ( |
| 167 | + <div className="bg-red-500/10 border border-red-500/20 rounded-lg p-4 text-sm text-red-200"> |
| 168 | + {state.error} |
| 169 | + </div> |
| 170 | + )} |
| 171 | + |
| 172 | + <div className="bg-white/5 backdrop-blur-md border border-white/10 rounded-xl p-6 shadow-xl"> |
| 173 | + <div className="text-sm text-gray-400 font-semibold mb-3">Response preview</div> |
| 174 | + <pre className="text-xs text-gray-200 overflow-auto whitespace-pre-wrap"> |
| 175 | + {state.loading ? 'Loading…' : truncateJson(state.data)} |
| 176 | + </pre> |
| 177 | + </div> |
| 178 | + |
| 179 | + {apiId === 'apod' && state.data?.media_type === 'image' && state.data?.url && ( |
| 180 | + <div className="bg-white/5 backdrop-blur-md border border-white/10 rounded-xl p-6 shadow-xl"> |
| 181 | + <div className="text-lg font-bold text-white mb-3 font-['Poppins']">APOD Image</div> |
| 182 | + <a href={state.data.hdurl || state.data.url} target="_blank" rel="noreferrer" className="block rounded-lg overflow-hidden border border-white/10"> |
| 183 | + <img src={state.data.url} alt={state.data.title || 'APOD'} className="w-full h-72 object-cover" loading="lazy" /> |
| 184 | + </a> |
| 185 | + </div> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + ); |
| 189 | +}; |
| 190 | + |
| 191 | +export default SpaceApiPage; |
0 commit comments