From 245db574f82f597ffe8293b3e5c79e7773fe284c Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 14 May 2024 19:04:12 +0800 Subject: [PATCH] Add query and preview to ChatStoreSearchResult --- src/search.tsx | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/search.tsx b/src/search.tsx index 80d2904..1494c2b 100644 --- a/src/search.tsx +++ b/src/search.tsx @@ -6,6 +6,8 @@ import { StateUpdater, useRef } from "preact/hooks"; interface ChatStoreSearchResult { key: IDBValidKey; cs: ChatStore; + query: string; + preview: string; } export default function Search(props: { @@ -80,10 +82,18 @@ export default function Search(props: { if (now !== searchingNow) setSearchingNow(now); const value: ChatStore = await db.get("chatgpt-api-web", key); - if (value.contents_for_index.join(" ").includes(query)) { + const content = value.contents_for_index.join(" "); + if (content.includes(query)) { + const beginIndex: number = content.indexOf(query); + const preview = content.slice( + Math.max(0, beginIndex - 100), + Math.min(content.length, beginIndex + 239) + ); result.push({ key, cs: value, + query: query, + preview: preview, }); } } @@ -152,9 +162,7 @@ export default function Search(props: { }} >
{result.key}
-
- {result.cs.contents_for_index.join(" ").slice(0, 390)} -
+
{result.preview}
); })}