1- import { ODataCollectionResponse } from '@sensenet/client-core'
21import { GenericContent } from '@sensenet/default-content-types'
32import { useRepository } from '@sensenet/hooks-react'
4- import React , { createContext , ReactNode , useEffect , useRef , useState } from 'react'
3+ import React , { createContext , ReactNode , useRef , useState } from 'react'
54// Meghatározzuk a Context típusát: egy string tömb és egy setter függvény
65type ExpandItemsContextType = [
76 Set < string > ,
@@ -10,70 +9,80 @@ type ExpandItemsContextType = [
109 React . Dispatch < React . SetStateAction < Set < string > > > ,
1110 ( path : string ) => Promise < GenericContent [ ] | undefined > ,
1211 React . Dispatch < React . SetStateAction < number > > ,
12+ ( path : string ) => void ,
1313]
1414
15- // Létrehozzuk a Context-et alapértelmezett értékekkel
1615export const ExpandItemsContext = createContext < ExpandItemsContextType | undefined > ( undefined )
1716
1817const ExpandedItemsProvider = ( { children } : { children : ReactNode } ) => {
1918 const [ expandItems , setExpandItems ] = useState < Set < string > > ( new Set ( ) )
2019 const [ expandOriginalItems , setExpandOriginalItems ] = useState < Set < string > > ( new Set ( ) )
21- const cache = useRef < { [ key : string ] : { data : GenericContent [ ] | undefined ; timestamp : number } } > ( { } ) // Globális cache objektum
2220 const [ cacheTime , setCacheTime ] = useState < number > ( 6000 )
21+ const cache = useRef < { [ key : string ] : { data : GenericContent [ ] | undefined ; timestamp : number } } > ( { } )
2322 const repo = useRepository ( )
23+
2424 const loadChildren = async ( path : string ) : Promise < GenericContent [ ] | undefined > => {
25- //letölti a connteneteket a megadott path alapján
26- const loadCollection = function loadCollection (
27- contentPath : string ,
28- ) : Promise < ODataCollectionResponse < GenericContent > > {
29- return repo . loadCollection < GenericContent > ( {
30- path : contentPath ,
31- oDataOptions : {
32- select : [ 'Id' , 'Path' , 'Name' , 'DisplayName' , 'Type' , 'Actions' , 'Icon' , 'ParentId' ] ,
33- onlyselectList : true ,
34- } ,
35- } )
36- }
37- //cache logika
38- if ( path === undefined || path === '' ) return undefined
25+ if ( ! path ) return undefined
26+
3927 const now = Date . now ( )
40- //ha a cachben nincs elem akkor tovább megyünk
41- if ( cache . current [ path ] !== undefined ) {
42- //ha a cacheben van elem és undefined akkor várunk
43- let i = 0
44- while ( cache . current [ path ] . data === undefined ) {
45- setTimeout ( ( ) => { } , 200 )
46- i ++
47- if ( i > 10 ) {
48- return undefined
49- }
28+
29+ const cached = cache . current [ path ]
30+ if ( cached ) {
31+ if ( cached . data !== undefined && now - cached . timestamp < cacheTime ) {
32+ return cached . data
5033 }
5134
52- //ha a cacheben van elem és nem undefined akkor visszadjuk azt
53- if ( now - cache . current [ path ] . timestamp < cacheTime ) {
54- return (
55- cache . current [ path ] as {
56- data : GenericContent [ ] | undefined
57- timestamp : number
35+ // Wait if data is still loading (i.e., placeholder is in cache with undefined data)
36+ if ( cached . data === undefined ) {
37+ // Polling-based wait
38+ for ( let i = 0 ; i < 10 ; i ++ ) {
39+ await new Promise ( ( res ) => setTimeout ( res , 200 ) )
40+ const recheck = cache . current [ path ]
41+ if ( recheck ?. data !== undefined && now - recheck . timestamp < cacheTime ) {
42+ return recheck . data
5843 }
59- ) ?. data
44+ }
45+ return undefined // Timeout
6046 }
6147 }
48+
49+ // Mark as loading
50+ cache . current [ path ] = { data : undefined , timestamp : now }
51+
6252 try {
63- const response = await loadCollection ( path )
64- //itt megkéne várni, esetleg egy await async meoldaná a problémát
53+ const response = await repo . loadCollection < GenericContent > ( {
54+ path,
55+ oDataOptions : {
56+ select : [ 'Id' , 'Path' , 'Name' , 'DisplayName' , 'Type' , 'Actions' , 'Icon' , 'ParentId' ] ,
57+ onlyselectList : true ,
58+ } ,
59+ } )
6560 const result = response ?. d . results
66- cache . current [ path ] = { data : result , timestamp : now }
61+ cache . current [ path ] = { data : result , timestamp : Date . now ( ) }
6762 return result
68- //ezt nem várja meg
6963 } catch ( error ) {
7064 console . error ( '#globalfetch: Fetch error:' , error )
65+ // Clean up failed cache
66+ delete cache . current [ path ]
67+ return undefined
7168 }
7269 }
7370
71+ const deleteCache = ( path : string ) => {
72+ delete cache . current [ path ]
73+ }
74+
7475 return (
7576 < ExpandItemsContext . Provider
76- value = { [ expandItems , setExpandItems , expandOriginalItems , setExpandOriginalItems , loadChildren , setCacheTime ] } >
77+ value = { [
78+ expandItems ,
79+ setExpandItems ,
80+ expandOriginalItems ,
81+ setExpandOriginalItems ,
82+ loadChildren ,
83+ setCacheTime ,
84+ deleteCache ,
85+ ] } >
7786 { children }
7887 </ ExpandItemsContext . Provider >
7988 )
0 commit comments