Skip to content

Conversation

@askvortsov1
Copy link
Member

**Fixes #2399 **

Changes proposed in this pull request:

  • Convert KeyboardNavigatable to TypeScript, as that is used internally here.
  • Convert search sources to TypeScript
  • Convert Search component to TypeScript
  • Convert Search States to Typescript
  • Add getInitialSearch to SearchState
  • Fix search disappearing on page reload / direct link

Reviewers should focus on:

  • Should the SearchSource interface go in the Search file, or be kept where it is?
  • Search Sources aren't components, should we move them somewhere else?

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).

@askvortsov1 askvortsov1 force-pushed the as/search-frontend-cleanup branch from 20c5f25 to 8d05a30 Compare May 10, 2021 06:20
@SychO9
Copy link
Member

SychO9 commented May 10, 2021

There seems to be some conflicts with forum/compat.js

Should the SearchSource interface go in the Search file, or be kept where it is?

I think we usually define them in the same file ? that's my personal preference at least, and if it's what we usually do, then it makes even more sense.

Search Sources aren't components, should we move them somewhere else?

Yea a search folder perhaps ? or sources if that's not too vague.

@askvortsov1
Copy link
Member Author

There seems to be some conflicts with forum/compat.js

Should the SearchSource interface go in the Search file, or be kept where it is?

I think we usually define them in the same file ? that's my personal preference at least, and if it's what we usually do, then it makes even more sense.

Search Sources aren't components, should we move them somewhere else?

Yea a search folder perhaps ? or sources if that's not too vague.

Moved it to Search, works fine (I was a bit worried about a circular import). I think we should reorganize all these files for v2, but not now (editor stuff could be its own folders too, for instance).

Comment on lines +14 to +26
search(query: string) {
query = query.toLowerCase();

this.results.set(query, []);

const params = {
filter: { q: query },
page: { limit: 3 },
include: 'mostRelevantPost',
};

return app.store.find('discussions', params).then((results) => this.results.set(query, results));
}
Copy link
Member

Choose a reason for hiding this comment

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

Could this be better off as an async function? It would reduce the callbacks and would automate the promise return.

Copy link
Member Author

Choose a reason for hiding this comment

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

That wouldn't be any changes other than just the modifier, right?

Copy link
Member

Choose a reason for hiding this comment

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

It would mean using await instead of .then.

E.g.

fetch("example.com").then(response => doSomething(response)

Would become

const response = await fetch("example.com")
doSomething(response)

Copy link
Member Author

Choose a reason for hiding this comment

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

Well yes, but we wouldn't actually want it to be blocking. So we'd still be using then.

return app.store.find('discussions', params).then((results) => this.results.set(query, results));
}

view(query: string): Array<Mithril.Vnode> {
Copy link
Member

@davwheat davwheat May 10, 2021

Choose a reason for hiding this comment

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

Maybe Mithril.Children could be a cleaner type as it's used within Mithril itself.

Thinking again, it's probably best left as it is.

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 one needs to be an array

view(query: string): Array<Mithril.Vnode> {
query = query.toLowerCase();

const results = (this.results.get(query) || []).map((discussion: unknown) => {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of or-ing against an empty array, could we do ?.map?

Copy link
Member Author

Choose a reason for hiding this comment

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

We need it to be a list even if it's undefined.

return [
<li className="Dropdown-header">{app.translator.trans('core.forum.search.discussions_heading')}</li>,
<li>
{LinkButton.component(
Copy link
Member

Choose a reason for hiding this comment

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

Would this be clearer as JSX?

Comment on lines 44 to +46
const name = username(user);

const children = [highlight(name.text, query)];
const children = [highlight(name.text as string, query)];
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to put the as string at declaration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we do that to an attribute?

Copy link
Member

@davwheat davwheat May 11, 2021

Choose a reason for hiding this comment

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

Sorry, I totally missed that it was an attribute. Carry on!

@davwheat
Copy link
Member

Sorry, this was a bit long 🙈

@askvortsov1 askvortsov1 force-pushed the as/search-frontend-cleanup branch from dba3669 to 9d2e599 Compare May 10, 2021 23:24
askvortsov1 and others added 6 commits May 10, 2021 19:27
Co-authored-by: David Wheatley <hi@davwheat.dev>
Co-authored-by: David Wheatley <hi@davwheat.dev>
Co-authored-by: David Wheatley <hi@davwheat.dev>
@askvortsov1 askvortsov1 requested a review from davwheat May 10, 2021 23:42
Copy link
Member

@davwheat davwheat left a comment

Choose a reason for hiding this comment

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

Looks good to me! 👍

@askvortsov1 askvortsov1 merged commit b2d053f into master May 11, 2021
@askvortsov1 askvortsov1 deleted the as/search-frontend-cleanup branch May 11, 2021 02:30
@iPurpl3x
Copy link
Contributor

Thank you @askvortsov1 for these fixes and cleanup, especially "Fix search disappearing on page reload / direct link". I had developed a dirty workaround but your implementation is very nice and clean.

I only saw this now, I think it was not written anywhere in the release notes, or was it?...

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 component accesses getInitialSearch which doesn't exist on SearchState

5 participants