Skip to content

Commit

Permalink
improve tile load time
Browse files Browse the repository at this point in the history
- may need to manually call refresh() for state change
  • Loading branch information
Fredenck committed Nov 14, 2024
1 parent 3726810 commit 9bf119a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ yarn-error.log*
.env.production.local

# Firebase cache
.firebase/
.firebase/

.vscode/launch.json
16 changes: 0 additions & 16 deletions .vscode/launch.json

This file was deleted.

17 changes: 8 additions & 9 deletions app/components/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ const Dashboard = () => {
// }, [formattedMatches])

// Fuzzy search
const fuse = useMemo(
() =>
new Fuse(formattedMatches, {
keys: searchableProperties,
threshold: 0.3
}),
[formattedMatches]
)
const fuse = useMemo(() => {
if (!formattedMatches.length) return null
return new Fuse(formattedMatches, {
keys: searchableProperties,
threshold: 0.3
})
}, [formattedMatches])

const filteredMatchSets = useMemo(() => {
if (!searchTerm) return []
if (!searchTerm || !fuse) return []
const result = fuse.search(searchTerm).map((result) => {
const match = result.item
return `${match.matchDate}#${match.teams.opponentTeam}`
Expand Down
43 changes: 18 additions & 25 deletions app/components/DataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,24 @@ export const DataProvider = ({ children }) => {
[fetchMatches, matches]
)

const createMatch = useCallback(
async (collectionName, newMatchData) => {
try {
const newMatch = {
id: 'temp-id',
collection: collectionName,
...newMatchData
}

setMatches((prevMatches) => [...prevMatches, newMatch])

const colRef = collection(db, collectionName)
await addDoc(colRef, newMatchData)

await fetchMatches()
} catch (err) {
setError(err)
console.error('Error creating new match:', err)
const createMatch = useCallback(async (collectionName, newMatchData) => {
try {
const newMatch = {
id: 'temp-id',
collection: collectionName,
...newMatchData
}
},
[fetchMatches]
)
setMatches((prevMatches) => [...prevMatches, newMatch])

// Actual Firestore addition
const colRef = collection(db, collectionName)
await addDoc(colRef, newMatchData)
await fetchMatches()
} catch (err) {
setError(err)
console.error('Error creating new match:', err)
}
}, [])

const fetchLogos = useCallback(async () => {
const storedLogos = localStorage.getItem('teamLogos')
Expand Down Expand Up @@ -172,9 +168,6 @@ export const useData = () => {
const { matches, logos, loading, error, refresh, updateMatch, createMatch } =
context

useEffect(() => {
refresh()
}, [refresh])

// Optionally keep `refresh` available for manual use in components
return { matches, logos, loading, error, refresh, updateMatch, createMatch }
}

0 comments on commit 9bf119a

Please sign in to comment.