debian-image-builder-frontend/src/Components/ImagesTable/RegionsPopover.js
lucasgarfield 4000b8d5e5 ESLint: Add prefer-const rule
Require const declarations for variables that are never reassigned after
being declared. If a variable is never reassigned, using the const
declaration is better. const declaration tells readers, “this variable
is never reassigned,” reducing cognitive load and improving
maintainability.
2023-02-10 11:37:06 +01:00

92 lines
2.3 KiB
JavaScript

import React, { useMemo } from 'react';
import { Button, Popover } from '@patternfly/react-core';
import { createSelector } from '@reduxjs/toolkit';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { selectComposeById, selectImagesById } from '../../store/composesSlice';
export const selectRegions = createSelector(
[selectComposeById, selectImagesById],
(compose, images) => {
const filteredImages = images.filter(
(image) =>
compose.share_with_accounts &&
compose.share_with_accounts[0] === image.share_with_accounts[0]
);
const regions = {};
filteredImages.forEach((image) => {
if (image.region && image.status === 'success') {
if (regions[image.region]) {
new Date(image.created_at) <
new Date(regions[image.region].created_at)
? null
: (regions[image.region] = {
ami: image.ami,
created_at: image.created_at,
});
} else {
regions[image.region] = {
ami: image.ami,
created_at: image.created_at,
};
}
}
});
return regions;
}
);
const ImageLinkRegion = ({ region, ami }) => {
const url =
'https://console.aws.amazon.com/ec2/v2/home?region=' +
region +
'#LaunchInstanceWizard:ami=' +
ami;
return (
<Button component="a" target="_blank" variant="link" isInline href={url}>
{region}
</Button>
);
};
export const RegionsPopover = ({ composeId }) => {
const regions = useSelector((state) => selectRegions(state, composeId));
const listItems = useMemo(() => {
const listItems = [];
for (const [key, value] of Object.entries(regions).sort()) {
listItems.push(
<li key={key}>
<ImageLinkRegion region={key} ami={value.ami} />
</li>
);
}
return listItems;
}, [regions]);
return (
<Popover
aria-label="Launch instance"
headerContent={<div>Launch instance</div>}
bodyContent={<ul>{listItems}</ul>}
>
<Button variant="link" isInline>
Launch
</Button>
</Popover>
);
};
ImageLinkRegion.propTypes = {
region: PropTypes.string,
ami: PropTypes.string,
};
RegionsPopover.propTypes = {
composeId: PropTypes.string,
};