Skip to content

Commit

Permalink
fix: Unedited markdown widgets not persisting (#2019)
Browse files Browse the repository at this point in the history
Resolves #1777

Changes Implemented:
- Added checks, modified hydration logic, and added new conditions for what is considered a closed markdown, to ensure proper persistence of markdown widget
  • Loading branch information
AkshatJawne authored May 24, 2024
1 parent 3115dd1 commit c17f136
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
35 changes: 3 additions & 32 deletions packages/dashboard-core-plugins/src/MarkdownPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { nanoid } from 'nanoid';
import {
assertIsDashboardPluginProps,
DashboardPluginComponentProps,
dehydrate,
hydrate,
LayoutUtils,
useListener,
} from '@deephaven/dashboard';
Expand All @@ -21,24 +19,6 @@ export type MarkdownComponentState = {
export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
assertIsDashboardPluginProps(props);
const { id, layout, panelManager, registerComponent } = props;
const dehydrateMarkdown = useCallback(config => {
const { title, componentState, props: configProps } = config;
let { panelState = null }: MarkdownComponentState = configProps;
if (componentState != null) {
({ panelState = null } = componentState as MarkdownComponentState);
}
if (
title == null ||
panelState == null ||
panelState.content == null ||
panelState.content.length === 0 ||
panelState.content === MarkdownUtils.DEFAULT_CONTENT
) {
// We don't want to save it if there's no content
return null;
}
return dehydrate(config);
}, []);

const handleOpen = useCallback(
({
Expand All @@ -52,16 +32,12 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
const openedMarkdowns = panelManager.getOpenedPanelConfigsOfType(
MarkdownPanel.COMPONENT
);
const closedMarkdowns = panelManager.getClosedPanelConfigsOfType(
MarkdownPanel.COMPONENT
);
const usedTitles = openedMarkdowns.map(markdown => markdown.title ?? '');
const panelTitle =
title != null && title !== ''
? title
: MarkdownUtils.getNewMarkdownTitle(usedTitles);
const content =
closedMarkdowns.length > 0 ? null : MarkdownUtils.DEFAULT_CONTENT;
const content = null;
const config = {
type: 'react-component' as const,
component: MarkdownPanel.COMPONENT,
Expand Down Expand Up @@ -90,19 +66,14 @@ export function MarkdownPlugin(props: MarkdownPluginProps): JSX.Element | null {
useEffect(
function registerComponentsAndReturnCleanup() {
const cleanups = [
registerComponent(
MarkdownPanel.COMPONENT,
MarkdownPanel,
hydrate,
dehydrateMarkdown
),
registerComponent(MarkdownPanel.COMPONENT, MarkdownPanel),
];

return () => {
cleanups.forEach(cleanup => cleanup());
};
},
[dehydrateMarkdown, registerComponent]
[registerComponent]
);

useListener(layout.eventHub, MarkdownEvent.OPEN, handleOpen);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ClosedPanels } from '@deephaven/dashboard';
import memoize from 'memoize-one';

class MarkdownUtils {
static DEFAULT_TITLE = 'Note';

Expand All @@ -19,6 +22,17 @@ class MarkdownUtils {

return title;
}

static getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
closedPanels
.filter(
panel =>
panel.component === 'MarkdownPanel' &&
panel.props.panelState.content !== '' &&
panel.props.panelState.content !== null
)
.reverse()
);
}

export default MarkdownUtils;
18 changes: 11 additions & 7 deletions packages/dashboard-core-plugins/src/panels/MarkdownPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import memoize from 'memoize-one';
import { connect } from 'react-redux';
import {
ClosedPanel,
ClosedPanels,
DashboardPanelProps,
getClosedPanelsForDashboard,
LayoutUtils,
Expand All @@ -27,6 +26,7 @@ import MarkdownContainer from '../controls/markdown/MarkdownContainer';
import MarkdownStartPage from '../controls/markdown/MarkdownStartPage';
import './MarkdownPanel.scss';
import type MarkdownEditorType from '../controls/markdown/MarkdownEditor';
import MarkdownUtils from '../controls/markdown/MarkdownUtils';

const MarkdownEditor = lazy(
() => import('../controls/markdown/MarkdownEditor')
Expand Down Expand Up @@ -77,11 +77,17 @@ export class MarkdownPanel extends Component<
const { panelState } = props;
let content = null;
if (panelState != null && panelState.content != null) {
({ content } = panelState);
if (panelState.content !== '') {
({ content } = panelState);
} else {
content = MarkdownUtils.DEFAULT_CONTENT;
}
}

this.state = {
isStartPageShown: content == null,
isStartPageShown:
content == null &&
this.getClosedMarkdowns(props.closedPanels).length !== 0,
isEditing: false,
content,

Expand Down Expand Up @@ -123,9 +129,7 @@ export class MarkdownPanel extends Component<
}
}

getClosedMarkdowns = memoize((closedPanels: ClosedPanels) =>
closedPanels.filter(panel => panel.component === 'MarkdownPanel').reverse()
);
getClosedMarkdowns = memoize(MarkdownUtils.getClosedMarkdowns);

handleContainerDoubleClick(event: MouseEvent<Element>): void {
const { isEditing } = this.state;
Expand Down Expand Up @@ -242,7 +246,7 @@ export class MarkdownPanel extends Component<
this.markdownEditor = markdownEditor;
}}
isEditing={isEditing}
content={content ?? undefined}
content={content ?? MarkdownUtils.DEFAULT_CONTENT}
onEditorInitialized={this.handleEditorInitialized}
/>
</Suspense>
Expand Down
8 changes: 5 additions & 3 deletions packages/dashboard/src/PanelManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,11 @@ class PanelManager {
config.component,
config
);
(dehydratedConfig as ClosedPanel).parentStackId =
LayoutUtils.getStackForConfig(root, config)?.config.id;
this.closed.push(dehydratedConfig);
if (dehydratedConfig != null) {
(dehydratedConfig as ClosedPanel).parentStackId =
LayoutUtils.getStackForConfig(root, config)?.config.id;
this.closed.push(dehydratedConfig);
}
}
}

Expand Down

0 comments on commit c17f136

Please sign in to comment.