|  |  | 
 |  |  | import React, { useState, useRef, useEffect, useMemo } from "react"; | 
 |  |  | import { useTranslate, useRefresh, useSidebarState } from "react-admin"; | 
 |  |  | import { | 
 |  |  |     TextField, | 
 |  |  |     Select, | 
 |  |  |     MenuItem, | 
 |  |  |     Button, | 
 |  |  |     Box, | 
 |  |  |     SpeedDial, | 
 |  |  |     SpeedDialAction, | 
 |  |  |     useTheme, | 
 |  |  |     Fab, | 
 |  |  | } from '@mui/material'; | 
 |  |  | import { | 
 |  |  |     MoreVert as MoreVertIcon, | 
 |  |  |     Edit as EditIcon, | 
 |  |  |     FileCopy as FileCopyIcon, | 
 |  |  |     Save as SaveIcon, | 
 |  |  |     Print as PrintIcon, | 
 |  |  |     Share as ShareIcon, | 
 |  |  | } from '@mui/icons-material'; | 
 |  |  | import { MAP_MODE } from "./constants"; | 
 |  |  | import Player from './player'; | 
 |  |  | import * as Tool from './tool'; | 
 |  |  | 
 |  |  | import * as Http from './http'; | 
 |  |  | import WebSocketClient from './websocket' | 
 |  |  | import ConfirmButton from "../page/components/ConfirmButton"; | 
 |  |  | import { FitScreen } from '@mui/icons-material'; | 
 |  |  | import { FitScreen, AltRoute, RotateRight } from '@mui/icons-material'; | 
 |  |  | import MapSearch from "./header/MapSearch"; | 
 |  |  | import { startupOrShutdown } from "./http"; | 
 |  |  | import PulseSignal from "../page/components/PulseSignal"; | 
 |  |  | import FakeFab from "./header/FakeFab"; | 
 |  |  |  | 
 |  |  | let player; | 
 |  |  | let websocket; | 
 |  |  | 
 |  |  |     const [mapContainer, setMapContainer] = useState(null); | 
 |  |  |  | 
 |  |  |     const [mode, setMode] = useState(MAP_MODE.OBSERVER_MODE); | 
 |  |  |     const modeRef = useRef(mode); | 
 |  |  |     const [dataFetched, setDataFetched] = useState(false); | 
 |  |  |     const [insightVisible, setInsightVisible] = useState(false); | 
 |  |  |     const [deviceVisible, setDeviceVisible] = useState(false); | 
 |  |  |     const [settingsVisible, setSettingsVisible] = useState(false); | 
 |  |  |     const [batchSelectionVisible, setBatchSelectionVisible] = useState(false); | 
 |  |  |  | 
 |  |  |     const [spriteSettings, setSpriteSettings] = useState(null); | 
 |  |  |     const prevSpriteSettingsRef = useRef(); | 
 |  |  |     const [curSprite, setCurSprite] = useState(null); | 
 |  |  |     const [batchSprites, setBatchSprites] = useState([]); | 
 |  |  |  | 
 |  |  |     const [rcsStatus, setRcsStatus] = useState(null); | 
 |  |  |     const [showRoutes, setShowRoutes] = useState(false); | 
 |  |  |     const [curZone, setCurZone] = useState(() => { | 
 |  |  |         const storedValue = localStorage.getItem('curZone'); | 
 |  |  |         return storedValue !== null ? JSON.parse(storedValue) : null; | 
 |  |  |     }); | 
 |  |  |  | 
 |  |  |     const handleResize = () => { | 
 |  |  |         if (!contentRef) { return; } | 
 |  |  |         const width = contentRef.current.offsetWidth; | 
 |  |  |         const height = contentRef.current.offsetHeight; | 
 |  |  |         player.resize(width, height); | 
 |  |  | 
 |  |  |             Http.setMapContainer(player.mapContainer); | 
 |  |  |             websocket = new WebSocketClient('/ws/map/websocket'); | 
 |  |  |  | 
 |  |  |             await Http.fetchMapData(); | 
 |  |  |             await Http.fetchMapData(curZone, setRcsStatus, setCurSprite); | 
 |  |  |             websocket.connect(); | 
 |  |  |             websocket.onMessage = (wsMsg) => { | 
 |  |  |                 Tool.generateDynamicGraphic(curZone, JSON.parse(wsMsg)); | 
 |  |  |                 if (modeRef.current === MAP_MODE.OBSERVER_MODE) { | 
 |  |  |                     Tool.generateDynamicGraphic(curZone, JSON.parse(wsMsg), setCurSprite); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             player.rotateMap(localStorage.getItem('mapRotation')); | 
 |  |  |             setTimeout(() => { | 
 |  |  |                 player.adaptScreen(); | 
 |  |  |                 notify.info(translate('page.map.welcome')); | 
 |  |  |                 player.adaptScreen(); | 
 |  |  |                 setDataFetched(true); | 
 |  |  |             }, 200) | 
 |  |  |         } | 
 |  |  |         initialize(); | 
 |  |  | 
 |  |  |     }, [themeMode]) | 
 |  |  |  | 
 |  |  |     const switchMode = (mode) => { | 
 |  |  |         Tool.removeSelectedEffect(); | 
 |  |  |         modeRef.current = mode; | 
 |  |  |  | 
 |  |  |         Tool.removeSelectedEffect(); | 
 |  |  |         player.hideGridLines(); | 
 |  |  |  | 
 |  |  |         setInsightVisible(false); | 
 |  |  |         setDeviceVisible(false); | 
 |  |  |         setSettingsVisible(false); | 
 |  |  |         setBatchSelectionVisible(false); | 
 |  |  |  | 
 |  |  |         setSpriteSettings(null); | 
 |  |  |         setCurSprite(null); | 
 |  |  |         setBatchSprites([]); | 
 |  |  |  | 
 |  |  |         switch (mode) { | 
 |  |  |             case MAP_MODE.OBSERVER_MODE: | 
 |  |  |                 player.hideGridLines(); | 
 |  |  |                 player.hideStarryBackground(); | 
 |  |  |  | 
 |  |  |                 player.activateMapMultiSelect((selectedSprites, restartFn) => { | 
 |  |  |                     setBatchSprites(selectedSprites); | 
 |  |  |                     Tool.multipleSelectEnhancer(selectedSprites, setCurSprite, setBatchSprites); | 
 |  |  |                 }); | 
 |  |  |  | 
 |  |  |                 mapContainer.children.forEach(child => { | 
 |  |  |                     Tool.beInsight(child, setCurSprite); | 
 |  |  |                 }) | 
 |  |  |                 break | 
 |  |  |             case MAP_MODE.MOVABLE_MODE: | 
 |  |  |                 player.showGridLines(); | 
 |  |  |                 player.hideStarryBackground(); | 
 |  |  |                 Tool.hideRoutes(curZone, setShowRoutes); | 
 |  |  |  | 
 |  |  |                 player.activateMapMultiSelect((selectedSprites, restartFn) => { | 
 |  |  |                     Tool.spriteListBeMovable(selectedSprites, () => { | 
 |  |  | 
 |  |  |                 }) | 
 |  |  |                 break | 
 |  |  |             case MAP_MODE.SETTINGS_MODE: | 
 |  |  |                 player.hideGridLines(); | 
 |  |  |                 // player.showStarryBackground(); // 0x2f68ac | 
 |  |  |  | 
 |  |  |                 player.activateMapMultiSelect((selectedSprites, restartFn) => { | 
 |  |  |                     setBatchSprites(selectedSprites); | 
 |  |  |                     Tool.multipleSelectEnhancer(selectedSprites, setCurSprite, setBatchSprites); | 
 |  |  |                 }); | 
 |  |  |  | 
 |  |  |                 mapContainer.children.forEach(child => { | 
 |  |  |                     Tool.beSettings(child, setSpriteSettings); | 
 |  |  |                     Tool.beSettings(child, setCurSprite); | 
 |  |  |                 }) | 
 |  |  |                 break | 
 |  |  |             default: | 
 |  |  | 
 |  |  |         sprite.x = mapX; | 
 |  |  |         sprite.y = mapY; | 
 |  |  |  | 
 |  |  |         // sprite.scale.set(mapContainer.scale.x); | 
 |  |  |         sprite.rotation = -mapContainer.rotation; | 
 |  |  |  | 
 |  |  |         Tool.initSprite(sprite, type); | 
 |  |  |         mapContainer.addChild(sprite); | 
 |  |  |         Tool.beMovable(sprite); | 
 |  |  |     }; | 
 |  |  |  | 
 |  |  |     // watch spriteSettings | 
 |  |  |     useEffect(() => { | 
 |  |  |     // watch curSprite | 
 |  |  |     React.useEffect(() => { | 
 |  |  |         if (!mapContainer) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         prevSpriteSettingsRef.current = spriteSettings; | 
 |  |  |         if (spriteSettings && prevSpriteSettings !== spriteSettings) { | 
 |  |  |             Tool.removeSelectedEffect(); | 
 |  |  |         } | 
 |  |  |         if (spriteSettings) { | 
 |  |  |             Tool.showSelectedEffect(spriteSettings) | 
 |  |  |             setSettingsVisible(true); | 
 |  |  |         Tool.removeSelectedEffect(); | 
 |  |  |         if (curSprite) { | 
 |  |  |             if (mode === MAP_MODE.OBSERVER_MODE) { | 
 |  |  |                 Tool.showSelectedEffect(curSprite); | 
 |  |  |                 setInsightVisible(true); | 
 |  |  |             } | 
 |  |  |             if (mode === MAP_MODE.SETTINGS_MODE) { | 
 |  |  |                 Tool.showSelectedEffect(curSprite); | 
 |  |  |                 setSettingsVisible(true); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             Tool.removeSelectedEffect(); | 
 |  |  |             setInsightVisible(false); | 
 |  |  |             setSettingsVisible(false); | 
 |  |  |         } | 
 |  |  |     }, [spriteSettings, mapContainer]) | 
 |  |  |     const prevSpriteSettings = prevSpriteSettingsRef.current; | 
 |  |  |     }, [curSprite]); | 
 |  |  |  | 
 |  |  |     // watch batchSprites | 
 |  |  |     React.useEffect(() => { | 
 |  |  | 
 |  |  |             setBatchSelectionVisible(false) | 
 |  |  |         } | 
 |  |  |     }, [batchSprites]) | 
 |  |  |  | 
 |  |  |     const actions = [ | 
 |  |  |         { icon: <FileCopyIcon />, name: '复制' }, | 
 |  |  |         { icon: <SaveIcon />, name: '保存' }, | 
 |  |  |         { icon: <PrintIcon />, name: '打印' }, | 
 |  |  |         { icon: <ShareIcon />, name: '分享' }, | 
 |  |  |         { icon: <EditIcon />, name: '编辑' }, | 
 |  |  |     ]; | 
 |  |  |  | 
 |  |  |     return ( | 
 |  |  |         <Box | 
 |  |  | 
 |  |  |                 <MapSearch | 
 |  |  |                     mode={mode} | 
 |  |  |                     setMode={setMode} | 
 |  |  |                     dataFetched={dataFetched} | 
 |  |  |                     curZone={curZone} | 
 |  |  |                     setSpriteSettings={setSpriteSettings} | 
 |  |  |                     curSprite={curSprite} | 
 |  |  |                     setCurSprite={setCurSprite} | 
 |  |  |                 /> | 
 |  |  |                 <Box sx={{ flexGrow: 1 }} /> | 
 |  |  |  | 
 |  |  |                 {mode === MAP_MODE.OBSERVER_MODE && ( | 
 |  |  |                     <> | 
 |  |  |                         <Box sx={{ mr: 2, display: 'flex', alignItems: 'center' }}> | 
 |  |  |                             <PulseSignal | 
 |  |  |                                 negative | 
 |  |  |                                 negativeColor='#a4b0be' | 
 |  |  |                                 flag={rcsStatus} | 
 |  |  |                                 width={12} | 
 |  |  |                             /> | 
 |  |  |                         </Box> | 
 |  |  |                         <Button | 
 |  |  |                             variant="contained" | 
 |  |  |                             color="primary" | 
 |  |  |                             color={rcsStatus ? 'inherit' : 'primary'} | 
 |  |  |                             sx={{ mr: 2 }} | 
 |  |  |                             onClick={() => { | 
 |  |  |                                 startupOrShutdown(() => { | 
 |  |  |                                     setRcsStatus(!rcsStatus); | 
 |  |  |                                 }); | 
 |  |  |                             }} | 
 |  |  |                         > | 
 |  |  |                             {translate('page.map.action.startup')} | 
 |  |  |                             {rcsStatus ? translate('page.map.action.shutdown') : translate('page.map.action.startup')} | 
 |  |  |                         </Button> | 
 |  |  |                         <Button variant="contained" color="primary"> | 
 |  |  |                             {translate('page.map.action.monitor')} | 
 |  |  | 
 |  |  |                 )} | 
 |  |  |  | 
 |  |  |                 <Select | 
 |  |  |                     value={mode} | 
 |  |  |                     value={mode ?? ''} | 
 |  |  |                     onChange={(event) => { | 
 |  |  |                         setMode(event.target.value); | 
 |  |  |                     }} | 
 |  |  | 
 |  |  |                     flexGrow: 1,    // fill remaining of map space  | 
 |  |  |                     position: 'relative', | 
 |  |  |                     backgroundColor: '#fff', | 
 |  |  |                     ...(mode === MAP_MODE.SETTINGS_MODE && { | 
 |  |  |                         animation: 'settingsPulse 1.5s infinite', | 
 |  |  |                         '@keyframes settingsPulse': { | 
 |  |  |                             '0%': { | 
 |  |  |                                 boxShadow: `0 0 3px 1px ${theme.palette.primary.main.replace('rgb', 'rgba').replace(')', `, 0.1)`)}`, | 
 |  |  |                             }, | 
 |  |  |                             '50%': { | 
 |  |  |                                 boxShadow: `0 0 8px 3px ${theme.palette.primary.main.replace('rgb', 'rgba').replace(')', `, 0.5)`)}`, | 
 |  |  |                             }, | 
 |  |  |                             '100%': { | 
 |  |  |                                 boxShadow: `0 0 3px 1px ${theme.palette.primary.main.replace('rgb', 'rgba').replace(')', `, 0.1)`)}`, | 
 |  |  |                             }, | 
 |  |  |                         }, | 
 |  |  |                     }) | 
 |  |  |                 }} | 
 |  |  |             > | 
 |  |  |                 <Box | 
 |  |  | 
 |  |  |                     }} /> | 
 |  |  |                 </Box> | 
 |  |  |  | 
 |  |  |                 {/* <SpeedDial | 
 |  |  |                     ariaLabel="SpeedDial 示例" | 
 |  |  |                     sx={{ position: 'absolute', bottom: 16, right: 16 }} | 
 |  |  |                     icon={<MoreVertIcon />} | 
 |  |  |                 > | 
 |  |  |                     {actions.map((action) => ( | 
 |  |  |                         <SpeedDialAction | 
 |  |  |                             key={action.name} | 
 |  |  |                             icon={action.icon} | 
 |  |  |                             tooltipTitle={action.name} | 
 |  |  |                         /> | 
 |  |  |                     ))} | 
 |  |  |                 </SpeedDial> */} | 
 |  |  |  | 
 |  |  |                 <Box | 
 |  |  |                     sx={{ | 
 |  |  |                         position: 'absolute', | 
 |  |  | 
 |  |  |                         bottom: 30, | 
 |  |  |                         display: 'flex', | 
 |  |  |                         flexDirection: 'column', | 
 |  |  |                         gap: 1 | 
 |  |  |                         gap: 2 | 
 |  |  |                     }} | 
 |  |  |                 > | 
 |  |  |                     {mode !== MAP_MODE.MOVABLE_MODE && ( | 
 |  |  |                         <> | 
 |  |  |                             <Fab | 
 |  |  |                                 variant="extended" | 
 |  |  |                                 color={showRoutes ? 'primary' : 'default'} | 
 |  |  |                                 size="small" | 
 |  |  |                                 onClick={() => { | 
 |  |  |                                     showRoutes ? Tool.hideRoutes(curZone, setShowRoutes) : Tool.showRoutes(curZone, setShowRoutes) | 
 |  |  |                                 }} | 
 |  |  |                             > | 
 |  |  |                                 <AltRoute /> | 
 |  |  |                             </Fab> | 
 |  |  |                             <FakeFab | 
 |  |  |                             /> | 
 |  |  |                         </> | 
 |  |  |                     )} | 
 |  |  |                     <Fab | 
 |  |  |                         variant="extended" | 
 |  |  |                         color="primary" | 
 |  |  |                         size="medium" | 
 |  |  |                         size="small" | 
 |  |  |                         onClick={() => { | 
 |  |  |                             player.rotateMap(Math.PI / 2); | 
 |  |  |                         }} | 
 |  |  |                     > | 
 |  |  |                         <RotateRight /> | 
 |  |  |                         {/*  {translate('page.map.action.adapt')} */} | 
 |  |  |                     </Fab> | 
 |  |  |                     <Fab | 
 |  |  |                         variant="extended" | 
 |  |  |                         color="primary" | 
 |  |  |                         size="small" | 
 |  |  |                         onClick={() => { | 
 |  |  |                             player.adaptScreen(); | 
 |  |  |                         }} | 
 |  |  |                     > | 
 |  |  |                         <FitScreen /> | 
 |  |  |                          {translate('page.map.action.adapt')} | 
 |  |  |                         {/*  {translate('page.map.action.adapt')} */} | 
 |  |  |                     </Fab> | 
 |  |  |                 </Box> | 
 |  |  |             </Box> | 
 |  |  | 
 |  |  |             <Insight | 
 |  |  |                 open={insightVisible} | 
 |  |  |                 onCancel={() => { | 
 |  |  |                     setInsightVisible(false); | 
 |  |  |                     setCurSprite(null); | 
 |  |  |                 }} | 
 |  |  |                 width={378} | 
 |  |  |                 sprite={curSprite} | 
 |  |  |                 width={570} | 
 |  |  |             /> | 
 |  |  |  | 
 |  |  |             <Device | 
 |  |  | 
 |  |  |             <Settings | 
 |  |  |                 open={settingsVisible} | 
 |  |  |                 onCancel={() => { | 
 |  |  |                     setSpriteSettings(null); | 
 |  |  |                     setSettingsVisible(false); | 
 |  |  |                     setCurSprite(null); | 
 |  |  |                 }} | 
 |  |  |                 sprite={spriteSettings} | 
 |  |  |                 setSpriteSettings={setSpriteSettings} | 
 |  |  |                 sprite={curSprite} | 
 |  |  |                 setSpriteSettings={setCurSprite} | 
 |  |  |                 width={570} | 
 |  |  |             /> | 
 |  |  |  | 
 |  |  | 
 |  |  |                 open={batchSelectionVisible} | 
 |  |  |                 onCancel={() => { | 
 |  |  |                     setBatchSprites([]); | 
 |  |  |                     setBatchSelectionVisible(false); | 
 |  |  |                 }} | 
 |  |  |                 batchSprites={batchSprites} | 
 |  |  |                 mode={mode} |