| 1 | import { SessionMetadata } from "core"; |
| 2 | import MiniSearch from "minisearch"; |
| 3 | import React, { |
| 4 | Fragment, |
| 5 | useContext, |
| 6 | useEffect, |
| 7 | useMemo, |
| 8 | useRef, |
| 9 | useState, |
| 10 | } from "react"; |
| 11 | import Shortcut from "../gui/Shortcut"; |
| 12 | |
| 13 | import { XMarkIcon } from "@heroicons/react/24/solid"; |
| 14 | import { useNavigate } from "react-router-dom"; |
| 15 | import { IdeMessengerContext } from "../../context/IdeMessenger"; |
| 16 | import { useAppDispatch, useAppSelector } from "../../redux/hooks"; |
| 17 | import { |
| 18 | newSession, |
| 19 | setAllSessionMetadata, |
| 20 | } from "../../redux/slices/sessionSlice"; |
| 21 | import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; |
| 22 | import { refreshSessionMetadata } from "../../redux/thunks/session"; |
| 23 | import { getFontSize, getPlatform } from "../../util"; |
| 24 | import { ROUTES } from "../../util/navigation"; |
| 25 | import ConfirmationDialog from "../dialogs/ConfirmationDialog"; |
| 26 | import { HistoryTableRow } from "./HistoryTableRow"; |
| 27 | |
| 28 | const parseDate = (date: string): Date => { |
| 29 | let dateObj = new Date(date); |
| 30 | if (isNaN(dateObj.getTime())) { |
| 31 | dateObj = new Date(parseInt(date)); |
| 32 | } |
| 33 | return dateObj; |
| 34 | }; |
| 35 | |
| 36 | const HEADER_CLASS = |
| 37 | "flex user-select-none pt-2 pb-3 opacity-75 text-center font-bold items-center justify-center sticky h-6"; |
| 38 | |
| 39 | export function History() { |
| 40 | const dispatch = useAppDispatch(); |
| 41 | const navigate = useNavigate(); |
| 42 | const searchInputRef = React.useRef<HTMLInputElement>(null); |
| 43 | const ideMessenger = useContext(IdeMessengerContext); |
| 44 | |
| 45 | const [searchTerm, setSearchTerm] = useState(""); |
| 46 | |
| 47 | const minisearch = useRef<MiniSearch>( |
| 48 | new MiniSearch({ |
| 49 | fields: ["title"], |
| 50 | storeFields: ["title", "sessionId", "id"], |
| 51 | }), |
| 52 | ).current; |
| 53 | |
| 54 | const allSessionMetadata = useAppSelector( |
| 55 | (state) => state.session.allSessionMetadata, |
| 56 | ); |
| 57 | const isSessionMetadataLoading = useAppSelector( |
| 58 | (state) => state.session.isSessionMetadataLoading, |
| 59 | ); |
| 60 | |
| 61 | useEffect(() => { |
| 62 | try { |
| 63 | minisearch.removeAll(); |
| 64 | minisearch.addAll( |
| 65 | allSessionMetadata.map((session) => ({ |
| 66 | title: session.title, |
| 67 | sessionId: session.sessionId, |
| 68 | id: session.sessionId, |
| 69 | })), |
| 70 | ); |
| 71 | } catch (e) { |
| 72 | console.log("error adding sessions to minisearch", e); |
| 73 | } |
| 74 | }, [allSessionMetadata]); |
| 75 | |
| 76 | const platform = useMemo(() => getPlatform(), []); |
| 77 | |
| 78 | const filteredAndSortedSessions: SessionMetadata[] = useMemo(() => { |
| 79 | // 1. Exact phrase matching |
| 80 | const exactResults = minisearch.search(searchTerm, { |
| 81 | fuzzy: false, |
| 82 | }); |
| 83 | |
| 84 | // 2. Fuzzy matching with higher tolerance |
| 85 | const fuzzyResults = minisearch.search(searchTerm, { |
| 86 | fuzzy: 0.3, |
| 87 | }); |
| 88 | |
| 89 | // 3. Prefix matching for partial words |
| 90 | const prefixResults = minisearch.search(searchTerm, { |
| 91 | prefix: true, |
| 92 | fuzzy: 0.2, |
| 93 | }); |
| 94 | |
| 95 | // Combine results, with exact matches having higher priority |
| 96 | const allResults = [ |
| 97 | ...exactResults.map((r) => ({ ...r, priority: 3 })), |
| 98 | ...fuzzyResults.map((r) => ({ ...r, priority: 2 })), |
| 99 | ...prefixResults.map((r) => ({ ...r, priority: 1 })), |
| 100 | ]; |
| 101 | |
| 102 | // Remove duplicates while preserving highest priority |
| 103 | const uniqueResultsMap = new Map<string, any>(); |
| 104 | allResults.forEach((result) => { |
| 105 | const existing = uniqueResultsMap.get(result.id); |
| 106 | if (!existing || existing.priority < result.priority) { |
| 107 | uniqueResultsMap.set(result.id, result); |
| 108 | } |
| 109 | }); |
| 110 | const uniqueResults = Array.from(uniqueResultsMap.values()); |
| 111 | |
| 112 | const sessionIds = uniqueResults |
| 113 | .sort((a, b) => b.priority - a.priority || b.score - a.score) |
| 114 | .map((result) => result.id); |
| 115 | |
| 116 | return allSessionMetadata |
| 117 | .filter((session) => { |
| 118 | return searchTerm === "" || sessionIds.includes(session.sessionId); |
| 119 | }) |
| 120 | .sort( |
| 121 | (a, b) => |
| 122 | parseDate(b.dateCreated).getTime() - |
| 123 | parseDate(a.dateCreated).getTime(), |
| 124 | ); |
| 125 | }, [allSessionMetadata, searchTerm, minisearch]); |
| 126 | |
| 127 | const yesterday = new Date(Date.now() - 1000 * 60 * 60 * 24); |
| 128 | const lastWeek = new Date(Date.now() - 1000 * 60 * 60 * 24 * 7); |
| 129 | const lastMonth = new Date(Date.now() - 1000 * 60 * 60 * 24 * 30); |
| 130 | const earlier = new Date(0); |
| 131 | |
| 132 | const showClearSessionsDialog = () => { |
| 133 | dispatch( |
| 134 | setDialogMessage( |
| 135 | <ConfirmationDialog |
| 136 | title={`Clear sessions`} |
| 137 | text={`Are you sure you want to permanently delete all chat sessions, including the current chat session?`} |
| 138 | onConfirm={async () => { |
| 139 | // optimistic update |
| 140 | dispatch(setAllSessionMetadata([])); |
| 141 | |
| 142 | // actual update + refresh |
| 143 | await ideMessenger.request("history/clear", undefined); |
| 144 | void dispatch(refreshSessionMetadata({})); |
| 145 | |
| 146 | // start a new session |
| 147 | dispatch(newSession()); |
| 148 | navigate(ROUTES.HOME); |
| 149 | }} |
| 150 | />, |
| 151 | ), |
| 152 | ); |
| 153 | dispatch(setShowDialog(true)); |
| 154 | }; |
| 155 | |
| 156 | return ( |
| 157 | <div |
| 158 | style={{ fontSize: getFontSize() }} |
| 159 | className="flex flex-1 flex-col overflow-auto px-1" |
| 160 | > |
| 161 | <div className="relative my-2 flex justify-center space-x-2"> |
| 162 | <input |
| 163 | className="bg-vsc-input-background text-vsc-foreground flex-1 rounded-md border border-none py-1 pl-2 pr-8 text-base outline-none focus:outline-none" |
| 164 | ref={searchInputRef} |
| 165 | placeholder="Search past sessions" |
| 166 | type="text" |
| 167 | value={searchTerm} |
| 168 | onChange={(e) => setSearchTerm(e.target.value)} |
| 169 | /> |
| 170 | {searchTerm && ( |
| 171 | <XMarkIcon |
| 172 | className="text-vsc-foreground hover:bg-vsc-background duration-50 absolute right-3 top-1/2 h-5 w-5 -translate-y-1/2 transform cursor-pointer rounded-full p-0.5 transition-colors" |
| 173 | onClick={() => { |
| 174 | setSearchTerm(""); |
| 175 | if (searchInputRef.current) { |
| 176 | searchInputRef.current.focus(); |
| 177 | } |
| 178 | }} |
| 179 | /> |
| 180 | )} |
| 181 | </div> |
| 182 | <div className="thin-scrollbar flex flex-1 flex-col overflow-y-auto pr-4"> |
| 183 | {filteredAndSortedSessions.length === 0 && ( |
| 184 | <div className="m-3 text-center"> |
| 185 | {isSessionMetadataLoading ? ( |
| 186 | "Loading Sessions..." |
| 187 | ) : ( |
| 188 | <> |
| 189 | No past sessions found. To start a new session, either click the |
| 190 | "+" button or use the keyboard shortcut:{" "} |
| 191 | <Shortcut>meta L</Shortcut> |
| 192 | </> |
| 193 | )} |
| 194 | </div> |
| 195 | )} |
| 196 | |
| 197 | <table className="flex flex-1 flex-col"> |
| 198 | <tbody className=""> |
| 199 | {filteredAndSortedSessions.map((session, index) => { |
| 200 | const prevDate = |
| 201 | index > 0 |
| 202 | ? parseDate(filteredAndSortedSessions[index - 1].dateCreated) |
| 203 | : earlier; |
| 204 | const date = parseDate(session.dateCreated); |
| 205 | return ( |
| 206 | <Fragment key={index}> |
| 207 | {index === 0 && date > yesterday && ( |
| 208 | <tr className={HEADER_CLASS}> |
| 209 | <td colSpan={3}>Today</td> |
| 210 | </tr> |
| 211 | )} |
| 212 | {date < yesterday && |
| 213 | date > lastWeek && |
| 214 | prevDate > yesterday && ( |
| 215 | <div className={HEADER_CLASS}> |
| 216 | <td colSpan={3}>This Week</td> |
| 217 | </div> |
| 218 | )} |
| 219 | {date < lastWeek && |
| 220 | date > lastMonth && |
| 221 | prevDate > lastWeek && ( |
| 222 | <div className={HEADER_CLASS}> |
| 223 | <td colSpan={3}>This Month</td> |
| 224 | </div> |
| 225 | )} |
| 226 | {date < lastMonth && prevDate > lastMonth && ( |
| 227 | <div className={HEADER_CLASS}> |
| 228 | <td colSpan={3}>Older</td> |
| 229 | </div> |
| 230 | )} |
| 231 | |
| 232 | <HistoryTableRow |
| 233 | sessionMetadata={session} |
| 234 | date={date} |
| 235 | index={index} |
| 236 | /> |
| 237 | </Fragment> |
| 238 | ); |
| 239 | })} |
| 240 | </tbody> |
| 241 | </table> |
| 242 | </div> |
| 243 | <div className="flex flex-col items-end justify-center border-0 border-t border-solid border-gray-400 px-2 py-1.5 text-sm"> |
| 244 | <i |
| 245 | className="" |
| 246 | data-testid="history-sessions-note" |
| 247 | >{`Data is saved in ${platform === "windows" ? "%USERPROFILE%/.continue" : "~/.continue/sessions"}`}</i> |
| 248 | <span |
| 249 | className="cursor-pointer text-xs text-gray-400 hover:underline" |
| 250 | onClick={showClearSessionsDialog} |
| 251 | > |
| 252 | Clear session history |
| 253 | </span> |
| 254 | </div> |
| 255 | </div> |
| 256 | ); |
| 257 | } |