|
| 1 | +import { useState, useContext } from 'react'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import { ApplicationContext } from '../../context/ApplicationContext'; |
| 4 | +import { QueryContext } from '../../context/QueryContext'; |
| 5 | +import { CommsContext } from '../../context/CommsContext'; |
| 6 | +import { HealthContext } from '../../context/HealthContext'; |
| 7 | + |
| 8 | +export const useGraphNavBar = (setChart: (route: string) => void) => { |
| 9 | + const [prevRoute, setPrevRoute] = useState<string>(''); |
| 10 | + const { servicesData } = useContext(ApplicationContext); |
| 11 | + const { selectedMetrics } = useContext(QueryContext); |
| 12 | + const { commsData } = useContext(CommsContext); |
| 13 | + const { services } = useContext(HealthContext); |
| 14 | + const navigate = useNavigate(); |
| 15 | + |
| 16 | + const routing = (route: string) => { |
| 17 | + if (location.href.includes('communications')) { |
| 18 | + if (prevRoute === '') navigate(`${servicesData[0].microservice}`); |
| 19 | + else navigate(prevRoute); |
| 20 | + } |
| 21 | + setChart(route); |
| 22 | + }; |
| 23 | + |
| 24 | + const getHealthAndEventComponents = (chart: string) => { |
| 25 | + const buttonList: JSX.Element[] = []; |
| 26 | + if (selectedMetrics) { |
| 27 | + selectedMetrics.forEach((element, id) => { |
| 28 | + const categoryName = Object.keys(element)[0]; |
| 29 | + let prefix; |
| 30 | + if (categoryName === 'Event') { |
| 31 | + prefix = 'event_'; |
| 32 | + } else if (['books', 'customers', 'frontend', 'orders'].includes(categoryName)) { |
| 33 | + prefix = 'docker_'; |
| 34 | + } else { |
| 35 | + prefix = 'health_'; |
| 36 | + } |
| 37 | + buttonList.push( |
| 38 | + <button |
| 39 | + id={`${prefix}${categoryName}-button`} |
| 40 | + className={chart === `${prefix}${categoryName}` ? 'selected' : undefined} |
| 41 | + onClick={() => routing(`${prefix}${categoryName}`)} |
| 42 | + key={`1-${id}`} |
| 43 | + > |
| 44 | + {categoryName} |
| 45 | + </button> |
| 46 | + ); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + return buttonList; |
| 51 | + }; |
| 52 | + |
| 53 | + return { |
| 54 | + services, |
| 55 | + servicesData, |
| 56 | + selectedMetrics, |
| 57 | + commsData, |
| 58 | + prevRoute, |
| 59 | + setPrevRoute, |
| 60 | + routing, |
| 61 | + getHealthAndEventComponents, |
| 62 | + }; |
| 63 | +}; |
0 commit comments