Skip to content

Commit

Permalink
35635685 Обновить стартовые настройки плагина
Browse files Browse the repository at this point in the history
Co-authored-by: michael-916310 <[email protected]>
  • Loading branch information
michael-916310 and Michael-834734673478 authored Jul 31, 2024
1 parent ee9bed2 commit af1c9a8
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 162 deletions.
233 changes: 142 additions & 91 deletions superset-frontend/src/Superstructure/Root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {
import {
AnnotationLayer,
Dashboard,
FullConfiguration,
MicrofrontendNavigation,
MicrofrontendParams,
RouteConfig,
} from '../types/global';
import { composeAPIConfig } from '../config';

Expand Down Expand Up @@ -44,7 +45,7 @@ import {
RootComponentWrapper,
} from './styles';

import { APP_VERSION, getNavigationConfig } from '../parseEnvFile';
import { APP_VERSION } from '../parseEnvFile';
import { serializeValue } from '../parseEnvFile/utils';
import { addSlash, logConfigs } from './helpers';

Expand All @@ -53,7 +54,6 @@ import {
MESSAGES,
SORTING_PREFIX,
STYLES_DODOPIZZA,
STYLES_DONER42,
STYLES_DRINKIT,
} from '../constants';
import { getDefaultDashboard } from '../utils/getDefaultDashboard';
Expand All @@ -64,6 +64,16 @@ import {

setupClient();

const StyledCollapseBtn = styled.button<{
isVisible: boolean;
}>`
color: ${({ isVisible }) => (isVisible ? 'initial' : '#ff6900')};
background: none;
border: none;
position: relative;
padding-top: 8px;
`;

export const RootComponent = (incomingParams: MicrofrontendParams) => {
const businessId = incomingParams.businessId || 'dodopizza';

Expand Down Expand Up @@ -97,12 +107,19 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
title: '',
stackTrace: '',
});
const [FULL_CONFIG, setFullConfig] = useState({
navigation: { showNavigationMenu: false, routes: [] },
const [FULL_CONFIG, setFullConfig] = useState<{
showNavigationMenu: boolean;
routes: Array<RouteConfig>;
originUrl: string;
frontendLogger: boolean;
basename: string;
}>({
showNavigationMenu: false,
routes: [],
originUrl: '',
frontendLogger: false,
basename: '',
} as FullConfiguration);
});
const [isFullConfigReady, setFullConfigReady] = useState(false);
const [stylesConfig, setStylesConfig] = useState(STYLES_DODOPIZZA);
const [annotationsObjects, setAnnotationsObjects] = useState(null) as any;
Expand Down Expand Up @@ -292,6 +309,8 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
basename: string;
frontendLogger: boolean;
business: string;
showNavigationMenu: boolean;
dashboards: MicrofrontendNavigation['dashboards'];
} = useMemo(() => {
const env = process.env.WEBPACK_MODE;

Expand All @@ -304,6 +323,8 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
: '/',
frontendLogger: incomingParams.frontendLogger || true,
business: businessId,
dashboards: incomingParams.navigation.dashboards,
showNavigationMenu: incomingParams.navigation.showNavigationMenu,
};

// Superset API works only with port 3000
Expand All @@ -330,7 +351,6 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
useEffect(() => {
composeAPIConfig(params);
if (params.business === 'drinkit') setStylesConfig(STYLES_DRINKIT);
else if (params.business === 'doner42') setStylesConfig(STYLES_DONER42);
}, [params]);

useEffect(() => {
Expand Down Expand Up @@ -359,64 +379,99 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
const csrf = await handleCsrfRequest({ useAuth });

if (csrf?.data?.result) {
const dashboards = await handleDashboardsRequest(params.business);

setAnnotationsObjects(await loadAnnotations());

if (dashboards?.data?.length) {
let SORTING_IDS = [] as any[];

const sortingAnnotationIds =
await handleAnnotationLayersRequestSorting();

if (sortingAnnotationIds) {
const annotations = await handleAnnotationsRequest(
sortingAnnotationIds,
);

if (annotations?.length) {
const filteredSortingAnnotations = annotations.filter(
annotation =>
annotation?.data?.result.short_descr.includes(
SORTING_PREFIX,
),
const hardcodedDashboard = params?.dashboards ?? {};
const hardcodedDashboardKeys = Object.keys(hardcodedDashboard ?? {});
if (hardcodedDashboardKeys.length > 0) {
const routes: Array<RouteConfig> = [];

hardcodedDashboardKeys.forEach(key => {
const item = hardcodedDashboard[key];

routes.push({
idOrSlug: item.idOrSlug,
name: item.name,
nameRU: item.nameRU,
hidden: false,
location: '',
});
});

setLoaded(true);

const { basename, originUrl, frontendLogger, showNavigationMenu } =
params;
setFullConfig({
basename,
originUrl,
frontendLogger,
showNavigationMenu,
routes,
});
setFullConfigReady(true);
} else {
const dashboards = await handleDashboardsRequest(params.business);

setAnnotationsObjects(await loadAnnotations());

if (dashboards?.data?.length) {
let SORTING_IDS = [] as any[];

const sortingAnnotationIds =
await handleAnnotationLayersRequestSorting();

if (sortingAnnotationIds) {
const annotations = await handleAnnotationsRequest(
sortingAnnotationIds,
);
const jsonObjectString = !filteredSortingAnnotations.length
? '{}'
: filteredSortingAnnotations[0]?.data?.result.json_metadata;

if (jsonObjectString) {
SORTING_IDS = JSON.parse(jsonObjectString)?.order || [];
if (annotations?.length) {
const filteredSortingAnnotations = annotations.filter(
annotation =>
annotation?.data?.result.short_descr.includes(
SORTING_PREFIX,
),
);
const jsonObjectString = !filteredSortingAnnotations.length
? '{}'
: filteredSortingAnnotations[0]?.data?.result.json_metadata;

if (jsonObjectString) {
SORTING_IDS = JSON.parse(jsonObjectString)?.order || [];
}
}
}
}

const navConfigFull = getNavigationConfig(
sortDashboards(
const routes = sortDashboards(
defineNavigation(dashboards.data),
SORTING_IDS || [],
),
);

if (navConfigFull?.navigation.routes.length) {
setLoaded(true);

const { basename, originUrl, frontendLogger } = params;
setFullConfig({
...navConfigFull,
basename,
originUrl,
frontendLogger,
});
setFullConfigReady(true);
} else {
setLoaded(false);
setError(true);
setErrorObject({
msg: 'Что-то пошло не так c настройкой меню',
title: 'UNEXPECTED_ERROR',
stackTrace: 'UNKNOWN',
});
);

if (routes.length) {
setLoaded(true);

const {
basename,
originUrl,
frontendLogger,
showNavigationMenu,
} = params;
setFullConfig({
basename,
originUrl,
frontendLogger,
showNavigationMenu,
routes,
});
setFullConfigReady(true);
} else {
setLoaded(false);
setError(true);
setErrorObject({
msg: 'Что-то пошло не так c настройкой меню',
title: 'UNEXPECTED_ERROR',
stackTrace: 'UNKNOWN',
});
}
}
}
}
Expand All @@ -441,57 +496,53 @@ export const RootComponent = (incomingParams: MicrofrontendParams) => {
</>
);
}
const StyledCollapseBtn = styled.button<{
isVisible: boolean;
}>`
color: ${({ isVisible }) => (isVisible ? 'initial' : '#ff6900')};
background: none;
border: none;
position: relative;
padding-top: 8px;
`;

const closeLeftNavigation = useCallback(() => setIsVisible(false), []); // DODO added #33605679

const startDashboard = getDefaultDashboard({
businessId,
routes: FULL_CONFIG.navigation.routes,
routes: FULL_CONFIG.routes,
});

const withNavigation = useMemo(
() => FULL_CONFIG.routes.length > 1 && FULL_CONFIG.showNavigationMenu,
[FULL_CONFIG.routes.length, FULL_CONFIG.showNavigationMenu],
);

return (
<div>
<Version appVersion={APP_VERSION} />
<ContentWrapper>
{!isLoaded || !isFullConfigReady ? (
<Loading />
) : (
<RootComponentWrapper
withNavigation={FULL_CONFIG.navigation.showNavigationMenu}
>
<RootComponentWrapper withNavigation={withNavigation}>
<Router>
<LeftNavigation
routesConfig={FULL_CONFIG.navigation.routes}
baseRoute={FULL_CONFIG.basename}
stylesConfig={stylesConfig}
language={userLanguage}
isVisible={isVisible}
onNavigate={closeLeftNavigation} // DODO added #33605679
/>
{withNavigation && (
<LeftNavigation
routes={FULL_CONFIG.routes}
baseRoute={FULL_CONFIG.basename}
stylesConfig={stylesConfig}
language={userLanguage}
isVisible={isVisible}
onNavigate={closeLeftNavigation} // DODO added #33605679
/>
)}
<DashboardComponentWrapper
withNavigation={
FULL_CONFIG.navigation.showNavigationMenu && isVisible
}
withNavigation={withNavigation && isVisible}
>
<StyledCollapseBtn
type="button"
onClick={() => setIsVisible(!isVisible)}
isVisible={isVisible}
>
{isVisible && <Icons.Expand />}
{!isVisible && <Icons.Collapse />}
</StyledCollapseBtn>
{withNavigation && (
<StyledCollapseBtn
type="button"
onClick={() => setIsVisible(!isVisible)}
isVisible={isVisible}
>
{isVisible && <Icons.Expand />}
{!isVisible && <Icons.Collapse />}
</StyledCollapseBtn>
)}
<Main
navigation={FULL_CONFIG.navigation}
routes={FULL_CONFIG.routes}
store={store}
basename={FULL_CONFIG.basename}
stylesConfig={stylesConfig}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { hot } from 'react-hot-loader/root';
import React from 'react';
import { RoutesConfig, StylesConfig } from '../../types/global';
import { StylesConfig } from '../../types/global';
import {
LeftNavigationWrapper,
UlContainer,
ListItem,
StyledLink,
UlContainer,
} from './styles';

const LeftNavigation = (props: {
routesConfig: RoutesConfig;
routes: Array<{
idOrSlug: null | string | number;
hidden?: boolean;
name: string;
nameRU: string;
}>;
baseRoute: string;
stylesConfig: StylesConfig;
language: string;
isVisible: boolean;
onNavigate?: () => void; // DODO added #33605679
}) => {
const allAvailableRoutes = props.routesConfig.filter(route => !route.hidden);
const allAvailableRoutes = props.routes.filter(route => !route.hidden);
const { isVisible, onNavigate } = props; // DODO changed #33605679
const { businessId } = props.stylesConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import { Redirect, Route, Switch } from 'react-router-dom';
import DashboardApp from '../App';
import { MainComponentProps } from '../../types/global';

export default function Main({
navigation,
routes,
store,
basename,
startDashboardId = 0,
Expand All @@ -19,10 +19,10 @@ export default function Main({
DASHBOARD_NATIVE_FILTERS_SET: false,
};

return navigation ? (
return routes ? (
<>
<Switch>
{navigation.routes.map((mappedRoute, index) => (
{routes.map((mappedRoute, index) => (
<Route
key={`${mappedRoute.idOrSlug}-${index}`}
path={`${basename}${mappedRoute.idOrSlug}`}
Expand Down
Loading

0 comments on commit af1c9a8

Please sign in to comment.