feat: sort results by last accessed time within each mode

Tabs sorted by lastAccessed, history by lastVisitTime, closed tabs by
lastModified. Search uses recency as tiebreaker for equal scores.

Assisted-by: Claude Code
Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
This commit is contained in:
2026-04-22 15:03:22 +05:30
parent 669d39e1dd
commit db9d63660c
6 changed files with 22 additions and 20 deletions
-1
View File
@@ -97,7 +97,6 @@
<option value="history">History</option> <option value="history">History</option>
<option value="bookmarks">Bookmarks</option> <option value="bookmarks">Bookmarks</option>
<option value="closed">Recently Closed</option> <option value="closed">Recently Closed</option>
<option value="all">All</option>
</select> </select>
</div> </div>
+13 -15
View File
@@ -1,14 +1,19 @@
import type { Message, SearchRequest, ActionRequest, SearchItem } from "./types"; import type { Message, SearchRequest, ActionRequest, SearchItem } from "./types";
function sortByRecent(items: SearchItem[]): SearchItem[] {
return items.sort((a, b) => (b.lastAccessed ?? 0) - (a.lastAccessed ?? 0));
}
async function getOpenTabs(): Promise<SearchItem[]> { async function getOpenTabs(): Promise<SearchItem[]> {
const tabs = await chrome.tabs.query({}); const tabs = await chrome.tabs.query({});
return tabs.map((tab) => ({ return sortByRecent(tabs.map((tab) => ({
id: `tab-${tab.id}`, id: `tab-${tab.id}`,
title: tab.title ?? "", title: tab.title ?? "",
url: tab.url ?? "", url: tab.url ?? "",
type: "tabs" as const, type: "tabs" as const,
favIconUrl: tab.favIconUrl, favIconUrl: tab.favIconUrl,
})); lastAccessed: tab.lastAccessed,
})));
} }
async function getHistory(query: string): Promise<SearchItem[]> { async function getHistory(query: string): Promise<SearchItem[]> {
@@ -17,12 +22,13 @@ async function getHistory(query: string): Promise<SearchItem[]> {
maxResults: 50, maxResults: 50,
startTime: 0, startTime: 0,
}); });
return results.map((item) => ({ return sortByRecent(results.map((item) => ({
id: `history-${item.id}`, id: `history-${item.id}`,
title: item.title ?? "", title: item.title ?? "",
url: item.url ?? "", url: item.url ?? "",
type: "history" as const, type: "history" as const,
})); lastAccessed: item.lastVisitTime,
})));
} }
async function getBookmarks(query: string): Promise<SearchItem[]> { async function getBookmarks(query: string): Promise<SearchItem[]> {
@@ -39,7 +45,7 @@ async function getBookmarks(query: string): Promise<SearchItem[]> {
async function getRecentlyClosed(): Promise<SearchItem[]> { async function getRecentlyClosed(): Promise<SearchItem[]> {
const sessions = await chrome.sessions.getRecentlyClosed({ maxResults: 25 }); const sessions = await chrome.sessions.getRecentlyClosed({ maxResults: 25 });
return sessions return sortByRecent(sessions
.filter((s) => s.tab) .filter((s) => s.tab)
.map((session) => ({ .map((session) => ({
id: `closed-${session.tab!.sessionId}`, id: `closed-${session.tab!.sessionId}`,
@@ -47,7 +53,8 @@ async function getRecentlyClosed(): Promise<SearchItem[]> {
url: session.tab!.url ?? "", url: session.tab!.url ?? "",
type: "closed" as const, type: "closed" as const,
favIconUrl: session.tab!.favIconUrl, favIconUrl: session.tab!.favIconUrl,
})); lastAccessed: session.lastModified ? session.lastModified * 1000 : undefined,
})));
} }
async function handleSearch(request: SearchRequest): Promise<SearchItem[]> { async function handleSearch(request: SearchRequest): Promise<SearchItem[]> {
@@ -61,15 +68,6 @@ async function handleSearch(request: SearchRequest): Promise<SearchItem[]> {
return getBookmarks(query); return getBookmarks(query);
case "closed": case "closed":
return getRecentlyClosed(); return getRecentlyClosed();
case "all": {
const [tabs, history, bookmarks, closed] = await Promise.all([
getOpenTabs(),
getHistory(query),
getBookmarks(query),
getRecentlyClosed(),
]);
return [...tabs, ...history, ...bookmarks, ...closed];
}
} }
} }
+2
View File
@@ -7,6 +7,7 @@ declare namespace chrome {
url?: string; url?: string;
favIconUrl?: string; favIconUrl?: string;
active?: boolean; active?: boolean;
lastAccessed?: number;
} }
function query(queryInfo: Record<string, unknown>): Promise<Tab[]>; function query(queryInfo: Record<string, unknown>): Promise<Tab[]>;
function get(tabId: number): Promise<Tab>; function get(tabId: number): Promise<Tab>;
@@ -42,6 +43,7 @@ declare namespace chrome {
namespace sessions { namespace sessions {
interface Session { interface Session {
lastModified?: number;
tab?: tabs.Tab & { sessionId?: string }; tab?: tabs.Tab & { sessionId?: string };
window?: { sessionId?: string }; window?: { sessionId?: string };
} }
+1 -2
View File
@@ -1,13 +1,12 @@
import { search } from "./search"; import { search } from "./search";
import type { SearchItem, SearchMode, SearchMethod, SearchResult, Message } from "./types"; import type { SearchItem, SearchMode, SearchMethod, SearchResult, Message } from "./types";
const MODES: SearchMode[] = ["tabs", "history", "bookmarks", "closed", "all"]; const MODES: SearchMode[] = ["tabs", "history", "bookmarks", "closed"];
const MODE_LABELS: Record<SearchMode, string> = { const MODE_LABELS: Record<SearchMode, string> = {
tabs: "Tabs", tabs: "Tabs",
history: "History", history: "History",
bookmarks: "Bookmarks", bookmarks: "Bookmarks",
closed: "Closed", closed: "Closed",
all: "All",
}; };
const METHODS: SearchMethod[] = ["fuzzy", "fulltext", "prefix"]; const METHODS: SearchMethod[] = ["fuzzy", "fulltext", "prefix"];
+4 -1
View File
@@ -138,6 +138,9 @@ export function search(items: SearchItem[], query: string, method: SearchMethod)
} }
} }
results.sort((a, b) => b.score - a.score); results.sort((a, b) => {
if (b.score !== a.score) return b.score - a.score;
return (b.item.lastAccessed ?? 0) - (a.item.lastAccessed ?? 0);
});
return results; return results;
} }
+2 -1
View File
@@ -1,4 +1,4 @@
export type SearchMode = "tabs" | "history" | "bookmarks" | "closed" | "all"; export type SearchMode = "tabs" | "history" | "bookmarks" | "closed";
export type SearchMethod = "fuzzy" | "fulltext" | "prefix"; export type SearchMethod = "fuzzy" | "fulltext" | "prefix";
export interface SearchItem { export interface SearchItem {
@@ -7,6 +7,7 @@ export interface SearchItem {
url: string; url: string;
type: SearchMode; type: SearchMode;
favIconUrl?: string; favIconUrl?: string;
lastAccessed?: number;
} }
export interface SearchResult { export interface SearchResult {