Elffuss BraidHub

kikocis/continue public

951aa1c26a3e · 6.1 KiBRawPermalink
1import { useContext, useEffect } from "react";
2import { Outlet, useLocation, useNavigate } from "react-router-dom";
3import styled from "styled-components";
4import { CustomScrollbarDiv, defaultBorderRadius } from ".";
5import { AuthProvider } from "../context/Auth";
6import { IdeMessengerContext } from "../context/IdeMessenger";
7import { LocalStorageProvider } from "../context/LocalStorage";
8import { useWebviewListener } from "../hooks/useWebviewListener";
9import { useAppDispatch, useAppSelector } from "../redux/hooks";
10import { setCodeToEdit } from "../redux/slices/editState";
11import { setShowDialog } from "../redux/slices/uiSlice";
12import { enterEdit, exitEdit } from "../redux/thunks/edit";
13import { saveCurrentSession } from "../redux/thunks/session";
14import { fontSize, isMetaEquivalentKeyPressed } from "../util";
15import { incrementFreeTrialCount } from "../util/freeTrial";
16import { ROUTES } from "../util/navigation";
17import { FatalErrorIndicator } from "./config/FatalErrorNotice";
18import TextDialog from "./dialogs";
19import Footer from "./Footer";
20import { LumpProvider } from "./mainInput/Lump/LumpContext";
21import { useMainEditor } from "./mainInput/TipTapEditor";
22import { isNewUserOnboarding, useOnboardingCard } from "./OnboardingCard";
23import OSRContextMenu from "./OSRContextMenu";
24import PostHogPageView from "./PosthogPageView";
25
26const LayoutTopDiv = styled(CustomScrollbarDiv)`
27 height: 100%;
28 border-radius: ${defaultBorderRadius};
29 position: relative;
30 overflow-x: hidden;
31`;
32
33const GridDiv = styled.div`
34 display: grid;
35 grid-template-rows: 1fr auto;
36 height: 100vh;
37 overflow-x: visible;
38`;
39
40const Layout = () => {
41 const navigate = useNavigate();
42 const location = useLocation();
43 const dispatch = useAppDispatch();
44 const onboardingCard = useOnboardingCard();
45 const ideMessenger = useContext(IdeMessengerContext);
46
47 const { mainEditor } = useMainEditor();
48 const dialogMessage = useAppSelector((state) => state.ui.dialogMessage);
49
50 const showDialog = useAppSelector((state) => state.ui.showDialog);
51 const isInEdit = useAppSelector((store) => store.session.isInEdit);
52
53 useWebviewListener(
54 "newSession",
55 async () => {
56 navigate(ROUTES.HOME);
57 if (isInEdit) {
58 await dispatch(exitEdit({}));
59 } else {
60 await dispatch(
61 saveCurrentSession({
62 openNewSession: true,
63 generateTitle: true,
64 }),
65 );
66 }
67 },
68 [isInEdit],
69 );
70
71 useWebviewListener(
72 "isContinueInputFocused",
73 async () => {
74 return false;
75 },
76 [location.pathname],
77 location.pathname === ROUTES.HOME,
78 );
79
80 useWebviewListener(
81 "focusContinueInputWithNewSession",
82 async () => {
83 navigate(ROUTES.HOME);
84 if (isInEdit) {
85 await dispatch(
86 exitEdit({
87 openNewSession: true,
88 }),
89 );
90 } else {
91 await dispatch(
92 saveCurrentSession({
93 openNewSession: true,
94 generateTitle: true,
95 }),
96 );
97 }
98 },
99 [location.pathname, isInEdit],
100 location.pathname === ROUTES.HOME,
101 );
102
103 useWebviewListener(
104 "addModel",
105 async () => {
106 navigate("/models");
107 },
108 [navigate],
109 );
110
111 useWebviewListener(
112 "navigateTo",
113 async (data) => {
114 if (data.toggle && location.pathname === data.path) {
115 navigate("/");
116 } else {
117 navigate(data.path);
118 }
119 },
120 [location, navigate],
121 );
122
123 useWebviewListener(
124 "incrementFtc",
125 async () => {
126 incrementFreeTrialCount();
127 },
128 [],
129 );
130
131 useWebviewListener(
132 "openOnboardingCard",
133 async () => {
134 onboardingCard.open("Best");
135 },
136 [],
137 );
138
139 useWebviewListener(
140 "setupLocalConfig",
141 async () => {
142 onboardingCard.open("Local");
143 },
144 [],
145 );
146
147 useWebviewListener(
148 "focusEdit",
149 async () => {
150 await ideMessenger.request("edit/addCurrentSelection", undefined);
151 await dispatch(enterEdit({ editorContent: mainEditor?.getJSON() }));
152 mainEditor?.commands.focus();
153 },
154 [ideMessenger, mainEditor],
155 );
156
157 useWebviewListener(
158 "setCodeToEdit",
159 async (payload) => {
160 dispatch(
161 setCodeToEdit({
162 codeToEdit: payload,
163 }),
164 );
165 },
166 [],
167 );
168
169 useWebviewListener(
170 "exitEditMode",
171 async () => {
172 await dispatch(exitEdit({}));
173 },
174 [],
175 );
176
177 useEffect(() => {
178 const handleKeyDown = (event: any) => {
179 if (isMetaEquivalentKeyPressed(event) && event.code === "KeyC") {
180 const selection = window.getSelection()?.toString();
181 if (selection) {
182 // Copy to clipboard
183 setTimeout(() => {
184 void navigator.clipboard.writeText(selection);
185 }, 100);
186 }
187 }
188 };
189
190 window.addEventListener("keydown", handleKeyDown);
191
192 return () => {
193 window.removeEventListener("keydown", handleKeyDown);
194 };
195 }, []);
196
197 useEffect(() => {
198 if (
199 isNewUserOnboarding() &&
200 (location.pathname === "/" || location.pathname === "/index.html")
201 ) {
202 onboardingCard.open("Quickstart");
203 }
204 }, [location]);
205
206 return (
207 <LocalStorageProvider>
208 <AuthProvider>
209 <LayoutTopDiv>
210 <LumpProvider>
211 <OSRContextMenu />
212 <div
213 style={{
214 scrollbarGutter: "stable both-edges",
215 minHeight: "100%",
216 display: "grid",
217 gridTemplateRows: "1fr auto",
218 }}
219 >
220 <TextDialog
221 showDialog={showDialog}
222 onEnter={() => {
223 dispatch(setShowDialog(false));
224 }}
225 onClose={() => {
226 dispatch(setShowDialog(false));
227 }}
228 message={dialogMessage}
229 />
230
231 <GridDiv className="">
232 <PostHogPageView />
233 <Outlet />
234 <FatalErrorIndicator />
235 <Footer />
236 </GridDiv>
237 </div>
238 <div style={{ fontSize: fontSize(-4) }} id="tooltip-portal-div" />
239 </LumpProvider>
240 </LayoutTopDiv>
241 </AuthProvider>
242 </LocalStorageProvider>
243 );
244};
245
246export default Layout;