@@ -12,6 +12,7 @@ import {
1212 doc ,
1313 updateDoc ,
1414 addDoc ,
15+ getDoc ,
1516 query ,
1617 where
1718} from 'firebase/firestore'
@@ -20,6 +21,7 @@ import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'
2021import { db , storage } from '@/app/services/initializeFirebase.js'
2122import { useAuth } from '@/app/AuthWrapper.js'
2223import getTeams from '@/app/services/getTeams.js'
24+ import { getLogoFromCache , setLogoInCache } from '@/app/services/logoCache'
2325
2426const DataContext = createContext ( )
2527
@@ -39,7 +41,7 @@ export const DataProvider = ({ children }) => {
3941
4042 const { userProfile } = useAuth ( )
4143
42- // Optmized fetchMatches version
44+ // Optimized fetchMatches version with query filtering
4345 const fetchMatches = useCallback ( async ( ) => {
4446 if ( ! userProfile ?. collections ?. length ) return
4547
@@ -74,6 +76,21 @@ export const DataProvider = ({ children }) => {
7476 }
7577 } , [ userProfile ] )
7678
79+ // Function to fetch detailed match data including points
80+ const fetchMatchDetails = useCallback ( async ( matchId , collectionName ) => {
81+ try {
82+ const matchRef = doc ( db , collectionName , matchId )
83+ const matchDoc = await getDoc ( matchRef )
84+ if ( matchDoc . exists ( ) ) {
85+ return matchDoc . data ( )
86+ }
87+ return null
88+ } catch ( error ) {
89+ console . error ( 'Error fetching match details:' , error )
90+ return null
91+ }
92+ } , [ ] )
93+
7794 const updateMatch = useCallback (
7895 async ( matchId , updatedData ) => {
7996 try {
@@ -143,34 +160,25 @@ export const DataProvider = ({ children }) => {
143160 )
144161
145162 const fetchLogos = useCallback ( async ( ) => {
146- // Cache expiry time, currently 24 hours
147- const CACHE_EXPIRY_MS = 24 * 60 * 60 * 1000
148- const storedLogos = localStorage . getItem ( 'teamLogos' )
149- const storedTimeStamp = localStorage . getItem ( 'teamLogosTimestamp' )
150-
151- if ( storedLogos && storedTimeStamp ) {
152- // check if cache expired
153- const cacheAge = Date . now ( ) - parseInt ( storedTimeStamp , 10 )
154- if ( cacheAge < CACHE_EXPIRY_MS ) {
155- setLogos ( JSON . parse ( storedLogos ) )
156- setLogosLoading ( false )
157- return
158- }
159- }
160-
161163 setLogosLoading ( true )
162164 setLogosError ( null )
163165
164166 try {
165167 const teams = await getTeams ( )
166168 const logosMap = teams . reduce ( ( acc , team ) => {
167- acc [ team . name ] = team . logoUrl
169+ // Check cache first
170+ const cachedLogo = getLogoFromCache ( team . name )
171+ if ( cachedLogo ) {
172+ acc [ team . name ] = cachedLogo
173+ } else {
174+ acc [ team . name ] = team . logoUrl
175+ // Cache the logo
176+ setLogoInCache ( team . name , team . logoUrl )
177+ }
168178 return acc
169179 } , { } )
170180
171181 setLogos ( logosMap )
172- localStorage . setItem ( 'teamLogos' , JSON . stringify ( logosMap ) )
173- localStorage . setItem ( 'teamLogosTimestamp' , Date . now ( ) . toString ( ) )
174182 } catch ( err ) {
175183 setLogosError ( err )
176184 console . error ( 'Error fetching team logos:' , err )
@@ -192,6 +200,7 @@ export const DataProvider = ({ children }) => {
192200 loading : loading || logosLoading ,
193201 error : error || logosError ,
194202 refresh : fetchMatches ,
203+ fetchMatchDetails,
195204 updateMatch,
196205 createMatch
197206 } }
@@ -208,9 +217,26 @@ export const useData = () => {
208217 throw new Error ( 'useData must be used within a MatchDataProvider' )
209218 }
210219
211- const { matches, logos, loading, error, refresh, updateMatch, createMatch } =
212- context
220+ const {
221+ matches,
222+ logos,
223+ loading,
224+ error,
225+ refresh,
226+ fetchMatchDetails,
227+ updateMatch,
228+ createMatch
229+ } = context
213230
214231 // Optionally keep `refresh` available for manual use in components
215- return { matches, logos, loading, error, refresh, updateMatch, createMatch }
232+ return {
233+ matches,
234+ logos,
235+ loading,
236+ error,
237+ refresh,
238+ fetchMatchDetails,
239+ updateMatch,
240+ createMatch
241+ }
216242}
0 commit comments