{"version":3,"file":"useDocumentContext.mjs","sources":["../../../admin/src/hooks/useDocumentContext.ts"],"sourcesContent":["import * as React from 'react';\n\nimport { useQueryParams } from '@strapi/admin/strapi-admin';\n\nimport { useDoc, useDocument, type UseDocument } from '../hooks/useDocument';\nimport { useRelationModal } from '../pages/EditView/components/FormInputs/Relations/RelationModal';\nimport { buildValidParams } from '../utils/api';\n\ninterface DocumentMeta {\n /**\n * The equivalent of the \":id\" url param value\n * i.e. gus5a67jcboa3o2zjnz39mb1\n */\n documentId?: string;\n /**\n * The equivalent of the url \":slug\" param value\n * i.e. api::articles.article\n */\n model: string;\n /**\n * The equivalent of the url \":collectionType\" param value\n * i.e. collection-types or single-types\n */\n collectionType: string;\n /**\n * Query params object\n * i.e. { locale: 'fr' }\n */\n params?: Record;\n}\n\ninterface DocumentContextValue {\n currentDocumentMeta: DocumentMeta;\n currentDocument: ReturnType;\n}\n\nfunction useDocumentContext(consumerName: string): DocumentContextValue {\n // Try to get state from the relation modal context first\n const currentRelationDocumentMeta = useRelationModal(\n consumerName,\n (state) => state.currentDocumentMeta,\n false\n );\n const currentRelationDocument = useRelationModal(\n consumerName,\n (state) => state.currentDocument,\n false\n );\n\n // Then try to get the same state from the URL\n const { collectionType, model, id: documentId } = useDoc();\n const [{ query }] = useQueryParams();\n\n // TODO: look into why we never seem to pass any params\n const params = React.useMemo(() => buildValidParams(query ?? {}), [query]);\n const urlDocumentMeta: DocumentMeta = { collectionType, model, documentId: documentId!, params };\n const urlDocument = useDocument(urlDocumentMeta);\n\n /**\n * If there's modal state, use it in priority as it's the most specific\n * Fallback to the state derived from the URL, which is the default behavior,\n * used for the edit view, history and preview.\n */\n return {\n currentDocumentMeta: currentRelationDocumentMeta ?? urlDocumentMeta,\n currentDocument: currentRelationDocument ?? urlDocument,\n };\n}\n\nexport { useDocumentContext };\nexport type { DocumentMeta };\n"],"names":["useDocumentContext","consumerName","currentRelationDocumentMeta","useRelationModal","state","currentDocumentMeta","currentRelationDocument","currentDocument","collectionType","model","id","documentId","useDoc","query","useQueryParams","params","React","useMemo","buildValidParams","urlDocumentMeta","urlDocument","useDocument"],"mappings":";;;;;;AAoCA,SAASA,mBAAmBC,YAAoB,EAAA;;AAE9C,IAAA,MAAMC,8BAA8BC,gBAClCF,CAAAA,YAAAA,EACA,CAACG,KAAUA,GAAAA,KAAAA,CAAMC,mBAAmB,EACpC,KAAA,CAAA;AAEF,IAAA,MAAMC,0BAA0BH,gBAC9BF,CAAAA,YAAAA,EACA,CAACG,KAAUA,GAAAA,KAAAA,CAAMG,eAAe,EAChC,KAAA,CAAA;;IAIF,MAAM,EAAEC,cAAc,EAAEC,KAAK,EAAEC,EAAIC,EAAAA,UAAU,EAAE,GAAGC,MAAAA,EAAAA;AAClD,IAAA,MAAM,CAAC,EAAEC,KAAK,EAAE,CAAC,GAAGC,cAAAA,EAAAA;;IAGpB,MAAMC,MAAAA,GAASC,MAAMC,OAAO,CAAC,IAAMC,gBAAiBL,CAAAA,KAAAA,IAAS,EAAK,CAAA,EAAA;AAACA,QAAAA;AAAM,KAAA,CAAA;AACzE,IAAA,MAAMM,eAAgC,GAAA;AAAEX,QAAAA,cAAAA;AAAgBC,QAAAA,KAAAA;QAAOE,UAAYA,EAAAA,UAAAA;AAAaI,QAAAA;AAAO,KAAA;AAC/F,IAAA,MAAMK,cAAcC,WAAYF,CAAAA,eAAAA,CAAAA;AAEhC;;;;AAIC,MACD,OAAO;AACLd,QAAAA,mBAAAA,EAAqBH,2BAA+BiB,IAAAA,eAAAA;AACpDZ,QAAAA,eAAAA,EAAiBD,uBAA2Bc,IAAAA;AAC9C,KAAA;AACF;;;;"}