Sort repos by org and name (#1548)

Ensure that repos are sorted in the UI after doing a search.

See #1466

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
smainz 2023-02-28 11:14:19 +01:00 committed by GitHub
parent 003773540b
commit 156b321c2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,16 @@ import { computed, Ref } from 'vue';
import { Repo } from '~/lib/api/types';
/*
* Compares Repos lexicographically using owner/name .
*/
function repoCompare(a: Repo, b: Repo) {
const x = `${a.owner}/${a.name}`;
const y = `${b.owner}/${b.name}`;
// eslint-disable-next-line no-nested-ternary
return x === y ? 0 : x > y ? 1 : -1;
}
export function useRepoSearch(repos: Ref<Repo[] | undefined>, search: Ref<string>) {
const searchIndex = computed(
() =>
@ -15,10 +25,13 @@ export function useRepoSearch(repos: Ref<Repo[] | undefined>, search: Ref<string
const searchedRepos = computed(() => {
if (search.value === '') {
return repos.value;
return repos.value?.sort(repoCompare);
}
return searchIndex.value.search(search.value).map((result) => result.item);
return searchIndex.value
.search(search.value)
.map((result) => result.item)
.sort(repoCompare);
});
return {