Skip to content

Commit

Permalink
fix: Re-running ui code initially rendering the old document
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrunyon committed Nov 13, 2024
1 parent 1343ec8 commit f7f250f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
8 changes: 1 addition & 7 deletions plugins/ui/src/js/src/DashboardPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,7 @@ export function DashboardPlugin(
>(new Map());

const handleWidgetOpen = useCallback(
({
widgetId = nanoid(),
widget,
}: {
widgetId: string;
widget: WidgetDescriptor;
}) => {
({ widgetId, widget }: { widgetId: string; widget: WidgetDescriptor }) => {
log.debug('Opening widget with ID', widgetId, widget);
setWidgetMap(prevWidgetMap => {
const newWidgetMap = new Map(prevWidgetMap);
Expand Down
28 changes: 22 additions & 6 deletions plugins/ui/src/js/src/layout/ReactPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
useLayoutManager,
useListener,
} from '@deephaven/dashboard';
import { View, ViewProps, Flex, FlexProps } from '@deephaven/components';
import {
View,
ViewProps,
Flex,
FlexProps,
LoadingOverlay,
} from '@deephaven/components';
import Log from '@deephaven/log';
import PortalPanel from './PortalPanel';
import { ReactPanelControl, useReactPanel } from './ReactPanelManager';
Expand Down Expand Up @@ -186,6 +192,20 @@ function ReactPanel({
);
const widgetStatus = useWidgetStatus();

let renderedChildren: React.ReactNode;
if (widgetStatus.status === 'loading') {
renderedChildren = (
<>
<LoadingOverlay />
{children}
</>
);
} else if (widgetStatus.status === 'error') {
renderedChildren = <WidgetErrorView error={widgetStatus.error} />;
} else {
renderedChildren = children;
}

return portal
? ReactDOM.createPortal(
<ReactPanelContext.Provider value={panelId}>
Expand Down Expand Up @@ -224,11 +244,7 @@ function ReactPanel({
* Don't render the children if there's an error with the widget. If there's an error with the widget, we can assume the children won't render properly,
* but we still want the panels to appear so things don't disappear/jump around.
*/}
{widgetStatus.status === 'error' ? (
<WidgetErrorView error={widgetStatus.error} />
) : (
children
)}
{renderedChildren}
</ReactPanelErrorBoundary>
</Flex>
</View>
Expand Down
14 changes: 13 additions & 1 deletion plugins/ui/src/js/src/widget/WidgetHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ function WidgetHandler({
initialData: initialDataProp,
}: WidgetHandlerProps): JSX.Element | null {
const { widget, error: widgetError } = useWidget(widgetDescriptor);
const [isLoading, setIsLoading] = useState(true);
const [prevWidget, setPrevWidget] = useState(widget);
// Cannot use usePrevious to change setIsLoading
// Since usePrevious runs in an effect, the value never gets updated if setIsLoading is called during render
if (widget !== prevWidget) {
setPrevWidget(widget);
setIsLoading(true);
}

const [document, setDocument] = useState<ReactNode>();

Expand Down Expand Up @@ -226,6 +234,7 @@ function WidgetHandler({
const newDocument = parseDocument(documentParam);
setInternalError(undefined);
setDocument(newDocument);
setIsLoading(false); // Must go after setDocument since setters are not batched in effects
if (stateParam != null) {
try {
const newState = JSON.parse(stateParam);
Expand Down Expand Up @@ -339,11 +348,14 @@ function WidgetHandler({
if (error != null) {
return { status: 'error', descriptor: widgetDescriptor, error };
}
if (isLoading) {
return { status: 'loading', descriptor: widgetDescriptor };
}
if (renderedDocument != null) {
return { status: 'ready', descriptor: widgetDescriptor };
}
return { status: 'loading', descriptor: widgetDescriptor };
}, [error, renderedDocument, widgetDescriptor]);
}, [error, renderedDocument, widgetDescriptor, isLoading]);

return useMemo(
() =>
Expand Down

0 comments on commit f7f250f

Please sign in to comment.