File size: 3,393 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React, { useEffect, useRef } from 'react';
import styles from './InstallWindow.module.css';
import { toast } from 'react-toastify';
import { toastOptions } from './Toasts';

interface InstallWindowProps {
  manifestUrl: string | null;
  setManifestUrl: (url: string | null) => void;
}

const InstallWindow: React.FC<InstallWindowProps> = ({
  manifestUrl,
  setManifestUrl,
}) => {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const handleOutSideClick = (event: MouseEvent) => {
      if (!ref.current?.contains(event.target as Node)) {
        setManifestUrl(null);
      }
    };

    window.addEventListener('mousedown', handleOutSideClick);

    return () => {
      window.removeEventListener('mousedown', handleOutSideClick);
    };
  }, [ref, setManifestUrl]);

  return (
    <div
      className={`${styles.popup}${!manifestUrl ? ` ${styles.hidden}` : ''}`}
      ref={ref}
    >
      <h2 style={{ marginBottom: '10px', textAlign: 'center' }}>
        Install Addon
      </h2>
      <p style={{ marginBottom: '10px', textAlign: 'center' }}>
        Click the buttons below to install the addon
      </p>
      <div className={styles.installButtons}>
        <a
          className={styles.installButton}
          href={(manifestUrl || '').replace(/https?:\/\//, 'stremio://')}
          // ensure a tag fills the button
          style={{
            display: 'block',
            width: '100%',
            height: '100%',
            textDecoration: 'none',
            WebkitTapHighlightColor: 'transparent',
          }}
          target="_blank"
        >
          Stremio
        </a>
        <a
          className={styles.installButton}
          target="_blank"
          href={`https://web.stremio.com/#/addons?addon=${encodeURIComponent(manifestUrl || '')}`}
          // ensure a tag fills the button
          style={{
            display: 'block',
            width: '100%',
            height: '100%',
            textDecoration: 'none',
            WebkitTapHighlightColor: 'transparent',
          }}
        >
          Stremio Web
        </a>
        <button
          className={styles.installButton}
          onClick={() => {
            const id = toast.loading('Copying to clipboard...');
            if (!navigator.clipboard) {
              toast.update(id, {
                ...toastOptions,
                type: 'error',
                render: 'Failed to copy to clipboard',
                isLoading: false,
                autoClose: 3000,
              });
              return;
            }

            navigator.clipboard
              .writeText(manifestUrl || '')
              .then(() => {
                toast.update(id, {
                  ...toastOptions,
                  type: 'success',
                  render: 'Copied to clipboard',
                  isLoading: false,
                  autoClose: 3000,
                  toastId: 'copied',
                });
              })
              .catch(() => {
                toast.update(id, {
                  ...toastOptions,
                  type: 'error',
                  render: 'Failed to copy to clipboard',
                  isLoading: false,
                  autoClose: 3000,
                });
              });
          }}
        >
          Copy
        </button>
      </div>
    </div>
  );
};

export default InstallWindow;