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EACzB,MAAO,CACLD,KAAAA,EACAG,MAAAA,EACAQ,YAAAA,EACAC,SAAAA,CACF,CACF,CA3DiC5B,EAAe,G","sources":["webpack://@affine/web/../core/src/hooks/use-query.ts"],"sourcesContent":["import type {\n GraphQLQuery,\n QueryOptions,\n QueryResponse,\n} from '@affine/graphql';\nimport { fetcher } from '@affine/graphql';\nimport type { GraphQLError } from 'graphql';\nimport { useCallback, useMemo } from 'react';\nimport type { SWRConfiguration, SWRResponse } from 'swr';\nimport useSWR from 'swr';\nimport useSWRImutable from 'swr/immutable';\nimport useSWRInfinite from 'swr/infinite';\n\n/**\n * A `useSWR` wrapper for sending graphql queries\n *\n * @example\n *\n * ```ts\n * import { someQuery, someQueryWithNoVars } from '@affine/graphql'\n *\n * const swrResponse1 = useQuery({\n * query: workspaceByIdQuery,\n * variables: { id: '1' }\n * })\n *\n * const swrResponse2 = useQuery({\n * query: someQueryWithNoVars\n * })\n * ```\n */\ntype useQueryFn = (\n options?: QueryOptions,\n config?: Omit<\n SWRConfiguration, GraphQLError, typeof fetcher>,\n 'fetcher'\n >\n) => SWRResponse<\n QueryResponse,\n GraphQLError,\n {\n suspense: true;\n }\n>;\n\nconst createUseQuery =\n (immutable: boolean): useQueryFn =>\n (options, config) => {\n const configWithSuspense: SWRConfiguration = useMemo(\n () => ({\n suspense: true,\n ...config,\n }),\n [config]\n );\n\n const useSWRFn = immutable ? useSWRImutable : useSWR;\n return useSWRFn(\n options ? () => ['cloud', options.query.id, options.variables] : null,\n options ? () => fetcher(options) : null,\n configWithSuspense\n );\n };\n\nexport const useQuery = createUseQuery(false);\nexport const useQueryImmutable = createUseQuery(true);\n\nexport function useQueryInfinite(\n options: Omit, 'variables'> & {\n getVariables: (\n pageIndex: number,\n previousPageData: QueryResponse\n ) => QueryOptions['variables'];\n },\n config?: Omit<\n SWRConfiguration<\n QueryResponse,\n GraphQLError | GraphQLError[],\n typeof fetcher\n >,\n 'fetcher'\n >\n) {\n const configWithSuspense: SWRConfiguration = useMemo(\n () => ({\n suspense: true,\n ...config,\n }),\n [config]\n );\n\n const { data, setSize, size, error } = useSWRInfinite<\n QueryResponse,\n GraphQLError | GraphQLError[]\n >(\n (pageIndex: number, previousPageData: QueryResponse) => [\n 'cloud',\n options.query.id,\n options.getVariables(pageIndex, previousPageData),\n ],\n async ([_, __, variables]) => {\n const params = { ...options, variables } as QueryOptions;\n return fetcher(params);\n },\n configWithSuspense\n );\n\n const loadingMore = size > 0 && data && !data[size - 1];\n\n // TODO(@Peng): find a generic way to know whether or not there are more items to load\n const loadMore = useCallback(() => {\n if (loadingMore) {\n return;\n }\n setSize(size => size + 1).catch(err => {\n console.error(err);\n });\n }, [loadingMore, setSize]);\n return {\n data,\n error,\n loadingMore,\n loadMore,\n };\n}\n"],"names":["createUseQuery","options","config","configWithSuspense","useMemo","suspense","useSWRFn","immutable","useSWRImutable","useSWR","query","id","variables","fetcher","useQuery","useQueryInfinite","data","setSize","size","error","useSWRInfinite","pageIndex","previousPageData","getVariables","_","__","params","loadingMore","loadMore","useCallback","catch","err","console"],"sourceRoot":""}