-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Show Profile info in join requests (#999)
Added React Query Profiles query Added batshit for batching queries Added helper hooks
- Loading branch information
Showing
10 changed files
with
162 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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 { queryClient } from "./queryClient"; | ||
|
||
const profileSocialsQueryKey = (account: string, peerAddress: string) => [ | ||
"profileSocials", | ||
account, | ||
peerAddress, | ||
]; | ||
|
||
const profileSocials = create({ | ||
fetcher: async (addresses: string[]) => { | ||
const data = await getProfilesForAddresses(addresses); | ||
return data; | ||
}, | ||
resolver: indexedResolver(), | ||
scheduler: windowedFiniteBatchScheduler({ | ||
windowMs: 10, | ||
maxBatchSize: 150, | ||
}), | ||
}); | ||
|
||
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, | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9834,6 +9834,18 @@ | |
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" | ||
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ== | ||
|
||
"@yornaath/batshit-devtools@^1.7.1": | ||
version "1.7.1" | ||
resolved "https://registry.yarnpkg.com/@yornaath/batshit-devtools/-/batshit-devtools-1.7.1.tgz#6c247f2c4c4e3322811beca96cb62bbf19489e04" | ||
integrity sha512-AyttV1Njj5ug+XqEWY1smV45dTWMlWKtj1B8jcFYgBKUFyUlF/qEhD+iP1E5UaRYW6hQRYD9T2WNDwFTrOMWzQ== | ||
|
||
"@yornaath/batshit@^0.10.1": | ||
version "0.10.1" | ||
resolved "https://registry.yarnpkg.com/@yornaath/batshit/-/batshit-0.10.1.tgz#f750b2bf6abb9207330eb6a5ab2356af9b79ae78" | ||
integrity sha512-WGZ1WNoiVN6CLf28O73+6SCf+2lUn4U7TLGM9f4zOad0pn9mdoXIq8cwu3Kpf7N2OTYgWGK4eQPTflwFlduDGA== | ||
dependencies: | ||
"@yornaath/batshit-devtools" "^1.7.1" | ||
|
||
"@zxing/[email protected]": | ||
version "0.9.0" | ||
resolved "https://registry.yarnpkg.com/@zxing/text-encoding/-/text-encoding-0.9.0.tgz#fb50ffabc6c7c66a0c96b4c03e3d9be74864b70b" | ||
|