debian-image-builder-frontend/src/Components/CreateImageWizard/formComponents/RepositoriesStatus.tsx
regexowl 9f5a0af826 Wizard: Update the Repositories step
This updates the Repositories and Review step as per [mocks](https://www.sketch.com/s/d7aa6d29-fca0-4283-a846-09cc5fd10612/a/MyEbDz7).

Repositories with the unavailable or invalid status have a popover that allows for further inspection. The time of the last introspection and the counter of failed attempts was added to the popover, together with the "Go to Repositories" button.

On Recreate the payload repositories are checked against "freshly" fetched list of repositories. In case any of the previously checked repositories is no longer available in content sources an Alert is rendered on both Repositories and Review steps. The unavailable repository is checked, but the checkbox is disabled and the information is dashed out. Since the information about the repository is stored in the Repository type, the only information available to be rendered is the baseurl.

Create image button is also disabled when recreating an image with unavailable repositories.
2023-09-11 10:30:04 +02:00

151 lines
4.5 KiB
TypeScript

import React from 'react';
import {
Alert,
Button,
DescriptionList,
DescriptionListDescription,
DescriptionListGroup,
DescriptionListTerm,
Popover,
} from '@patternfly/react-core';
import {
CheckCircleIcon,
ExclamationCircleIcon,
ExclamationTriangleIcon,
ExternalLinkAltIcon,
InProgressIcon,
} from '@patternfly/react-icons';
import { ApiRepositoryResponse } from '../../../store/contentSourcesApi';
import {
convertStringToDate,
timestampToDisplayString,
} from '../../../Utilities/time';
import { useGetEnvironment } from '../../../Utilities/useGetEnvironment';
const getLastIntrospection = (
repoIntrospections: RepositoryStatusProps['repoIntrospections']
) => {
const currentDate = Date.now();
const lastIntrospectionDate = convertStringToDate(repoIntrospections);
const timeDeltaInSeconds = Math.floor(
(currentDate - lastIntrospectionDate) / 1000
);
if (timeDeltaInSeconds <= 60) {
return 'A few seconds ago';
} else if (timeDeltaInSeconds <= 60 * 60) {
return 'A few minutes ago';
} else if (timeDeltaInSeconds <= 60 * 60 * 24) {
return 'A few hours ago';
} else {
return timestampToDisplayString(repoIntrospections);
}
};
type RepositoryStatusProps = {
repoStatus: ApiRepositoryResponse['status'];
repoUrl: ApiRepositoryResponse['url'];
repoIntrospections: ApiRepositoryResponse['last_introspection_time'];
repoFailCount: ApiRepositoryResponse['failed_introspections_count'];
};
const RepositoriesStatus = ({
repoStatus,
repoUrl,
repoIntrospections,
repoFailCount,
}: RepositoryStatusProps) => {
const { isBeta } = useGetEnvironment();
if (repoStatus === 'Valid') {
return (
<>
<CheckCircleIcon className="success" /> {repoStatus}
</>
);
} else if (repoStatus === 'Invalid' || repoStatus === 'Unavailable') {
return (
<>
<Popover
position="bottom"
minWidth="30rem"
bodyContent={
<>
<Alert
variant={repoStatus === 'Invalid' ? 'danger' : 'warning'}
title={repoStatus}
className="pf-u-pb-sm"
isInline
isPlain
/>
<p className="pf-u-pb-md">Cannot fetch {repoUrl}</p>
{(repoIntrospections || repoFailCount) && (
<>
<DescriptionList
columnModifier={{
default: '2Col',
}}
>
{repoIntrospections && (
<DescriptionListGroup>
<DescriptionListTerm>
Last introspection
</DescriptionListTerm>
<DescriptionListDescription>
{getLastIntrospection(repoIntrospections)}
</DescriptionListDescription>
</DescriptionListGroup>
)}
{repoFailCount && (
<DescriptionListGroup>
<DescriptionListTerm>
Failed attempts
</DescriptionListTerm>
<DescriptionListDescription>
{repoFailCount}
</DescriptionListDescription>
</DescriptionListGroup>
)}
</DescriptionList>
<br />
</>
)}
<Button
component="a"
target="_blank"
variant="link"
iconPosition="right"
isInline
icon={<ExternalLinkAltIcon />}
href={
isBeta() ? '/preview/settings/content' : '/settings/content'
}
>
Go to Repositories
</Button>
</>
}
>
<Button variant="link" className="pf-u-p-0 pf-u-font-size-sm">
{repoStatus === 'Invalid' && (
<ExclamationCircleIcon className="error" />
)}
{repoStatus === 'Unavailable' && (
<ExclamationTriangleIcon className="expiring" />
)}{' '}
<span className="failure-button">{repoStatus}</span>
</Button>
</Popover>
</>
);
} else if (repoStatus === 'Pending') {
return (
<>
<InProgressIcon className="pending" /> {repoStatus}
</>
);
}
};
export default RepositoriesStatus;