This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathuseTaskSearch.ts
More file actions
98 lines (87 loc) · 3.01 KB
/
Copy pathuseTaskSearch.ts
File metadata and controls
98 lines (87 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { useState, useEffect, useMemo } from "react"
import { Fzf } from "fzf"
import { highlightFzfMatch } from "@/utils/highlight"
import { useExtensionState } from "@/context/ExtensionStateContext"
type SortOption = "newest" | "oldest" | "mostExpensive" | "mostTokens" | "mostRelevant"
export const useTaskSearch = () => {
const { taskHistory, cwd } = useExtensionState()
const [searchQuery, setSearchQuery] = useState("")
const [sortOption, setSortOption] = useState<SortOption>("newest")
const [lastNonRelevantSort, setLastNonRelevantSort] = useState<SortOption | null>("newest")
const [showAllWorkspaces, setShowAllWorkspaces] = useState(false)
const [showBackgroundTasks, setShowBackgroundTasks] = useState(true)
useEffect(() => {
if (searchQuery && sortOption !== "mostRelevant" && !lastNonRelevantSort) {
setLastNonRelevantSort(sortOption)
setSortOption("mostRelevant")
} else if (!searchQuery && sortOption === "mostRelevant" && lastNonRelevantSort) {
setSortOption(lastNonRelevantSort)
setLastNonRelevantSort(null)
}
}, [searchQuery, sortOption, lastNonRelevantSort])
const presentableTasks = useMemo(() => {
let tasks = taskHistory.filter((item) => item.ts && item.task)
if (!showAllWorkspaces) {
tasks = tasks.filter((item) => item.workspace === cwd)
}
if (!showBackgroundTasks) {
tasks = tasks.filter((item) => !item.background)
}
return tasks
}, [taskHistory, showAllWorkspaces, showBackgroundTasks, cwd])
const fzf = useMemo(() => {
return new Fzf(presentableTasks, {
selector: (item) => item.task,
})
}, [presentableTasks])
const tasks = useMemo(() => {
let results = presentableTasks
if (searchQuery) {
const searchResults = fzf.find(searchQuery)
results = searchResults.map((result) => {
const positions = Array.from(result.positions)
const taskEndIndex = result.item.task.length
return {
...result.item,
highlight: highlightFzfMatch(
result.item.task,
positions.filter((p) => p < taskEndIndex),
),
workspace: result.item.workspace,
}
})
}
// Then sort the results
return [...results].sort((a, b) => {
switch (sortOption) {
case "oldest":
return (a.ts || 0) - (b.ts || 0)
case "mostExpensive":
return (b.totalCost || 0) - (a.totalCost || 0)
case "mostTokens":
const aTokens = (a.tokensIn || 0) + (a.tokensOut || 0) + (a.cacheWrites || 0) + (a.cacheReads || 0)
const bTokens = (b.tokensIn || 0) + (b.tokensOut || 0) + (b.cacheWrites || 0) + (b.cacheReads || 0)
return bTokens - aTokens
case "mostRelevant":
// Keep fuse order if searching, otherwise sort by newest
return searchQuery ? 0 : (b.ts || 0) - (a.ts || 0)
case "newest":
default:
return (b.ts || 0) - (a.ts || 0)
}
})
}, [presentableTasks, searchQuery, fzf, sortOption])
return {
tasks,
searchQuery,
setSearchQuery,
sortOption,
setSortOption,
lastNonRelevantSort,
setLastNonRelevantSort,
showAllWorkspaces,
setShowAllWorkspaces,
showBackgroundTasks,
setShowBackgroundTasks,
}
}