|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { use, Suspense } from "react" |
| 7 | +import { useNavigate } from "@tanstack/react-router" |
| 8 | +import { Stack, Spinner } from "@cloudoperators/juno-ui-components" |
| 9 | +import { Vulnerability } from "../../utils" |
| 10 | +import { ApolloQueryResult } from "@apollo/client" |
| 11 | +import { GetVulnerabilitiesQuery } from "../../../../generated/graphql" |
| 12 | +import { getNormalizedVulnerabilitiesResponse } from "../../utils" |
| 13 | + |
| 14 | +type VulnerabilityServicesProps = { |
| 15 | + vulnerabilityName: string |
| 16 | + vulnerabilitiesPromise: Promise<ApolloQueryResult<GetVulnerabilitiesQuery>> |
| 17 | + onServiceClick?: (serviceCcrn: string) => void |
| 18 | +} |
| 19 | + |
| 20 | +export const VulnerabilityServices = ({ |
| 21 | + vulnerabilityName, |
| 22 | + vulnerabilitiesPromise, |
| 23 | + onServiceClick, |
| 24 | +}: VulnerabilityServicesProps) => { |
| 25 | + const navigate = useNavigate() |
| 26 | + |
| 27 | + const handleServiceClick = (serviceCcrn: string) => { |
| 28 | + if (onServiceClick) { |
| 29 | + onServiceClick(serviceCcrn) |
| 30 | + } else { |
| 31 | + navigate({ |
| 32 | + to: "/services/$service", |
| 33 | + params: { service: serviceCcrn }, |
| 34 | + }) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Use the promise passed from the parent |
| 39 | + const { data } = use(vulnerabilitiesPromise) |
| 40 | + |
| 41 | + // Get vulnerability data from the response |
| 42 | + const { vulnerabilities } = getNormalizedVulnerabilitiesResponse(data) |
| 43 | + const vulnerabilityData = vulnerabilities.find((vuln: Vulnerability) => vuln.name === vulnerabilityName) |
| 44 | + |
| 45 | + if (!vulnerabilityData) { |
| 46 | + return <div className="text-sm text-theme-light">Vulnerability not found: {vulnerabilityName}</div> |
| 47 | + } |
| 48 | + |
| 49 | + const services = vulnerabilityData.services || [] |
| 50 | + |
| 51 | + if (services.length === 0) { |
| 52 | + return <div className="text-sm text-theme-light">No services affected by this vulnerability.</div> |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="mb-4"> |
| 57 | + <Suspense |
| 58 | + fallback={ |
| 59 | + <Stack gap="2" alignment="center"> |
| 60 | + <div>Loading</div> |
| 61 | + <Spinner variant="primary"></Spinner> |
| 62 | + </Stack> |
| 63 | + } |
| 64 | + > |
| 65 | + <Stack gap="4" direction="horizontal" wrap> |
| 66 | + {services.map((service, index) => ( |
| 67 | + <a |
| 68 | + key={index} |
| 69 | + href="#" |
| 70 | + onClick={(e) => { |
| 71 | + e.preventDefault() |
| 72 | + handleServiceClick(service.ccrn) |
| 73 | + }} |
| 74 | + className="link-hover" |
| 75 | + > |
| 76 | + {service.ccrn} |
| 77 | + </a> |
| 78 | + ))} |
| 79 | + </Stack> |
| 80 | + </Suspense> |
| 81 | + </div> |
| 82 | + ) |
| 83 | +} |
0 commit comments