Skip to content

Commit

Permalink
Fix bug with commodity tabs sometimes not rendering
Browse files Browse the repository at this point in the history
Was not rendering after switching to the About tab and back to another tab as the query cache wasn't being updated when it should have been and so results were not being displayed or even fetched.
  • Loading branch information
iaincollins committed Nov 4, 2024
1 parent fb3e7f5 commit 9b6b72a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ardent-www",
"version": "0.95.1",
"version": "0.95.2",
"description": "Ardent Industry",
"main": "index.js",
"scripts": {
Expand Down
22 changes: 8 additions & 14 deletions pages/commodity/[commodity-name]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ export default () => {
const commodityName = window.location.pathname.split('/')[2]
if (!commodityName) return

const activeTabName = window.location.pathname.split('/')[3]?.toLowerCase()
if (!activeTabName) return
const activeTab = window.location.pathname.split('/')[3]?.toLowerCase() ?? TABS[0]
if (activeTab === TABS[0]) return

if (activeTabName === 'importers') {
if (activeTab === 'importers') {
setImports(undefined)
;(async () => {
const imports = await getImports(commodityName)
Expand All @@ -79,7 +79,7 @@ export default () => {
})()
}

if (activeTabName === 'exporters') {
if (activeTab === 'exporters') {
setExports(undefined)
; (async () => {
const exports = await getExports(commodityName)
Expand Down Expand Up @@ -109,16 +109,10 @@ export default () => {

// Compare current tab (i.e. Importers, Exporters) and query options
// If they have not actually changed then avoid triggering a redraw.
const activeTabName = window.location.pathname.split('/')[3]?.toLowerCase()
if (activeTabName) {
const cacheFingerprint = JSON.stringify({ activeTabName, query: router.query })
if (cachedQuery && cachedQuery === cacheFingerprint) {
return // If query has not changed, we don't need to do anything
} else {
// If query really has changed then update the cached query
setCachedQuery(cacheFingerprint)
}
}
const activeTab = window.location.pathname.split('/')[3]?.toLowerCase() ?? TABS[0]
const cacheFingerprint = JSON.stringify({ activeTab, query: router.query }) // Could be a checksum, but not worth it
if (cachedQuery && cachedQuery === cacheFingerprint) return // If the query hasn't *really* changed, return early
setCachedQuery(cacheFingerprint) // If the query has changed, continue and update the "last seen" query

setImports(undefined)
setExports(undefined)
Expand Down

0 comments on commit 9b6b72a

Please sign in to comment.