File size: 1,329 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import { useQuery, UseQueryResult, QueryKey } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import { useHomeLayoutQueryParams, HomeLayoutQueryParams } from './use-home-layout-query-params';
interface Options {
enabled?: boolean;
}
const useHomeLayoutQuery = (
siteId: number | null,
{ enabled = true }: Options = {}
): UseQueryResult => {
const query = useHomeLayoutQueryParams();
return useQuery( {
queryKey: getCacheKey( siteId ),
queryFn: () => fetchHomeLayout( siteId, query ),
enabled: !! siteId && enabled,
// The `/layout` endpoint can return a random view. Disable implicit refetches
// so the view doesn't change without some user action.
staleTime: Infinity,
refetchInterval: false,
refetchOnMount: 'always',
} );
};
export function fetchHomeLayout(
siteId: number | null,
query: HomeLayoutQueryParams = {}
): Promise< unknown > {
return wpcom.req.get(
{
path: `/sites/${ siteId }/home/layout`,
apiNamespace: 'wpcom/v2',
},
query
);
}
export function getCacheKey( siteId: number | null ): QueryKey {
// The `dev` and `view` query params are not included in the cache key because we want all
// the hooks to have the same idea of what the current view is, regardless of dev flags.
return [ 'home-layout', siteId ];
}
export default useHomeLayoutQuery;
|