{"version":3,"file":"usePostsInfiniteQuery-1qJJl1bf.js","sources":["../../../frontend/src/Hooks/usePostsInfiniteQuery.ts"],"sourcesContent":["import { useInfiniteQuery } from 'react-query';\n\nimport Api from '../Services/Api';\nimport { Post } from '../Utils/Types/Posts';\nimport useCurrentUser from './useCurrentUser';\n\ninterface Page {\n data: {\n posts: Post[];\n paging: any;\n };\n}\n\ninterface PostsQueryResponse {\n pages: Page[];\n paging: any;\n postsCount: number;\n hasNextPage: boolean;\n isFetching: boolean;\n isLoading: boolean;\n isSuccess: boolean;\n isError: boolean;\n error: any;\n fetchNextPage: () => void;\n}\n\nconst usePostsInfiniteQuery = (key: any | any[], params: any): PostsQueryResponse => {\n const { space, enabled = true, post, ...rest } = params;\n // Note: Posts return permissions based on the current user\n // so we need to refetch the post when the user changes\n const { currentUser } = useCurrentUser();\n\n const {\n authorId = '',\n extraParams,\n iVotedFor,\n limit,\n originalPostId,\n page = 0,\n parentId = [],\n search = '',\n sort,\n status,\n tags = [],\n types = [],\n visibilities = [],\n } = rest;\n\n const queryParams = {\n authorId,\n extraParams,\n iVotedFor,\n limit,\n originalPostId,\n page,\n parentId,\n search,\n sort,\n status,\n tags,\n types,\n visibilities,\n };\n\n const queryKeys = [\n ...(Array.isArray(key) ? key : [key]),\n currentUser?.id,\n post,\n ...Object.values(queryParams),\n ];\n\n const { data, ...result } = useInfiniteQuery(\n queryKeys,\n ({ pageParam = page }) => Api.getPosts({ ...queryParams, page: pageParam }),\n {\n enabled: enabled && (!!space?.id || !!post?.id),\n keepPreviousData: true,\n getNextPageParam: (lastPage: any, pages: any) =>\n pages.length - 1 === lastPage.data.paging.total_pages ? null : lastPage.data.paging.page,\n refetchOnWindowFocus: false,\n },\n );\n\n const pages = (data?.pages || []) as Page[];\n const paging = pages[pages.length - 1]?.data?.paging || { total: 0, total_pages: 0 };\n\n return {\n ...result,\n pages,\n paging,\n postsCount: paging.total || 0,\n hasNextPage: paging.page < paging.total_pages,\n };\n};\n\nexport default usePostsInfiniteQuery;\n"],"names":["usePostsInfiniteQuery","key","params","_pages","space","_params$enabled","enabled","post","rest","_objectWithoutProperties","_excluded","_useCurrentUser","useCurrentUser","currentUser","_rest$authorId","authorId","extraParams","iVotedFor","limit","originalPostId","_rest$page","page","_rest$parentId","parentId","_rest$search","search","sort","status","_rest$tags","tags","_rest$types","types","_rest$visibilities","visibilities","queryParams","queryKeys","concat","_toConsumableArray","Array","isArray","id","Object","values","_useInfiniteQuery","useInfiniteQuery","_ref","_ref$pageParam","pageParam","Api","getPosts","_objectSpread","keepPreviousData","getNextPageParam","lastPage","pages","length","data","paging","total_pages","refetchOnWindowFocus","result","_excluded2","total","postsCount","hasNextPage"],"mappings":";09BA0BA,IAAMA,GAAwB,SAACC,EAAkBC,EAAoC,CAAAC,IAAAA,EAC3EC,EAAyCF,EAAzCE,MAAKC,EAAoCH,EAAlCI,QAAAA,EAAOD,IAAG,OAAA,GAAIA,EAAEE,EAAkBL,EAAlBK,KAASC,EAAIC,EAAKP,EAAMQ,CAAA,EAGvDC,EAAwBC,EAAAA,EAAhBC,EAAWF,EAAXE,YAERC,EAcIN,EAbFO,SAAAA,EAAQD,IAAG,OAAA,GAAEA,EACbE,EAYER,EAZFQ,YACAC,EAWET,EAXFS,UACAC,EAUEV,EAVFU,MACAC,EASEX,EATFW,eAAcC,EASZZ,EARFa,KAAAA,EAAID,IAAG,OAAA,EAACA,EAAAE,EAQNd,EAPFe,SAAAA,EAAQD,IAAG,OAAA,CAAA,EAAEA,EAAAE,EAOXhB,EANFiB,OAAAA,EAAMD,IAAG,OAAA,GAAEA,EACXE,EAKElB,EALFkB,KACAC,EAIEnB,EAJFmB,OAAMC,EAIJpB,EAHFqB,KAAAA,EAAID,IAAG,OAAA,CAAEA,EAAAA,EAAAE,EAGPtB,EAFFuB,MAAAA,EAAKD,IAAG,OAAA,CAAA,EAAEA,EAAAE,EAERxB,EADFyB,aAAAA,EAAYD,IAAG,OAAA,CAAA,EAAEA,EAGbE,EAAc,CAClB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