Skip to content

Commit 42b69cb

Browse files
authored
Merge pull request #2299 from shanberg/reuse-comments-toast
Reuse comments toast
2 parents 9c45bfc + b4bd9a6 commit 42b69cb

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/js/components/AppToolbar/AppToolbar.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { LayoutContext, layoutAnimationProps, layoutAnimationTransition } from "
3636
import { FakeTrafficLights } from './components/FakeTrafficLights';
3737
import { WindowButtons } from './components/WindowButtons';
3838
import { LocationIndicator } from './components/LocationIndicator';
39+
import { reusableToast } from '@/utils/reusableToast';
3940

4041
const PAGE_TITLE_SHOW_HEIGHT = 24;
4142

@@ -187,6 +188,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
187188
const [isScrolledPastTitle, setIsScrolledPastTitle] = React.useState(null);
188189

189190
const toast = useToast();
191+
const commentsToggleToastRef = React.useRef(null);
190192

191193
// add event listener to detect when the user scrolls past the title
192194
React.useLayoutEffect(() => {
@@ -226,7 +228,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
226228
onClick: () => {
227229
if (isShowComments) {
228230
handleClickComments();
229-
toast({
231+
reusableToast(toast, commentsToggleToastRef, {
230232
title: "Comments hidden",
231233
status: "info",
232234
duration: 5000,
@@ -235,9 +237,8 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
235237

236238
} else {
237239
handleClickComments();
238-
toast({
240+
reusableToast(toast, commentsToggleToastRef, {
239241
title: "Comments shown",
240-
status: "info",
241242
duration: 5000,
242243
position: "top-right"
243244
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Creates or updates a toast message with the given options.
3+
* @param toast The toast function
4+
* @param ref The ref to store the toast
5+
* @param props
6+
*/
7+
export const reusableToast = (toast, ref, props) => {
8+
if (ref.current) {
9+
toast.update(ref.current, {
10+
...props,
11+
onCloseComplete: () => {
12+
props.onCloseComplete && props.onCloseComplete();
13+
ref.current = null;
14+
}
15+
});
16+
} else {
17+
ref.current = toast({
18+
...props,
19+
onCloseComplete: () => {
20+
props.onCloseComplete && props.onCloseComplete();
21+
ref.current = null;
22+
}
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)