Return

All tests / components/DeploymentStatus index.tsx

88.46% Statements 23/26
85.71% Branches 6/7
100% Functions 3/3
91.66% Lines 22/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 753x 3x 3x 3x 3x 3x     3x   3x 2x 2x     2x                   3x 7x 7x                         3x 7x 7x   7x 7x 7x 7x                                                   5x  
import React from 'react';
import useSWR from 'swr';
import styles from './deploymentstatus.module.css';
import Tooltip from '@/components/Tooltip';
import { useIntl } from 'react-intl';
import RenderManager from '@/components/RenderManager';
import type { DeploymentData } from '../../types/api';
 
const url = `${process.env.NEXT_PUBLIC_DOMAIN}/api/deployments`;
 
const fetchDeployment = async (url: string): Promise<DeploymentData> => {
    const response = await fetch(url);
    Iif (!response.ok) {
        throw new Error(`Failed to fetch deployment data: ${response.statusText}`);
    }
    const data = await response.json();
    return data as DeploymentData;
};
 
/**
 *
 * @returns { {data: DeploymentData | undefined, isLoading: boolean, isError: boolean} } - The deployment status
 * @description - Fetches the deployment status
 * @example - const { data, isLoading, isError } = useDeploymentStatus();
 */
export const useDeploymentStatus = () => {
    const { data, error } = useSWR<DeploymentData>(url, fetchDeployment);
    return {
        data,
        isLoading: !error && !data,
        isError: error,
    };
};
 
/**
 *
 * @returns {JSX.Element} - DeploymentStatus component
 * @description - Shows the current deployment status
 * @example - <DeploymentStatus />
 */
const DeploymentStatus = () => {
    const { data, isLoading, isError } = useDeploymentStatus();
    const { formatMessage: f } = useIntl();
 
    const status = data?.status;
    const username = data?.username;
    const environment = data?.environment;
    const createdAt = data?.createdAt;
 
    return (
        <RenderManager error={isError} loading={isLoading}>
            <Tooltip>
                <Tooltip.Trigger>
                    <div className={`${styles.status} ${status ? styles[status.toLowerCase()] : ''}`} />
                </Tooltip.Trigger>
                <Tooltip.Content>
                    {f(
                        {
                            id: 'deploymentstatus.tooltip',
                        },
                        {
                            status,
                            username,
                            environment,
                            createdAt: createdAt ? new Date(createdAt).toLocaleString() : '',
                        }
                    )}
                </Tooltip.Content>
            </Tooltip>
        </RenderManager>
    );
};
 
export default DeploymentStatus;