From 7bca847f113da8fc529c97485f976d363a156fe8 Mon Sep 17 00:00:00 2001 From: Yunus M Date: Wed, 21 Feb 2024 15:38:18 +0530 Subject: [PATCH] fix: show expired token label (#4581) * fix: show expired token label * fix: handle no expiry --------- Co-authored-by: Vishal Sharma --- frontend/src/container/APIKeys/APIKeys.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/frontend/src/container/APIKeys/APIKeys.tsx b/frontend/src/container/APIKeys/APIKeys.tsx index 0540e86954..c24bad7009 100644 --- a/frontend/src/container/APIKeys/APIKeys.tsx +++ b/frontend/src/container/APIKeys/APIKeys.tsx @@ -26,6 +26,7 @@ import updateAPIKeyApi from 'api/APIKeys/updateAPIKey'; import axios, { AxiosError } from 'axios'; import cx from 'classnames'; import { SOMETHING_WENT_WRONG } from 'constants/api'; +import dayjs from 'dayjs'; import { useGetAllAPIKeys } from 'hooks/APIKeys/useGetAllAPIKeys'; import { useNotifications } from 'hooks/useNotifications'; import { @@ -320,10 +321,20 @@ function APIKeys(): JSX.Element { return differenceInSeconds / (60 * 60 * 24); }; + const isExpiredToken = (expiryTimestamp: number): boolean => { + if (expiryTimestamp === 0) { + return false; + } + const currentTime = dayjs(); + const tokenExpiresAt = dayjs.unix(expiryTimestamp); + return tokenExpiresAt.isBefore(currentTime); + }; + const columns: TableProps['columns'] = [ { title: 'API Key', key: 'api-key', + // eslint-disable-next-line sonarjs/cognitive-complexity render: (APIKey: APIKeyProps): JSX.Element => { const formattedDateAndTime = APIKey && APIKey?.lastUsed && APIKey?.lastUsed !== 0 @@ -337,6 +348,8 @@ function APIKeys(): JSX.Element { ? Number.POSITIVE_INFINITY : getDateDifference(APIKey?.createdAt, APIKey?.expiresAt); + const isExpired = isExpiredToken(APIKey.expiresAt); + const expiresOn = !APIKey.expiresAt || APIKey.expiresAt === 0 ? 'No Expiry' @@ -473,7 +486,8 @@ function APIKeys(): JSX.Element { Last used {formattedDateAndTime} - {expiresIn <= EXPIRATION_WITHIN_SEVEN_DAYS && ( + + {!isExpired && expiresIn <= EXPIRATION_WITHIN_SEVEN_DAYS && (
Expires in {expiresIn} Days
)} + + {isExpired && ( +
+ Expired +
+ )} );