File size: 1,316 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { FC, useEffect } from 'react';
import { toastType } from '@/interface';
import { defaultToastPosition } from '@/core/config';
import { createRoot } from 'react-dom/client';
import './toast.scss';

const removeToast = () => {
  if (document.querySelector('#jolPlayerToast')) {
    document
      .querySelector('#JoL-player-container')!
      .removeChild(document.querySelector('#jolPlayerToast')!);
  }
};
const Toast: FC<toastType> = function Toast({ message, duration, position }) {
  const close = () => {
    removeToast();
  };
  useEffect(() => {
    let timer: NodeJS.Timeout | null = null;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      close();
    }, duration || 2000);
    return () => {
      timer && clearTimeout(timer);
    };
  }, []);
  return <div className={`jolPlayerToast ${position || defaultToastPosition}`}>{message}</div>;
};

const toast = (option: toastType) => {
  const jolPlayerToast = document.querySelector('#jolPlayerToast');
  if (!jolPlayerToast) {
    const container = document.createElement('div');
    container.id = 'jolPlayerToast';
    document.querySelector('#JoL-player-container')!.appendChild(container);
    const root = createRoot(container);
    root.render(<Toast {...option} />);
  }
};
export default toast;