@@ -32,6 +32,7 @@ import type { AppResourcesOutlet } from './index';
3232import { globalResourceKey } from './tree' ;
3333import type { ScopedAppResourceDir } from './types' ;
3434import { appResourceSubTypes } from './types' ;
35+ import { replaceViewsetNameInXml } from './xmlUtils' ;
3536
3637export function AppResourceView ( ) : JSX . Element {
3738 return < Wrapper mode = "appResources" /> ;
@@ -197,50 +198,33 @@ function useAppResource(
197198 ) ;
198199}
199200
200- /*
201- * REFACTOR:
202- * Split this function up.
203- * Currently, the resource is not needed until subtype needs to be determined.
204- * All the functionality that does not depend on resource should be part of a different
205- * function.
206- */
207201function useInitialData (
208202 resource : SerializedResource < SpAppResource | SpViewSetObj > ,
209203 initialDataFrom : number | undefined ,
210204 templateFile : string | undefined
211205) : string | false | undefined {
212206 return useAsyncState (
213207 React . useCallback ( async ( ) => {
214- const escapeXml = ( s : string ) : string =>
215- s
216- . replace ( / & / g, '&' )
217- . replace ( / < / g, '<' )
218- . replace ( / > / g, '>' )
219- . replace ( / " / g, '"' ) ;
220-
221208 const replaceViewsetName = ( data : string | null | undefined ) : string => {
222- const xml = data ?? '' ;
223209 const resourceName = ( resource as any ) ?. name ?? '' ;
224- if ( typeof resourceName !== 'string' || resourceName . length === 0 )
225- return xml ;
226- return xml . replace (
227- / ( < v i e w s e t \b [ ^ > ] * \b n a m e = ) ( [ " ] ) ( .* ?) \2/ ,
228- ( _match , p1 , p2 ) => `${ p1 } ${ p2 } ${ escapeXml ( resourceName ) } ${ p2 } `
229- ) ;
210+ return replaceViewsetNameInXml ( data , resourceName ) ;
230211 } ;
231212
232- if ( typeof initialDataFrom === 'number' ) {
233- const { data } = await fetchResource ( 'SpAppResourceData' , initialDataFrom ) ;
234- return replaceViewsetName ( data ) ;
235- }
236- if ( typeof templateFile === 'string' ) {
237- try {
238- const { data } = await ajax ( `/static/config/${ templateFile } ` , { headers : { } } ) ;
239- return replaceViewsetName ( data ) ;
240- } catch {
241- return '' ;
213+ if ( typeof initialDataFrom === 'number' )
214+ return fetchResource ( 'SpAppResourceData' , initialDataFrom ) . then (
215+ ( { data } ) => replaceViewsetName ( data ?? '' ) ) ;
216+ else if ( typeof templateFile === 'string' ) {
217+ if ( templateFile . includes ( '..' ) )
218+ console . error (
219+ 'Relative paths not allowed. Path is always relative to /static/config/'
220+ ) ;
221+ else
222+ return ajax ( `/static/config/${ templateFile } ` , {
223+ headers : { } ,
224+ } )
225+ . then ( ( { data } ) => replaceViewsetName ( data ) )
226+ . catch ( ( ) => '' ) ;
242227 }
243- }
244228 const subType = f . maybe (
245229 toResource ( resource , 'SpAppResource' ) ,
246230 getAppResourceType
@@ -250,15 +234,13 @@ function useInitialData(
250234 const useTemplate =
251235 typeof type . name === 'string' &&
252236 ( ! ( 'useTemplate' in type ) || type . useTemplate ) ;
253- if ( useTemplate ) {
254- const { data } = await ajax ( getAppResourceUrl ( type . name , 'quiet' ) , {
237+ if ( useTemplate )
238+ return ajax ( getAppResourceUrl ( type . name , 'quiet' ) , {
255239 headers : { } ,
256- } ) ;
257- return replaceViewsetName ( data ) ;
258- }
240+ } ) . then ( ( { data } ) => replaceViewsetName ( data ) ) ;
259241 }
260242 return false ;
261- } , [ initialDataFrom , templateFile , resource ] ) ,
243+ } , [ initialDataFrom , templateFile ] ) ,
262244 false
263245 ) [ 0 ] ;
264246}
0 commit comments