Download wp-calypso/client/data/users/use-users-query.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/data/users/use-users-query.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/data/users/use-users-query.js
-
curl -L -o use-users-query.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/data/users/use-users-query.js
1.49 kB
| import { uniqueBy } from '@automattic/js-utils'; | |
| import { useInfiniteQuery } from '@tanstack/react-query'; | |
| import wpcom from 'calypso/lib/wp'; | |
| export const defaults = { | |
| number: 100, | |
| order: 'ASC', | |
| order_by: 'display_name', | |
| }; | |
| const extractPages = ( pages = [] ) => pages.flatMap( ( page ) => page.users ); | |
| const compareUnique = ( a, b ) => a.ID === b.ID; | |
| const useUsersQuery = ( siteId, fetchOptions = {}, queryOptions = {} ) => { | |
| const { search } = fetchOptions; | |
| return useInfiniteQuery( { | |
| queryKey: [ 'users', siteId, search ], | |
| queryFn: ( { pageParam } ) => | |
| wpcom.req.get( `/sites/${ siteId }/users`, { | |
| ...defaults, | |
| ...fetchOptions, | |
| offset: pageParam, | |
| } ), | |
| enabled: !! siteId, | |
| initialPageParam: 0, | |
| getNextPageParam: ( lastPage, allPages ) => { | |
| const n = fetchOptions.number ?? defaults.number; | |
| if ( lastPage.found <= allPages.length * n ) { | |
| return; | |
| } | |
| return allPages.length * n; | |
| }, | |
| select: ( data ) => { | |
| /* @TODO: | |
| * `uniqueBy` is necessary, because the API can return duplicates. | |
| * This is most commonly seen where a user has both a "regular" user role | |
| * such as Administrator and Editor, and has also been added as a "Viewer" . | |
| */ | |
| const users = uniqueBy( extractPages( data.pages ), compareUnique ); | |
| return { | |
| users: uniqueBy( extractPages( data.pages ), compareUnique ), | |
| total: users?.length ?? data.pages[ 0 ].found, | |
| ...data, | |
| }; | |
| }, | |
| ...queryOptions, | |
| } ); | |
| }; | |
| export default useUsersQuery; | |