Elffuss BraidHub

kikocis/continue public

7bdcbb7e117f · 6.2 KiBRawPermalink
1import { OnboardingModes } from "core/protocol/core";
2import { useContext, useEffect } from "react";
3import { Outlet, useLocation, useNavigate } from "react-router-dom";
4import styled from "styled-components";
5import { CustomScrollbarDiv, defaultBorderRadius } from ".";
6import { AuthProvider } from "../context/Auth";
7import { IdeMessengerContext } from "../context/IdeMessenger";
8import { LocalStorageProvider } from "../context/LocalStorage";
9import { useWebviewListener } from "../hooks/useWebviewListener";
10import { useAppDispatch, useAppSelector } from "../redux/hooks";
11import { setCodeToEdit } from "../redux/slices/editState";
12import { setShowDialog } from "../redux/slices/uiSlice";
13import { enterEdit, exitEdit } from "../redux/thunks/edit";
14import { saveCurrentSession } from "../redux/thunks/session";
15import { fontSize, isMetaEquivalentKeyPressed } from "../util";
16import { incrementFreeTrialCount } from "../util/freeTrial";
17import { ROUTES } from "../util/navigation";
18import { FatalErrorIndicator } from "./config/FatalErrorNotice";
19import TextDialog from "./dialogs";
20import Footer from "./Footer";
21import { LumpProvider } from "./mainInput/Lump/LumpContext";
22import { useMainEditor } from "./mainInput/TipTapEditor";
23import { isNewUserOnboarding, useOnboardingCard } from "./OnboardingCard";
24import OSRContextMenu from "./OSRContextMenu";
25import PostHogPageView from "./PosthogPageView";
26
27const LayoutTopDiv = styled(CustomScrollbarDiv)`
28 height: 100%;
29 border-radius: ${defaultBorderRadius};
30 position: relative;
31 overflow-x: hidden;
32`;
33
34const GridDiv = styled.div`
35 display: grid;
36 grid-template-rows: 1fr auto;
37 height: 100vh;
38 overflow-x: visible;
39`;
40
41const Layout = () => {
42 const navigate = useNavigate();
43 const location = useLocation();
44 const dispatch = useAppDispatch();
45 const onboardingCard = useOnboardingCard();
46 const ideMessenger = useContext(IdeMessengerContext);
47
48 const { mainEditor } = useMainEditor();
49 const dialogMessage = useAppSelector((state) => state.ui.dialogMessage);
50
51 const showDialog = useAppSelector((state) => state.ui.showDialog);
52 const isInEdit = useAppSelector((store) => store.session.isInEdit);
53
54 useWebviewListener(
55 "newSession",
56 async () => {
57 navigate(ROUTES.HOME);
58 if (isInEdit) {
59 await dispatch(exitEdit({}));
60 } else {
61 await dispatch(
62 saveCurrentSession({
63 openNewSession: true,
64 generateTitle: true,
65 }),
66 );
67 }
68 },
69 [isInEdit],
70 );
71
72 useWebviewListener(
73 "isContinueInputFocused",
74 async () => {
75 return false;
76 },
77 [location.pathname],
78 location.pathname === ROUTES.HOME,
79 );
80
81 useWebviewListener(
82 "focusContinueInputWithNewSession",
83 async () => {
84 navigate(ROUTES.HOME);
85 if (isInEdit) {
86 await dispatch(
87 exitEdit({
88 openNewSession: true,
89 }),
90 );
91 } else {
92 await dispatch(
93 saveCurrentSession({
94 openNewSession: true,
95 generateTitle: true,
96 }),
97 );
98 }
99 },
100 [location.pathname, isInEdit],
101 location.pathname === ROUTES.HOME,
102 );
103
104 useWebviewListener(
105 "addModel",
106 async () => {
107 navigate("/models");
108 },
109 [navigate],
110 );
111
112 useWebviewListener(
113 "navigateTo",
114 async (data) => {
115 if (data.toggle && location.pathname === data.path) {
116 navigate("/");
117 } else {
118 navigate(data.path);
119 }
120 },
121 [location, navigate],
122 );
123
124 useWebviewListener(
125 "incrementFtc",
126 async () => {
127 incrementFreeTrialCount();
128 },
129 [],
130 );
131
132 useWebviewListener(
133 "openOnboardingCard",
134 async () => {
135 onboardingCard.open(OnboardingModes.API_KEY);
136 },
137 [],
138 );
139
140 useWebviewListener(
141 "setupLocalConfig",
142 async () => {
143 onboardingCard.open(OnboardingModes.LOCAL);
144 },
145 [],
146 );
147
148 useWebviewListener(
149 "focusEdit",
150 async () => {
151 await ideMessenger.request("edit/addCurrentSelection", undefined);
152 await dispatch(enterEdit({ editorContent: mainEditor?.getJSON() }));
153 mainEditor?.commands.focus();
154 },
155 [ideMessenger, mainEditor],
156 );
157
158 useWebviewListener(
159 "setCodeToEdit",
160 async (payload) => {
161 dispatch(
162 setCodeToEdit({
163 codeToEdit: payload,
164 }),
165 );
166 },
167 [],
168 );
169
170 useWebviewListener(
171 "exitEditMode",
172 async () => {
173 await dispatch(exitEdit({}));
174 },
175 [],
176 );
177
178 useEffect(() => {
179 const handleKeyDown = (event: any) => {
180 if (isMetaEquivalentKeyPressed(event) && event.code === "KeyC") {
181 const selection = window.getSelection()?.toString();
182 if (selection) {
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();
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;