Skip to content

Commit 3dfbb38

Browse files
committed
poll only if the plugin title is visible
1 parent 9534064 commit 3dfbb38

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

packages/compass-global-writes/src/plugin-title.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { connect } from 'react-redux';
2-
import React from 'react';
3-
import { type RootState, ShardingStatuses } from './store/reducer';
2+
import React, { useEffect } from 'react';
3+
import {
4+
type RootState,
5+
setPluginTitleVisibility,
6+
ShardingStatuses,
7+
} from './store/reducer';
48
import {
59
Body,
610
css,
@@ -30,8 +34,20 @@ const iconStylesDark = css({
3034
color: palette.yellow.base,
3135
});
3236

33-
const PluginTitle = ({ showWarning }: { showWarning: boolean }) => {
37+
const PluginTitle = ({
38+
showWarning,
39+
onVisibilityChanged,
40+
}: {
41+
showWarning: boolean;
42+
onVisibilityChanged: (isVisible: boolean) => void;
43+
}) => {
3444
const darkMode = useDarkMode();
45+
useEffect(() => {
46+
onVisibilityChanged(true);
47+
return () => {
48+
onVisibilityChanged(false);
49+
};
50+
});
3551
return (
3652
<div data-testid="global-writes-tab-title" className={containerStyles}>
3753
Global Writes{' '}
@@ -74,5 +90,8 @@ const PluginTitle = ({ showWarning }: { showWarning: boolean }) => {
7490
export const GlobalWritesTabTitle = connect(
7591
({ managedNamespace, status }: RootState) => ({
7692
showWarning: !managedNamespace && status !== ShardingStatuses.NOT_READY,
77-
})
93+
}),
94+
{
95+
onVisibilityChanged: setPluginTitleVisibility,
96+
}
7897
)(PluginTitle);

packages/compass-global-writes/src/store/reducer.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ enum GlobalWritesActionTypes {
4646
UnmanagingNamespaceStarted = 'global-writes/UnmanagingNamespaceStarted',
4747
UnmanagingNamespaceFinished = 'global-writes/UnmanagingNamespaceFinished',
4848
UnmanagingNamespaceErrored = 'global-writes/UnmanagingNamespaceErrored',
49+
50+
PluginTitleVisibilityChanged = 'global-writes/PluginTitleVisibilityChanged',
4951
}
5052

5153
type ManagedNamespaceFetchedAction = {
@@ -115,6 +117,11 @@ type UnmanagingNamespaceErroredAction = {
115117
type: GlobalWritesActionTypes.UnmanagingNamespaceErrored;
116118
};
117119

120+
type PluginTitleVisibilityChangedAction = {
121+
type: GlobalWritesActionTypes.PluginTitleVisibilityChanged;
122+
isVisible: boolean;
123+
};
124+
118125
export enum ShardingStatuses {
119126
/**
120127
* Initial status, no information available yet.
@@ -193,6 +200,7 @@ export type RootState = {
193200
namespace: string;
194201
managedNamespace?: ManagedNamespace;
195202
shardZones: ShardZoneData[];
203+
isPluginTitleVisible: boolean;
196204
} & (
197205
| {
198206
status: ShardingStatuses.NOT_READY;
@@ -245,6 +253,7 @@ const initialState: RootState = {
245253
namespace: '',
246254
status: ShardingStatuses.NOT_READY,
247255
shardZones: [],
256+
isPluginTitleVisible: true,
248257
};
249258

250259
const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
@@ -472,6 +481,19 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
472481
};
473482
}
474483

484+
if (
485+
isAction<PluginTitleVisibilityChangedAction>(
486+
action,
487+
GlobalWritesActionTypes.PluginTitleVisibilityChanged
488+
)
489+
) {
490+
if (state.isPluginTitleVisible === action.isVisible) return state;
491+
return {
492+
...state,
493+
isPluginTitleVisible: action.isVisible,
494+
};
495+
}
496+
475497
return state;
476498
};
477499

@@ -646,7 +668,11 @@ const pollForShardKey = (): GlobalWritesThunkAction<
646668
NextPollingTimeoutSetAction
647669
> => {
648670
return (dispatch, getState) => {
649-
if (getState().pollingTimeout) {
671+
const { pollingTimeout, isPluginTitleVisible } = getState();
672+
if (
673+
!isPluginTitleVisible || // user is not in the Collection Workspace
674+
pollingTimeout // prevent double polling
675+
) {
650676
return;
651677
}
652678
const timeout = setTimeout(
@@ -753,6 +779,30 @@ export const fetchShardingZones = (): GlobalWritesThunkAction<
753779
};
754780
};
755781

782+
export const setPluginTitleVisibility = (
783+
isVisible: boolean
784+
): GlobalWritesThunkAction<void, PluginTitleVisibilityChangedAction> => {
785+
return (dispatch, getState) => {
786+
const {
787+
status,
788+
pollingTimeout,
789+
isPluginTitleVisible: previousIsVisible,
790+
} = getState();
791+
dispatch({
792+
type: GlobalWritesActionTypes.PluginTitleVisibilityChanged,
793+
isVisible,
794+
});
795+
if (
796+
isVisible &&
797+
!previousIsVisible &&
798+
status === ShardingStatuses.SHARDING &&
799+
!pollingTimeout
800+
) {
801+
dispatch(pollForShardKey());
802+
}
803+
};
804+
};
805+
756806
export const unmanageNamespace = (): GlobalWritesThunkAction<
757807
Promise<void>,
758808
| UnmanagingNamespaceStartedAction

0 commit comments

Comments
 (0)