| 1 | import { OnboardingModes } from "core/protocol/core"; |
| 2 | import { useContext, useEffect, useState } from "react"; |
| 3 | import { Outlet, useLocation, useNavigate } from "react-router-dom"; |
| 4 | import styled from "styled-components"; |
| 5 | import { CustomScrollbarDiv } from "."; |
| 6 | import { AuthProvider } from "../context/Auth"; |
| 7 | import { IdeMessengerContext } from "../context/IdeMessenger"; |
| 8 | import { LocalStorageProvider } from "../context/LocalStorage"; |
| 9 | import TelemetryProviders from "../hooks/TelemetryProviders"; |
| 10 | import { useWebviewListener } from "../hooks/useWebviewListener"; |
| 11 | import { useAppDispatch, useAppSelector } from "../redux/hooks"; |
| 12 | import { setCodeToEdit } from "../redux/slices/editState"; |
| 13 | import { setDialogMessage, setShowDialog } from "../redux/slices/uiSlice"; |
| 14 | import { enterEdit, exitEdit } from "../redux/thunks/edit"; |
| 15 | import { saveCurrentSession } from "../redux/thunks/session"; |
| 16 | import { fontSize, isMetaEquivalentKeyPressed } from "../util"; |
| 17 | import { incrementFreeTrialCount } from "../util/freeTrial"; |
| 18 | import { ROUTES } from "../util/navigation"; |
| 19 | import { FatalErrorIndicator } from "./config/FatalErrorNotice"; |
| 20 | import TextDialog from "./dialogs"; |
| 21 | import { GenerateRuleDialog } from "./GenerateRuleDialog"; |
| 22 | import { useMainEditor } from "./mainInput/TipTapEditor"; |
| 23 | import { |
| 24 | isNewUserOnboarding, |
| 25 | OnboardingCard, |
| 26 | useOnboardingCard, |
| 27 | } from "./OnboardingCard"; |
| 28 | import OSRContextMenu from "./OSRContextMenu"; |
| 29 | import PostHogPageView from "./PosthogPageView"; |
| 30 | |
| 31 | const LayoutTopDiv = styled(CustomScrollbarDiv)` |
| 32 | height: 100%; |
| 33 | position: relative; |
| 34 | overflow-x: hidden; |
| 35 | `; |
| 36 | |
| 37 | const GridDiv = styled.div` |
| 38 | display: grid; |
| 39 | grid-template-rows: 1fr auto; |
| 40 | height: 100vh; |
| 41 | overflow-x: visible; |
| 42 | `; |
| 43 | |
| 44 | const Layout = () => { |
| 45 | const [showStagingIndicator, setShowStagingIndicator] = useState(false); |
| 46 | const navigate = useNavigate(); |
| 47 | const location = useLocation(); |
| 48 | const dispatch = useAppDispatch(); |
| 49 | const onboardingCard = useOnboardingCard(); |
| 50 | const ideMessenger = useContext(IdeMessengerContext); |
| 51 | |
| 52 | const { mainEditor } = useMainEditor(); |
| 53 | const dialogMessage = useAppSelector((state) => state.ui.dialogMessage); |
| 54 | |
| 55 | const showDialog = useAppSelector((state) => state.ui.showDialog); |
| 56 | const isInEdit = useAppSelector((store) => store.session.isInEdit); |
| 57 | const isHome = |
| 58 | location.pathname === ROUTES.HOME || |
| 59 | location.pathname === ROUTES.HOME_INDEX; |
| 60 | |
| 61 | useEffect(() => { |
| 62 | (async () => { |
| 63 | const response = await ideMessenger.request( |
| 64 | "controlPlane/getEnvironment", |
| 65 | undefined, |
| 66 | ); |
| 67 | response.status === "success" && |
| 68 | setShowStagingIndicator(response.content.AUTH_TYPE.includes("staging")); |
| 69 | })(); |
| 70 | }, []); |
| 71 | |
| 72 | useWebviewListener( |
| 73 | "newSession", |
| 74 | async () => { |
| 75 | navigate(ROUTES.HOME); |
| 76 | if (isInEdit) { |
| 77 | await dispatch(exitEdit({})); |
| 78 | } else { |
| 79 | await dispatch( |
| 80 | saveCurrentSession({ |
| 81 | openNewSession: true, |
| 82 | generateTitle: true, |
| 83 | }), |
| 84 | ); |
| 85 | } |
| 86 | }, |
| 87 | [isInEdit], |
| 88 | ); |
| 89 | |
| 90 | useWebviewListener( |
| 91 | "isContinueInputFocused", |
| 92 | async () => { |
| 93 | return false; |
| 94 | }, |
| 95 | [isHome], |
| 96 | isHome, |
| 97 | ); |
| 98 | |
| 99 | useWebviewListener( |
| 100 | "focusContinueInputWithNewSession", |
| 101 | async () => { |
| 102 | navigate(ROUTES.HOME); |
| 103 | if (isInEdit) { |
| 104 | await dispatch( |
| 105 | exitEdit({ |
| 106 | openNewSession: true, |
| 107 | }), |
| 108 | ); |
| 109 | } else { |
| 110 | await dispatch( |
| 111 | saveCurrentSession({ |
| 112 | openNewSession: true, |
| 113 | generateTitle: true, |
| 114 | }), |
| 115 | ); |
| 116 | } |
| 117 | }, |
| 118 | [isHome, isInEdit], |
| 119 | isHome, |
| 120 | ); |
| 121 | |
| 122 | useWebviewListener( |
| 123 | "addModel", |
| 124 | async () => { |
| 125 | navigate("/models"); |
| 126 | }, |
| 127 | [navigate], |
| 128 | ); |
| 129 | |
| 130 | useWebviewListener( |
| 131 | "navigateTo", |
| 132 | async (data) => { |
| 133 | if (data.toggle && location.pathname === data.path) { |
| 134 | navigate("/"); |
| 135 | } else { |
| 136 | navigate(data.path); |
| 137 | } |
| 138 | }, |
| 139 | [location, navigate], |
| 140 | ); |
| 141 | |
| 142 | useWebviewListener( |
| 143 | "incrementFtc", |
| 144 | async () => { |
| 145 | incrementFreeTrialCount(); |
| 146 | }, |
| 147 | [], |
| 148 | ); |
| 149 | |
| 150 | useWebviewListener( |
| 151 | "setupLocalConfig", |
| 152 | async () => { |
| 153 | onboardingCard.open(OnboardingModes.LOCAL); |
| 154 | }, |
| 155 | [], |
| 156 | ); |
| 157 | |
| 158 | useWebviewListener( |
| 159 | "freeTrialExceeded", |
| 160 | async () => { |
| 161 | dispatch(setShowDialog(true)); |
| 162 | onboardingCard.setActiveTab(OnboardingModes.MODELS_ADD_ON); |
| 163 | dispatch( |
| 164 | setDialogMessage( |
| 165 | <div className="flex-1"> |
| 166 | <OnboardingCard isDialog showFreeTrialExceededAlert /> |
| 167 | </div>, |
| 168 | ), |
| 169 | ); |
| 170 | }, |
| 171 | [], |
| 172 | ); |
| 173 | |
| 174 | useWebviewListener( |
| 175 | "setupApiKey", |
| 176 | async () => { |
| 177 | onboardingCard.open(OnboardingModes.API_KEY); |
| 178 | }, |
| 179 | [], |
| 180 | ); |
| 181 | |
| 182 | useWebviewListener( |
| 183 | "focusEdit", |
| 184 | async () => { |
| 185 | await ideMessenger.request("edit/addCurrentSelection", undefined); |
| 186 | await dispatch(enterEdit({ editorContent: mainEditor?.getJSON() })); |
| 187 | mainEditor?.commands.focus(); |
| 188 | }, |
| 189 | [ideMessenger, mainEditor], |
| 190 | ); |
| 191 | |
| 192 | useWebviewListener( |
| 193 | "setCodeToEdit", |
| 194 | async (payload) => { |
| 195 | dispatch( |
| 196 | setCodeToEdit({ |
| 197 | codeToEdit: payload, |
| 198 | }), |
| 199 | ); |
| 200 | }, |
| 201 | [], |
| 202 | ); |
| 203 | |
| 204 | useWebviewListener( |
| 205 | "exitEditMode", |
| 206 | async () => { |
| 207 | await dispatch(exitEdit({})); |
| 208 | }, |
| 209 | [], |
| 210 | ); |
| 211 | |
| 212 | useWebviewListener( |
| 213 | "generateRule", |
| 214 | async () => { |
| 215 | dispatch(setShowDialog(true)); |
| 216 | dispatch(setDialogMessage(<GenerateRuleDialog />)); |
| 217 | }, |
| 218 | [], |
| 219 | ); |
| 220 | |
| 221 | useEffect(() => { |
| 222 | const handleKeyDown = (event: any) => { |
| 223 | if (isMetaEquivalentKeyPressed(event) && event.code === "KeyC") { |
| 224 | const selection = window.getSelection()?.toString(); |
| 225 | if (selection) { |
| 226 | setTimeout(() => { |
| 227 | void navigator.clipboard.writeText(selection); |
| 228 | }, 100); |
| 229 | } |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | window.addEventListener("keydown", handleKeyDown); |
| 234 | |
| 235 | return () => { |
| 236 | window.removeEventListener("keydown", handleKeyDown); |
| 237 | }; |
| 238 | }, []); |
| 239 | |
| 240 | useEffect(() => { |
| 241 | if (isNewUserOnboarding() && isHome) { |
| 242 | onboardingCard.open(); |
| 243 | } |
| 244 | }, [isHome]); |
| 245 | |
| 246 | return ( |
| 247 | <LocalStorageProvider> |
| 248 | <AuthProvider> |
| 249 | <TelemetryProviders> |
| 250 | <LayoutTopDiv> |
| 251 | {showStagingIndicator && ( |
| 252 | <span |
| 253 | title="Staging environment" |
| 254 | className="absolute right-0 mx-1.5 h-1.5 w-1.5 rounded-full" |
| 255 | style={{ |
| 256 | backgroundColor: "var(--vscode-list-warningForeground)", |
| 257 | }} |
| 258 | /> |
| 259 | )} |
| 260 | <OSRContextMenu /> |
| 261 | <div |
| 262 | style={{ |
| 263 | scrollbarGutter: "stable both-edges", |
| 264 | minHeight: "100%", |
| 265 | display: "grid", |
| 266 | gridTemplateRows: "1fr auto", |
| 267 | }} |
| 268 | > |
| 269 | <TextDialog |
| 270 | showDialog={showDialog} |
| 271 | onEnter={() => { |
| 272 | dispatch(setShowDialog(false)); |
| 273 | }} |
| 274 | onClose={() => { |
| 275 | dispatch(setShowDialog(false)); |
| 276 | }} |
| 277 | message={dialogMessage} |
| 278 | /> |
| 279 | |
| 280 | <GridDiv> |
| 281 | <PostHogPageView /> |
| 282 | <Outlet /> |
| 283 | {/* The fatal error for chat is shown below input */} |
| 284 | {!isHome && <FatalErrorIndicator />} |
| 285 | </GridDiv> |
| 286 | </div> |
| 287 | <div style={{ fontSize: fontSize(-4) }} id="tooltip-portal-div" /> |
| 288 | </LayoutTopDiv> |
| 289 | </TelemetryProviders> |
| 290 | </AuthProvider> |
| 291 | </LocalStorageProvider> |
| 292 | ); |
| 293 | }; |
| 294 | |
| 295 | export default Layout; |