File size: 9,593 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import clsx from 'clsx';
import {
  X, Plus, Globe, Trash2, Pencil, Check, Image, PenLine,
} from 'lucide-react';
import { useState, useEffect } from 'react';
import { createPortal } from 'react-dom';
import { useNavigate } from 'react-router-dom';
import { useOptions } from '../utils/optionsContext';
import loaderStore from '../utils/hooks/loader/useLoaderStore';
import { process } from '../utils/hooks/loader/utils';

const Bookmarks = ({ isOpen, onClose, inLoader = false }) => {
  const { options: o, updateOption } = useOptions();
  const nav = useNavigate();
  const { tabs, updateUrl, setLoading } = loaderStore();

  const [bms, setBms] = useState([]);
  const [url, setUrl] = useState('');
  const [nm, setNm] = useState('');
  const [ico, setIco] = useState('');
  const [editId, setEditId] = useState(null);
  const [showForm, setShowForm] = useState(false);
  const [anim, setAnim] = useState(false);
  const [render, setRender] = useState(false);
  const [badIcons, setBadIcons] = useState(new Set());
  const [loadedIcons, setLoadedIcons] = useState(new Set());

  useEffect(() => {
    if (isOpen) {
      setRender(true);
      setTimeout(() => setAnim(true), 10);
    } else {
      setAnim(false);
      setTimeout(() => setRender(false), 200);
    }
  }, [isOpen]);

  useEffect(() => setBms(o.bookmarks || []), [o.bookmarks]);

  const save = v => {
    setBms(v);
    updateOption({ bookmarks: v });
  };

  const uniq = (base, skip) => {
    let i = 1, n = base;
    const names = bms.filter(b => b.id !== skip).map(b => b.name);
    while (names.includes(n)) n = `${base} (${i++})`;
    return n;
  };

  const reset = () => {
    setUrl(''); setNm(''); setIco('');
    setEditId(null); setShowForm(false);
  };

  const add = () => {
    if (!url.trim()) return;
    const regex = u => (/^(https?:)?\/\//.test(u) ? u : `https://${u}`);
    save([...bms, {
      id: crypto.randomUUID(),
      name: uniq(nm.trim() || 'New Bookmark'),
      url: regex(url.trim()),
      icon: ico.trim() ? regex(ico.trim()) : null,
    }]);
    reset();
  };

  const update = () => {
    if (!url.trim() || !editId) return;
    const regex = u => (/^(https?:)?\/\//.test(u) ? u : `https://${u}`);
    const oldBm = bms.find(b => b.id === editId);
    const newIcon = ico.trim() ? regex(ico.trim()) : null;
    
    if (oldBm && oldBm.icon !== newIcon) {
      setLoadedIcons(p => {
        const n = new Set(p);
        n.delete(editId);
        return n;
      });
      setBadIcons(p => {
        const n = new Set(p);
        n.delete(editId);
        return n;
      });
    }
    
    save(bms.map(b =>
      b.id === editId
        ? {
            ...b,
            name: uniq(nm.trim() || 'New Bookmark', editId),
            url: regex(url.trim()),
            icon: newIcon,
          }
        : b
    ));
    reset();
  };

  const edit = b => {
    setEditId(b.id);
    setUrl(b.url);
    setNm(b.name);
    setIco(b.icon || '');
    setShowForm(true);
  };

  const del = id => save(bms.filter(b => b.id !== id));
  const fixUrl = u => (/^(https?:)?\/\//.test(u) ? u : `https://${u}`);

  if (!render) return null;
  const light = o.type === 'light';

  return createPortal(
    <div
      className={clsx(
        'fixed inset-0 z-[9999] flex items-center justify-center p-4 transition-opacity',
        anim ? 'opacity-100' : 'opacity-0',
      )}
    >
      <div className="fixed inset-0 bg-black/50" onClick={onClose} />

      <div
        className={clsx(
          'relative w-full max-w-2xl max-h-[80vh] rounded-lg border shadow-lg overflow-hidden transition-all',
          anim ? 'scale-100 opacity-100' : 'scale-95 opacity-0',
        )}
        style={{ backgroundColor: o.menuColor || '#1a252f' }}
      >
        <div
          className="flex items-center justify-between px-4 py-3 border-b"
          style={{ borderColor: light ? '#e5e7eb' : '#374151' }}
        >
          <div className="flex items-center gap-2">
            <h2 className="text-lg font-medium">
              {showForm ? (editId ? 'Edit Bookmark' : 'Create Bookmark') : 'Bookmarks'}
            </h2>
            {!showForm && (
              <button
                onClick={() => setShowForm(true)}
                className={clsx('p-1 rounded-md', light ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]')}
              >
                <Plus size={18} />
              </button>
            )}
          </div>
          <button
            onClick={onClose}
            className={clsx('p-1 rounded-md', light ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]')}
          >
            <X size={20} />
          </button>
        </div>

        <div className="p-4 overflow-y-auto max-h-[calc(80vh-4rem)]">
          {showForm ? (
            <div className="space-y-2">
              {[
                ['Bookmark Name', nm, setNm, PenLine],
                ['Bookmark URL', url, setUrl, Globe],
                ['Bookmark Icon (URL)', ico, setIco, Image],
              ].map(([ph, v, s, I], i) => (
                <div
                  key={i}
                  className={clsx(
                    'flex items-center gap-2 px-3 rounded-md border',
                    light ? 'bg-gray-50 border-gray-300' : 'bg-[#ffffff0c] border-gray-700',
                  )}
                >
                  <I size={16} />
                  <input
                    value={v}
                    onChange={e => s(e.target.value)}
                    placeholder={ph}
                    className="w-full py-2 text-sm bg-transparent outline-none"
                  />
                </div>
              ))}

              <div className="flex gap-2">
                <button
                  onClick={editId ? update : add}
                  disabled={!url.trim()}
                  className={clsx(
                    'flex-1 px-3 py-2 text-sm rounded-md flex items-center justify-center gap-2',
                    light ? 'bg-gray-200 hover:bg-gray-300' : 'bg-[#ffffff0c] hover:bg-[#ffffff15]',
                    !url.trim() && 'opacity-50 cursor-not-allowed',
                  )}
                >
                  {editId ? <Check size={16} /> : <Plus size={16} />}
                  {editId ? 'Save Changes' : 'Add Bookmark'}
                </button>
                <button
                  onClick={reset}
                  className={clsx(
                    'px-3 py-2 text-sm rounded-md',
                    light ? 'bg-gray-200 hover:bg-gray-300' : 'bg-[#ffffff0c] hover:bg-[#ffffff15]',
                  )}
                >
                  Cancel
                </button>
              </div>
            </div>
          ) : bms.length === 0 ? (
            <p className="text-sm text-gray-500">No bookmarks yet.</p>
          ) : (
            <div className="space-y-2">
              {bms.map(b => (
                <div
                  key={b.id}
                  onClick={() => {
                    if (inLoader) {
                      const activeTab = tabs.find(t => t.active);
                      if (activeTab) {
                        const processedUrl = process(fixUrl(b.url), false, o.prType || 'scr', o.engine || null);
                        if (processedUrl) {
                          setLoading(activeTab.id, true);
                          updateUrl(activeTab.id, processedUrl);
                        }
                        onClose();
                      }
                    } else {
                      nav('/search', { state: { url: fixUrl(b.url) } });
                    }
                  }}
                  className={clsx(
                    'flex items-center gap-3 px-3 py-2 rounded-md cursor-pointer',
                    light ? 'hover:bg-gray-100' : 'hover:bg-[#ffffff0c]',
                  )}
                >
                  <div className="w-5 h-5 relative flex-shrink-0">
                    {b.icon && !badIcons.has(b.id) ? (
                      <>
                        {!loadedIcons.has(b.id) && (
                          <div className="absolute inset-0 bg-gray-600 rounded animate-pulse" />
                        )}
                        <img
                          src={b.icon}
                          className="w-5 h-5 relative"
                          onLoad={() => setLoadedIcons(p => new Set(p).add(b.id))}
                          onError={() => setBadIcons(p => new Set(p).add(b.id))}
                        />
                      </>
                    ) : (
                      <Globe size={20} />
                    )}
                  </div>

                  <div className="flex-1 min-w-0">
                    <div className="text-sm font-medium truncate">{b.name}</div>
                    <div className="text-xs text-gray-500 truncate">{b.url}</div>
                  </div>

                  <button
                    onClick={e => { e.stopPropagation(); edit(b); }}
                    className={clsx('p-1.5 rounded-md', light ? 'hover:bg-gray-200' : 'hover:bg-[#ffffff15]')}
                  >
                    <Pencil size={16} />
                  </button>
                  <button
                    onClick={e => { e.stopPropagation(); del(b.id); }}
                    className={clsx('p-1.5 rounded-md', light ? 'hover:bg-gray-200' : 'hover:bg-[#ffffff15]')}
                  >
                    <Trash2 size={16} />
                  </button>
                </div>
              ))}
            </div>
          )}
        </div>
      </div>
    </div>,
    document.body
  );
};

export default Bookmarks;