Skip to content

Commit 70191ac

Browse files
committed
Rework error snackbar for header scripts to use toasts
1 parent 7ca2f60 commit 70191ac

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

packages/ui/src/results/useResultRunner.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useContext, useState } from 'react';
2-
2+
import { isObjectLike } from 'lodash-es';
33
import { GlobalStore } from 'app/globalContext';
44

55
import { ClobbrLogItem } from '@clobbr/api/src/models/ClobbrLog';
@@ -8,6 +8,7 @@ import { Everbs } from 'shared/enums/http';
88
import { ClobbrUIHeaderItem } from 'models/ClobbrUIHeaderItem';
99
import { ClobbrUIProperties } from 'models/ClobbrUIProperties';
1010
import { HEADER_MODES } from 'search/SearchSettings/HeaderSettings/HeaderSettings';
11+
import { useToastStore } from 'toasts/state/toastStore';
1112

1213
import { run } from '@clobbr/api';
1314

@@ -45,7 +46,7 @@ export const useResultRunner = ({
4546
}) => {
4647
const globalStore = useContext(GlobalStore);
4748

48-
const [headerError, setHeaderError] = useState<string>('');
49+
const addToast = useToastStore((state) => state.addToast);
4950

5051
const runEventCallback = useCallback(
5152
(itemId: string) => {
@@ -131,9 +132,10 @@ export const useResultRunner = ({
131132
options.headers = parsedJson;
132133
}
133134
} catch (error) {
134-
setHeaderError(
135-
'Header shell script failed. Using default headers.'
136-
);
135+
addToast({
136+
message: 'Header shell script failed. Using default headers.',
137+
type: 'error'
138+
});
137139
}
138140
}
139141
}
@@ -152,12 +154,24 @@ export const useResultRunner = ({
152154
}
153155

154156
const parsedJson = JSON.parse(result);
157+
158+
if (!isObjectLike(parsedJson)) {
159+
addToast({
160+
message:
161+
'Header node.js script did not return a JSON object. Using default headers.',
162+
type: 'warning'
163+
});
164+
}
165+
155166
options.headers = parsedJson;
156167
} catch (error) {
157168
console.error(error);
158-
setHeaderError(
159-
'Header node.js script failed to run. Using default headers.'
160-
);
169+
170+
addToast({
171+
message:
172+
'Header node.js script failed to run. Using default headers.',
173+
type: 'error'
174+
});
161175
}
162176
}
163177

@@ -195,9 +209,6 @@ export const useResultRunner = ({
195209
);
196210

197211
return {
198-
startRun,
199-
200-
headerError,
201-
setHeaderError
212+
startRun
202213
};
203214
};

packages/ui/src/search/Search/Search.tsx

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ import {
1616
Tooltip,
1717
SelectChangeEvent,
1818
Typography,
19-
CircularProgress,
20-
Alert,
21-
Snackbar
19+
CircularProgress
2220
} from '@mui/material';
23-
import { Close } from '@mui/icons-material';
2421
import { GlobalStore } from 'app/globalContext';
2522

2623
import { ReactComponent as ParallelIcon } from 'shared/icons/Parallel.svg';
@@ -93,6 +90,7 @@ const Search = forwardRef(
9390
ref: React.ForwardedRef<HTMLDivElement>
9491
) => {
9592
const globalStore = useContext(GlobalStore);
93+
9694
const [wssUrl, setWssUrl] = useState<string>('ws://localhost');
9795

9896
const getSocketUrl = useCallback((): Promise<string> => {
@@ -107,7 +105,7 @@ const Search = forwardRef(
107105

108106
const wsReady = readyState === ReadyState.OPEN;
109107

110-
const { startRun, headerError, setHeaderError } = useResultRunner({
108+
const { startRun } = useResultRunner({
111109
requestUrl: globalStore.search.url.requestUrl,
112110
parallel: globalStore.search.parallel,
113111
iterations: globalStore.search.iterations,
@@ -152,8 +150,6 @@ const Search = forwardRef(
152150
[globalStore.search, globalStore.results]
153151
);
154152

155-
const dismissHeaderErrorToast = () => setHeaderError('');
156-
157153
const settingsAnimations = {
158154
animate:
159155
inputFocused || globalStore.search.url.requestUrl ? 'shown' : 'hidden',
@@ -401,34 +397,6 @@ const Search = forwardRef(
401397
) : (
402398
''
403399
)}
404-
405-
<Snackbar
406-
open={!!headerError}
407-
autoHideDuration={6000}
408-
onClose={dismissHeaderErrorToast}
409-
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
410-
className="pointer-events-none"
411-
>
412-
<Alert
413-
className="bg-red-200/80 dark:bg-red-900/80 backdrop-blur-sm mb-10 pointer-events-auto"
414-
onClose={dismissHeaderErrorToast}
415-
severity="error"
416-
icon={false}
417-
sx={{ width: '100%' }}
418-
action={
419-
<IconButton
420-
aria-label="Dismiss"
421-
onClick={dismissHeaderErrorToast}
422-
color="inherit"
423-
className="!mb-1"
424-
>
425-
<Close />
426-
</IconButton>
427-
}
428-
>
429-
<p className="flex h-full items-center">{headerError}</p>
430-
</Alert>
431-
</Snackbar>
432400
</section>
433401
)}
434402
</GlobalStore.Consumer>

0 commit comments

Comments
 (0)