import { Widget } from '@/hooks/Mapper/components/mapInterface/components'; import { useMapRootState } from '@/hooks/Mapper/mapRootProvider'; import { LayoutEventBlocker, SystemViewStandalone, TooltipPosition, WdCheckbox, WdImgButton, } from '@/hooks/Mapper/components/ui-kit'; import { useLoadSystemStatic } from '@/hooks/Mapper/mapRootProvider/hooks/useLoadSystemStatic.ts'; import { MouseEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { getSystemById } from '@/hooks/Mapper/helpers/getSystemById.ts'; import classes from './RoutesWidget.module.scss'; import { useLoadRoutes } from './hooks'; import { RoutesList } from './RoutesList'; import clsx from 'clsx'; import { Route } from '@/hooks/Mapper/types/routes.ts'; import { PrimeIcons } from 'primereact/api'; import { RoutesSettingsDialog } from './RoutesSettingsDialog'; import { RoutesProvider, useRouteProvider } from './RoutesProvider.tsx'; import { ContextMenuSystemInfo, useContextMenuSystemInfoHandlers } from '@/hooks/Mapper/components/contexts'; import useMaxWidth from '@/hooks/Mapper/hooks/useMaxWidth.ts'; import { WdTooltipWrapper } from '@/hooks/Mapper/components/ui-kit/WdTooltipWrapper'; import { AddSystemDialog, SearchOnSubmitCallback, } from '@/hooks/Mapper/components/mapInterface/components/AddSystemDialog'; import { OutCommand } from '@/hooks/Mapper/types'; const sortByDist = (a: Route, b: Route) => { const distA = a.has_connection ? a.systems?.length || 0 : Infinity; const distB = b.has_connection ? b.systems?.length || 0 : Infinity; return distA - distB; }; export const RoutesWidgetContent = () => { const { data: { selectedSystems, hubs = [], systems, routes }, outCommand, } = useMapRootState(); const [systemId] = selectedSystems; const { loading } = useLoadRoutes(); const { systems: systemStatics, loadSystems, lastUpdateKey } = useLoadSystemStatic({ systems: hubs ?? [] }); const { open, ...systemCtxProps } = useContextMenuSystemInfoHandlers({ outCommand, hubs, }); const preparedHubs = useMemo(() => { return hubs.map(x => { const sys = getSystemById(systems, x.toString()); return { ...systemStatics.get(parseInt(x))!, ...(sys && { customName: sys.name ?? '' }) }; }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [hubs, systems, systemStatics, lastUpdateKey]); const preparedRoutes: Route[] = useMemo(() => { return ( routes?.routes .sort(sortByDist) .filter(x => x.destination.toString() !== systemId) .map(route => ({ ...route, mapped_systems: route.systems?.map(solar_system_id => routes?.systems_static_data.find( system_static_data => system_static_data.solar_system_id === solar_system_id, ), ) ?? [], })) ?? [] ); }, [routes?.routes, routes?.systems_static_data, systemId]); const refData = useRef({ open, loadSystems, preparedRoutes }); refData.current = { open, loadSystems, preparedRoutes }; useEffect(() => { (async () => await refData.current.loadSystems(hubs))(); }, [hubs]); const handleClick = useCallback((e: MouseEvent, systemId: string) => { const route = refData.current.preparedRoutes.find(x => x.destination.toString() === systemId); refData.current.open(e, systemId, route?.mapped_systems ?? []); }, []); const handleContextMenu = useCallback( async (e: MouseEvent, systemId: string) => { await refData.current.loadSystems([systemId]); handleClick(e, systemId); }, [handleClick], ); if (loading) { return (