Skip to content

Commit cf1d4e2

Browse files
author
Eric Wheeler
committed
ui: prevent search responses from updating unrelated components
Add request ID tracking to useTaskSearch hook to ensure each component only processes responses to its own search requests. This prevents the issue where multiple components using the hook would all receive updates when a search response comes back, regardless of which component initiated the search. - Add global serial counter to generate unique request IDs - Add component-isolated ref to track current request ID - Modify message handler to only process matching responses - Pass request ID back in webviewMessageHandler response Signed-off-by: Eric Wheeler <[email protected]>
1 parent a5b144a commit cf1d4e2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,11 @@ export const webviewMessageHandler = async (
451451
break
452452
case "getHistoryItems":
453453
const historyResults = await getHistoryItemsForSearch(message.historySearchOptions || {})
454-
provider.postMessageToWebview({ type: "historyItems", items: historyResults.items })
454+
provider.postMessageToWebview({
455+
type: "historyItems",
456+
items: historyResults.items,
457+
requestId: message.requestId, // Pass the requestId back in the response
458+
})
455459
break
456460
case "deleteMultipleTasksWithIds": {
457461
const ids = message.ids

webview-ui/src/components/history/useTaskSearch.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { vscode } from "@src/utils/vscode"
44
import { useExtensionState } from "@/context/ExtensionStateContext"
55
import { highlightFzfMatch } from "@/utils/highlight"
66

7+
// Static counter for generating unique request IDs
8+
let nextRequestId = 1
9+
710
export const useTaskSearch = (options: HistorySearchOptions = {}) => {
811
const { cwd } = useExtensionState()
912
const [tasks, setTasks] = useState<(HistoryItem & { highlight?: string })[]>([])
@@ -15,6 +18,7 @@ export const useTaskSearch = (options: HistorySearchOptions = {}) => {
1518
const [sortOption, setSortOption] = useState<HistorySortOption>(options.sortOption || "newest")
1619
const [lastNonRelevantSort, setLastNonRelevantSort] = useState<HistorySortOption | null>("newest")
1720
const [showAllWorkspaces, setShowAllWorkspaces] = useState(options.showAllWorkspaces || false)
21+
const currentRequestId = useRef<string>("")
1822

1923
// Debounced search query setter
2024
const debouncedSetSearchQuery = useCallback((query: string) => {
@@ -51,7 +55,7 @@ export const useTaskSearch = (options: HistorySearchOptions = {}) => {
5155

5256
const handler = (event: MessageEvent) => {
5357
const message = event.data
54-
if (message.type === "historyItems") {
58+
if (message.type === "historyItems" && message.requestId === currentRequestId.current) {
5559
// Process the items to add highlight HTML based on match positions
5660
const processedItems = (message.items || []).map((item: HistorySearchResultItem) => {
5761
if (item.match?.positions) {
@@ -78,6 +82,10 @@ export const useTaskSearch = (options: HistorySearchOptions = {}) => {
7882
console.log("Task deletion confirmed, refreshing list...")
7983

8084
// Refresh the task list without showing loading state
85+
// Generate a new request ID for this search
86+
const refreshRequestId = `search_${nextRequestId++}`
87+
currentRequestId.current = refreshRequestId
88+
8189
vscode.postMessage({
8290
type: "getHistoryItems",
8391
historySearchOptions: {
@@ -86,6 +94,7 @@ export const useTaskSearch = (options: HistorySearchOptions = {}) => {
8694
workspacePath: showAllWorkspaces ? undefined : cwd,
8795
limit: options.limit,
8896
},
97+
requestId: refreshRequestId,
8998
})
9099
}
91100
}
@@ -101,9 +110,14 @@ export const useTaskSearch = (options: HistorySearchOptions = {}) => {
101110
limit: options.limit,
102111
}
103112

113+
// Generate a new request ID for this search
114+
const requestId = `search_${nextRequestId++}`
115+
currentRequestId.current = requestId
116+
104117
vscode.postMessage({
105118
type: "getHistoryItems",
106119
historySearchOptions: searchOptions,
120+
requestId,
107121
})
108122

109123
return () => {

0 commit comments

Comments
 (0)