Skip to content

Commit

Permalink
feat: sorted search results
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Nov 1, 2024
1 parent 0035740 commit 8395fd4
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions src/home/sorting/sorting-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,44 @@ export class SortingManager {
try {
const filterText = textBox.value;
const issues = Array.from(issuesContainer.children) as HTMLDivElement[];

// Reset any active sort buttons when searching
if (filterText) {
this._resetSortButtons();
}

// Get all issue IDs
// Get issue IDs and search results
const issueIds = issues
.map((issue) => issue.children[0].getAttribute("data-issue-id"))
.filter((id): id is string => id !== null)
.map((id) => parseInt(id));

// Get visibility results
const searchResults = taskManager.issueSearcher.search(filterText, issueIds);

// Update DOM
// Update visibility and scores
issues.forEach((issue) => {
const issueId = issue.children[0].getAttribute("data-issue-id");
if (!issueId) return;

const result = searchResults.get(parseInt(issueId));
if (!result) return;

issue.classList.add("active");
issue.style.display = result.visible ? "block" : "none";

if (result.score !== undefined) {
issue.setAttribute("data-relevance-score", result.score.toFixed(3));
}
});

// If there's a search term, sort by relevance
if (filterText) {
issues.sort((a, b) => {
const scoreA = parseFloat(a.getAttribute("data-relevance-score") || "0");
const scoreB = parseFloat(b.getAttribute("data-relevance-score") || "0");
return scoreB - scoreA; // Sort in descending order of relevance score
}).forEach((issue) => issuesContainer.appendChild(issue));
}
} catch (error) {
return renderErrorInModal(error as Error);
}
Expand All @@ -96,14 +112,28 @@ export class SortingManager {
const filterText = textBox.value;
// Update the URL with the search parameter
const newURL = new URL(window.location.href);
newURL.searchParams.set("search", filterText);
if (filterText) {
newURL.searchParams.set("search", filterText);
} else {
newURL.searchParams.delete("search");
}
window.history.replaceState({}, "", newURL.toString());
filterIssues(); // Run the filter function immediately on input
filterIssues();
});

return textBox;
}

private _resetSortButtons() {
this._sortingButtons.querySelectorAll('input[type="radio"]').forEach((input) => {
if (input instanceof HTMLInputElement) {
input.checked = false;
input.setAttribute("data-ordering", "");
}
});
this._lastChecked = null;
}

private _generateSortingButtons(sortingOptions: readonly string[]) {
const buttons = document.createElement("div");
buttons.className = "labels";
Expand Down Expand Up @@ -164,6 +194,19 @@ export class SortingManager {
}
});

// Clear search when applying a different sort
this._filterTextBox.value = "";
const newURL = new URL(window.location.href);
newURL.searchParams.delete("search");
window.history.replaceState({}, "", newURL.toString());

// Reset other buttons
input.parentElement?.childNodes.forEach((node) => {
if (node instanceof HTMLInputElement) {
node.setAttribute("data-ordering", "");
}
});

if (newOrdering === "disabled") {
this._lastChecked = null;
input.checked = false;
Expand Down

0 comments on commit 8395fd4

Please sign in to comment.