Skip to content

Commit

Permalink
fix: show expired token label (#4581)
Browse files Browse the repository at this point in the history
* fix: show expired token label

* fix: handle no expiry

---------

Co-authored-by: Vishal Sharma <makeavish786@gmail.com>
  • Loading branch information
YounixM and makeavish committed Feb 21, 2024
1 parent 0cb60e1 commit 7bca847
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion frontend/src/container/APIKeys/APIKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<APIKeyProps>['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
Expand All @@ -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'
Expand Down Expand Up @@ -473,7 +486,8 @@ function APIKeys(): JSX.Element {
Last used <Minus size={12} />
<Typography.Text>{formattedDateAndTime}</Typography.Text>
</div>
{expiresIn <= EXPIRATION_WITHIN_SEVEN_DAYS && (

{!isExpired && expiresIn <= EXPIRATION_WITHIN_SEVEN_DAYS && (
<div
className={cx(
'api-key-expires-in',
Expand All @@ -483,6 +497,12 @@ function APIKeys(): JSX.Element {
<span className="dot" /> Expires in {expiresIn} Days
</div>
)}

{isExpired && (
<div className={cx('api-key-expires-in danger')}>
<span className="dot" /> Expired
</div>
)}
</div>
</div>
);
Expand Down

0 comments on commit 7bca847

Please sign in to comment.