Skip to content

Commit

Permalink
fix preview url
Browse files Browse the repository at this point in the history
  • Loading branch information
christineweng committed Jun 27, 2024
1 parent 47e0111 commit 569d0e2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ interface PreviewSectionProps {
* Left position used when rendering the panel
*/
leftPosition: number;
/**
* Display the back button in the header
*/
showBackButton: boolean;
/**
* Preview banner shown at the top of preview panel
*/
Expand All @@ -84,7 +80,6 @@ interface PreviewSectionProps {
*/
export const PreviewSection: React.FC<PreviewSectionProps> = ({
component,
showBackButton,
leftPosition,
banner,
}: PreviewSectionProps) => {
Expand All @@ -103,7 +98,7 @@ export const PreviewSection: React.FC<PreviewSectionProps> = ({
/>
</EuiFlexItem>
);
const header = showBackButton ? (
const header = (
<EuiFlexGroup justifyContent="spaceBetween" responsive={false}>
<EuiFlexItem grow={false}>
<EuiButtonEmpty
Expand All @@ -119,10 +114,6 @@ export const PreviewSection: React.FC<PreviewSectionProps> = ({
</EuiFlexItem>
{closeButton}
</EuiFlexGroup>
) : (
<EuiFlexGroup justifyContent="flexEnd" responsive={false}>
{closeButton}
</EuiFlexGroup>
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { useCallback, useMemo } from 'react';
import { useHistory } from 'react-router-dom';
import { REDUX_ID_FOR_MEMORY_STORAGE } from '../constants';
import { useExpandableFlyoutContext } from '../context';
import {
Expand All @@ -30,6 +31,7 @@ export type { ExpandableFlyoutApi };
*/
export const useExpandableFlyoutApi = () => {
const dispatch = useDispatch();
const history = useHistory();

const { urlKey } = useExpandableFlyoutContext();
// if no urlKey is provided, we are in memory storage mode and use the reserved word 'memory'
Expand Down Expand Up @@ -75,10 +77,13 @@ export const useExpandableFlyoutApi = () => {
[dispatch, id]
);

const previousPreviewPanel = useCallback(
() => dispatch(previousPreviewPanelAction({ id })),
[dispatch, id]
);
const previousPreviewPanel = useCallback(() => {
dispatch(previousPreviewPanelAction({ id }));

if (id !== REDUX_ID_FOR_MEMORY_STORAGE) {
history.goBack();
}
}, [dispatch, id, history]);

const closePanels = useCallback(() => dispatch(closePanelsAction({ id })), [dispatch, id]);

Expand Down
2 changes: 0 additions & 2 deletions packages/kbn-expandable-flyout/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export const ExpandableFlyout: React.FC<ExpandableFlyoutProps> = ({
? mostRecentPreview?.params?.banner
: undefined;

const showBackButton = !!preview && preview.length > 1;
const previewSection = useMemo(
() => registeredPanels.find((panel) => panel.key === mostRecentPreview?.id),
[mostRecentPreview, registeredPanels]
Expand Down Expand Up @@ -129,7 +128,6 @@ export const ExpandableFlyout: React.FC<ExpandableFlyoutProps> = ({
{showPreview ? (
<PreviewSection
component={previewSection.component({ ...(mostRecentPreview as FlyoutPanelProps) })}
showBackButton={showBackButton}
leftPosition={previewSectionLeft}
banner={previewBanner}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-expandable-flyout/src/provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('UrlSynchronizer', () => {
expect(mockSet).toHaveBeenCalledWith('urlKey', {
left: undefined,
right: undefined,
preview: undefined,
preview: [undefined],
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/kbn-expandable-flyout/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export const UrlSynchronizer = () => {
dispatch(
urlChangedAction({
...currentValue,
preview: currentValue?.preview?.[0],
preview: currentValue?.preview?.at(-1),
id: urlKey,
})
);
}

const subscription = urlStorage.change$<FlyoutState>(urlKey).subscribe((value) => {
dispatch(urlChangedAction({ ...value, preview: value?.preview?.[0], id: urlKey }));
dispatch(urlChangedAction({ ...value, preview: value?.preview?.at(-1), id: urlKey }));
});

return () => subscription.unsubscribe();
Expand All @@ -68,7 +68,7 @@ export const UrlSynchronizer = () => {
}

const { left, right, preview } = panels;
urlStorage.set(urlKey, { left, right, preview });
urlStorage.set(urlKey, { left, right, preview: [preview?.at(-1)] });
}, [needsSync, panels, urlKey, urlStorage]);

return null;
Expand Down
8 changes: 4 additions & 4 deletions packages/kbn-expandable-flyout/src/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ describe('reducer', () => {
const action = previousPreviewPanelAction({ id: id1 });
const newState: State = reducer(state, action);

expect(newState).toEqual({ ...initialState, needsSync: true });
expect(newState).toEqual({ ...initialState, needsSync: false });
});

it(`should return unmodified state when previous preview panel when no preview panel exist`, () => {
Expand All @@ -638,7 +638,7 @@ describe('reducer', () => {
const action = previousPreviewPanelAction({ id: id1 });
const newState: State = reducer(state, action);

expect(newState).toEqual({ ...state, needsSync: true });
expect(newState).toEqual({ ...state, needsSync: false });
});

it('should remove only last preview panel', () => {
Expand All @@ -662,7 +662,7 @@ describe('reducer', () => {
preview: [previewPanel1],
},
},
needsSync: true,
needsSync: false,
});
});

Expand All @@ -687,7 +687,7 @@ describe('reducer', () => {
preview: [previewPanel1],
},
},
needsSync: true,
needsSync: false,
});
});
});
Expand Down
10 changes: 8 additions & 2 deletions packages/kbn-expandable-flyout/src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { createReducer } from '@reduxjs/toolkit';
import deepEqual from 'react-fast-compare';
import {
openPanelsAction,
openLeftPanelAction,
Expand Down Expand Up @@ -69,7 +70,11 @@ export const reducer = createReducer(initialState, (builder) => {
builder.addCase(openPreviewPanelAction, (state, { payload: { preview, id } }) => {
if (id in state.byId) {
if (state.byId[id].preview) {
state.byId[id].preview?.push(preview);
const previewIdenticalToLastOne = deepEqual(preview, state.byId[id].preview?.at(-1));
// Only append preview when it does not match the last item in state.byId[id].preview
if (!previewIdenticalToLastOne) {
state.byId[id].preview?.push(preview);
}
} else {
state.byId[id].preview = preview ? [preview] : undefined;
}
Expand All @@ -89,7 +94,8 @@ export const reducer = createReducer(initialState, (builder) => {
state.byId[id].preview?.pop();
}

state.needsSync = true;
// if state is stored in url, click go back in preview should utilize browser history
state.needsSync = false;
});

builder.addCase(closePanelsAction, (state, { payload: { id } }) => {
Expand Down

0 comments on commit 569d0e2

Please sign in to comment.