Skip to content
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

🐶 Implement the forum search results popup #1309

Merged
merged 11 commits into from
Sep 2, 2021
Merged

Conversation

thesan
Copy link
Member

@thesan thesan commented Sep 1, 2021

Closes #1081

Comment on lines +42 to +59
const byBestMatch = <T extends Record<any, any>>(search: string, fields: ((x: T) => string)[]) => {
const patterns = [RegExp(`\\b${search}\\b`, 'gi'), RegExp(search, 'gi')]

return (a: T, b: T): number => {
for (const field of fields) {
const fieldA = field(a)
const fieldB = field(b)
if (fieldA === fieldB) continue
for (const pattern of patterns) {
const matchA = fieldA.match(pattern)?.length ?? 0
const matchB = fieldB.match(pattern)?.length ?? 0

if (matchA !== matchB) return matchB - matchA
}
}
return 0
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be done by the query node. I will open an issue for a text search query.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the issue: Joystream/joystream#2608

Copy link
Contributor

@jodator jodator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd feel safer with some tests for the highlight component :)

shorten?: boolean
children: string
}
export const HighlightedText = memo(({ pattern, shorten, children }: HighlightedTextProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of logic in the component. Could you write a test for it?

children: string
}
export const HighlightedText = memo(({ pattern, shorten, children }: HighlightedTextProps) => {
const nodes = (pattern ? [...children.matchAll(pattern)] : []).reduceRight(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read this correctly when there's no pattern we don't do any matching and just use the existing nodes? If so, I'd do that explicitly to not do anything on an empty array.

Another question is for the pattern property – what is its use-case? I couldn't figure it out from the storybook so maybe the tests will help.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the pattern this is the RegExp used to find what to highlight. In this case it would only be a string with no special character but passing the RegExp rather than a string makes this component more reusable by allowing fancier RegExp. Also it allows to create the RegExp once and just pass it down by reference.

Comment on lines +6 to +7
// The goal of this hook is to utilize apollo cache to be faster than `useForumCategoryBreadcrumbs`
// when there are a lot of breadcrumbs to show, like on search results
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you briefly explain why this behaves better with Apollo cache?

Also, can we have just one or does the previous one have other purposes as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous useForumCategoryBreadcrumbs is much better for fetching a single breadcrumbs because it does it in only one query. But because there can many results to render with different breadcrumbs but which are probably sharing some root categories, I thought that for this particular case, it could be better to have slightly more but shorter cacheable queries.

Eg:
There are several results from C1 > C2 > thread1, from C1 > C2 > C3 > thread2, and from C1 > thread3
For their breadcrumbs:

  • C1 > C2 > thread1: queries C2, and gets C1 from the cache
  • C1 > C2 > C3 > thread2: queries C3, and gets C2 and C1 from the cache
  • C1 > thread3: queries C1

WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the idea is pretty neat – thanks for the explanation. If we could discuss that earlier I would opt-in for using the existing hook and adding optimization if or when we'd saw a need for that. Just because it is hard to tell how real-world data will present. For instance, if most of the results are from a single category, then they should be cached already. OTOH it might be a case that most of the results came from single parents, but multiple sub-categories. Then, our solution will be better.

But since you already implemented this let's keep that and see what will happen.

ps.: Another solution could be to add breadcrumbs to the forum thread/category in Hydra itself as it is used in multiple places.

@jodator jodator merged commit 50fad3b into main Sep 2, 2021
@jodator jodator deleted the forum-search-result branch September 2, 2021 17:17
@thesan
Copy link
Member Author

thesan commented Sep 2, 2021

@jodator sorry I should have changed it into a draft, I kept finding some stuff I forgot about. I'll commit those in another PR soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search forum result
2 participants