| 1 | import { OnboardingModes } from "core/protocol/core"; |
| 2 | import { useContext, useEffect } 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 { useWebviewListener } from "../hooks/useWebviewListener"; |
| 10 | import { useAppDispatch, useAppSelector } from "../redux/hooks"; |
| 11 | import { setCodeToEdit } from "../redux/slices/editState"; |
| 12 | import { setDialogMessage, setShowDialog } from "../redux/slices/uiSlice"; |
| 13 | import { enterEdit, exitEdit } from "../redux/thunks/edit"; |
| 14 | import { saveCurrentSession } from "../redux/thunks/session"; |
| 15 | import { fontSize, isMetaEquivalentKeyPressed } from "../util"; |
| 16 | import { incrementFreeTrialCount } from "../util/freeTrial"; |
| 17 | import { ROUTES } from "../util/navigation"; |
| 18 | import { FatalErrorIndicator } from "./config/FatalErrorNotice"; |
| 19 | import TextDialog from "./dialogs"; |
| 20 | import { GenerateRuleDialog } from "./GenerateRuleDialog"; |
| 21 | import { LumpProvider } from "./mainInput/Lump/LumpContext"; |
| 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 navigate = useNavigate(); |
| 46 | const location = useLocation(); |
| 47 | const dispatch = useAppDispatch(); |
| 48 | const onboardingCard = useOnboardingCard(); |
| 49 | const ideMessenger = useContext(IdeMessengerContext); |
| 50 | const currentSessionId = useAppSelector((state) => state.session.id); |
| 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 | |
| 58 | useWebviewListener( |
| 59 | "newSession", |
| 60 | async () => { |
| 61 | navigate(ROUTES.HOME); |
| 62 | if (isInEdit) { |
| 63 | await dispatch(exitEdit({})); |
| 64 | } else { |
| 65 | await dispatch( |
| 66 | saveCurrentSession({ |
| 67 | openNewSession: true, |
| 68 | generateTitle: true, |
| 69 | }), |
| 70 | ); |
| 71 | } |
| 72 | }, |
| 73 | [isInEdit], |
| 74 | ); |
| 75 | |
| 76 | useWebviewListener( |
| 77 | "isContinueInputFocused", |
| 78 | async () => { |
| 79 | return false; |
| 80 | }, |
| 81 | [location.pathname], |
| 82 | location.pathname === ROUTES.HOME, |
| 83 | ); |
| 84 | |
| 85 | useWebviewListener( |
| 86 | "focusContinueInputWithNewSession", |
| 87 | async () => { |
| 88 | navigate(ROUTES.HOME); |
| 89 | if (isInEdit) { |
| 90 | await dispatch( |
| 91 | exitEdit({ |
| 92 | openNewSession: true, |
| 93 | }), |
| 94 | ); |
| 95 | } else { |
| 96 | await dispatch( |
| 97 | saveCurrentSession({ |
| 98 | openNewSession: true, |
| 99 | generateTitle: true, |
| 100 | }), |
| 101 | ); |
| 102 | } |
| 103 | }, |
| 104 | [location.pathname, isInEdit], |
| 105 | location.pathname === ROUTES.HOME, |
| 106 | ); |
| 107 | |
| 108 | useWebviewListener( |
| 109 | "addModel", |
| 110 | async () => { |
| 111 | navigate("/models"); |
| 112 | }, |
| 113 | [navigate], |
| 114 | ); |
| 115 | |
| 116 | useWebviewListener( |
| 117 | "navigateTo", |
| 118 | async (data) => { |
| 119 | if (data.toggle && location.pathname === data.path) { |
| 120 | navigate("/"); |
| 121 | } else { |
| 122 | navigate(data.path); |
| 123 | } |
| 124 | }, |
| 125 | [location, navigate], |
| 126 | ); |
| 127 | |
| 128 | useWebviewListener( |
| 129 | "incrementFtc", |
| 130 | async () => { |
| 131 | incrementFreeTrialCount(); |
| 132 | }, |
| 133 | [], |
| 134 | ); |
| 135 | |
| 136 | useWebviewListener( |
| 137 | "setupLocalConfig", |
| 138 | async () => { |
| 139 | onboardingCard.open(OnboardingModes.LOCAL); |
| 140 | }, |
| 141 | [], |
| 142 | ); |
| 143 | |
| 144 | useWebviewListener( |
| 145 | "freeTrialExceeded", |
| 146 | async () => { |
| 147 | dispatch(setShowDialog(true)); |
| 148 | onboardingCard.setActiveTab(OnboardingModes.MODELS_ADD_ON); |
| 149 | dispatch( |
| 150 | setDialogMessage( |
| 151 | <div className="flex-1"> |
| 152 | <OnboardingCard isDialog showFreeTrialExceededAlert /> |
| 153 | </div>, |
| 154 | ), |
| 155 | ); |
| 156 | }, |
| 157 | [], |
| 158 | ); |
| 159 | |
| 160 | useWebviewListener( |
| 161 | "setupApiKey", |
| 162 | async () => { |
| 163 | onboardingCard.open(OnboardingModes.API_KEY); |
| 164 | }, |
| 165 | [], |
| 166 | ); |
| 167 | |
| 168 | useWebviewListener( |
| 169 | "focusEdit", |
| 170 | async () => { |
| 171 | await ideMessenger.request("edit/addCurrentSelection", undefined); |
| 172 | await dispatch(enterEdit({ editorContent: mainEditor?.getJSON() })); |
| 173 | mainEditor?.commands.focus(); |
| 174 | }, |
| 175 | [ideMessenger, mainEditor], |
| 176 | ); |
| 177 | |
| 178 | useWebviewListener( |
| 179 | "setCodeToEdit", |
| 180 | async (payload) => { |
| 181 | dispatch( |
| 182 | setCodeToEdit({ |
| 183 | codeToEdit: payload, |
| 184 | }), |
| 185 | ); |
| 186 | }, |
| 187 | [], |
| 188 | ); |
| 189 | |
| 190 | useWebviewListener( |
| 191 | "exitEditMode", |
| 192 | async () => { |
| 193 | await dispatch(exitEdit({})); |
| 194 | }, |
| 195 | [], |
| 196 | ); |
| 197 | |
| 198 | useWebviewListener( |
| 199 | "generateRule", |
| 200 | async () => { |
| 201 | dispatch(setShowDialog(true)); |
| 202 | dispatch(setDialogMessage(<GenerateRuleDialog />)); |
| 203 | }, |
| 204 | [], |
| 205 | ); |
| 206 | |
| 207 | useEffect(() => { |
| 208 | const handleKeyDown = (event: any) => { |
| 209 | if (isMetaEquivalentKeyPressed(event) && event.code === "KeyC") { |
| 210 | const selection = window.getSelection()?.toString(); |
| 211 | if (selection) { |
| 212 | setTimeout(() => { |
| 213 | void navigator.clipboard.writeText(selection); |
| 214 | }, 100); |
| 215 | } |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | window.addEventListener("keydown", handleKeyDown); |
| 220 | |
| 221 | return () => { |
| 222 | window.removeEventListener("keydown", handleKeyDown); |
| 223 | }; |
| 224 | }, []); |
| 225 | |
| 226 | useEffect(() => { |
| 227 | if ( |
| 228 | isNewUserOnboarding() && |
| 229 | (location.pathname === "/" || location.pathname === "/index.html") |
| 230 | ) { |
| 231 | onboardingCard.open(); |
| 232 | } |
| 233 | }, [location]); |
| 234 | |
| 235 | return ( |
| 236 | <LocalStorageProvider> |
| 237 | <AuthProvider> |
| 238 | <LayoutTopDiv> |
| 239 | <LumpProvider> |
| 240 | <OSRContextMenu /> |
| 241 | <div |
| 242 | style={{ |
| 243 | scrollbarGutter: "stable both-edges", |
| 244 | minHeight: "100%", |
| 245 | display: "grid", |
| 246 | gridTemplateRows: "1fr auto", |
| 247 | }} |
| 248 | > |
| 249 | <TextDialog |
| 250 | showDialog={showDialog} |
| 251 | onEnter={() => { |
| 252 | dispatch(setShowDialog(false)); |
| 253 | }} |
| 254 | onClose={() => { |
| 255 | dispatch(setShowDialog(false)); |
| 256 | }} |
| 257 | message={dialogMessage} |
| 258 | /> |
| 259 | |
| 260 | <GridDiv className=""> |
| 261 | <PostHogPageView /> |
| 262 | <Outlet /> |
| 263 | <FatalErrorIndicator /> |
| 264 | </GridDiv> |
| 265 | </div> |
| 266 | <div style={{ fontSize: fontSize(-4) }} id="tooltip-portal-div" /> |
| 267 | </LumpProvider> |
| 268 | </LayoutTopDiv> |
| 269 | </AuthProvider> |
| 270 | </LocalStorageProvider> |
| 271 | ); |
| 272 | }; |
| 273 | |
| 274 | export default Layout; |