Skip to content

Commit

Permalink
Update spaces for references on save
Browse files Browse the repository at this point in the history
  • Loading branch information
cqliu1 committed Nov 10, 2023
1 parent 3e64193 commit a130ed0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ export const useDashboardMenuItems = ({
isLabsShown,
setIsLabsShown,
showResetChange,
updateSpacesForReferences,
}: {
redirectTo: DashboardRedirect;
isLabsShown: boolean;
setIsLabsShown: Dispatch<SetStateAction<boolean>>;
showResetChange?: boolean;
updateSpacesForReferences: () => void;
}) => {
const [isSaveInProgress, setIsSaveInProgress] = useState(false);

Expand All @@ -57,7 +59,9 @@ export const useDashboardMenuItems = ({
const hasUnsavedChanges = dashboard.select((state) => state.componentState.hasUnsavedChanges);
const hasOverlays = dashboard.select((state) => state.componentState.hasOverlays);
const lastSavedId = dashboard.select((state) => state.componentState.lastSavedId);
const namespaces = dashboard.select((state) => state.componentState.namespaces);
const dashboardTitle = dashboard.select((state) => state.explicitInput.title);
const dashboardId = dashboard.select((state) => state.explicitInput.id);
const viewMode = dashboard.select((state) => state.explicitInput.viewMode);
const managed = dashboard.select((state) => state.componentState.managed);
const disableTopNav = isSaveInProgress || hasOverlays;
Expand Down Expand Up @@ -98,17 +102,21 @@ export const useDashboardMenuItems = ({
*/
const quickSaveDashboard = useCallback(() => {
setIsSaveInProgress(true);
dashboard
.runQuickSave()
.then(() => setTimeout(() => setIsSaveInProgress(false), CHANGE_CHECK_DEBOUNCE));
}, [dashboard]);
dashboard.runQuickSave().then(() => {
updateSpacesForReferences();
setTimeout(() => setIsSaveInProgress(false), CHANGE_CHECK_DEBOUNCE);
});
}, [dashboard, updateSpacesForReferences]);

/**
* Show the dashboard's save modal
*/
const saveDashboardAs = useCallback(() => {
dashboard.runSaveAs().then((result) => maybeRedirect(result));
}, [maybeRedirect, dashboard]);
dashboard.runSaveAs().then((result) => {
updateSpacesForReferences();
maybeRedirect(result);
});
}, [dashboard, updateSpacesForReferences, maybeRedirect]);

/**
* Clone the dashboard
Expand Down Expand Up @@ -194,7 +202,9 @@ export const useDashboardMenuItems = ({
isLoading: isSaveInProgress,
testId: 'dashboardQuickSaveMenuItem',
disableButton: disableTopNav || !(hasRunMigrations || hasUnsavedChanges),
run: () => quickSaveDashboard(),
run: () => {
quickSaveDashboard();
},
} as TopNavMenuData,

saveAs: {
Expand All @@ -205,7 +215,9 @@ export const useDashboardMenuItems = ({
testId: 'dashboardSaveMenuItem',
iconType: Boolean(lastSavedId) ? undefined : 'save',
label: Boolean(lastSavedId) ? topNavStrings.saveAs.label : topNavStrings.quickSave.label,
run: () => saveDashboardAs(),
run: () => {
saveDashboardAs();
},
} as TopNavMenuData,

switchToViewMode: {
Expand Down Expand Up @@ -241,19 +253,19 @@ export const useDashboardMenuItems = ({
} as TopNavMenuData,
};
}, [
quickSaveDashboard,
disableTopNav,
isSaveInProgress,
hasRunMigrations,
hasUnsavedChanges,
dashboardBackup,
saveDashboardAs,
setIsLabsShown,
disableTopNav,
resetChanges,
isLabsShown,
lastSavedId,
showShare,
dashboard,
setIsLabsShown,
isLabsShown,
dashboardBackup,
quickSaveDashboard,
saveDashboardAs,
resetChanges,
clone,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
* Side Public License, v 1.
*/

import { SpacesContextProps } from '@kbn/spaces-plugin/public';
import React from 'react';
import { DashboardAPIContext } from '../dashboard_app/dashboard_app';
import { DashboardContainer } from '../dashboard_container';
import { pluginServices } from '../services/plugin_services';
import {
InternalDashboardTopNav,
InternalDashboardTopNavProps,
Expand All @@ -17,11 +19,22 @@ export interface DashboardTopNavProps extends InternalDashboardTopNavProps {
dashboardContainer: DashboardContainer;
}

export const DashboardTopNavWithContext = (props: DashboardTopNavProps) => (
<DashboardAPIContext.Provider value={props.dashboardContainer}>
<InternalDashboardTopNav {...props} />
</DashboardAPIContext.Provider>
);
const getEmptyFunctionComponent: React.FC<SpacesContextProps> = ({ children }) => <>{children}</>;

export const DashboardTopNavWithContext = (props: DashboardTopNavProps) => {
const {
spaces: { spacesApi },
} = pluginServices.getServices();
const SpacesContextWrapper =
spacesApi?.ui.components.getSpacesContextProvider ?? getEmptyFunctionComponent;
return (
<SpacesContextWrapper>
<DashboardAPIContext.Provider value={props.dashboardContainer}>
<InternalDashboardTopNav {...props} />
</DashboardAPIContext.Provider>
</SpacesContextWrapper>
);
};

// eslint-disable-next-line import/no-default-export
export default DashboardTopNavWithContext;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import UseUnmount from 'react-use/lib/useUnmount';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import {
withSuspense,
Expand Down Expand Up @@ -82,7 +82,10 @@ export function InternalDashboardTopNav({
embeddable: { getStateTransfer },
initializerContext: { allowByValueEmbeddables },
dashboardCapabilities: { saveQuery: allowSaveQuery, showWriteControls },
spaces: { spacesApi },
} = pluginServices.getServices();
const spacesService = spacesApi?.ui.useSpaces();

const isLabsEnabled = uiSettings.get(UI_SETTINGS.ENABLE_LABS_UI);
const { setHeaderActionMenu, onAppLeave } = useDashboardMountContext();

Expand All @@ -97,6 +100,7 @@ export function InternalDashboardTopNav({
const savedQueryId = dashboard.select((state) => state.componentState.savedQueryId);
const lastSavedId = dashboard.select((state) => state.componentState.lastSavedId);
const focusedPanelId = dashboard.select((state) => state.componentState.focusedPanelId);
const namespaces = dashboard.select((state) => state.componentState.namespaces);
const managed = dashboard.select((state) => state.componentState.managed);

const viewMode = dashboard.select((state) => state.explicitInput.viewMode);
Expand Down Expand Up @@ -266,11 +270,25 @@ export function InternalDashboardTopNav({
viewMode,
]);

const updateSpacesForReferences = useCallback(async () => {
if (!spacesService || !lastSavedId || !namespaces?.length) return;

const { spacesManager } = spacesService;

const shareableReferences = await spacesManager.getShareableReferences([
{ id: lastSavedId, type: 'dashboard' },
]);
const objects = shareableReferences.objects.map(({ id, type }) => ({ id, type }));

spacesManager.updateSavedObjectsSpaces(objects, namespaces, []);
}, [lastSavedId, namespaces, spacesService]);

const { viewModeTopNavConfig, editModeTopNavConfig } = useDashboardMenuItems({
redirectTo,
isLabsShown,
setIsLabsShown,
showResetChange,
updateSpacesForReferences,
});

UseUnmount(() => {
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/dashboard/public/services/spaces/spaces.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const spacesServiceFactory: SpacesServiceFactory = () => {
const pluginMock = spacesPluginMock.createStartContract();

return {
getActiveSpace$: pluginMock.getActiveSpace$,
getLegacyUrlConflict: pluginMock.ui.components.getLegacyUrlConflict,
redirectLegacyUrl: pluginMock.ui.redirectLegacyUrl,
spacesApi: pluginMock,
};
};
16 changes: 2 additions & 14 deletions src/plugins/dashboard/public/services/spaces/spaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@ export type SpacesServiceFactory = KibanaPluginServiceFactory<
>;
export const spacesServiceFactory: SpacesServiceFactory = ({ startPlugins }) => {
const { spaces } = startPlugins;
if (!spaces || !spaces.ui) return {};
if (!spaces) return {};

const {
getActiveSpace$,
ui: {
components: { getLegacyUrlConflict, getSpacesContextProvider },
redirectLegacyUrl,
useSpaces,
},
} = spaces;
return {
getActiveSpace$,
getLegacyUrlConflict,
getSpacesContextProvider,
redirectLegacyUrl,
useSpaces,
spacesApi: spaces,
};
};
8 changes: 2 additions & 6 deletions src/plugins/dashboard/public/services/spaces/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
* Side Public License, v 1.
*/

import type { SpacesPluginStart } from '@kbn/spaces-plugin/public';
import type { SpacesApi } from '@kbn/spaces-plugin/public';

export interface DashboardSpacesService {
getActiveSpace$?: SpacesPluginStart['getActiveSpace$'];
getLegacyUrlConflict?: SpacesPluginStart['ui']['components']['getLegacyUrlConflict'];
getSpacesContextProvider?: SpacesPluginStart['ui']['components']['getSpacesContextProvider'];
redirectLegacyUrl?: SpacesPluginStart['ui']['redirectLegacyUrl'];
useSpaces?: SpacesPluginStart['ui']['useSpaces'];
spacesApi?: SpacesApi;
}

0 comments on commit a130ed0

Please sign in to comment.