@@ -13,6 +13,8 @@ import { getDocumentPathFromSlug } from "~/actions/get-document-path-from-slug";
1313import { renderDocumentToPDF , RenderDocumentToPDFProps } from "~/actions/render-document-to-pdf" ;
1414import { PageConfig } from "~/lib/types" ;
1515import logger from "~/lib/logger" ;
16+ import { JSONSchema7Definition } from "json-schema" ;
17+ import { getDocumentSchema } from "~/actions/get-document-schema" ;
1618
1719const DocumentsContext = createContext <
1820 | {
@@ -27,6 +29,7 @@ const DocumentsContext = createContext<
2729 renderDocumentToPDF : ( { url, ...props } : RenderDocumentToPDFProps ) => Promise < Buffer | Error > ;
2830 pageConfigs : Record < string , PageConfig > ;
2931 setPageConfig : ( documentPath : string , config : PageConfig ) => void ;
32+ documentSchemas : Record < string , JSONSchema7Definition > ;
3033 }
3134 | undefined
3235> ( undefined ) ;
@@ -54,6 +57,7 @@ export const DocumentsProvider = (props: {
5457 useState < Record < string , DocumentRenderingResult > > ( { } ) ;
5558
5659 const [ pageConfigs , setPageConfigs ] = useState < Record < string , PageConfig > > ( { } ) ;
60+ const [ documentSchemas , setDocumentSchemas ] = useState < Record < string , JSONSchema7Definition > > ( { } ) ;
5761
5862 const setPageConfig = ( documentPath : string , config : PageConfig ) => {
5963 setPageConfigs ( prev => ( {
@@ -62,6 +66,20 @@ export const DocumentsProvider = (props: {
6266 } ) ) ;
6367 } ;
6468
69+ const loadSchema = async ( documentPath : string ) => {
70+ try {
71+ const schema = await getDocumentSchema ( documentPath ) ;
72+ if ( schema ) {
73+ setDocumentSchemas ( prev => ( {
74+ ...prev ,
75+ [ documentPath ] : schema
76+ } ) ) ;
77+ }
78+ } catch ( error ) {
79+ logger . warn ( 'Failed to load schema:' , error ) ;
80+ }
81+ } ;
82+
6583 if ( process . env . NEXT_PUBLIC_IS_BUILDING !== "true" ) {
6684 // this will not change on runtime so it doesn't violate
6785 // the rules of hooks
@@ -112,6 +130,9 @@ export const DocumentsProvider = (props: {
112130 ...map ,
113131 [ pathForChangedDocument ] : renderingResult ,
114132 } ) ) ;
133+
134+ // Load or reload schema for the changed document
135+ await loadSchema ( pathForChangedDocument ) ;
115136 }
116137 }
117138 } ) ;
@@ -149,6 +170,7 @@ export const DocumentsProvider = (props: {
149170 renderDocumentToPDF,
150171 pageConfigs,
151172 setPageConfig,
173+ documentSchemas,
152174 } }
153175 >
154176 { props . children }
0 commit comments