File size: 5,491 Bytes
dbcdc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import clsx from 'clsx';
import { useCallback, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useOptions } from '/src/utils/optionsContext';
import loaderStore from '/src/utils/hooks/loader/useLoaderStore';
import Zoom from './menu/Zoom';
import Bookmarks from '../Bookmarks';

const devTools = (fr) => {
  if (!fr?.contentWindow || !fr?.contentDocument) return;

  try {
    const doc = fr.contentDocument;
    const win = fr.contentWindow;
    const erudaEl = doc.getElementById('eruda');

    if (erudaEl?.shadowRoot) {
      win.eruda?.destroy();
      return;
    }

    const s = doc.createElement('script');
    s.src = 'https://cdn.jsdelivr.net/npm/eruda';
    s.onload = () => {
      win.eruda?.init();
      win.eruda?.show();
      win.eruda?.show('elements');
      setTimeout(() => {
        const root = doc.getElementById('eruda')?.shadowRoot;
        root?.querySelector('div.eruda-entry-btn')?.remove();
      }, 100);
    };
    doc.body.appendChild(s);
  } catch (e) {
    console.error(e);
  }
};

export default function Menu() {
  const {
    showMenu,
    toggleMenu,
    tabs,
    addTab,
    setActive,
    removeTab,
    showTabs,
    activeFrameRef,
    toggleUI,
  } = loaderStore();
  const { options } = useOptions();
  const nav = useNavigate();
  const [showBookmarks, setShowBm] = useState(false);

  const newTab = useCallback(() => {
    if (tabs.length < 20) {
      let uuid = crypto.randomUUID();
      addTab({
        title: 'New Tab',
        id: uuid,
        url: 'tabs://new',
      });
      setActive(uuid);
    }
  }, [tabs.length]);

  const fs = useCallback(() => {
    activeFrameRef?.current && activeFrameRef.current?.requestFullscreen?.();
  }, [activeFrameRef]);

  const clearTabs = useCallback(() => {
    tabs.forEach((tab) => removeTab(tab.id));
    newTab();
  }, [tabs, removeTab, newTab]);

  const togEruda = useCallback(() => {
    activeFrameRef?.current && devTools(activeFrameRef.current);
  }, [activeFrameRef]);

  const items = [
    {
      name: 'New Tab',
      shortcut: 'alt + n',
      fn: newTab,
    },
    {
      name: 'Clear Tabs',
      shortcut: 'alt + c',
      fn: clearTabs,
    },
    { name: 'Bookmarks', shortcut: 'alt + b', fn: () => setShowBm(true) },
    {
      name: 'Fullscreen',
      shortcut: 'shift + f',
      fn: fs,
      disabled: !activeFrameRef?.current,
    },
    { name: 'zoom-cmpn', isComponent: true, divider: true },
    { name: 'Hide UI', shortcut: 'alt + z', fn: toggleUI },
    {
      name: 'DevTools',
      shortcut: 'alt + i',
      fn: togEruda,
      disabled: !activeFrameRef?.current,
      divider: true,
    },
    { name: 'Return Home', fn: () => nav('/') },
  ];

  useEffect(() => {
    const shortcuts = {
      'alt+n': newTab,
      'alt+c': clearTabs,
      'shift+F': () => activeFrameRef?.current && fs(),
      'alt+z': toggleUI,
      'alt+i': togEruda,
      'alt+b': () => setShowBm(true),
    };

    const handleKey = (e) => {
      const key = `${e.altKey ? 'alt+' : ''}${e.shiftKey ? 'shift+' : ''}${e.key}`;
      const action = shortcuts[key];

      if (action) {
        e.preventDefault();
        action();
      }
    };

    window.addEventListener('keydown', handleKey);
    return () => window.removeEventListener('keydown', handleKey);
  }, [newTab, clearTabs, fs, activeFrameRef, toggleUI, togEruda]);

  const cnt = clsx(
    'absolute right-2 w-56 rounded-lg shadow-lg overflow-hidden text-sm z-50',
    'border transition-all duration-200 origin-top-right',
    showTabs ? 'mt-21' : 'mt-11',
    showMenu
      ? 'scale-100 opacity-100 pointer-events-auto'
      : 'scale-95 opacity-0 pointer-events-none',
  );

  const item = clsx(
    'w-full flex justify-between items-center text-left text-[0.8rem] px-3 py-2 focus:outline-none',
    options.type === 'light' ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]',
  );

  return (
    <>
      <Bookmarks isOpen={showBookmarks} onClose={() => setShowBm(false)} inLoader={true} />
      <div className={cnt} style={{ backgroundColor: options.menuColor || '#1a252f' }}>
        {items.map(
          (
            {
              name,
              shortcut = null,
              divider = null,
              fn = null,
              disabled = false,
              isComponent = false,
            },
            id,
          ) => (
            <div
              key={id}
              disabled={disabled}
              className={clsx(disabled ? 'opacity-50 pointer-events-none' : '')}
            >
              {isComponent ? (
                <Zoom />
              ) : (
                <button
                  type="button"
                  onClick={() => {
                    !disabled && fn();
                    showMenu && toggleMenu();
                  }}
                  className={item}
                >
                  <span>{name}</span>
                  {shortcut && (
                    <span className="text-[0.7rem] text-gray-500 dark:text-gray-400">
                      {shortcut}
                    </span>
                  )}
                </button>
              )}
              {divider && (
                <hr
                  className={clsx(
                    'border-t',
                    options.type === 'light' ? 'border-gray-300' : 'border-gray-700',
                  )}
                />
              )}
            </div>
          ),
        )}
      </div>
    </>
  );
}