/**
Copyright 2021 Forestry.io Holdings, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import React , { useEffect , useState } from 'react' ; import type { TinaCMS } from '@tinacms/toolkit' ; interface Document { node : { sys : { template : string ; breadcrumbs : string [ ] ; path : string ; basename : string ; relativePath : string ; filename : string ; extension : string ; } ; } ; } export interface Template { name : string ; label : string ; fields : any [ ] ; } export interface Collection { label : string ; name : string ; format : string ; templates : Template [ ] ; documents : { totalCount : number ; edges : Document [ ] ; } ; } export const useGetCollection = ( cms , collectionName , includeDocuments = true ) => { const [ collection , setCollection ] = useState < Collection | undefined > ( undefined ) ; useEffect ( ( ) => { const fetchCollection = async ( ) => { const response = await cms . api . tina . request ( `
          query($collection: String!, $includeDocuments: Boolean!){
            getCollection(collection: $collection){
              name
              label
              format
              templates
              documents @include(if: $includeDocuments) {
                totalCount
                edges {
                  node {
                    ... on Document {
                      sys {
                        template
                        breadcrumbs
                        path
                        basename
                        relativePath
                        filename
                        extension
                      }
                    }
                  }
                }
              }
            }
          }` , { variables : { collection : collectionName , includeDocuments } } ) ; setCollection ( response . getCollection ) ; } ; fetchCollection ( ) ; } , [ cms , collectionName ] ) ; return collection ; } ; const GetCollection = ( { cms , collectionName , includeDocuments = true , children , } ) => { const collection = useGetCollection ( cms , collectionName , includeDocuments ) ; if ( ! collection ) { return null ; } return < > { children ( collection ) } < />; } ; export default GetCollection ;