Skip to content

Commit 2c6b5a1

Browse files
Code rabbit implementations
1 parent 3756ce5 commit 2c6b5a1

File tree

5 files changed

+85
-20
lines changed

5 files changed

+85
-20
lines changed

admin-ui/app/components/GluuTable/GluuTable.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo, useCallback, useEffect } from 'react'
1+
import React, { useState, useMemo, useCallback, useEffect, useRef } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import ArrowUpwardIcon from '@mui/icons-material/ArrowUpward'
44
import ArrowDownwardIcon from '@mui/icons-material/ArrowDownward'
@@ -13,7 +13,11 @@ import { useStyles, EXPAND_BUTTON_SIZE } from './GluuTable.style'
1313
import type { GluuTableProps, SortDirection, ColumnKey } from './types'
1414
import { ChevronIcon } from '@/components/SVG'
1515
import { BORDER_RADIUS } from '@/constants'
16-
import { getRowsPerPageOptions } from '@/utils/pagingUtils'
16+
import {
17+
getDefaultPagingSize,
18+
getRowsPerPageOptions,
19+
PAGING_SIZE_CHANGED_EVENT,
20+
} from '@/utils/pagingUtils'
1721

1822
const T_KEYS = {
1923
FIELDS_ACTIONS: 'fields.actions',
@@ -45,6 +49,7 @@ function GluuTable<T>(props: Readonly<GluuTableProps<T>>) {
4549
emptyMessage,
4650
stickyHeader = false,
4751
tableClassName,
52+
onPagingSizeSync,
4853
} = props
4954

5055
const { t } = useTranslation()
@@ -60,6 +65,31 @@ function GluuTable<T>(props: Readonly<GluuTableProps<T>>) {
6065
setExpandedRows(new Set())
6166
}, [data])
6267

68+
const rowsPerPageRef = useRef(pagination?.rowsPerPage ?? 0)
69+
rowsPerPageRef.current = pagination?.rowsPerPage ?? 0
70+
useEffect(() => {
71+
if (!pagination || !onPagingSizeSync) return
72+
const notifyIfChanged = (newSize: number) => {
73+
if (newSize !== rowsPerPageRef.current) onPagingSizeSync(newSize)
74+
}
75+
const handler = (e: Event) => {
76+
const detail = (e as CustomEvent<number>).detail
77+
const newSize = detail != null && Number.isInteger(detail) ? detail : getDefaultPagingSize()
78+
notifyIfChanged(newSize)
79+
}
80+
const onVisibilityChange = () => {
81+
if (document.visibilityState === 'visible') {
82+
notifyIfChanged(getDefaultPagingSize())
83+
}
84+
}
85+
window.addEventListener(PAGING_SIZE_CHANGED_EVENT, handler)
86+
document.addEventListener('visibilitychange', onVisibilityChange)
87+
return () => {
88+
window.removeEventListener(PAGING_SIZE_CHANGED_EVENT, handler)
89+
document.removeEventListener('visibilitychange', onVisibilityChange)
90+
}
91+
}, [pagination, onPagingSizeSync])
92+
6393
const totalCols = (expandable ? 1 : 0) + columns.length + (actions?.length ? 1 : 0)
6494
const defaultEmptyMessage = useMemo(() => t(T_KEYS.MESSAGES_NO_DATA), [t])
6595

