Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/frontend/src/lib/api/idb-nfts.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { browser } from '$app/environment';
import type { SetIdbNftsParams } from '$lib/types/idb-nfts';
import { isNullish } from '@dfinity/utils';
import type { Principal } from '@icp-sdk/core/principal';
import { createStore, get, set as idbSet, type UseStore } from 'idb-keyval';

// There is no IndexedDB in SSG. Since this initialization occurs at the module's root, SvelteKit would encounter an error during the dapp bundling process, specifically a "ReferenceError [Error]: indexedDB is not defined". Therefore, the object for bundling on NodeJS side.
const idbNftsStore = (key: string): UseStore =>
browser ? createStore(`oisy-${key}-nfts`, `${key}-nfts`) : ({} as unknown as UseStore);

const idbAllNftsStore = idbNftsStore('all');

export const setIdbNftsStore = async ({
identity,
nfts,
idbNftsStore
}: SetIdbNftsParams & {
idbNftsStore: UseStore;
}) => {
if (isNullish(identity)) {
return;
}

await idbSet(identity.getPrincipal().toText(), nfts, idbNftsStore);
};

export const setIdbAllNfts = (params: SetIdbNftsParams): Promise<void> =>
setIdbNftsStore({ ...params, idbNftsStore: idbAllNftsStore });

export const getIdbAllNfts = (
principal: Principal
): Promise<SetIdbNftsParams['nfts'] | undefined> => get(principal.toText(), idbAllNftsStore);
37 changes: 36 additions & 1 deletion src/frontend/src/lib/components/loaders/LoaderNfts.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { debounce } from '@dfinity/utils';
import { debounce, isNullish, nonNullish } from '@dfinity/utils';
import { untrack } from 'svelte';
import { getIdbAllNfts, setIdbAllNfts } from '$lib/api/idb-nfts.api';
import IntervalLoader from '$lib/components/core/IntervalLoader.svelte';
import { NFT_TIMER_INTERVAL_MILLIS } from '$lib/constants/app.constants';
import { ethAddress } from '$lib/derived/address.derived';
Expand All @@ -10,7 +11,31 @@
import { nftStore } from '$lib/stores/nft.store';
import { getTokensByNetwork } from '$lib/utils/nft.utils';

let nftCacheLoaded = $state(false);

const loadCachedNfts = async () => {
if (isNullish($authIdentity)) {
return;
}

const cachedNfts = await getIdbAllNfts($authIdentity.getPrincipal());

if (isNullish(cachedNfts) || cachedNfts.length === 0) {
nftCacheLoaded = true;

return;
}

nftStore.addAll(cachedNfts);

nftCacheLoaded = true;
};

const onLoad = async () => {
if (!nftCacheLoaded) {
await loadCachedNfts();
}

const tokensByNetwork = getTokensByNetwork($enabledNonFungibleTokens);

const promises = Array.from(tokensByNetwork).map(async ([networkId, tokens]) => {
Expand All @@ -34,6 +59,16 @@

untrack(() => debounceLoad());
});

$effect(() => {
if (nftCacheLoaded && nonNullish($nftStore)) {
// TODO: Needs to parse symbols properly before storing
setIdbAllNfts({
identity: $authIdentity,
nfts: $nftStore
});
}
});
</script>

<IntervalLoader interval={NFT_TIMER_INTERVAL_MILLIS} {onLoad} skipInitialLoad={true} />
7 changes: 7 additions & 0 deletions src/frontend/src/lib/types/idb-nfts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { OptionIdentity } from '$lib/types/identity';
import type { Nft } from '$lib/types/nft';

export interface SetIdbNftsParams {
identity: OptionIdentity;
nfts: Nft[];
}
Loading