mirror of
https://github.com/Lissy93/web-check.git
synced 2025-05-30 23:15:45 +02:00
Implements loading status bar
This commit is contained in:
parent
590178983e
commit
0c9e8c1ec9
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ import HeadersCard from 'components/Results/Headers';
|
|||||||
import CookiesCard from 'components/Results/Cookies';
|
import CookiesCard from 'components/Results/Cookies';
|
||||||
import RobotsTxtCard from 'components/Results/RobotsTxt';
|
import RobotsTxtCard from 'components/Results/RobotsTxt';
|
||||||
import DnsRecordsCard from 'components/Results/DnsRecords';
|
import DnsRecordsCard from 'components/Results/DnsRecords';
|
||||||
|
import ProgressBar, { LoadingJob, LoadingState, initialJobs } from 'components/misc/ProgressBar';
|
||||||
import keys from 'utils/get-keys';
|
import keys from 'utils/get-keys';
|
||||||
import { determineAddressType, AddressType } from 'utils/address-type-checker';
|
import { determineAddressType, AddressType } from 'utils/address-type-checker';
|
||||||
|
|
||||||
@ -37,7 +38,6 @@ const ResultsOuter = styled.div`
|
|||||||
|
|
||||||
const ResultsContent = styled.section`
|
const ResultsContent = styled.section`
|
||||||
width: 95vw;
|
width: 95vw;
|
||||||
|
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-auto-flow: dense;
|
grid-auto-flow: dense;
|
||||||
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
|
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
|
||||||
@ -57,6 +57,8 @@ const Header = styled(Card)`
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const Results = (): JSX.Element => {
|
const Results = (): JSX.Element => {
|
||||||
|
const startTime = new Date().getTime();
|
||||||
|
|
||||||
const [ serverInfo, setServerInfo ] = useState<ServerInfo>();
|
const [ serverInfo, setServerInfo ] = useState<ServerInfo>();
|
||||||
const [ hostNames, setHostNames ] = useState<HostNames | null>();
|
const [ hostNames, setHostNames ] = useState<HostNames | null>();
|
||||||
const [ locationResults, setLocationResults ] = useState<ServerLocation>();
|
const [ locationResults, setLocationResults ] = useState<ServerLocation>();
|
||||||
@ -73,41 +75,14 @@ const Results = (): JSX.Element => {
|
|||||||
const [ addressType, setAddressType ] = useState<AddressType>('empt');
|
const [ addressType, setAddressType ] = useState<AddressType>('empt');
|
||||||
const { address } = useParams();
|
const { address } = useParams();
|
||||||
|
|
||||||
type LoadingState = 'loading' | 'skipped' | 'success' | 'error';
|
|
||||||
|
|
||||||
interface LoadingJob {
|
|
||||||
name: string,
|
|
||||||
state: LoadingState,
|
|
||||||
error?: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
const jobNames = [
|
|
||||||
'get-ip',
|
|
||||||
'ssl',
|
|
||||||
'dns',
|
|
||||||
'cookies',
|
|
||||||
'robots-txt',
|
|
||||||
'headers',
|
|
||||||
'lighthouse',
|
|
||||||
'location',
|
|
||||||
'server-info',
|
|
||||||
'whois',
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
const initialJobs = jobNames.map((job: string) => {
|
|
||||||
return {
|
|
||||||
name: job,
|
|
||||||
state: 'loading' as LoadingState,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const [ loadingJobs, setLoadingJobs ] = useState<LoadingJob[]>(initialJobs);
|
const [ loadingJobs, setLoadingJobs ] = useState<LoadingJob[]>(initialJobs);
|
||||||
|
|
||||||
const updateLoadingJobs = (job: string, newState: LoadingState, error?: string) => {
|
const updateLoadingJobs = useCallback((job: string, newState: LoadingState, error?: string) => {
|
||||||
|
const timeTaken = new Date().getTime() - startTime;
|
||||||
setLoadingJobs((prevJobs) => {
|
setLoadingJobs((prevJobs) => {
|
||||||
const newJobs = prevJobs.map((loadingJob: LoadingJob) => {
|
const newJobs = prevJobs.map((loadingJob: LoadingJob) => {
|
||||||
if (loadingJob.name === job) {
|
if (loadingJob.name === job) {
|
||||||
return { ...loadingJob, error, state: newState };
|
return { ...loadingJob, error, state: newState, timeTaken };
|
||||||
}
|
}
|
||||||
return loadingJob;
|
return loadingJob;
|
||||||
});
|
});
|
||||||
@ -118,9 +93,22 @@ const Results = (): JSX.Element => {
|
|||||||
|
|
||||||
return newJobs;
|
return newJobs;
|
||||||
});
|
});
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
|
/* Cancel remaining jobs after 20 second timeout */
|
||||||
|
useEffect(() => {
|
||||||
|
const checkJobs = () => {
|
||||||
|
loadingJobs.forEach(job => {
|
||||||
|
if (job.state === 'loading') {
|
||||||
|
updateLoadingJobs(job.name, 'timed-out');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const timeoutId = setTimeout(checkJobs, 10000);
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
};
|
||||||
|
}, [loadingJobs, updateLoadingJobs]); // dependencies for the effect
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setAddressType(determineAddressType(address || ''));
|
setAddressType(determineAddressType(address || ''));
|
||||||
@ -347,6 +335,7 @@ const Results = (): JSX.Element => {
|
|||||||
</Heading>
|
</Heading>
|
||||||
}
|
}
|
||||||
</Header>
|
</Header>
|
||||||
|
<ProgressBar loadStatus={loadingJobs} />
|
||||||
<ResultsContent>
|
<ResultsContent>
|
||||||
{ locationResults && <ServerLocationCard {...locationResults} />}
|
{ locationResults && <ServerLocationCard {...locationResults} />}
|
||||||
{ sslResults && <SslCertCard sslCert={sslResults} />}
|
{ sslResults && <SslCertCard sslCert={sslResults} />}
|
||||||
@ -360,7 +349,6 @@ const Results = (): JSX.Element => {
|
|||||||
{ technologyResults && <BuiltWithCard technologies={technologyResults} />}
|
{ technologyResults && <BuiltWithCard technologies={technologyResults} />}
|
||||||
{ robotsTxtResults && <RobotsTxtCard robotTxt={robotsTxtResults} />}
|
{ robotsTxtResults && <RobotsTxtCard robotTxt={robotsTxtResults} />}
|
||||||
{ serverInfo && <ServerInfoCard {...serverInfo} />}
|
{ serverInfo && <ServerInfoCard {...serverInfo} />}
|
||||||
|
|
||||||
</ResultsContent>
|
</ResultsContent>
|
||||||
</ResultsOuter>
|
</ResultsOuter>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user