Skip to content

Commit e498226

Browse files
committed
Don't lose the isOperationInProgress state when the user switches tabs
1 parent ccdfa90 commit e498226

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

packages/compass-shell/src/components/compass-shell/tab-compass-shell.tsx

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,17 @@ type CompassShellProps = {
5858
onHistoryChange: (history: string[]) => void;
5959
initialEvaluate?: string | string[];
6060
initialInput?: string;
61+
isOperationInProgress: boolean;
62+
onOperationStarted: () => void;
63+
onOperationEnd: () => void;
6164
};
6265

63-
function useInitialEval(initialEvaluate?: string | string[]) {
64-
const [initialEvalApplied, setInitialEvalApplied] = useTabState(
65-
'initialEvalApplied',
66-
false
67-
);
68-
useEffect(() => {
69-
setInitialEvalApplied(true);
70-
}, [setInitialEvalApplied]);
71-
return initialEvalApplied ? undefined : initialEvaluate;
72-
}
73-
7466
const Shell = React.forwardRef<ShellType, ShellProps>(function Shell(
75-
{ initialEvaluate: _initialEvaluate, ...props },
67+
{ ...props },
7668
ref
7769
) {
7870
const shellRef = useRef<ShellType | null>(null);
79-
const initialEvaluate = useInitialEval(_initialEvaluate);
71+
8072
const mergeRef = useCallback(
8173
(shell: ShellType | null) => {
8274
shellRef.current = shell;
@@ -93,26 +85,55 @@ const Shell = React.forwardRef<ShellType, ShellProps>(function Shell(
9385
shellRef.current?.focusEditor();
9486
});
9587
}, []);
88+
89+
return <_Shell ref={mergeRef} {...props}></_Shell>;
90+
});
91+
92+
function useInitialEval(initialEvaluate?: string | string[]) {
93+
const [initialEvalApplied, setInitialEvalApplied] = useTabState(
94+
'initialEvalApplied',
95+
false
96+
);
97+
useEffect(() => {
98+
setInitialEvalApplied(true);
99+
}, [setInitialEvalApplied]);
100+
return initialEvalApplied ? undefined : initialEvaluate;
101+
}
102+
103+
const normalizeInitialEvaluate = (initialEvaluate: string | string[]) => {
96104
return (
97-
<_Shell
98-
ref={mergeRef}
99-
initialEvaluate={initialEvaluate}
100-
{...props}
101-
></_Shell>
105+
Array.isArray(initialEvaluate) ? initialEvaluate : [initialEvaluate]
106+
).filter((line) => {
107+
// Filter out empty lines if passed by accident
108+
return !!line;
109+
});
110+
};
111+
112+
const isInitialEvaluateEmpty = (
113+
initialEvaluate?: string | string[] | undefined
114+
) => {
115+
return (
116+
!initialEvaluate || normalizeInitialEvaluate(initialEvaluate).length === 0
102117
);
103-
});
118+
};
104119

105120
const CompassShell: React.FC<CompassShellProps> = ({
106121
runtime,
107122
initialHistory,
108123
onHistoryChange,
109-
initialEvaluate,
124+
initialEvaluate: _initialEvaluate,
110125
initialInput,
111126
}) => {
127+
const initialEvaluate = useInitialEval(_initialEvaluate);
128+
129+
const [isOperationInProgress, setIsOperationInProgress] = useTabState(
130+
'isOperationInProgress',
131+
!isInitialEvaluateEmpty(initialEvaluate)
132+
);
133+
112134
const enableShell = usePreference('enableShell');
113135
const shellRef: ShellRef = useRef(null);
114136
const [infoModalVisible, setInfoModalVisible] = useState(false);
115-
const [isOperationInProgress, setIsOperationInProgress] = useState(false);
116137
const [shellOutput, setShellOutput] = useTabState<
117138
readonly ShellOutputEntry[]
118139
>('shellOutput', []);
@@ -148,13 +169,13 @@ const CompassShell: React.FC<CompassShellProps> = ({
148169
[setShellOutput]
149170
);
150171

151-
const notifyOperationStarted = useCallback(() => {
172+
const onOperationStarted = useCallback(() => {
152173
setIsOperationInProgress(true);
153-
}, []);
174+
}, [setIsOperationInProgress]);
154175

155-
const notifyOperationEnd = useCallback(() => {
176+
const onOperationEnd = useCallback(() => {
156177
setIsOperationInProgress(false);
157-
}, []);
178+
}, [setIsOperationInProgress]);
158179

159180
const canRenderShell = enableShell && initialHistory && runtime;
160181

@@ -212,8 +233,9 @@ const CompassShell: React.FC<CompassShellProps> = ({
212233
onHistoryChanged={(history) => {
213234
onHistoryChange([...history]);
214235
}}
215-
onOperationStarted={notifyOperationStarted}
216-
onOperationEnd={notifyOperationEnd}
236+
onOperationStarted={onOperationStarted}
237+
onOperationEnd={onOperationEnd}
238+
isOperationInProgress={isOperationInProgress}
217239
maxOutputLength={1000}
218240
maxHistoryLength={1000}
219241
/>

packages/compass/webpack.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ module.exports = (_env, args) => {
5757
},
5858
};
5959

60+
const snapshot = {
61+
unmanagedPaths: [
62+
path.resolve('..', '..', 'node_modules', '@mongosh', 'browser-repl'),
63+
],
64+
};
65+
6066
// Having runtime outside of entries means less rebuilding when dependencies
6167
// change (default is runtime is part of the entry and the whole entry needs
6268
// a rebuild when dependency tree changes)
@@ -121,6 +127,7 @@ module.exports = (_env, args) => {
121127
return [
122128
merge(mainConfig, {
123129
cache,
130+
snapshot,
124131
externals,
125132
plugins: [
126133
new webpack.EnvironmentPlugin(hadronEnvConfig),
@@ -137,6 +144,7 @@ module.exports = (_env, args) => {
137144
}),
138145
merge(rendererConfig, {
139146
cache,
147+
snapshot,
140148
// Chunk splitting makes sense only for renderer processes where the
141149
// amount of dependencies is massive and can benefit from them more
142150
optimization,

0 commit comments

Comments
 (0)