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

[CT-3469] Refactor: add autocomplete to navbar, fix redirect #124

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ben-brooker
Copy link
Contributor

@ben-brooker ben-brooker commented Jul 11, 2022

Ticket

Making function suggestions redirect straight to topic page instead of global search results page.

Was going to do the same for packages suggestions, however there are some broken links so a risk that the suggestion will link to a "oops, page not found".

Changes

  • OnSearch function ensures that the searchbar clears and the autocomplete disappears once a search has been made. (This is has more of a ui effect on the navbar search as the autocomplete would cover search results otherwise).

  • I have also wrapped autosuggestion text to ensure there is no overflow.

  • I have changed the function redirect to point to specific topic pages and placed in a switch statement.

Pictures

Before (Desktop):
Screenshot 2022-07-12 at 16 02 31

After (Desktop):
Screenshot 2022-07-12 at 16 06 11

Before (Mobile):
Screenshot 2022-07-12 at 16 03 38

After (Mobile):
Screenshot 2022-07-12 at 16 03 26

Comment on lines 16 to 20
if (type==="topic") {
router.push(`/packages/${encodeURIComponent(query?.fields?.package_name)}/versions/${encodeURIComponent(query?.fields?.version)}/topics/${encodeURIComponent(query?.fields?.name)}`);
} else {
router.push(`/search?q=${encodeURIComponent(query)}`);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could also rewrite this as a switch case to handle all types 👍
Another thing for safety, you can push to the exact topics route if the params you use all exist, otherwise its a broken link so better to just send people to /search?q=${encodeURIComponent(query)}

@ben-brooker ben-brooker changed the title fix: fix function redirect [CT-3469] Refactor: add autocomplete to navbar, fix redirect Jul 12, 2022
Comment on lines +21 to +30
case "package":
router.push(`/search?q=${encodeURIComponent(query)}`);
onSearch();
break
case "search":
router.push(`/search?q=${encodeURIComponent(query)}`);
onSearch();
break
default:
break
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can just do:

case "package":
case "search":
          router.push(`/search?q=${encodeURIComponent(query)}`);
          onSearch();
          break;
default:
          break;

Copy link
Collaborator

@ztsorojev ztsorojev 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! Well done on the changes brother ✊
There is one thing I realize the more I play around with the autocomplete, it's too weird that you can get suggestions that are completely unrelated to your search query. I think we should only suggest options that start with the same characters as what the user inputs. Then you just select the top 5 options that have the highest score (no need to set a threshold otherwise you will get the issue we had earlier where some inputs didn't have 5 options, just pick the top 5)

image

@ztsorojev
Copy link
Collaborator

Here I think we should remove this, and just pick the 5 items with the highest score that start with what the user entered:

const relevantPackages = packages?.filter((p)=>(p?.score>1));
const relevantTopics = topics?.filter((p)=>(p?.score>1));

So something similar to this:

     const relevantPackages = packages?.sort((pA, pB) => pB.score-pA.score)
                             ?.filter(p => p.toLowerCase().startsWith(query.toLowerCase()))
                             ?.slice(0, 5);

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.

None yet

2 participants