UNPKG

@pantheon-systems/pcc-react-sdk

Version:
2 lines 6.73 kB
import {useQuery}from'@apollo/client/react/hooks/useQuery.js';export{useQuery}from'@apollo/client/react/hooks/useQuery.js';import {updateConfig,getSite,getRecommendedArticles,getPaginatedArticles,getArticles,getArticleURLFromSiteWithOptions,getArticleURLFromSite,getArticlePathComponentsFromContentStructure,getArticleBySlugOrId,getArticle,getAllTags,flattenDocumentTabs,findTab,PantheonClient,PCCConvenienceFunctions,GQL,buildContentType,generateArticleQuery,ARTICLE_UPDATE_SUBSCRIPTION,LIST_ARTICLES_QUERY,convertSearchParamsToGQL,PantheonAPI}from'@pantheon-systems/pcc-sdk-core';export{GQL,PCCConvenienceFunctions,PantheonClient,findTab,flattenDocumentTabs,getAllTags,getArticle,getArticleBySlugOrId,getArticlePathComponentsFromContentStructure,getArticleURLFromSite,getArticleURLFromSiteWithOptions,getArticles,getPaginatedArticles,getRecommendedArticles,getSite,updateConfig}from'@pantheon-systems/pcc-sdk-core';import X,{useMemo,useEffect,useState}from'react';import {useMutation}from'@apollo/client/react/hooks/useMutation.js';export{useMutation}from'@apollo/client/react/hooks/useMutation.js';import {useSubscription}from'@apollo/client/react/hooks/useSubscription.js';export{useSubscription}from'@apollo/client/react/hooks/useSubscription.js';import*as it from'@pantheon-systems/pcc-sdk-core/types';import {ApolloProvider}from'@apollo/client/react/context/ApolloProvider.js';import {jsx}from'react/jsx-runtime';import {markdownToTxt}from'markdown-to-txt';var y=Object.defineProperty,$=Object.defineProperties,ee=Object.getOwnPropertyDescriptor,te=Object.getOwnPropertyDescriptors,re=Object.getOwnPropertyNames,x=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var S=(e,t,r)=>t in e?y(e,t,{enumerable:true,configurable:true,writable:true,value:r}):e[t]=r,i=(e,t)=>{for(var r in t||(t={}))v.call(t,r)&&S(e,r,t[r]);if(x)for(var r of x(t))N.call(t,r)&&S(e,r,t[r]);return e},A=(e,t)=>$(e,te(t));var w=(e,t)=>{var r={};for(var o in e)v.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(e!=null&&x)for(var o of x(e))t.indexOf(o)<0&&N.call(e,o)&&(r[o]=e[o]);return r};var I=(e,t)=>{for(var r in t)y(e,r,{get:t[r],enumerable:true});},k=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of re(t))!v.call(e,n)&&n!==r&&y(e,n,{get:()=>t[n],enumerable:!(o=ee(t,n))||o.enumerable});return e},m=(e,t,r)=>(k(e,t,"default"),r);var g=(e,t,r)=>new Promise((o,n)=>{var c=a=>{try{s(r.next(a));}catch(l){n(l);}},u=a=>{try{s(r.throw(a));}catch(l){n(l);}},s=a=>a.done?o(a.value):Promise.resolve(a.value).then(c,u);s((r=r.apply(e,t)).next());});var f={};I(f,{GQL:()=>GQL,PCCConvenienceFunctions:()=>PCCConvenienceFunctions,PantheonAPI:()=>T,PantheonClient:()=>PantheonClient,PantheonProvider:()=>ye,findTab:()=>findTab,flattenDocumentTabs:()=>flattenDocumentTabs,getAllTags:()=>getAllTags,getArticle:()=>getArticle,getArticleBySlugOrId:()=>getArticleBySlugOrId,getArticlePathComponentsFromContentStructure:()=>getArticlePathComponentsFromContentStructure,getArticleURLFromSite:()=>getArticleURLFromSite,getArticleURLFromSiteWithOptions:()=>getArticleURLFromSiteWithOptions,getArticles:()=>getArticles,getPaginatedArticles:()=>getPaginatedArticles,getRecommendedArticles:()=>getRecommendedArticles,getSite:()=>getSite,markdownToText:()=>Ne,updateConfig:()=>updateConfig,useArticle:()=>L,useArticles:()=>Q,useMutation:()=>useMutation,usePagination:()=>q,usePantheonClient:()=>ve,useQuery:()=>useQuery,useSubscription:()=>useSubscription});var L=(e,t,r,o)=>{var P;let n=t==null?void 0:t.publishingLevel,c=buildContentType(t==null?void 0:t.contentType),u=useMemo(()=>i(i({},n&&{publishingLevel:n}),c&&{contentType:c}),[n,c]),l=useQuery(generateArticleQuery({withSite:o==null?void 0:o.site}),A(i({},r),{variables:i({id:e},u)})),{subscribeToMore:s}=l,a=w(l,["subscribeToMore"]);return useEffect(()=>{r!=null&&r.skip||s({document:ARTICLE_UPDATE_SUBSCRIPTION,variables:i({id:e},u),updateQuery:(C,{subscriptionData:d})=>{if(!d.data)return C;let{article:h}=d.data;return {article:h,updatedAt:h.updatedAt}}});},[e,u,s,r==null?void 0:r.skip]),A(i({},a),{subscribeToMore:s,article:(P=a.data)==null?void 0:P.article})};var Q=(e,t,r)=>{var c;let o=buildContentType(e==null?void 0:e.contentType),n=useQuery(LIST_ARTICLES_QUERY,A(i({},r),{variables:A(i(i({},e),convertSearchParamsToGQL(t)),{contentType:o})}));return A(i({},n),{articles:(c=n.data)==null?void 0:c.articles})};function q({cursor:e,initialArticles:t,pageSize:r}){let[o,n]=useState(e),[c,u]=useState(t?[t]:[]),[s,a]=useState(false),[l,P]=useState(0);return useEffect(()=>{g(this,null,function*(){if(c[l])return;a(true);let{data:C,cursor:d}=yield PCCConvenienceFunctions.getPaginatedArticles(i({pageSize:r},o&&{cursor:o}));a(false),u(h=>{let b=[...h];return b[l]=C,b}),n(d);});},[l]),{data:c[l]||c[c.length-1],fetching:s,onPageChange:P,currentPage:l}}var p={};I(p,{GQL:()=>GQL,PCCConvenienceFunctions:()=>PCCConvenienceFunctions,PantheonAPI:()=>T,PantheonClient:()=>PantheonClient,findTab:()=>findTab,flattenDocumentTabs:()=>flattenDocumentTabs,getAllTags:()=>getAllTags,getArticle:()=>getArticle,getArticleBySlugOrId:()=>getArticleBySlugOrId,getArticlePathComponentsFromContentStructure:()=>getArticlePathComponentsFromContentStructure,getArticleURLFromSite:()=>getArticleURLFromSite,getArticleURLFromSiteWithOptions:()=>getArticleURLFromSiteWithOptions,getArticles:()=>getArticles,getPaginatedArticles:()=>getPaginatedArticles,getRecommendedArticles:()=>getRecommendedArticles,getSite:()=>getSite,updateConfig:()=>updateConfig});m(p,it);function T(e){let t=PantheonAPI(e);return (o,n)=>g(this,null,function*(){if(ge(n))return void(yield t(o,n));let c=n,u=new Headers(i({},c.headers||{}));return yield t({query:i(i({},Object.fromEntries(o.nextUrl.searchParams)),c.params),cookies:Re(o.cookies)},{getHeader:s=>u.get(s)||"",setHeader:(s,a)=>u.set(s,a.toString()),redirect:(s,a)=>(u.set("Location",a),new Response(null,{status:s,headers:u})),json:s=>Response.json(s,{headers:u})})})}function ge(e){return e!=null&&typeof e=="object"&&!("params"in e)}function Re(e){return e.getAll().reduce((t,r)=>(t[r.name]=r.value,t),{})}m(f,p);var Z=X.createContext(null),ye=({client:e,children:t})=>jsx(Z.Provider,{value:e,children:jsx(ApolloProvider,{client:e.apolloClient,children:t})}),ve=()=>{let e=X.useContext(Z);if(!e)throw new Error("Cannot use outside of a PantheonProvider");return e};var be=/\s*\{#h\.[^}]*\}/g,Se=/^#+\s*/gm,ke={gfm:true,mangle:false},Ne=function(e){return markdownToTxt(e.replace(be,""),ke).replace(Se,"")};export{T as PantheonAPI,ye as PantheonProvider,Ne as markdownToText,L as useArticle,Q as useArticles,q as usePagination,ve as usePantheonClient};//# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map