-
Notifications
You must be signed in to change notification settings - Fork 197
Bed 5985 #1664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
elikmiller
wants to merge
9
commits into
main
Choose a base branch
from
BED-5985
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+748
−494
Draft
Bed 5985 #1664
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
572c163
wip: adding pagination controls to DetailsList component
elikmiller 7bcf3d2
wip: added new infinitequeryfixedlist component. added new useMeasure…
elikmiller 86701a4
wip: saving work
elikmiller 57c64c6
wip: revert changes to DetailsList
elikmiller 47b1f1d
wip: correctly export infinitequeryfixedlist, add licenses
elikmiller 90a6186
chore: remove console.log statement
elikmiller 5268a5f
chore: add license
elikmiller b8101fc
wip: saving progess
elikmiller 7e5192d
wip: consolidate hooks
elikmiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
.../javascript/bh-shared-ui/src/components/InfiniteQueryFixedList/InfiniteQueryFixedList.tsx
This file contains hidden or 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,85 @@ | ||
// Copyright 2025 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React, { ForwardedRef, useCallback, useMemo, useRef } from 'react'; | ||
import { UseInfiniteQueryResult } from 'react-query'; | ||
import { FixedSizeList, ListChildComponentProps } from 'react-window'; | ||
import { useMeasure } from '../../hooks/useMeasure'; | ||
import { PaginatedResult } from '../../utils/paginatedFetcher'; | ||
|
||
export type InfiniteQueryFixedListProps<T> = { | ||
itemSize: number; | ||
queryResult: UseInfiniteQueryResult<PaginatedResult<T>>; | ||
renderRow: (item: T, index: number, style: React.CSSProperties, isScrolling?: boolean) => React.ReactNode; | ||
overscanCount?: number; | ||
thresholdCount?: number; | ||
listRef?: ForwardedRef<FixedSizeList<T[]>>; | ||
}; | ||
|
||
export const InfiniteQueryFixedList = <T,>({ | ||
itemSize, | ||
queryResult, | ||
renderRow, | ||
overscanCount = 5, | ||
thresholdCount = 5, | ||
listRef, | ||
}: InfiniteQueryFixedListProps<T>) => { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
const [width, height] = useMeasure(containerRef); | ||
|
||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = queryResult; | ||
|
||
const items = useMemo(() => data?.pages.flatMap((page) => page.items) ?? [], [data]); | ||
|
||
const isItemLoaded = useCallback((index: number) => index < items.length, [items.length]); | ||
|
||
const loadMoreItems = useCallback(async () => { | ||
if (!isFetchingNextPage) await fetchNextPage(); | ||
}, [isFetchingNextPage, fetchNextPage]); | ||
|
||
const Row = useCallback( | ||
({ index, style, data: itemData, isScrolling }: ListChildComponentProps<T[]>) => { | ||
if (!isItemLoaded(index)) { | ||
return <div style={style}>Loading...</div>; | ||
} | ||
return renderRow(itemData[index], index, style, isScrolling); | ||
}, | ||
[isItemLoaded, renderRow] | ||
); | ||
|
||
return ( | ||
<div ref={containerRef} className='h-full w-full'> | ||
{height > 0 && width > 0 && ( | ||
<FixedSizeList<T[]> | ||
ref={listRef} | ||
height={height} | ||
itemSize={itemSize} | ||
itemCount={hasNextPage ? items.length + 1 : items.length} | ||
overscanCount={overscanCount} | ||
width={width} | ||
visibleStopIndex }) => { | ||
if (hasNextPage && !isFetchingNextPage && visibleStopIndex >= items.length - thresholdCount) { | ||
loadMoreItems(); | ||
} | ||
}} | ||
itemData={items}> | ||
{Row} | ||
</FixedSizeList> | ||
)} | ||
</div> | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/javascript/bh-shared-ui/src/components/InfiniteQueryFixedList/index.ts
This file contains hidden or 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,17 @@ | ||
// Copyright 2025 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './InfiniteQueryFixedList'; |
This file contains hidden or 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,41 @@ | ||
// Copyright 2025 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { RefObject, useEffect, useState } from 'react'; | ||
|
||
export function useMeasure(ref: RefObject<HTMLElement>) { | ||
const [width, setWidth] = useState(0); | ||
const [height, setHeight] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!ref.current) return; | ||
|
||
const updateMeasurements = () => { | ||
if (ref.current) { | ||
setWidth(ref.current.clientWidth); | ||
setHeight(ref.current.clientHeight); | ||
} | ||
}; | ||
|
||
updateMeasurements(); | ||
const observer = new ResizeObserver(updateMeasurements); | ||
observer.observe(ref.current); | ||
|
||
return () => observer.disconnect(); | ||
}, [ref]); | ||
|
||
return [width, height]; | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/javascript/bh-shared-ui/src/utils/paginatedFetcher.ts
This file contains hidden or 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,50 @@ | ||
// Copyright 2025 Specter Ops, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export type PageParam = { | ||
skip: number; | ||
limit: number; | ||
}; | ||
|
||
export type PaginatedResult<T> = { | ||
items: T[]; | ||
nextPageParam?: PageParam; | ||
}; | ||
|
||
export type PaginatedApiResponse<T> = { | ||
data: { | ||
data: { [key: string]: T[] }; | ||
count: number; | ||
}; | ||
}; | ||
|
||
/** | ||
* Generic helper to wrap paginated API responses into a React Query-friendly shape. | ||
*/ | ||
export const createPaginatedFetcher = <T>( | ||
fetchFn: () => Promise<PaginatedApiResponse<T>>, | ||
key: string, | ||
skip: number, | ||
limit: number | ||
): Promise<PaginatedResult<T>> => | ||
fetchFn().then((res) => { | ||
const items = res.data.data[key]; | ||
const hasMore = skip + limit < res.data.count; | ||
return { | ||
items, | ||
nextPageParam: hasMore ? { skip: skip + limit, limit } : undefined, | ||
}; | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review the edit button state logic.
The current implementation only disables the edit button when ALL queries are loading or ALL queries have errors. This might not be the intended behavior - typically you'd want to disable the button if ANY query is loading or has an error.
📝 Committable suggestion
🤖 Prompt for AI Agents