Merge pull request #31085 from Speissi/home-wait-for-loading
Wait for storage before loading the custom homepage layout
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@backstage/plugin-home': patch
|
||||
---
|
||||
|
||||
Fixed race condition in CustomHomepageGrid by waiting for storage to load before rendering custom layout to prevent
|
||||
rendering of the default content.
|
||||
@@ -34,7 +34,11 @@ import {
|
||||
} from '@material-ui/core/styles';
|
||||
import { compact } from 'lodash';
|
||||
import useObservable from 'react-use/esm/useObservable';
|
||||
import { ContentHeader, ErrorBoundary } from '@backstage/core-components';
|
||||
import {
|
||||
ContentHeader,
|
||||
ErrorBoundary,
|
||||
Progress,
|
||||
} from '@backstage/core-components';
|
||||
import Typography from '@material-ui/core/Typography';
|
||||
import { WidgetSettingsOverlay } from './WidgetSettingsOverlay';
|
||||
import { AddWidgetDialog } from './AddWidgetDialog';
|
||||
@@ -90,7 +94,7 @@ const useStyles = makeStyles((theme: Theme) =>
|
||||
|
||||
function useHomeStorage(
|
||||
defaultWidgets: GridWidget[],
|
||||
): [GridWidget[], (value: GridWidget[]) => void] {
|
||||
): [GridWidget[], (value: GridWidget[]) => void, boolean] {
|
||||
const key = 'home';
|
||||
const storageApi = useApi(storageApiRef).forBucket('home.customHomepage');
|
||||
// TODO: Support multiple home pages
|
||||
@@ -110,6 +114,9 @@ function useHomeStorage(
|
||||
storageApi.observe$<string>(key),
|
||||
storageApi.snapshot(key),
|
||||
);
|
||||
|
||||
const isStorageLoading = homeSnapshot.presence === 'unknown' || !homeSnapshot;
|
||||
|
||||
const widgets: GridWidget[] = useMemo(() => {
|
||||
if (homeSnapshot.presence === 'absent') {
|
||||
return defaultWidgets;
|
||||
@@ -122,7 +129,7 @@ function useHomeStorage(
|
||||
}
|
||||
}, [homeSnapshot, defaultWidgets]);
|
||||
|
||||
return [widgets, setWidgets];
|
||||
return [widgets, setWidgets, isStorageLoading];
|
||||
}
|
||||
|
||||
const convertConfigToDefaultWidgets = (
|
||||
@@ -213,7 +220,7 @@ export const CustomHomepageGrid = (props: CustomHomepageGridProps) => {
|
||||
? convertConfigToDefaultWidgets(props.config, availableWidgets)
|
||||
: [];
|
||||
}, [props.config, availableWidgets]);
|
||||
const [widgets, setWidgets] = useHomeStorage(defaultLayout);
|
||||
const [widgets, setWidgets, isStorageLoading] = useHomeStorage(defaultLayout);
|
||||
const [addWidgetDialogOpen, setAddWidgetDialogOpen] = useState(false);
|
||||
const editModeOn = widgets.find(w => w.layout.isResizable) !== undefined;
|
||||
const [editMode, setEditMode] = useState(editModeOn);
|
||||
@@ -322,6 +329,10 @@ export const CustomHomepageGrid = (props: CustomHomepageGridProps) => {
|
||||
);
|
||||
};
|
||||
|
||||
if (isStorageLoading) {
|
||||
return <Progress />;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<ContentHeader title={props.title}>
|
||||
|
||||
Reference in New Issue
Block a user