246 lines
6.1 KiB
TypeScript
246 lines
6.1 KiB
TypeScript
import { useContext, useEffect } from "react";
|
|
import { Outlet, useLocation, useNavigate } from "react-router-dom";
|
|
import styled from "styled-components";
|
|
import { CustomScrollbarDiv, defaultBorderRadius } from ".";
|
|
import { AuthProvider } from "../context/Auth";
|
|
import { IdeMessengerContext } from "../context/IdeMessenger";
|
|
import { LocalStorageProvider } from "../context/LocalStorage";
|
|
import { useWebviewListener } from "../hooks/useWebviewListener";
|
|
import { useAppDispatch, useAppSelector } from "../redux/hooks";
|
|
import { setCodeToEdit } from "../redux/slices/editState";
|
|
import { setShowDialog } from "../redux/slices/uiSlice";
|
|
import { enterEdit, exitEdit } from "../redux/thunks/edit";
|
|
import { saveCurrentSession } from "../redux/thunks/session";
|
|
import { fontSize, isMetaEquivalentKeyPressed } from "../util";
|
|
import { incrementFreeTrialCount } from "../util/freeTrial";
|
|
import { ROUTES } from "../util/navigation";
|
|
import { FatalErrorIndicator } from "./config/FatalErrorNotice";
|
|
import TextDialog from "./dialogs";
|
|
import Footer from "./Footer";
|
|
import { LumpProvider } from "./mainInput/Lump/LumpContext";
|
|
import { useMainEditor } from "./mainInput/TipTapEditor";
|
|
import { isNewUserOnboarding, useOnboardingCard } from "./OnboardingCard";
|
|
import OSRContextMenu from "./OSRContextMenu";
|
|
import PostHogPageView from "./PosthogPageView";
|
|
|
|
const LayoutTopDiv = styled(CustomScrollbarDiv)`
|
|
height: 100%;
|
|
border-radius: ${defaultBorderRadius};
|
|
position: relative;
|
|
overflow-x: hidden;
|
|
`;
|
|
|
|
const GridDiv = styled.div`
|
|
display: grid;
|
|
grid-template-rows: 1fr auto;
|
|
height: 100vh;
|
|
overflow-x: visible;
|
|
`;
|
|
|
|
const Layout = () => {
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
const dispatch = useAppDispatch();
|
|
const onboardingCard = useOnboardingCard();
|
|
const ideMessenger = useContext(IdeMessengerContext);
|
|
|
|
const { mainEditor } = useMainEditor();
|
|
const dialogMessage = useAppSelector((state) => state.ui.dialogMessage);
|
|
|
|
const showDialog = useAppSelector((state) => state.ui.showDialog);
|
|
const isInEdit = useAppSelector((store) => store.session.isInEdit);
|
|
|
|
useWebviewListener(
|
|
"newSession",
|
|
async () => {
|
|
navigate(ROUTES.HOME);
|
|
if (isInEdit) {
|
|
await dispatch(exitEdit({}));
|
|
} else {
|
|
await dispatch(
|
|
saveCurrentSession({
|
|
openNewSession: true,
|
|
generateTitle: true,
|
|
}),
|
|
);
|
|
}
|
|
},
|
|
[isInEdit],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"isContinueInputFocused",
|
|
async () => {
|
|
return false;
|
|
},
|
|
[location.pathname],
|
|
location.pathname === ROUTES.HOME,
|
|
);
|
|
|
|
useWebviewListener(
|
|
"focusContinueInputWithNewSession",
|
|
async () => {
|
|
navigate(ROUTES.HOME);
|
|
if (isInEdit) {
|
|
await dispatch(
|
|
exitEdit({
|
|
openNewSession: true,
|
|
}),
|
|
);
|
|
} else {
|
|
await dispatch(
|
|
saveCurrentSession({
|
|
openNewSession: true,
|
|
generateTitle: true,
|
|
}),
|
|
);
|
|
}
|
|
},
|
|
[location.pathname, isInEdit],
|
|
location.pathname === ROUTES.HOME,
|
|
);
|
|
|
|
useWebviewListener(
|
|
"addModel",
|
|
async () => {
|
|
navigate("/models");
|
|
},
|
|
[navigate],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"navigateTo",
|
|
async (data) => {
|
|
if (data.toggle && location.pathname === data.path) {
|
|
navigate("/");
|
|
} else {
|
|
navigate(data.path);
|
|
}
|
|
},
|
|
[location, navigate],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"incrementFtc",
|
|
async () => {
|
|
incrementFreeTrialCount();
|
|
},
|
|
[],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"openOnboardingCard",
|
|
async () => {
|
|
onboardingCard.open("Best");
|
|
},
|
|
[],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"setupLocalConfig",
|
|
async () => {
|
|
onboardingCard.open("Local");
|
|
},
|
|
[],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"focusEdit",
|
|
async () => {
|
|
await ideMessenger.request("edit/addCurrentSelection", undefined);
|
|
await dispatch(enterEdit({ editorContent: mainEditor?.getJSON() }));
|
|
mainEditor?.commands.focus();
|
|
},
|
|
[ideMessenger, mainEditor],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"setCodeToEdit",
|
|
async (payload) => {
|
|
dispatch(
|
|
setCodeToEdit({
|
|
codeToEdit: payload,
|
|
}),
|
|
);
|
|
},
|
|
[],
|
|
);
|
|
|
|
useWebviewListener(
|
|
"exitEditMode",
|
|
async () => {
|
|
await dispatch(exitEdit({}));
|
|
},
|
|
[],
|
|
);
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (event: any) => {
|
|
if (isMetaEquivalentKeyPressed(event) && event.code === "KeyC") {
|
|
const selection = window.getSelection()?.toString();
|
|
if (selection) {
|
|
// Copy to clipboard
|
|
setTimeout(() => {
|
|
void navigator.clipboard.writeText(selection);
|
|
}, 100);
|
|
}
|
|
}
|
|
};
|
|
|
|
window.addEventListener("keydown", handleKeyDown);
|
|
|
|
return () => {
|
|
window.removeEventListener("keydown", handleKeyDown);
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
isNewUserOnboarding() &&
|
|
(location.pathname === "/" || location.pathname === "/index.html")
|
|
) {
|
|
onboardingCard.open("Quickstart");
|
|
}
|
|
}, [location]);
|
|
|
|
return (
|
|
<LocalStorageProvider>
|
|
<AuthProvider>
|
|
<LayoutTopDiv>
|
|
<LumpProvider>
|
|
<OSRContextMenu />
|
|
<div
|
|
style={{
|
|
scrollbarGutter: "stable both-edges",
|
|
minHeight: "100%",
|
|
display: "grid",
|
|
gridTemplateRows: "1fr auto",
|
|
}}
|
|
>
|
|
<TextDialog
|
|
showDialog={showDialog}
|
|
onEnter={() => {
|
|
dispatch(setShowDialog(false));
|
|
}}
|
|
onClose={() => {
|
|
dispatch(setShowDialog(false));
|
|
}}
|
|
message={dialogMessage}
|
|
/>
|
|
|
|
<GridDiv className="">
|
|
<PostHogPageView />
|
|
<Outlet />
|
|
<FatalErrorIndicator />
|
|
<Footer />
|
|
</GridDiv>
|
|
</div>
|
|
<div style={{ fontSize: fontSize(-4) }} id="tooltip-portal-div" />
|
|
</LumpProvider>
|
|
</LayoutTopDiv>
|
|
</AuthProvider>
|
|
</LocalStorageProvider>
|
|
);
|
|
};
|
|
|
|
export default Layout;
|