Skip to content

Commit 7ce8555

Browse files
authored
Merge pull request #404 from devtron-labs/refactor/rb-tabs
refactor: resource browser tabs clean up and add abortControllerRef in fetchInTime
2 parents 8e59ff2 + 9ed6719 commit 7ce8555

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Api.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,33 @@ function fetchInTime<T = object>(
192192
options?: APIOptions,
193193
isMultipartRequest?: boolean,
194194
): Promise<ResponseType> {
195-
const controller = new AbortController()
196-
const { signal } = controller
195+
const controller = options?.abortControllerRef?.current ?? new AbortController()
196+
const signal = options?.abortControllerRef?.current?.signal || options?.signal || controller.signal
197197
const timeoutPromise: Promise<ResponseType> = new Promise((resolve, reject) => {
198198
const requestTimeout = (window as any)?._env_?.GLOBAL_API_TIMEOUT || FALLBACK_REQUEST_TIMEOUT
199199
const timeout = options?.timeout ? options.timeout : requestTimeout
200200

201201
setTimeout(() => {
202202
controller.abort()
203+
if (options?.abortControllerRef?.current) {
204+
options.abortControllerRef.current = new AbortController()
205+
}
206+
203207
reject({
204208
code: 408,
205209
errors: [{ code: 408, internalMessage: 'Request cancelled', userMessage: 'Request Cancelled' }],
206210
})
207211
}, timeout)
208212
})
209213
return Promise.race([
210-
fetchAPI(url, type, data, options?.signal || signal, options?.preventAutoLogout || false, isMultipartRequest),
214+
fetchAPI(
215+
url,
216+
type,
217+
data,
218+
signal,
219+
options?.preventAutoLogout || false,
220+
isMultipartRequest,
221+
),
211222
timeoutPromise,
212223
]).catch((err) => {
213224
if (err instanceof ServerErrors) {

src/Common/Types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import React, { ReactNode, CSSProperties, ReactElement } from 'react'
17+
import React, { ReactNode, CSSProperties, ReactElement, MutableRefObject } from 'react'
1818
import { Placement } from 'tippy.js'
1919
import { UserGroupDTO } from '@Pages/GlobalConfigurations'
2020
import { ImageComment, ReleaseTag } from './ImageTags.Types'
@@ -50,7 +50,11 @@ export interface ResponseType<T = any> {
5050

5151
export interface APIOptions {
5252
timeout?: number
53+
/**
54+
* @deprecated Use abortController instead
55+
*/
5356
signal?: AbortSignal
57+
abortControllerRef?: MutableRefObject<AbortController>
5458
preventAutoLogout?: boolean
5559
}
5660

@@ -1007,4 +1011,4 @@ export interface WidgetEventDetails {
10071011
count: number
10081012
age: string
10091013
lastSeen: string
1010-
}
1014+
}

src/Shared/types.ts

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -748,36 +748,108 @@ export interface CustomRoleAndMeta {
748748
}
749749

750750
interface CommonTabArgsType {
751+
/**
752+
* Name for the tab.
753+
*
754+
* Note: Used for the title
755+
*/
751756
name: string
752757
kind?: string
758+
/**
759+
* URL for the tab
760+
*/
753761
url: string
762+
/**
763+
* If true, the tab is selected
764+
*/
754765
isSelected: boolean
766+
/**
767+
* Title for the tab
768+
*/
755769
title?: string
756770
isDeleted?: boolean
757-
position: number
771+
/**
772+
* Type for the tab
773+
*
774+
* Note: Fixed tabs are always places before dynamic tabs
775+
*/
776+
type: 'fixed' | 'dynamic'
777+
/**
778+
* Path of the icon for the tab
779+
*
780+
* @default ''
781+
*/
758782
iconPath?: string
783+
/**
784+
* Dynamic title for the tab
785+
*
786+
* @default ''
787+
*/
759788
dynamicTitle?: string
789+
/**
790+
* Whether to show the tab name when selected
791+
*
792+
* @default false
793+
*/
760794
showNameOnSelect?: boolean
761795
/**
796+
* Would remove the title/name from tab heading, but that does not mean name is not required, since it is used in other calculations
762797
* @default false
763798
*/
764799
hideName?: boolean
800+
/**
801+
* Indicates if showNameOnSelect tabs have been selected once
802+
*
803+
* @default false
804+
*/
765805
isAlive?: boolean
766806
lastSyncMoment?: Dayjs
767807
componentKey?: string
808+
/**
809+
* Custom tippy config for the tab
810+
*
811+
* This overrides the tippy being computed from tab title
812+
*/
768813
tippyConfig?: {
769814
title: string
770815
descriptions: {
771816
info: string
772817
value: string
773818
}[]
774819
}
775-
}
776-
777-
export interface InitTabType extends CommonTabArgsType {
778-
idPrefix: string
779-
}
820+
/**
821+
* If true, the fixed tab remains mounted on initial load of the component
822+
*
823+
* Note: Not for dynamic tabs atm
824+
*
825+
* @default false
826+
*/
827+
shouldRemainMounted?: boolean
828+
}
829+
830+
export type InitTabType = Omit<CommonTabArgsType, 'type'> &
831+
(
832+
| {
833+
type: 'fixed'
834+
/**
835+
* Unique identifier for the fixed tab
836+
*
837+
* Note: Shouldn't contain '-'
838+
*/
839+
id: string
840+
idPrefix?: never
841+
}
842+
| {
843+
type: 'dynamic'
844+
id?: never
845+
idPrefix: string
846+
}
847+
)
780848

781849
export interface DynamicTabType extends CommonTabArgsType {
782850
id: string
851+
/**
852+
* Id of the last active tab before switching to current tab
853+
*/
854+
lastActiveTabId: string | null
783855
}

0 commit comments

Comments
 (0)