Elffuss BraidHub

kikocis/continue public

9d8db5fc402e · 7.1 KiBRawPermalink
1import { SessionMetadata } from "core";
2import MiniSearch from "minisearch";
3import React, {
4 Fragment,
5 useContext,
6 useEffect,
7 useMemo,
8 useRef,
9 useState,
10} from "react";
11import Shortcut from "../gui/Shortcut";
12
13import { XMarkIcon } from "@heroicons/react/24/solid";
14import { useNavigate } from "react-router-dom";
15import { IdeMessengerContext } from "../../context/IdeMessenger";
16import { useAppDispatch, useAppSelector } from "../../redux/hooks";
17import {
18 newSession,
19 setAllSessionMetadata,
20} from "../../redux/slices/sessionSlice";
21import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice";
22import { refreshSessionMetadata } from "../../redux/thunks/session";
23import { getFontSize, getPlatform } from "../../util";
24import { ROUTES } from "../../util/navigation";
25import ConfirmationDialog from "../dialogs/ConfirmationDialog";
26import { HistoryTableRow } from "./HistoryTableRow";
27
28const 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
36const 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
39export 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
58 useEffect(() => {
59 try {
60 minisearch.removeAll();
61 minisearch.addAll(
62 allSessionMetadata.map((session) => ({
63 title: session.title,
64 sessionId: session.sessionId,
65 id: session.sessionId,
66 })),
67 );
68 } catch (e) {
69 console.log("error adding sessions to minisearch", e);
70 }
71 }, [allSessionMetadata]);
72
73 const platform = useMemo(() => getPlatform(), []);
74
75 const filteredAndSortedSessions: SessionMetadata[] = useMemo(() => {
76 const sessionIds = minisearch
77 .search(searchTerm, {
78 fuzzy: 0.1,
79 })
80 .map((result) => result.id);
81
82 return allSessionMetadata
83 .filter((session) => {
84 return searchTerm === "" || sessionIds.includes(session.sessionId);
85 })
86 .sort(
87 (a, b) =>
88 parseDate(b.dateCreated).getTime() -
89 parseDate(a.dateCreated).getTime(),
90 );
91 }, [allSessionMetadata, searchTerm, minisearch]);
92
93 const yesterday = new Date(Date.now() - 1000 * 60 * 60 * 24);
94 const lastWeek = new Date(Date.now() - 1000 * 60 * 60 * 24 * 7);
95 const lastMonth = new Date(Date.now() - 1000 * 60 * 60 * 24 * 30);
96 const earlier = new Date(0);
97
98 const showClearSessionsDialog = () => {
99 dispatch(
100 setDialogMessage(
101 <ConfirmationDialog
102 title={`Clear sessions`}
103 text={`Are you sure you want to permanently delete all chat sessions, including the current chat session?`}
104 onConfirm={async () => {
105 // optimistic update
106 dispatch(setAllSessionMetadata([]));
107
108 // actual update + refresh
109 await ideMessenger.request("history/clear", undefined);
110 dispatch(refreshSessionMetadata({}));
111
112 // start a new session
113 dispatch(newSession());
114 navigate(ROUTES.HOME);
115 }}
116 />,
117 ),
118 );
119 dispatch(setShowDialog(true));
120 };
121
122 return (
123 <div
124 style={{ fontSize: getFontSize() }}
125 className="flex flex-1 flex-col overflow-auto px-1"
126 >
127 <div className="relative my-2 flex justify-center space-x-2">
128 <input
129 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"
130 ref={searchInputRef}
131 placeholder="Search past sessions"
132 type="text"
133 value={searchTerm}
134 onChange={(e) => setSearchTerm(e.target.value)}
135 />
136 {searchTerm && (
137 <XMarkIcon
138 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"
139 onClick={() => {
140 setSearchTerm("");
141 if (searchInputRef.current) {
142 searchInputRef.current.focus();
143 }
144 }}
145 />
146 )}
147 </div>
148 <div className="thin-scrollbar flex flex-1 flex-col overflow-y-auto pr-4">
149 {filteredAndSortedSessions.length === 0 && (
150 <div className="m-3 text-center">
151 No past sessions found. To start a new session, either click the "+"
152 button or use the keyboard shortcut: <Shortcut>meta L</Shortcut>
153 </div>
154 )}
155
156 <table className="flex flex-1 flex-col">
157 <tbody className="">
158 {filteredAndSortedSessions.map((session, index) => {
159 const prevDate =
160 index > 0
161 ? parseDate(filteredAndSortedSessions[index - 1].dateCreated)
162 : earlier;
163 const date = parseDate(session.dateCreated);
164 return (
165 <Fragment key={index}>
166 {index === 0 && date > yesterday && (
167 <tr className={HEADER_CLASS}>
168 <td colSpan={3}>Today</td>
169 </tr>
170 )}
171 {date < yesterday &&
172 date > lastWeek &&
173 prevDate > yesterday && (
174 <div className={HEADER_CLASS}>
175 <td colSpan={3}>This Week</td>
176 </div>
177 )}
178 {date < lastWeek &&
179 date > lastMonth &&
180 prevDate > lastWeek && (
181 <div className={HEADER_CLASS}>
182 <td colSpan={3}>This Month</td>
183 </div>
184 )}
185 {date < lastMonth && prevDate > lastMonth && (
186 <div className={HEADER_CLASS}>
187 <td colSpan={3}>Older</td>
188 </div>
189 )}
190
191 <HistoryTableRow
192 sessionMetadata={session}
193 date={date}
194 index={index}
195 />
196 </Fragment>
197 );
198 })}
199 </tbody>
200 </table>
201 </div>
202 <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">
203 <i
204 className=""
205 data-testid="history-sessions-note"
206 >{`Data is saved in ${platform === "windows" ? "%USERPROFILE%/.continue" : "~/.continue/sessions"}`}</i>
207 <span
208 className="cursor-pointer text-xs text-gray-400 hover:underline"
209 onClick={showClearSessionsDialog}
210 >
211 Clear session history
212 </span>
213 </div>
214 </div>
215 );
216}