-
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
Profiles to React Queries #633
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Avatar, { AvatarProps } from "@components/Avatar"; | ||
import { useProfileSocials } from "@hooks/useProfileSocials"; | ||
import { getPreferredAvatar, getPreferredName } from "@utils/profile"; | ||
import { FC } from "react"; | ||
|
||
interface ConnectedAvatarProps | ||
extends Pick< | ||
AvatarProps, | ||
"size" | "style" | "color" | "showIndicator" | "invertColor" | ||
> { | ||
peerAddress: string; | ||
} | ||
|
||
export const ConnectedAvatar: FC<ConnectedAvatarProps> = ({ | ||
peerAddress, | ||
...avatarProps | ||
}) => { | ||
const { data } = useProfileSocials(peerAddress); | ||
|
||
const preferredAvatar = data ? getPreferredAvatar(data) : undefined; | ||
const preferredName = data ? getPreferredName(data, peerAddress) : undefined; | ||
return <Avatar uri={preferredAvatar} name={preferredName} {...avatarProps} />; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { getPreferredName } from "@utils/profile"; | ||
|
||
import { useProfileSocials } from "./useProfileSocials"; | ||
|
||
export const usePreferredName = (peerAddress: string) => { | ||
const { data } = useProfileSocials(peerAddress); | ||
return data ? getPreferredName(data, peerAddress) : peerAddress; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { getPreferredName } from "@utils/profile"; | ||
import { useMemo } from "react"; | ||
|
||
import { useProfilesSocials } from "./useProfilesSocials"; | ||
|
||
/** | ||
* | ||
* @param peerAddress Multiple peer addresses to get their socials | ||
* @returns array of preferred names or the address if not found | ||
*/ | ||
export const usePreferredNames = (peerAddresses: string[]) => { | ||
const data = useProfilesSocials(peerAddresses); | ||
const names = useMemo(() => { | ||
// Not sure how performant this will be, or if we can safely rely on the index | ||
// If we can't, we should probably use a Map instead | ||
return data.map(({ data: socials }, index) => { | ||
const peerAddress = peerAddresses[index]; | ||
return socials ? getPreferredName(socials, peerAddress) : peerAddress; | ||
}); | ||
}, [data, peerAddresses]); | ||
return names; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { useCurrentAccount } from "@data/store/accountsStore"; | ||
import { useProfileSocialsQuery } from "@queries/useProfileSocialsQuery"; | ||
|
||
export const useProfileSocials = (peerAddress: string) => { | ||
const currentAccount = useCurrentAccount(); | ||
return useProfileSocialsQuery(currentAccount!, peerAddress); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useCurrentAccount } from "@data/store/accountsStore"; | ||
import { useProfileSocialsQueries } from "@queries/useProfileSocialsQuery"; | ||
|
||
/** | ||
* | ||
* @param peerAddresses Use multiple peer addresses to get their socials | ||
* @returns | ||
*/ | ||
export const useProfilesSocials = (peerAddresses: string[]) => { | ||
const currentAccount = useCurrentAccount(); | ||
return useProfileSocialsQueries(currentAccount!, peerAddresses); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { ProfileSocials } from "@data/store/profilesStore"; | ||
import { useQueries, useQuery } from "@tanstack/react-query"; | ||
import { getProfilesForAddresses } from "@utils/api"; | ||
import { | ||
create, | ||
windowedFiniteBatchScheduler, | ||
indexedResolver, | ||
} from "@yornaath/batshit"; | ||
|
||
import { profileSocialsQueryKey } from "./QueryKeys"; | ||
import { queryClient } from "./queryClient"; | ||
|
||
const profileSocials = create({ | ||
fetcher: async (addresses: string[]) => { | ||
const data = await getProfilesForAddresses(addresses); | ||
return data; | ||
}, | ||
resolver: indexedResolver(), | ||
scheduler: windowedFiniteBatchScheduler({ | ||
windowMs: 10, | ||
maxBatchSize: 150, | ||
}), | ||
}); | ||
Comment on lines
+13
to
+23
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. I think react-query handle request deduplication 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. It does, but this helps with batching all requests 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. OH very nice yeah makes sense didn't know about this |
||
|
||
const fetchProfileSocials = async (peerAddress: string) => { | ||
const data = await profileSocials.fetch(peerAddress); | ||
return data; | ||
}; | ||
|
||
const profileSocialesQueryConfig = (account: string, peerAddress: string) => ({ | ||
queryKey: profileSocialsQueryKey(account, peerAddress), | ||
queryFn: () => fetchProfileSocials(peerAddress), | ||
enabled: !!account, | ||
// Store for 30 days | ||
gcTime: 1000 * 60 * 60 * 24 * 30, | ||
refetchIntervalInBackground: false, | ||
refetchOnWindowFocus: false, | ||
// We really just want a 24 hour cache here | ||
// And automatic retries if there was an error fetching | ||
refetchOnMount: false, | ||
staleTime: 1000 * 60 * 60 * 24, | ||
}); | ||
|
||
export const useProfileSocialsQuery = ( | ||
account: string, | ||
peerAddress: string | ||
) => { | ||
return useQuery(profileSocialesQueryConfig(account, peerAddress)); | ||
}; | ||
|
||
export const useProfileSocialsQueries = ( | ||
account: string, | ||
peerAddresses: string[] | ||
) => { | ||
return useQueries({ | ||
queries: peerAddresses.map((peerAddress) => | ||
profileSocialesQueryConfig(account, peerAddress) | ||
), | ||
}); | ||
}; | ||
|
||
export const fetchProfileSocialsQuery = ( | ||
account: string, | ||
peerAddress: string | ||
) => { | ||
return queryClient.fetchQuery( | ||
profileSocialesQueryConfig(account, peerAddress) | ||
); | ||
}; | ||
|
||
export const setProfileSocialsQueryData = ( | ||
account: string, | ||
peerAddress: string, | ||
data: ProfileSocials, | ||
updatedAt?: number | ||
) => { | ||
return queryClient.setQueryData( | ||
profileSocialsQueryKey(account, peerAddress), | ||
data, | ||
{ | ||
updatedAt, | ||
} | ||
); | ||
}; |
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.
Curious, why did you choose to add all keys into 1 file? Just really curious. I'm not doing that but maybe it's the best practice. What I do is put each keys into it's own file
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 just kinda did it one day, but I think how we are structuring folders by feature it probably makes more sense to split them into the separate files