@@ -78,7 +108,8 @@ function GluuTable<T>(props: Readonly<GluuTableProps<T>>) {
78108
const resolveRowKey = useCallback(
79109
(item: T, index: number): string | number => {
80110
if (getRowKey) return getRowKey(item, index)
81-
return index
111+
const row = item as { id?: string | number }
112+
return row.id ?? index
82113
},
83114
[getRowKey],
84115
)

admin-ui/app/components/GluuTable/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export interface PaginationConfig {
3838
onRowsPerPageChange: (rowsPerPage: number) => void
3939
}
4040

41+
/** When Settings (or another tab) changes paging size, GluuTable can notify so the parent can sync and refetch. Optional. */
42+
export type OnPagingSizeSync = (newSize: number) => void
43+
4144
export interface GluuTableProps<T> {
4245
columns: ColumnDef<T>[]
4346
data: T[]
@@ -54,4 +57,6 @@ export interface GluuTableProps<T> {
5457
stickyHeader?: boolean
5558
/** Optional class applied to the <table> for parent overrides (e.g. Audit) */
5659
tableClassName?: string
60+
/** When provided with pagination, GluuTable listens for paging-size changes (Settings / visibility) and calls this so the parent can sync and refetch. */
61+
onPagingSizeSync?: OnPagingSizeSync
5762
}

admin-ui/app/utils/pagingUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { useState, useCallback } from 'react'
2+
13
export const ROWS_PER_PAGE_OPTIONS = [5, 10, 25, 50] as const
24

35
export const DEFAULT_PAGING_SIZE = 10
46

57
const STORAGE_KEY = 'gluu.pagingSize'
68

9+
export const PAGING_SIZE_CHANGED_EVENT = 'gluu:pagingSizeChanged'
10+
711
export const getPagingSize = (defaultSize: number = DEFAULT_PAGING_SIZE): number => {
812
if (typeof window === 'undefined' || !window.localStorage) {
913
return defaultSize
@@ -53,7 +57,20 @@ export const savePagingSize = (size: number): void => {
5357

5458
try {
5559
localStorage.setItem(STORAGE_KEY, String(validSize))
60+
if (typeof window !== 'undefined') {
61+
window.dispatchEvent(new CustomEvent(PAGING_SIZE_CHANGED_EVENT, { detail: validSize }))
62+
}
5663
} catch (error) {
5764
console.warn('Failed to save paging size to localStorage:', error)
5865
}
5966
}
67+
68+
export function usePaginationState() {
69+
const [limit, setLimit] = useState(getDefaultPagingSize)
70+
const [pageNumber, setPageNumber] = useState(0)
71+
const onPagingSizeSync = useCallback((newSize: number) => {
72+
setLimit(newSize)
73+
setPageNumber(0)
74+
}, [])
75+
return { limit, setLimit, pageNumber, setPageNumber, onPagingSizeSync }
76+
}

admin-ui/plugins/admin/components/Audit/AuditListPage.style.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const useStyles = makeStyles<{ isDark: boolean; themeColors: ThemeConfig }>()((
5252
minWidth: 0,
5353
},
5454
'& table td': {
55-
verticalAlign: 'top',
55+
verticalAlign: 'middle',
5656
minWidth: 0,
5757
lineHeight: '28px',
5858
},
@@ -71,15 +71,12 @@ const useStyles = makeStyles<{ isDark: boolean; themeColors: ThemeConfig }>()((
7171
},
7272
},
7373
logEntryCell: {
74-
'display': 'flex',
75-
'alignItems': 'flex-start',
76-
'gap': `${SPACING.SECTION_GAP}px`,
77-
'flexWrap': 'nowrap',
78-
'minWidth': 0,
79-
'lineHeight': '28px',
80-
'& > *': {
81-
alignSelf: 'flex-start',
82-
},
74+
display: 'flex',
75+
alignItems: 'center',
76+
gap: `${SPACING.SECTION_GAP}px`,
77+
flexWrap: 'nowrap',
78+
minWidth: 0,
79+
lineHeight: '28px',
8380
},
8481
logEntryContent: {
8582
minWidth: 0,
@@ -99,6 +96,7 @@ const useStyles = makeStyles<{ isDark: boolean; themeColors: ThemeConfig }>()((
9996
alignItems: 'center',
10097
gap: '6px',
10198
lineHeight: 1,
99+
verticalAlign: 'middle',
102100
},
103101
searchActionIcon: {
104102
fontSize: 20,

admin-ui/plugins/admin/components/Audit/AuditListPage.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo, useCallback, useEffect } from 'react'
1+
import React, { useState, useMemo, useCallback, useEffect, useRef } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { createDate, subtractDate, isValidDate, isAfterDate } from '@/utils/dayjsUtils'
44
import type { Dayjs } from '@/utils/dayjsUtils'
@@ -21,7 +21,11 @@ import { useStyles } from './AuditListPage.style'
2121
import { auditListTimestampRegex, dateConverter, hasBothDates } from 'Plugins/admin/helper/utils'
2222
import { useGetAuditData, GetAuditDataParams } from 'JansConfigApi'
2323
import type { AuditRow } from './types'
24-
import { getDefaultPagingSize, getRowsPerPageOptions } from '@/utils/pagingUtils'
24+
import {
25+
getDefaultPagingSize,
26+
getRowsPerPageOptions,
27+
usePaginationState,
28+
} from '@/utils/pagingUtils'
2529

2630
const AUDIT_LOGS_RESOURCE_ID = ADMIN_UI_RESOURCES.AuditLogs
2731
const AUDIT_LOGS_SCOPES = CEDAR_RESOURCE_SCOPES[AUDIT_LOGS_RESOURCE_ID] ?? []
@@ -35,8 +39,6 @@ const T_KEYS = {
3539
PLACEHOLDER_SEARCH_PATTERN: 'placeholders.search_pattern',
3640
} as const
3741

38-
const getAuditRowKey = (row: AuditRow) => row.id
39-
4042
const splitTimestamp = (timestamp: string): { datePart: string; timePart: string } => {
4143
if (!timestamp) return { datePart: '', timePart: '' }
4244
const spaceIdx = timestamp.indexOf(' ')
@@ -68,9 +70,8 @@ const AuditListPage: React.FC = () => {
6870
}
6971
}, [authorizeHelper])
7072

71-
const [limit, setLimit] = useState(getDefaultPagingSize)
73+
const { limit, setLimit, pageNumber, setPageNumber, onPagingSizeSync } = usePaginationState()
7274
const [pattern, setPattern] = useState('')
73-
const [pageNumber, setPageNumber] = useState(0)
7475
const [startDate, setStartDate] = useState<Dayjs | null>(() =>
7576
subtractDate(createDate(), 14, 'day'),
7677
)
@@ -123,6 +124,19 @@ const AuditListPage: React.FC = () => {
123124
[],
124125
)
125126

127+
const patternRef = useRef(pattern)
128+
const filterStateRef = useRef(filterState)
129+
patternRef.current = pattern
130+
filterStateRef.current = filterState
131+
132+
const handlePagingSizeSync = useCallback(
133+
(newSize: number) => {
134+
onPagingSizeSync(newSize)
135+
setQueryParams(buildQueryParams(newSize, 0, patternRef.current, filterStateRef.current))
136+
},
137+
[buildQueryParams, onPagingSizeSync],
138+
)
139+
126140
const handleSearch = useCallback(() => {
127141
if (!filterState.hasBothDates) return
128142
setPageNumber(0)
@@ -373,7 +387,7 @@ const AuditListPage: React.FC = () => {
373387
loading={false}
374388
expandable
375389
pagination={pagination}
376-
getRowKey={getAuditRowKey}
390+
onPagingSizeSync={handlePagingSizeSync}
377391
emptyMessage={emptyMessage}
378392
/>
379393
</div>

0 commit comments

Comments
 (0)