From 702755c79796ce5c612620f3e320d9589cb93e0e Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 22 Feb 2022 22:21:00 -0600 Subject: [PATCH] Prevent client error with > 10 tags on contract --- web/lib/firebase/folds.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/web/lib/firebase/folds.ts b/web/lib/firebase/folds.ts index 7ca4e912..ffe2bdef 100644 --- a/web/lib/firebase/folds.ts +++ b/web/lib/firebase/folds.ts @@ -54,13 +54,12 @@ export async function getFoldBySlug(slug: 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( contractCollection, - where( - 'lowercaseTags', - 'array-contains-any', - tags.map((tag) => tag.toLowerCase()) - ) + where('lowercaseTags', 'array-contains-any', lowercaseTags) ) } @@ -74,7 +73,6 @@ export async function getFoldContracts(fold: Fold) { } = fold const [tagsContracts, includedContracts] = await Promise.all([ - // TODO: if tags.length > 10, execute multiple parallel queries tags.length > 0 ? getValues(contractsByTagsQuery(tags)) : [], // TODO: if contractIds.length > 10, execute multiple parallel queries @@ -163,9 +161,10 @@ export function listenForFollow( export async function getFoldsByTags(tags: string[]) { 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( - // TODO: split into multiple queries if tags.length > 10. query( foldCollection, where('lowercaseTags', 'array-contains-any', lowercaseTags) @@ -179,13 +178,13 @@ export function listenForFoldsWithTags( tags: string[], setFolds: (folds: Fold[]) => void ) { - const lowercaseTags = tags.map((tag) => tag.toLowerCase()) - const q = - // TODO: split into multiple queries if tags.length > 10. - query( - foldCollection, - where('lowercaseTags', 'array-contains-any', lowercaseTags) - ) + // TODO: split into multiple queries if tags.length > 10. + const lowercaseTags = tags.map((tag) => tag.toLowerCase()).slice(0, 10) + + const q = query( + foldCollection, + where('lowercaseTags', 'array-contains-any', lowercaseTags) + ) return listenForValues(q, (folds) => { const sorted = _.sortBy(folds, (fold) => -1 * fold.followCount)