-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI: Optimize Target, Alert and Service Discovery pages (#5119)
* add generic component for infinite scroll and search bar Signed-off-by: Augustin Husson <[email protected]> * rework service discovery page Signed-off-by: Augustin Husson <[email protected]> * rework alert page Signed-off-by: Augustin Husson <[email protected]> * rework target page Signed-off-by: Augustin Husson <[email protected]> * add entry in the changelog Signed-off-by: Augustin Husson <[email protected]> * fix test Signed-off-by: Augustin Husson <[email protected]> * fix linter issue Signed-off-by: Augustin Husson <[email protected]> * add infinite scroll on target Signed-off-by: Augustin Husson <[email protected]> * modify the assets Signed-off-by: Augustin Husson <[email protected]> * fix Changelog Signed-off-by: Augustin Husson <[email protected]> * persis the search event Signed-off-by: Augustin Husson <[email protected]> * add missing bindata Signed-off-by: Augustin Husson <[email protected]>
- Loading branch information
Showing
23 changed files
with
678 additions
and
461 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,50 @@ | ||
import { ComponentType, useEffect, useState } from 'react'; | ||
import InfiniteScroll from 'react-infinite-scroll-component'; | ||
|
||
const initialNumberOfItemsDisplayed = 50; | ||
|
||
export interface InfiniteScrollItemsProps<T> { | ||
items: T[]; | ||
} | ||
|
||
interface CustomInfiniteScrollProps<T> { | ||
allItems: T[]; | ||
child: ComponentType<InfiniteScrollItemsProps<T>>; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
const CustomInfiniteScroll = <T extends unknown>({ allItems, child }: CustomInfiniteScrollProps<T>) => { | ||
const [items, setItems] = useState<T[]>(allItems.slice(0, 50)); | ||
const [index, setIndex] = useState<number>(initialNumberOfItemsDisplayed); | ||
const [hasMore, setHasMore] = useState<boolean>(allItems.length > initialNumberOfItemsDisplayed); | ||
const Child = child; | ||
|
||
useEffect(() => { | ||
setItems(allItems.slice(0, initialNumberOfItemsDisplayed)); | ||
setHasMore(allItems.length > initialNumberOfItemsDisplayed); | ||
}, [allItems]); | ||
|
||
const fetchMoreData = () => { | ||
if (items.length === allItems.length) { | ||
setHasMore(false); | ||
} else { | ||
const newIndex = index + initialNumberOfItemsDisplayed; | ||
setIndex(newIndex); | ||
setItems(allItems.slice(0, newIndex)); | ||
} | ||
}; | ||
|
||
return ( | ||
<InfiniteScroll | ||
next={fetchMoreData} | ||
hasMore={hasMore} | ||
loader={<h4>loading...</h4>} | ||
dataLength={items.length} | ||
height={items.length > 25 ? '75vh' : ''} | ||
> | ||
<Child items={items} /> | ||
</InfiniteScroll> | ||
); | ||
}; | ||
|
||
export default CustomInfiniteScroll; |
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,34 @@ | ||
import React, { ChangeEvent, FC } from 'react'; | ||
import { Input, InputGroup, InputGroupAddon, InputGroupText } from 'reactstrap'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { faSearch } from '@fortawesome/free-solid-svg-icons'; | ||
|
||
export interface SearchBarProps { | ||
handleChange: (e: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => void; | ||
placeholder: string; | ||
} | ||
|
||
const SearchBar: FC<SearchBarProps> = ({ handleChange, placeholder }) => { | ||
let filterTimeout: NodeJS.Timeout; | ||
|
||
const handleSearchChange = (e: ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => { | ||
clearTimeout(filterTimeout); | ||
// TODO e.persist() should be removed once the upgrade to react v17 is done. | ||
// https://reactjs.org/docs/legacy-event-pooling.html | ||
e.persist(); | ||
filterTimeout = setTimeout(() => { | ||
handleChange(e); | ||
}, 300); | ||
}; | ||
|
||
return ( | ||
<InputGroup> | ||
<InputGroupAddon addonType="prepend"> | ||
<InputGroupText>{<FontAwesomeIcon icon={faSearch} />}</InputGroupText> | ||
</InputGroupAddon> | ||
<Input autoFocus onChange={handleSearchChange} placeholder={placeholder} /> | ||
</InputGroup> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
Oops, something went wrong.