PromucFlow_constructor/app/client/src/pages/AppViewer/index.tsx
ashit-rath a47dba5e26
feat: List V2 (#15839)
## Description

TL;DR
This is a complete architectural change of of List widget works to
support all widgets we currently have and should automatically support
any future widgets.
It also introduces nested List widgets i.e a list widget can have a
another list widget which in turn can have another list widget.

Fixes #18206
Fixes #6775
Fixes #13211
Fixes #16582
Fixes #11739
Fixes #15094
Fixes #6840
Fixes #10841
Fixes #17386
Fixes #18340
Fixes #16898
Fixes #17555
Fixes #6858
Fixes #9568
Fixes #17480
Fixes #18523
Fixes #18206  
Fixes #16586
Fixes #18106
Fixes #16576
Fixes #14697
Fixes #9607
Fixes #19648 
Fixes #19739
Fixes #19652 
Fixes #18730 
Fixes #19503 
Fixes #19498
Fixes #19437
Fixes #5245 
Fixes #19150
Fixes #18638
Fixes #11332
Fixes #17901
Fixes #19043
Fixes #17777
Fixes #8237
Fixes #15487
Fixes #15988
Fixes #18621
Fixes #16788
Fixes #18110
Fixes #18382
Fixes #17427
Fixes #18105
Fixes #18287
Fixes #19808
Fixes #14655

## Type of change

- New feature (non-breaking change which adds functionality)

## How Has This Been Tested?
- Cypress
- Jest
- Manual

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes

---------

Co-authored-by: Tolulope Adetula <31691737+Tooluloope@users.noreply.github.com>
Co-authored-by: Favour Ohanekwu <fohanekwu@gmail.com>
2023-02-14 17:07:31 +01:00

213 lines
6.8 KiB
TypeScript

import React, { useEffect, useState } from "react";
import styled, { ThemeProvider } from "styled-components";
import { useDispatch } from "react-redux";
import { withRouter, RouteComponentProps } from "react-router";
import { AppState } from "@appsmith/reducers";
import {
AppViewerRouteParams,
BuilderRouteParams,
GIT_BRANCH_QUERY_KEY,
} from "constants/routes";
import {
getIsInitialized,
getAppViewHeaderHeight,
} from "selectors/appViewSelectors";
import EditorContextProvider from "components/editorComponents/EditorContextProvider";
import AppViewerPageContainer from "./AppViewerPageContainer";
import { editorInitializer } from "utils/editor/EditorUtils";
import * as Sentry from "@sentry/react";
import { getViewModePageList } from "selectors/editorSelectors";
import { getThemeDetails, ThemeMode } from "selectors/themeSelectors";
import webfontloader from "webfontloader";
import { getSearchQuery } from "utils/helpers";
import { getSelectedAppTheme } from "selectors/appThemingSelectors";
import { useSelector } from "react-redux";
import BrandingBadge from "./BrandingBadge";
import { setAppViewHeaderHeight } from "actions/appViewActions";
import { showPostCompletionMessage } from "selectors/onboardingSelectors";
import { CANVAS_SELECTOR } from "constants/WidgetConstants";
import { fetchPublishedPage } from "actions/pageActions";
import usePrevious from "utils/hooks/usePrevious";
import { getIsBranchUpdated } from "../utils";
import { APP_MODE } from "entities/App";
import { initAppViewer } from "actions/initActions";
import { WidgetGlobaStyles } from "globalStyles/WidgetGlobalStyles";
import { getAppsmithConfigs } from "@appsmith/configs";
import useWidgetFocus from "utils/hooks/useWidgetFocus/useWidgetFocus";
const AppViewerBody = styled.section<{
hasPages: boolean;
headerHeight: number;
showGuidedTourMessage: boolean;
}>`
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: flex-start;
height: calc(100vh - ${({ headerHeight }) => headerHeight}px);
--view-mode-header-height: ${({ headerHeight }) => headerHeight}px;
`;
const AppViewerBodyContainer = styled.div<{
width?: string;
backgroundColor: string;
}>`
flex: 1;
overflow: auto;
margin: 0 auto;
background: ${({ backgroundColor }) => backgroundColor};
`;
export type AppViewerProps = RouteComponentProps<BuilderRouteParams>;
type Props = AppViewerProps & RouteComponentProps<AppViewerRouteParams>;
const DEFAULT_FONT_NAME = "System Default";
function AppViewer(props: Props) {
const dispatch = useDispatch();
const { pathname, search } = props.location;
const { applicationId, pageId } = props.match.params;
const [registered, setRegistered] = useState(false);
const isInitialized = useSelector(getIsInitialized);
const pages = useSelector(getViewModePageList);
const selectedTheme = useSelector(getSelectedAppTheme);
const lightTheme = useSelector((state: AppState) =>
getThemeDetails(state, ThemeMode.LIGHT),
);
const showGuidedTourMessage = useSelector(showPostCompletionMessage);
const headerHeight = useSelector(getAppViewHeaderHeight);
const branch = getSearchQuery(search, GIT_BRANCH_QUERY_KEY);
const prevValues = usePrevious({ branch, location: props.location, pageId });
const { hideWatermark } = getAppsmithConfigs();
const focusRef = useWidgetFocus();
/**
* initializes the widgets factory and registers all widgets
*/
useEffect(() => {
editorInitializer().then(() => {
setRegistered(true);
});
// onMount initPage
if (applicationId || pageId) {
dispatch(
initAppViewer({
applicationId,
branch,
pageId,
mode: APP_MODE.PUBLISHED,
}),
);
}
}, []);
/**
* initialize the app if branch, pageId or application is changed
*/
useEffect(() => {
const prevBranch = prevValues?.branch;
const prevLocation = prevValues?.location;
const prevPageId = prevValues?.pageId;
let isBranchUpdated = false;
if (prevBranch && prevLocation) {
isBranchUpdated = getIsBranchUpdated(props.location, prevLocation);
}
const isPageIdUpdated = pageId !== prevPageId;
if (prevBranch && isBranchUpdated && (applicationId || pageId)) {
dispatch(
initAppViewer({
applicationId,
branch,
pageId,
mode: APP_MODE.PUBLISHED,
}),
);
} else {
/**
* First time load is handled by init sagas
* If we don't check for `prevPageId`: fetch page is retriggered
* when redirected to the default page
*/
if (prevPageId && pageId && isPageIdUpdated) {
dispatch(fetchPublishedPage(pageId, true));
}
}
}, [branch, pageId, applicationId, pathname]);
useEffect(() => {
const header = document.querySelector(".js-appviewer-header");
dispatch(setAppViewHeaderHeight(header?.clientHeight || 0));
}, [pages.length, isInitialized]);
/**
* returns the font to be used for the canvas
*/
const appFontFamily =
selectedTheme.properties.fontFamily.appFont === DEFAULT_FONT_NAME
? "inherit"
: selectedTheme.properties.fontFamily.appFont;
/**
* loads font for canvas based on theme
*/
useEffect(() => {
if (selectedTheme.properties.fontFamily.appFont !== DEFAULT_FONT_NAME) {
webfontloader.load({
google: {
families: [
`${selectedTheme.properties.fontFamily.appFont}:300,400,500,700`,
],
},
});
}
document.body.style.fontFamily = appFontFamily;
return function reset() {
document.body.style.fontFamily = "inherit";
};
}, [selectedTheme.properties.fontFamily.appFont]);
return (
<ThemeProvider theme={lightTheme}>
<EditorContextProvider renderMode="PAGE">
<WidgetGlobaStyles
fontFamily={selectedTheme.properties.fontFamily.appFont}
primaryColor={selectedTheme.properties.colors.primaryColor}
/>
<AppViewerBodyContainer
backgroundColor={selectedTheme.properties.colors.backgroundColor}
>
<AppViewerBody
className={CANVAS_SELECTOR}
hasPages={pages.length > 1}
headerHeight={headerHeight}
ref={focusRef}
showGuidedTourMessage={showGuidedTourMessage}
>
{isInitialized && registered && <AppViewerPageContainer />}
</AppViewerBody>
{!hideWatermark && (
<a
className="fixed hidden right-8 bottom-4 z-3 hover:no-underline md:flex"
href="https://appsmith.com"
rel="noreferrer"
target="_blank"
>
<BrandingBadge />
</a>
)}
</AppViewerBodyContainer>
</EditorContextProvider>
</ThemeProvider>
);
}
export default withRouter(Sentry.withProfiler(AppViewer));