diff --git a/django/core/jinja_config.py b/django/core/jinja_config.py index 9c87d20bd..3af072f37 100644 --- a/django/core/jinja_config.py +++ b/django/core/jinja_config.py @@ -131,10 +131,7 @@ def generate_search_form_inputs(query_params): Returns: list: A list of tuples representing the hidden input fields. """ - # set default ordering to relevance, if it is not specified - search_parameters = { - "ordering": "relevance", - } + search_parameters = {} if query_params: # parse_qsl handles splitting and unquoting key-value pairs and generates a list of tuples # do not include the actual query in the query parameters diff --git a/django/library/views.py b/django/library/views.py index 6b84c4477..8d43942f0 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -399,12 +399,13 @@ def filter_queryset(self, request, queryset, view): criteria.update(id__in=codebases.values_list("id", flat=True)) # or we could include the PL in the query # qs += " ".join(programming_languages) + + default_ordering = "-first_published_at" if ordering: - criteria.update(ordering=ordering) + criteria.update(ordering=ordering if qs else default_ordering) else: - if qs: - # set default ordering for search when ordering is not specified - criteria.update(ordering="relevance") + # set default ordering for search when ordering is not specified + criteria.update(ordering="relevance" if qs else default_ordering) return get_search_queryset(qs, queryset, tags=tags, criteria=criteria) diff --git a/frontend/src/apps/codebase_list.ts b/frontend/src/apps/codebase_list.ts index 4d295df91..dda2779d3 100644 --- a/frontend/src/apps/codebase_list.ts +++ b/frontend/src/apps/codebase_list.ts @@ -8,11 +8,19 @@ import { extractDataParams } from "@/util"; const props = extractDataParams("sidebar", ["languageFacets"]); createApp(CodebaseListSidebar, props).mount("#sidebar"); -createApp(SortBy, { - sortOptions: [ - { value: "relevance", label: "Relevance" }, - { value: "-first_published_at", label: "Publish date: newest" }, - { value: "first_published_at", label: "Publish date: oldest" }, - { value: "-last_modified", label: "Recently Modified" }, - ], -}).mount("#sortby"); +// Function to check if 'query' exists and is not empty +function hasQueryParam(param: string) { + const params = new URLSearchParams(window.location.search); + const value = params.get(param); + return value !== null && value.trim() !== ""; +} + +// Conditionally include the 'relevance' sorting option +const sortOptions = [ + ...(hasQueryParam("query") ? [{ value: "relevance", label: "Relevance" }] : []), + { value: "-first_published_at", label: "Publish date: newest" }, + { value: "first_published_at", label: "Publish date: oldest" }, + { value: "-last_modified", label: "Recently Modified" }, +]; + +createApp(SortBy, { sortOptions }).mount("#sortby"); diff --git a/frontend/src/components/CodebaseListSidebar.vue b/frontend/src/components/CodebaseListSidebar.vue index 4e8a82e9f..9551966be 100644 --- a/frontend/src/components/CodebaseListSidebar.vue +++ b/frontend/src/components/CodebaseListSidebar.vue @@ -120,7 +120,7 @@ const initialFilterValues = { onMounted(() => { if (props.languageFacets) { const localLanguageFacets = { ...props.languageFacets }; - console.log(localLanguageFacets); + // console.log(localLanguageFacets); parsedLanguageFacets = Object.entries(localLanguageFacets) .sort(([, valueA], [, valueB]) => valueB - valueA) // Sort by value in descending order .map(([name, value]) => ({ value: name, label: `${name} (${value})` }));