mirror of
https://github.com/avinal/sciezka.git
synced 2026-07-03 23:30:09 +05:30
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:
@@ -97,7 +97,6 @@
|
||||
<option value="history">History</option>
|
||||
<option value="bookmarks">Bookmarks</option>
|
||||
<option value="closed">Recently Closed</option>
|
||||
<option value="all">All</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
|
||||
+13
-15
@@ -1,14 +1,19 @@
|
||||
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[]> {
|
||||
const tabs = await chrome.tabs.query({});
|
||||
return tabs.map((tab) => ({
|
||||
return sortByRecent(tabs.map((tab) => ({
|
||||
id: `tab-${tab.id}`,
|
||||
title: tab.title ?? "",
|
||||
url: tab.url ?? "",
|
||||
type: "tabs" as const,
|
||||
favIconUrl: tab.favIconUrl,
|
||||
}));
|
||||
lastAccessed: tab.lastAccessed,
|
||||
})));
|
||||
}
|
||||
|
||||
async function getHistory(query: string): Promise<SearchItem[]> {
|
||||
@@ -17,12 +22,13 @@ async function getHistory(query: string): Promise<SearchItem[]> {
|
||||
maxResults: 50,
|
||||
startTime: 0,
|
||||
});
|
||||
return results.map((item) => ({
|
||||
return sortByRecent(results.map((item) => ({
|
||||
id: `history-${item.id}`,
|
||||
title: item.title ?? "",
|
||||
url: item.url ?? "",
|
||||
type: "history" as const,
|
||||
}));
|
||||
lastAccessed: item.lastVisitTime,
|
||||
})));
|
||||
}
|
||||
|
||||
async function getBookmarks(query: string): Promise<SearchItem[]> {
|
||||
@@ -39,7 +45,7 @@ async function getBookmarks(query: string): Promise<SearchItem[]> {
|
||||
|
||||
async function getRecentlyClosed(): Promise<SearchItem[]> {
|
||||
const sessions = await chrome.sessions.getRecentlyClosed({ maxResults: 25 });
|
||||
return sessions
|
||||
return sortByRecent(sessions
|
||||
.filter((s) => s.tab)
|
||||
.map((session) => ({
|
||||
id: `closed-${session.tab!.sessionId}`,
|
||||
@@ -47,7 +53,8 @@ async function getRecentlyClosed(): Promise<SearchItem[]> {
|
||||
url: session.tab!.url ?? "",
|
||||
type: "closed" as const,
|
||||
favIconUrl: session.tab!.favIconUrl,
|
||||
}));
|
||||
lastAccessed: session.lastModified ? session.lastModified * 1000 : undefined,
|
||||
})));
|
||||
}
|
||||
|
||||
async function handleSearch(request: SearchRequest): Promise<SearchItem[]> {
|
||||
@@ -61,15 +68,6 @@ async function handleSearch(request: SearchRequest): Promise<SearchItem[]> {
|
||||
return getBookmarks(query);
|
||||
case "closed":
|
||||
return getRecentlyClosed();
|
||||
case "all": {
|
||||
const [tabs, history, bookmarks, closed] = await Promise.all([
|
||||
getOpenTabs(),
|
||||
getHistory(query),
|
||||
getBookmarks(query),
|
||||
getRecentlyClosed(),
|
||||
]);
|
||||
return [...tabs, ...history, ...bookmarks, ...closed];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Vendored
+2
@@ -7,6 +7,7 @@ declare namespace chrome {
|
||||
url?: string;
|
||||
favIconUrl?: string;
|
||||
active?: boolean;
|
||||
lastAccessed?: number;
|
||||
}
|
||||
function query(queryInfo: Record<string, unknown>): Promise<Tab[]>;
|
||||
function get(tabId: number): Promise<Tab>;
|
||||
@@ -42,6 +43,7 @@ declare namespace chrome {
|
||||
|
||||
namespace sessions {
|
||||
interface Session {
|
||||
lastModified?: number;
|
||||
tab?: tabs.Tab & { sessionId?: string };
|
||||
window?: { sessionId?: string };
|
||||
}
|
||||
|
||||
+1
-2
@@ -1,13 +1,12 @@
|
||||
import { search } from "./search";
|
||||
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> = {
|
||||
tabs: "Tabs",
|
||||
history: "History",
|
||||
bookmarks: "Bookmarks",
|
||||
closed: "Closed",
|
||||
all: "All",
|
||||
};
|
||||
const METHODS: SearchMethod[] = ["fuzzy", "fulltext", "prefix"];
|
||||
|
||||
|
||||
+4
-1
@@ -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;
|
||||
}
|
||||
|
||||
+2
-1
@@ -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 interface SearchItem {
|
||||
@@ -7,6 +7,7 @@ export interface SearchItem {
|
||||
url: string;
|
||||
type: SearchMode;
|
||||
favIconUrl?: string;
|
||||
lastAccessed?: number;
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
|
||||
Reference in New Issue
Block a user