-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
channel search: instead of persisting search results, enhance them wi…
…th the author for display
- Loading branch information
1 parent
fefee41
commit 1ce713a
Showing
10 changed files
with
119 additions
and
15 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
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 @@ | ||
export * from './useChannelSearch'; |
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,40 @@ | ||
import _ from 'lodash'; | ||
import { useEffect } from 'react'; | ||
|
||
import * as api from '../api'; | ||
import { useAttachAuthorToPostInserts } from './util/useAttachAuthorToPostInserts'; | ||
|
||
const MIN_RESULT_LOAD_THRESHOLD = 20; | ||
|
||
export function useChannelSearch(channelId: string, query: string) { | ||
const { | ||
results, | ||
searchedThroughDate, | ||
isLoading: apiLoading, | ||
isError: apiError, | ||
hasNextPage, | ||
fetchNextPage, | ||
} = api.useInfiniteChannelSearch(channelId, query); | ||
|
||
const posts = useAttachAuthorToPostInserts(results); | ||
|
||
// Makes sure we load enough results to fill the screen before relying on infinite scroll | ||
useEffect(() => { | ||
if ( | ||
results.length < MIN_RESULT_LOAD_THRESHOLD && | ||
hasNextPage && | ||
!apiLoading | ||
) { | ||
fetchNextPage(); | ||
} | ||
}, [results, hasNextPage, apiLoading, fetchNextPage]); | ||
|
||
return { | ||
posts, | ||
searchedThroughDate, | ||
loading: apiLoading, | ||
errored: apiError, | ||
hasMore: hasNextPage, | ||
loadMore: fetchNextPage, | ||
}; | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/shared/src/hooks/util/useAttachAuthorToPostInserts.ts
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,66 @@ | ||
import _ from 'lodash'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
|
||
import * as db from '../../db'; | ||
|
||
export function useAttachAuthorToPostInserts(posts: db.PostInsert[]) { | ||
const [postsWithAuthor, setPostsWithAuthor] = useState< | ||
db.PostInsertWithAuthor[] | ||
>([]); | ||
const [authorsCache, setAuthorsCache] = useState<Record<string, db.Contact>>( | ||
{} | ||
); | ||
const missingAuthors = useMemo( | ||
() => | ||
_.uniq(posts.map((post) => post.authorId)).filter( | ||
(author) => !authorsCache[author] | ||
), | ||
[posts, authorsCache] | ||
); | ||
const appendedPostIds = useMemo( | ||
() => postsWithAuthor.map((post) => post.id), | ||
[postsWithAuthor] | ||
); | ||
|
||
const addAuthorToCache = (author: db.Contact) => { | ||
if (!authorsCache[author.id]) { | ||
setAuthorsCache((prev) => ({ ...prev, [author.id]: author })); | ||
} | ||
}; | ||
|
||
const getMissingAuthors = async (missingAuthors: string[]) => { | ||
const contactPromises = missingAuthors.map((authorId) => | ||
db.getContact({ id: authorId }) | ||
); | ||
const newContacts = await Promise.all(contactPromises); | ||
newContacts.forEach((newContact) => | ||
newContact ? addAuthorToCache(newContact) : null | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
if (missingAuthors.length > 0) { | ||
getMissingAuthors(missingAuthors); | ||
} | ||
}, [posts, missingAuthors]); | ||
|
||
useEffect(() => { | ||
const maybeReset = posts.length === 0 && postsWithAuthor.length > 0; | ||
const incomplete = postsWithAuthor.length < posts.length; | ||
const shouldUpdate = | ||
posts.filter( | ||
(post) => | ||
authorsCache[post.authorId] && !appendedPostIds.includes(post.id) | ||
).length > 0; | ||
|
||
if ((incomplete && shouldUpdate) || maybeReset) { | ||
setPostsWithAuthor( | ||
posts | ||
.filter((post) => authorsCache[post.authorId]) | ||
.map((post) => ({ ...post, author: authorsCache[post.authorId] })) | ||
); | ||
} | ||
}, [posts, authorsCache, postsWithAuthor, missingAuthors, appendedPostIds]); | ||
|
||
return postsWithAuthor; | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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