-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Profile Caching #1441
feat: Profile Caching #1441
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { IProfileSocials } from "@/features/profiles/profile-types"; | ||
import { useQueries, useQuery } from "@tanstack/react-query"; | ||
import { QueryKey, useQueries, useQuery } from "@tanstack/react-query"; | ||
import { getProfilesForInboxIds } from "@utils/api"; | ||
import { | ||
create, | ||
|
@@ -11,7 +11,10 @@ import { queryClient } from "./queryClient"; | |
import { InboxId } from "@xmtp/react-native-sdk"; | ||
import mmkv from "@/utils/mmkv"; | ||
|
||
const profileSocialsQueryKey = (account: string, peerAddress: string) => [ | ||
const profileSocialsQueryKey = ( | ||
account: string, | ||
peerAddress: string | ||
): QueryKey => [ | ||
"inboxProfileSocials", | ||
account?.toLowerCase(), | ||
peerAddress?.toLowerCase(), | ||
|
@@ -34,7 +37,10 @@ const profileSocials = create({ | |
}), | ||
}); | ||
|
||
const fetchInboxProfileSocials = async (account: string, inboxId: InboxId) => { | ||
const fetchInboxProfileSocials = async ( | ||
account: string, | ||
inboxId: InboxId | ||
): Promise<IProfileSocials[] | null> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return type should be inferred no? |
||
const data = await profileSocials.fetch(inboxId); | ||
|
||
const key = inboxProfileSocialsQueryStorageKey(account, inboxId); | ||
|
@@ -63,7 +69,18 @@ const inboxProfileSocialsQueryConfig = ( | |
// And automatic retries if there was an error fetching | ||
refetchOnMount: false, | ||
staleTime: 1000 * 60 * 60 * 24, | ||
// persister: reactQueryPersister, | ||
initialData: (): IProfileSocials[] | null | undefined => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return type should be inferred no? |
||
if (!account || !inboxId) { | ||
return undefined; | ||
} | ||
if (mmkv.contains(inboxProfileSocialsQueryStorageKey(account, inboxId))) { | ||
const data = JSON.parse( | ||
mmkv.getString(inboxProfileSocialsQueryStorageKey(account, inboxId))! | ||
) as IProfileSocials[]; | ||
return data; | ||
} | ||
}, | ||
initialDataUpdatedAt: 0, | ||
}); | ||
|
||
export const useInboxProfileSocialsQuery = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { IProfileSocials } from "@/features/profiles/profile-types"; | ||
import { useQueries, useQuery } from "@tanstack/react-query"; | ||
import { QueryKey, useQueries, useQuery } from "@tanstack/react-query"; | ||
import { getProfilesForAddresses } from "@utils/api"; | ||
import { | ||
create, | ||
|
@@ -12,7 +12,10 @@ import mmkv from "@/utils/mmkv"; | |
|
||
type ProfileSocialsData = IProfileSocials | null | undefined; | ||
|
||
const profileSocialsQueryKey = (account: string, peerAddress: string) => [ | ||
const profileSocialsQueryKey = ( | ||
account: string, | ||
peerAddress: string | ||
): QueryKey => [ | ||
"profileSocials", | ||
account?.toLowerCase(), | ||
// Typesafe because there's a lot of account! usage | ||
|
@@ -62,6 +65,15 @@ const profileSocialsQueryConfig = (account: string, peerAddress: string) => ({ | |
// And automatic retries if there was an error fetching | ||
refetchOnMount: false, | ||
staleTime: 1000 * 60 * 60 * 24, | ||
initialData: (): ProfileSocialsData => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return type should be inferred no? |
||
if (mmkv.contains(profileSocialsQueryStorageKey(account, peerAddress))) { | ||
const data = JSON.parse( | ||
mmkv.getString(profileSocialsQueryStorageKey(account, peerAddress))! | ||
) as ProfileSocialsData; | ||
return data; | ||
} | ||
}, | ||
initialDataUpdatedAt: 0, | ||
// persister: reactQueryPersister, | ||
}); | ||
|
||
|
@@ -96,7 +108,7 @@ export const fetchProfileSocialsQuery = ( | |
account: string, | ||
peerAddress: string | ||
) => { | ||
return queryClient.fetchQuery<ProfileSocialsData>( | ||
return queryClient.fetchQuery<IProfileSocials | null>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this I love having something like in
and reusing |
||
profileSocialsQueryConfig(account, peerAddress) | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this out as it's a prefetch, we don't want to await it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've put it there and we might need some refactoring for this but I think having the InboxId available is a must before showing the content to the user. Since we use it so much.
But again, with the refactor of accounts maybe that will be somewhere else or etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be cached better now, so I think it will be a better experience, where previously it was not cached