import React, { useState, useEffect, useCallback } from 'react';
|
import { useTranslate } from 'react-admin';
|
import { Box, Tabs, Tab, Divider } from '@mui/material';
|
import JsonShow from '../../JsonShow';
|
import { getCodeInfo, getCodeRouteList, getCodeFuncStaList } from '../../http';
|
import CodeMain from './CodeMain';
|
|
const CodeInsight = ({ sprite, setTitle }) => {
|
const translate = useTranslate();
|
const [activeTab, setActiveTab] = useState(0);
|
const [currentCode, setCurrentCode] = useState(null);
|
const [codeInfo, setCodeInfo] = useState(null);
|
const [loading, setLoading] = useState(false);
|
|
const codeLabel = translate('page.map.devices.code', {
|
_: '地面码'
|
});
|
|
const fetchCodeInfo = useCallback(async (code, options = {}) => {
|
if (!code) {
|
return;
|
}
|
if (!options?.silent) {
|
setLoading(true);
|
}
|
setTitle(`${codeLabel} - ${code}`);
|
setCurrentCode(code);
|
try {
|
const [info, routeList, funcStaList] = await Promise.all([
|
new Promise((resolve) => {
|
getCodeInfo(code, resolve, () => resolve(null));
|
}),
|
new Promise((resolve) => {
|
getCodeRouteList(code, resolve, () => resolve([]));
|
}),
|
new Promise((resolve) => {
|
getCodeFuncStaList(code, resolve, () => resolve([]));
|
}),
|
]);
|
setCodeInfo(info ? {
|
...info,
|
routeList,
|
funcStaList,
|
} : null);
|
} finally {
|
if (!options?.silent) {
|
setLoading(false);
|
}
|
}
|
}, [codeLabel, setTitle]);
|
|
useEffect(() => {
|
if (sprite?.data?.no) {
|
fetchCodeInfo(sprite.data.no);
|
setActiveTab(0);
|
} else {
|
setCodeInfo(null);
|
setCurrentCode(null);
|
setTitle(null);
|
}
|
return () => {
|
setCodeInfo(null);
|
setCurrentCode(null);
|
setTitle(null);
|
};
|
}, [sprite, fetchCodeInfo, setTitle]);
|
|
const handleTabChange = (_, newValue) => {
|
setActiveTab(newValue);
|
};
|
|
return (
|
<Box sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
|
<Tabs
|
value={activeTab}
|
onChange={handleTabChange}
|
centered
|
sx={{ mb: 0 }}
|
>
|
<Tab label={translate('page.map.insight.title')} />
|
<Tab label={'JSON'} />
|
</Tabs>
|
|
<Divider />
|
|
<Box flex={1} pt={2} sx={{ overflow: 'hidden' }}>
|
{activeTab === 0 && (
|
<CodeMain
|
sprite={sprite}
|
codeInfo={codeInfo}
|
loading={loading}
|
code={currentCode}
|
/>
|
)}
|
{activeTab === 1 && (
|
<JsonShow
|
data={codeInfo || sprite?.data}
|
height={550}
|
/>
|
)}
|
</Box>
|
</Box>
|
);
|
};
|
|
export default CodeInsight;
|