Skip to content

Fix and improve split-issue-pr-search-results#3837

Merged
fregante merged 19 commits intorefined-github:masterfrom
dertieran:fix-split-issue-pr-search-results
Jan 17, 2021
Merged

Fix and improve split-issue-pr-search-results#3837
fregante merged 19 commits intorefined-github:masterfrom
dertieran:fix-split-issue-pr-search-results

Conversation

@dertieran
Copy link
Contributor

@dertieran dertieran commented Dec 22, 2020

Fixes #3824

Steps to test as mentioned in the issue:

  1. Visit https://github.com/search?q=refined+github&type=Issues
  2. Click Pull requests (and wait for load)
  3. Click Issues

The problem was that createUrl always appended the next is:<type> query part.
This is also used by Github for the filter and every previous is:<type> is ignored.
But the detection did just check for the first occurence of is:<type> so the ui wasn't updated correctly.

I changed the code to first clear all is:<type> and then add the new filter.
I also improved the detection if multiple is:<types> are in the search.
As a bonus I also added that the language and state search parameters are also copied for each url

@fregante
Copy link
Member

Good catch. The code is a little verbose but I haven’t looked exactly at how it can be improved

@fregante fregante marked this pull request as draft December 27, 2020 08:47
@dertieran dertieran marked this pull request as ready for review January 7, 2021 11:56
Copy link
Member

@fregante fregante left a comment

Choose a reason for hiding this comment

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

Untested but it looks right!

Can you add some tests to ensure that the query is cleaned correctly? Also add some tricky queries to ensure that they're not broken by mistake.

@dertieran
Copy link
Contributor Author

dertieran commented Jan 8, 2021

@fregante Sure, just not sure what a "tricky" query would be 😅
Right now I added a test that uses the query from the issue ?q=refined+github and the .add('is:issue').
I guess that's fairly simple, would tricky be something with multiple is:pr/issue?

Also I noticed that before the passed URLSearchParams where just assigned, I changed it that it now creates a new copy, should I change that back?


Edit: Just testing something for another PR

// https://github.com/fregante

@fregante
Copy link
Member

fregante commented Jan 8, 2021

I just realized that the regex is acting on the raw string like it did at the beginning of the PR. I’d rather use getQueryParts as previously suggested

SearchQuery was added specifically to avoid having to reparse the query (…)

This way you don’t also have drop duplicate spaces because it already does that.

@dertieran
Copy link
Contributor Author

dertieran commented Jan 8, 2021

I just realized that the regex is acting on the raw string like it did at the beginning of the PR

So you mean something like this?

set(query) {
	const parts = splitQueryParts(query);
	this.searchParams.set('q', deduplicate(parts).join(' '));
}

@dertieran
Copy link
Contributor Author

dertieran commented Jan 8, 2021

Okay this now are changes then I initially intended 😅
But I now also removed to TODO for supporting spaces in quoted query strings.

(Not sure if this is the cleanest code for deduplicating the query parts.)


test('.getQueryParts', t => {
const query = new SearchQuery(new URLSearchParams('?q=cool+is%3Aissue'));
const query = new SearchQuery('?q=cool+is%3Aissue');
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather see a more readable

Suggested change
const query = new SearchQuery('?q=cool+is%3Aissue');
const query = new SearchQuery(['q', 'cool is:issue']);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, so just accept all URLSearchParams options?

URLSearchParams(init?: string | string[][] | Record<string, string> | URLSearchParams)

Would be this then

new SearchQuery([['q', 'cool is:issue']]);

But it would also work to just pass the not escaped string if it is just for readability in the tests

new SearchQuery('q=cool is:issue');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just changed SearchQuery to accept all URLSearchParams options.
And then used the {q: 'query'} parameter (thought it was more readable than the entries option).

Hope that is okay otherwise I can also change it back or to something else.

Comment on lines 12 to 24
let indexToKeep = -1;
for (const value of values) {
const lastIndex = array.lastIndexOf(value);
if (lastIndex > indexToKeep) {
indexToKeep = lastIndex;
}
}

if (indexToKeep === -1) {
return array;
}

return array.filter((value, index) => index >= indexToKeep || !values.includes(value));
Copy link
Member

@fregante fregante Jan 12, 2021

Choose a reason for hiding this comment

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

This uses only a loop:

Suggested change
let indexToKeep = -1;
for (const value of values) {
const lastIndex = array.lastIndexOf(value);
if (lastIndex > indexToKeep) {
indexToKeep = lastIndex;
}
}
if (indexToKeep === -1) {
return array;
}
return array.filter((value, index) => index >= indexToKeep || !values.includes(value));
let wasKeywordFound = false;
const deduplicated = []
for (const current of array.reverse()) {
const isKeyword = keywords.includes(current);
if (!isKeyword || !wasKeywordFound) {
deduplicated.unshift(current);
wasKeywordFound = wasKeywordFound || isKeyword;
}
}
return deduplicated;

Copy link
Contributor Author

@dertieran dertieran Jan 12, 2021

Choose a reason for hiding this comment

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

I think this might be a bit "slower" in some edgcases (e.g. if the keywords are not in the array at all) but it's more readable 👍

@fregante fregante merged commit 413a461 into refined-github:master Jan 17, 2021
@fregante
Copy link
Member

Thanks for fixing this!

@dertieran dertieran deleted the fix-split-issue-pr-search-results branch January 17, 2021 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

The UI doesn't update when only showing issues (split-issue-pr-search-results)

3 participants