aboutsummaryrefslogtreecommitdiffstats
path: root/excalidraw-app/collab/CollabError.tsx
blob: 76828cf1a8aa167b369ad5e7e656f7618b1bb240 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Tooltip } from "@excalidraw/excalidraw/components/Tooltip";
import { warning } from "@excalidraw/excalidraw/components/icons";
import clsx from "clsx";
import { useEffect, useRef, useState } from "react";
import { atom } from "../app-jotai";

import "./CollabError.scss";

type ErrorIndicator = {
  message: string | null;
  /** used to rerun the useEffect responsible for animation */
  nonce: number;
};

export const collabErrorIndicatorAtom = atom<ErrorIndicator>({
  message: null,
  nonce: 0,
});

const CollabError = ({ collabError }: { collabError: ErrorIndicator }) => {
  const [isAnimating, setIsAnimating] = useState(false);
  const clearAnimationRef = useRef<string | number>(0);

  useEffect(() => {
    setIsAnimating(true);
    clearAnimationRef.current = window.setTimeout(() => {
      setIsAnimating(false);
    }, 1000);

    return () => {
      window.clearTimeout(clearAnimationRef.current);
    };
  }, [collabError.message, collabError.nonce]);

  if (!collabError.message) {
    return null;
  }

  return (
    <Tooltip label={collabError.message} long={true}>
      <div
        className={clsx("collab-errors-button", {
          "collab-errors-button-shake": isAnimating,
        })}
      >
        {warning}
      </div>
    </Tooltip>
  );
};

CollabError.displayName = "CollabError";

export default CollabError;