Skip to content

Commit

Permalink
feat: wip session state
Browse files Browse the repository at this point in the history
  • Loading branch information
simodrws committed Jun 13, 2023
2 parents 54eaa51 + 8343024 commit c3c0386
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 167 deletions.
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@masa-finance/masa-react",
"version": "2.4.7",
"version": "2.4.8",
"license": "MIT",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts",
Expand Down Expand Up @@ -136,7 +136,7 @@
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"tsconfig-paths-webpack-plugin": "^4.0.1",
"typedoc": "^0.24.4",
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.3",
"typescript": "^5.0.4",
"url": "^0.11.0",
Expand Down
13 changes: 8 additions & 5 deletions src/provider/modules/custom-sbts/custom-sbts.ts
Expand Up @@ -4,6 +4,7 @@ import { Signer } from 'ethers';
import { useQuery } from 'react-query';
import { useLocalStorage } from '../../use-local-storage';
import { queryClient } from '../../masa-query-client';
import { CustomGallerySBT } from 'components/masa-interface/pages/gallery/gallery';

function getLocalStorageRecordsByPrefix(
prefix: string
Expand Down Expand Up @@ -39,15 +40,17 @@ export const useSavedSbts = (masa, prefix): { savedContracts: any[] } => {
return { savedContracts };
};

const fetchContracts = async (masa, customGallerySBT) => {
const fetchContracts = async (
masa: Masa,
customGallerySBT: CustomGallerySBT[]
) => {
if (customGallerySBT && customGallerySBT.length > 0) {
const newContracts: any[] = [];
for (const sbt of customGallerySBT) {
if (sbt.networks) {
if (sbt.network !== masa.config.networkName) {
continue;
}
if (sbt.network && sbt.network !== masa.config.networkName) {
continue;
}

try {
const sbtContract = await masa?.sbt.connect(sbt.address);

Expand Down
111 changes: 58 additions & 53 deletions src/refactor/masa-client/query-client.ts
Expand Up @@ -6,70 +6,75 @@ import type {
} from '@tanstack/react-query-persist-client';
import { persistQueryClient } from '@tanstack/react-query-persist-client';
import { noopStorage } from '@wagmi/core';
import { createContext } from 'react';
import { createStorage } from 'wagmi';

export const createQueryClient = () => {
// const queryClient = new QueryClient({
// defaultOptions: {
// queries: {
// cacheTime: 1000 * 60 * 60 * 24, // 24 hours
// networkMode: 'offlineFirst',
// refetchOnWindowFocus: false,
// retry: 0,
// },
// mutations: {
// networkMode: 'offlineFirst',
// },
// },
// });

// const storage = createStorage({
// storage:
// typeof window !== 'undefined' && window.localStorage
// ? window.localStorage
// : noopStorage,
// });
// let persister: Persister | undefined;

// if (typeof window !== 'undefined') {
// persister = createSyncStoragePersister({
// key: 'masa-cache',
// storage,
// // Serialization is handled in `storage`.
// serialize: (x: unknown) => x as string,
// // Deserialization is handled in `storage`.
// deserialize: (x: unknown) => x as PersistedClient,
// });
// }
export const MasaQueryClientContext = createContext<QueryClient | undefined>(
undefined
);

// if (persister)
// persistQueryClient({
// queryClient,
// persister,
// dehydrateOptions: {
// shouldDehydrateQuery: (
// query: Query<unknown, unknown, unknown, QueryKey>
// ) =>
// query.cacheTime !== 0 &&
// // Note: adding a `persist` flag to a query key will instruct the
// // persister whether or not to persist the response of the query.
// (query.queryKey[0] as { persist?: boolean }).persist !== false,
// },
// });

const test = new QueryClient({
export const createQueryClient = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
networkMode: 'offlineFirst',
refetchOnWindowFocus: false,
retry: 0,
},
mutations: {
networkMode: 'offlineFirst',
},
},
});

const testPersist = createSyncStoragePersister({
storage: window.localStorage,
const storage = createStorage({
storage:
typeof window !== 'undefined' && window.localStorage
? window.localStorage
: noopStorage,
});
let persister: Persister | undefined;

if (typeof window !== 'undefined') {
persister = createSyncStoragePersister({
key: 'masa-cache',
storage,
// Serialization is handled in `storage`.
serialize: (x: unknown) => x as string,
// Deserialization is handled in `storage`.
deserialize: (x: unknown) => x as PersistedClient,
});
}

if (persister)
persistQueryClient({
queryClient,
persister,
dehydrateOptions: {
shouldDehydrateQuery: (
query: Query<unknown, unknown, unknown, QueryKey>
) =>
query.cacheTime !== 0 &&
// Note: adding a `persist` flag to a query key will instruct the
// persister whether or not to persist the response of the query.
(query.queryKey[0] as { persist?: boolean }).persist !== false,
},
});

// const test = new QueryClient({
// defaultOptions: {
// queries: {
// refetchOnWindowFocus: false,
// cacheTime: 1000 * 60 * 60 * 24, // 24 hours
// },
// },
// });

// const testPersist = createSyncStoragePersister({
// storage: window.localStorage,
// });

return test;
// return test;
return queryClient;
};
5 changes: 4 additions & 1 deletion src/refactor/masa-feature/use-identity.ts
Expand Up @@ -2,10 +2,13 @@ import { useAsync, useAsyncFn } from 'react-use';
import { useMasaClient } from '../masa-client/use-masa-client';
import { useWallet } from '../wallet-client/wallet/use-wallet';




export const useIdentity = () => {
const { sdk: masa } = useMasaClient();
const { address } = useWallet();

const [{ value: identity, loading: isLoadingIdentity }, loadIdentity] =
useAsyncFn(async () => {
try {
Expand Down

0 comments on commit c3c0386

Please sign in to comment.