Skip to content

Commit 26ae636

Browse files
authored
fix: a bunch of fixes and refactoring the data fetching code (#19)
* fix: a bunch of fixes and refactoring the data fetching code
1 parent 5eb06c2 commit 26ae636

34 files changed

+712
-608
lines changed

package-lock.json

Lines changed: 43 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"pre-commit": "run-s check-types lint:fix audit test"
2323
},
2424
"dependencies": {
25-
"@algorandfoundation/algokit-subscriber": "^1.2.0",
25+
"@algorandfoundation/algokit-subscriber": "^1.3.0-beta.2",
2626
"@algorandfoundation/algokit-utils": "^6.0.0-beta.1",
2727
"@radix-ui/react-dialog": "^1.0.5",
2828
"@radix-ui/react-dropdown-menu": "^2.0.6",
@@ -61,7 +61,7 @@
6161
"@types/react-dom": "^18.2.19",
6262
"@typescript-eslint/eslint-plugin": "^7.0.2",
6363
"@typescript-eslint/parser": "^7.0.2",
64-
"@vitejs/plugin-react-swc": "^3.5.0",
64+
"@vitejs/plugin-react-swc": "^3.6.0",
6565
"autoprefixer": "^10.4.18",
6666
"better-npm-audit": "^3.7.3",
6767
"conventional-changelog-conventionalcommits": "^7.0.2",
@@ -79,8 +79,8 @@
7979
"semantic-release-export-data": "^1.0.1",
8080
"tailwindcss": "^3.4.1",
8181
"typescript": "^5.2.2",
82-
"vite": "^5.1.7",
83-
"vitest": "^1.3.1"
82+
"vite": "^5.2.9",
83+
"vitest": "^1.5.0"
8484
},
8585
"release": {
8686
"branches": [
@@ -142,4 +142,4 @@
142142
"semantic-release-export-data"
143143
]
144144
}
145-
}
145+
}

src/features/assets/data.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/features/assets/data/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { atom, useAtomValue, useStore } from 'jotai'
2+
import { useMemo } from 'react'
3+
import { AssetLookupResult, AssetResult } from '@algorandfoundation/algokit-utils/types/indexer'
4+
import { atomEffect } from 'jotai-effect'
5+
import { loadable } from 'jotai/utils'
6+
import { JotaiStore } from '@/features/common/data/types'
7+
import { indexer } from '@/features/common/data'
8+
import { AssetIndex } from './types'
9+
10+
// TODO: Size should be capped at some limit, so memory usage doesn't grow indefinitely
11+
export const assetsAtom = atom<Map<AssetIndex, AssetResult>>(new Map())
12+
13+
export const fetchAssetAtomBuilder = (store: JotaiStore, assetIndex: AssetIndex) => {
14+
const syncEffect = atomEffect((get, set) => {
15+
;(async () => {
16+
try {
17+
const asset = await get(assetAtom)
18+
set(assetsAtom, (prev) => {
19+
return new Map([...prev, [asset.index, asset]])
20+
})
21+
} catch (e) {
22+
// Ignore any errors as there is nothing to sync
23+
}
24+
})()
25+
})
26+
const assetAtom = atom((get) => {
27+
const assets = store.get(assetsAtom)
28+
const cachedAsset = assets.get(assetIndex)
29+
if (cachedAsset) {
30+
return cachedAsset
31+
}
32+
33+
get(syncEffect)
34+
35+
return indexer
36+
.lookupAssetByID(assetIndex)
37+
.do()
38+
.then((result) => {
39+
return (result as AssetLookupResult).asset
40+
})
41+
})
42+
return assetAtom
43+
}
44+
45+
export const fetchAssetsAtomBuilder = (store: JotaiStore, assetIndexes: AssetIndex[]) => {
46+
return atom(async (get) => {
47+
return await Promise.all(assetIndexes.map((assetIndex) => get(fetchAssetAtomBuilder(store, assetIndex))))
48+
})
49+
}
50+
51+
export const useAssetAtom = (assetIndex: AssetIndex) => {
52+
const store = useStore()
53+
return useMemo(() => {
54+
return fetchAssetAtomBuilder(store, assetIndex)
55+
}, [store, assetIndex])
56+
}
57+
58+
export const useLoadableAsset = (assetIndex: AssetIndex) => {
59+
return useAtomValue(loadable(useAssetAtom(assetIndex)))
60+
}

src/features/assets/data/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type AssetIndex = number

0 commit comments

Comments
 (0)