From 1b11d5723d474fb86cb05a1876a97dab6b4ea063 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 27 Jun 2023 15:50:41 -0700 Subject: [PATCH] Extract search request URL builder into js function in web interface --- src/khoj/interface/web/index.html | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/khoj/interface/web/index.html b/src/khoj/interface/web/index.html index 78b24d56..906f3912 100644 --- a/src/khoj/interface/web/index.html +++ b/src/khoj/interface/web/index.html @@ -93,15 +93,8 @@ if (rerank) setQueryFieldInUrl(query); - // Generate Backend API URL to execute Search - if (type == 'all') - url = `/api/search?q=${encodeURIComponent(query)}&n=${results_count}&client=web`; - else if (type === "image") - url = `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&client=web`; - else - url = `/api/search?q=${encodeURIComponent(query)}&t=${type}&n=${results_count}&r=${rerank}&client=web`; - // Execute Search and Render Results + url = createRequestUrl(query, type, results_count, rerank); fetch(url) .then(response => response.json()) .then(data => { @@ -157,6 +150,18 @@ }); } + function createRequestUrl(query, results_count, type, rerank) { + // Generate Backend API URL to execute Search + let url = `/api/search?q=${encodeURIComponent(query)}&n=${results_count}&client=web`; + // If type is not 'all', append type to URL + if (type !== 'all') + url += `&t=${type}`; + // Rerank is only supported by text types + if (type !== "image") + url += `&r=${rerank}`; + return url; + } + function setTypeFieldInUrl(type) { var url = new URL(window.location.href); url.searchParams.set("t", type.value);