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