Squashed commit of the following:
commit 290b3fbc5e18a2cc8694fb2d5f777952d971dfd6
Merge: fe5c67e62 2313eda12
Author: Artem Baskal <a.baskal@adguard.com>
Date: Tue Dec 8 13:57:38 2020 +0300
Merge branch 'master' into fix/2367
commit fe5c67e624280d7fc08192ed3e953a09ca10a9ee
Author: Artem Baskal <a.baskal@adguard.com>
Date: Mon Dec 7 16:44:41 2020 +0300
- client: 2367 Show SSL Certificate Expire Banner in 5 Days
78 lines
1.9 KiB
JavaScript
78 lines
1.9 KiB
JavaScript
import React from 'react';
|
|
import { Trans } from 'react-i18next';
|
|
import isAfter from 'date-fns/is_after';
|
|
import addDays from 'date-fns/add_days';
|
|
import { useSelector } from 'react-redux';
|
|
import Topline from './Topline';
|
|
import { EMPTY_DATE } from '../../helpers/constants';
|
|
|
|
const EXPIRATION_ENUM = {
|
|
VALID: 'VALID',
|
|
EXPIRED: 'EXPIRED',
|
|
EXPIRING: 'EXPIRING',
|
|
};
|
|
|
|
const EXPIRATION_STATE = {
|
|
[EXPIRATION_ENUM.EXPIRED]: {
|
|
toplineType: 'danger',
|
|
i18nKey: 'topline_expired_certificate',
|
|
},
|
|
[EXPIRATION_ENUM.EXPIRING]: {
|
|
toplineType: 'warning',
|
|
i18nKey: 'topline_expiring_certificate',
|
|
},
|
|
};
|
|
|
|
const getExpirationFlags = (not_after) => {
|
|
const DAYS_BEFORE_EXPIRATION = 5;
|
|
|
|
const now = Date.now();
|
|
const isExpiring = isAfter(addDays(now, DAYS_BEFORE_EXPIRATION), not_after);
|
|
const isExpired = isAfter(now, not_after);
|
|
|
|
return {
|
|
isExpiring,
|
|
isExpired,
|
|
};
|
|
};
|
|
|
|
const getExpirationEnumKey = (not_after) => {
|
|
const { isExpiring, isExpired } = getExpirationFlags(not_after);
|
|
|
|
if (isExpired) {
|
|
return EXPIRATION_ENUM.EXPIRED;
|
|
}
|
|
|
|
if (isExpiring) {
|
|
return EXPIRATION_ENUM.EXPIRING;
|
|
}
|
|
|
|
return EXPIRATION_ENUM.VALID;
|
|
};
|
|
|
|
const EncryptionTopline = () => {
|
|
const not_after = useSelector((state) => state.encryption.not_after);
|
|
|
|
if (not_after === EMPTY_DATE) {
|
|
return null;
|
|
}
|
|
|
|
const expirationStateKey = getExpirationEnumKey(not_after);
|
|
|
|
if (expirationStateKey === EXPIRATION_ENUM.VALID) {
|
|
return null;
|
|
}
|
|
|
|
const { toplineType, i18nKey } = EXPIRATION_STATE[expirationStateKey];
|
|
|
|
return (
|
|
<Topline type={toplineType}>
|
|
<Trans components={[<a href="#encryption" key="0">link</a>]}>
|
|
{i18nKey}
|
|
</Trans>
|
|
</Topline>
|
|
);
|
|
};
|
|
|
|
export default EncryptionTopline;
|