-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEN-2187]: notify in UI about expiring/expired token (#2208)
This pull request introduces several changes to enhance the token tracking system, improve connection status handling, and refactor the notification system. The most important changes include adding a new hook for token tracking, updating the connection store, and modifying the status component to handle different notification types. ### Token Tracking Enhancements: * Added `useTokenTracker` hook to monitor token expiration and trigger notifications (`frontend/webapp/hooks/tokens/useTokenTracker.ts`). * Updated `useTokenCRUD` import to include `useTokenTracker` (`frontend/webapp/hooks/tokens/index.ts`). ### Connection Store Updates: * Refactored `useConnectionStore` to include new state values and setters for SSE and token statuses (`frontend/webapp/store/useConnectionStore.ts`). * Modified `useSSE` hook to use the new `setSseStatus` method for updating connection status (`frontend/webapp/hooks/notification/useSSE.ts`) [[1]](diffhunk://#diff-db9ebe0ce8cdabc0ede2f45de12661d55fb7aa50a5ecfc0787f5898c55b044d6R8-L11) [[2]](diffhunk://#diff-db9ebe0ce8cdabc0ede2f45de12661d55fb7aa50a5ecfc0787f5898c55b044d6L64-R66) [[3]](diffhunk://#diff-db9ebe0ce8cdabc0ede2f45de12661d55fb7aa50a5ecfc0787f5898c55b044d6L78-R80). ### Notification System Improvements: * Updated `Status` component to handle different notification types and statuses (`frontend/webapp/reuseable-components/status/index.tsx`) [[1]](diffhunk://#diff-8828fa39eed82424bd382e5bd240ee5efc607723527e6dc649d92a3d0c32aed3L18-R20) [[2]](diffhunk://#diff-8828fa39eed82424bd382e5bd240ee5efc607723527e6dc649d92a3d0c32aed3L28-R29) [[3]](diffhunk://#diff-8828fa39eed82424bd382e5bd240ee5efc607723527e6dc649d92a3d0c32aed3L39-R50) [[4]](diffhunk://#diff-8828fa39eed82424bd382e5bd240ee5efc607723527e6dc649d92a3d0c32aed3L62-R107). * Added constants and utility functions for time calculations (`frontend/webapp/utils/constants/numbers.ts`, `frontend/webapp/utils/functions/resolvers/is-over-time/index.ts`, `frontend/webapp/utils/functions/resolvers/is-within-time/index.ts`) [[1]](diffhunk://#diff-8d9da7862db2daaa157f3e56ea67544a60558bbba464c46e09a4cccd7ac035ecR1) [[2]](diffhunk://#diff-6aa0c63fcbc4f8f8d99955949445d606fa200f5f19944befe1e270d6c674b376R1-R6) [[3]](diffhunk://#diff-d8c142b409a4755de8cc6254b2eb3d325a5f38fe98583539008a180c20fd8190R1-R6). ### Miscellaneous Changes: * Added `useTokenTracker` to `MainPage` component to initialize token tracking (`frontend/webapp/app/(overview)/overview/page.tsx`) [[1]](diffhunk://#diff-b5ae53796816cc6e1e253bb851d6a2fa404789188ea7964c716ee2ebcbcb776eL4-R4) [[2]](diffhunk://#diff-b5ae53796816cc6e1e253bb851d6a2fa404789188ea7964c716ee2ebcbcb776eR13). * Updated various components to use the new notification types and token tracking logic (`frontend/webapp/components/main/header/index.tsx`, `frontend/webapp/components/overview/all-drawers/cli-drawer.tsx`) [[1]](diffhunk://#diff-2c96f91ec30d2116981a9c0a562820ff9fd87c8292cb5dca11a45d6fb2ac6c04L6-R6) [[2]](diffhunk://#diff-2c96f91ec30d2116981a9c0a562820ff9fd87c8292cb5dca11a45d6fb2ac6c04L36-R36) [[3]](diffhunk://#diff-2c96f91ec30d2116981a9c0a562820ff9fd87c8292cb5dca11a45d6fb2ac6c04L46-R46) [[4]](diffhunk://#diff-05c3af50f20a5a1195555bcff0f9a44be51f03ad2f4cd080dff2a3b2ee6ababcL6-R7) [[5]](diffhunk://#diff-05c3af50f20a5a1195555bcff0f9a44be51f03ad2f4cd080dff2a3b2ee6ababcL86-R96). These changes collectively improve the robustness of the token management system and enhance the user experience by providing timely notifications about token statuses. --- In header and in notification manager: <img width="1715" alt="Screenshot 2025-01-13 at 11 30 30" src="https://github.com/user-attachments/assets/f2f260e9-98f1-466a-8b22-e1c358750cf1" /> In CLI drawer: <img width="400" alt="Screenshot 2025-01-13 at 11 30 39" src="https://github.com/user-attachments/assets/1e31c11a-8c77-441a-8cd0-ea9656092ffb" />
- Loading branch information
1 parent
a88cd13
commit 7cc914b
Showing
15 changed files
with
154 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useTokenCRUD'; | ||
export * from './useTokenTracker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect } from 'react'; | ||
import { useTokenCRUD } from '.'; | ||
import { useTimeAgo } from '../common'; | ||
import { NOTIFICATION_TYPE } from '@/types'; | ||
import { isOverTime, SEVEN_DAYS_IN_MS } from '@/utils'; | ||
import { useConnectionStore, useNotificationStore } from '@/store'; | ||
|
||
// This hook is responsible for tracking the tokens and their expiration times. | ||
// When a token is about to expire or has expired, a notification is added to the notification store, and the connection status is updated accordingly. | ||
|
||
export const useTokenTracker = () => { | ||
const timeago = useTimeAgo(); | ||
const { tokens } = useTokenCRUD(); | ||
const { setTokenStatus } = useConnectionStore(); | ||
const { addNotification } = useNotificationStore(); | ||
|
||
useEffect(() => { | ||
tokens.forEach(({ expiresAt, name }) => { | ||
if (isOverTime(expiresAt)) { | ||
const notif = { | ||
type: NOTIFICATION_TYPE.WARNING, | ||
title: 'API Token', | ||
message: `The token "${name}" has expired ${timeago.format(expiresAt)}.`, | ||
}; | ||
|
||
addNotification(notif); | ||
setTokenStatus({ | ||
tokenExpired: true, | ||
tokenExpiring: false, | ||
title: notif.title, | ||
message: notif.message, | ||
}); | ||
} else if (isOverTime(expiresAt, SEVEN_DAYS_IN_MS)) { | ||
const notif = { | ||
type: NOTIFICATION_TYPE.WARNING, | ||
title: 'API Token', | ||
message: `The token "${name}" is about to expire ${timeago.format(expiresAt)}.`, | ||
}; | ||
|
||
addNotification(notif); | ||
setTokenStatus({ | ||
tokenExpired: false, | ||
tokenExpiring: true, | ||
title: notif.title, | ||
message: notif.message, | ||
}); | ||
} | ||
}); | ||
}, [tokens]); | ||
|
||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,36 @@ | ||
import { NOTIFICATION_TYPE } from '@/types'; | ||
import { create } from 'zustand'; | ||
|
||
interface StoreStateValues { | ||
connecting: boolean; | ||
active: boolean; | ||
interface StateValues { | ||
title: string; | ||
message: string; | ||
} | ||
|
||
interface SseStateValues extends StateValues { | ||
sseConnecting: boolean; | ||
sseStatus: NOTIFICATION_TYPE; | ||
} | ||
|
||
interface TokenStateValues extends StateValues { | ||
tokenExpired: boolean; | ||
tokenExpiring: boolean; | ||
} | ||
|
||
interface StoreStateSetters { | ||
setConnectionStore: (state: StoreStateValues) => void; | ||
setConnecting: (bool: boolean) => void; | ||
setActive: (bool: boolean) => void; | ||
setTitle: (str: string) => void; | ||
setMessage: (str: string) => void; | ||
setSseStatus: (state: SseStateValues) => void; | ||
setTokenStatus: (state: TokenStateValues) => void; | ||
} | ||
|
||
export const useConnectionStore = create<StoreStateValues & StoreStateSetters>((set) => ({ | ||
connecting: true, | ||
active: false, | ||
export const useConnectionStore = create<SseStateValues & TokenStateValues & StoreStateSetters>((set) => ({ | ||
title: '', | ||
message: '', | ||
|
||
setConnectionStore: (state) => set(state), | ||
setConnecting: (bool) => set({ connecting: bool }), | ||
setActive: (bool) => set({ active: bool }), | ||
setTitle: (str) => set({ title: str }), | ||
setMessage: (str) => set({ message: str }), | ||
sseConnecting: true, | ||
sseStatus: NOTIFICATION_TYPE.DEFAULT, | ||
|
||
tokenExpired: false, | ||
tokenExpiring: false, | ||
|
||
setSseStatus: (state) => set(state), | ||
setTokenStatus: (state) => set(state), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SEVEN_DAYS_IN_MS = 604800000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './compare-condition'; | ||
export * from './get-value-for-range'; | ||
export * from './is-emtpy'; | ||
export * from './is-over-time'; | ||
export * from './is-within-time'; |
6 changes: 6 additions & 0 deletions
6
frontend/webapp/utils/functions/resolvers/is-over-time/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const isOverTime = (originDate: Date | string | number, difference: number = 0) => { | ||
const now = new Date().getTime(); | ||
const compareWith = new Date(originDate).getTime(); | ||
|
||
return compareWith - now <= difference; | ||
}; |
6 changes: 6 additions & 0 deletions
6
frontend/webapp/utils/functions/resolvers/is-within-time/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const isWithinTime = (originDate: Date | string | number, difference: number = 0) => { | ||
const now = new Date().getTime(); | ||
const compareWith = new Date(originDate).getTime(); | ||
|
||
return now - compareWith <= difference; | ||
}; |