Prevent client error with > 10 tags on contract

This commit is contained in:
James Grugett 2022-02-22 22:21:00 -06:00
parent cc375256a1
commit 702755c797

View File

@ -54,13 +54,12 @@ export async function getFoldBySlug(slug: string) {
} }
function contractsByTagsQuery(tags: string[]) { function contractsByTagsQuery(tags: string[]) {
// TODO: if tags.length > 10, execute multiple parallel queries
const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
return query( return query(
contractCollection, contractCollection,
where( where('lowercaseTags', 'array-contains-any', lowercaseTags)
'lowercaseTags',
'array-contains-any',
tags.map((tag) => tag.toLowerCase())
)
) )
} }
@ -74,7 +73,6 @@ export async function getFoldContracts(fold: Fold) {
} = fold } = fold
const [tagsContracts, includedContracts] = await Promise.all([ const [tagsContracts, includedContracts] = await Promise.all([
// TODO: if tags.length > 10, execute multiple parallel queries
tags.length > 0 ? getValues<Contract>(contractsByTagsQuery(tags)) : [], tags.length > 0 ? getValues<Contract>(contractsByTagsQuery(tags)) : [],
// TODO: if contractIds.length > 10, execute multiple parallel queries // TODO: if contractIds.length > 10, execute multiple parallel queries
@ -163,9 +161,10 @@ export function listenForFollow(
export async function getFoldsByTags(tags: string[]) { export async function getFoldsByTags(tags: string[]) {
if (tags.length === 0) return [] if (tags.length === 0) return []
const lowercaseTags = tags.map((tag) => tag.toLowerCase()) // TODO: split into multiple queries if tags.length > 10.
const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
const folds = await getValues<Fold>( const folds = await getValues<Fold>(
// TODO: split into multiple queries if tags.length > 10.
query( query(
foldCollection, foldCollection,
where('lowercaseTags', 'array-contains-any', lowercaseTags) where('lowercaseTags', 'array-contains-any', lowercaseTags)
@ -179,13 +178,13 @@ export function listenForFoldsWithTags(
tags: string[], tags: string[],
setFolds: (folds: Fold[]) => void setFolds: (folds: Fold[]) => void
) { ) {
const lowercaseTags = tags.map((tag) => tag.toLowerCase()) // TODO: split into multiple queries if tags.length > 10.
const q = const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10)
// TODO: split into multiple queries if tags.length > 10.
query( const q = query(
foldCollection, foldCollection,
where('lowercaseTags', 'array-contains-any', lowercaseTags) where('lowercaseTags', 'array-contains-any', lowercaseTags)
) )
return listenForValues<Fold>(q, (folds) => { return listenForValues<Fold>(q, (folds) => {
const sorted = _.sortBy(folds, (fold) => -1 * fold.followCount) const sorted = _.sortBy(folds, (fold) => -1 * fold.followCount)