Skip to content

Commit

Permalink
Merge pull request #2299 from shanberg/reuse-comments-toast
Browse files Browse the repository at this point in the history
Reuse comments toast
  • Loading branch information
tangjeff0 authored Aug 20, 2022
2 parents 9c45bfc + b4bd9a6 commit 42b69cb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/js/components/AppToolbar/AppToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { LayoutContext, layoutAnimationProps, layoutAnimationTransition } from "
import { FakeTrafficLights } from './components/FakeTrafficLights';
import { WindowButtons } from './components/WindowButtons';
import { LocationIndicator } from './components/LocationIndicator';
import { reusableToast } from '@/utils/reusableToast';

const PAGE_TITLE_SHOW_HEIGHT = 24;

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

const toast = useToast();
const commentsToggleToastRef = React.useRef(null);

// add event listener to detect when the user scrolls past the title
React.useLayoutEffect(() => {
Expand Down Expand Up @@ -226,7 +228,7 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {
onClick: () => {
if (isShowComments) {
handleClickComments();
toast({
reusableToast(toast, commentsToggleToastRef, {
title: "Comments hidden",
status: "info",
duration: 5000,
Expand All @@ -235,9 +237,8 @@ export const AppToolbar = (props: AppToolbarProps): React.ReactElement => {

} else {
handleClickComments();
toast({
reusableToast(toast, commentsToggleToastRef, {
title: "Comments shown",
status: "info",
duration: 5000,
position: "top-right"
});
Expand Down
25 changes: 25 additions & 0 deletions src/js/components/utils/reusableToast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Creates or updates a toast message with the given options.
* @param toast The toast function
* @param ref The ref to store the toast
* @param props
*/
export const reusableToast = (toast, ref, props) => {
if (ref.current) {
toast.update(ref.current, {
...props,
onCloseComplete: () => {
props.onCloseComplete && props.onCloseComplete();
ref.current = null;
}
});
} else {
ref.current = toast({
...props,
onCloseComplete: () => {
props.onCloseComplete && props.onCloseComplete();
ref.current = null;
}
});
}
}

0 comments on commit 42b69cb

Please sign in to comment.