forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookmarkButton.tsx
More file actions
228 lines (213 loc) 路 7.46 KB
/
Copy pathBookmarkButton.tsx
File metadata and controls
228 lines (213 loc) 路 7.46 KB
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
'use client';
import { useCallback, useEffect, useRef, useState } from 'react';
import { usePathname } from 'next/navigation';
import {
deleteBookmark,
getBookmarks,
renameBookmark,
saveBookmark,
type Bookmark,
} from '@/lib/bookmarks';
function StarIcon({ filled }: { filled: boolean }) {
return (
<svg
width={18}
height={18}
viewBox="0 0 24 24"
fill={filled ? 'currentColor' : 'none'}
stroke="currentColor"
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
>
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2" />
</svg>
);
}
export default function BookmarkButton() {
const pathname = usePathname();
const [bookmarks, setBookmarks] = useState<Bookmark[]>([]);
const [open, setOpen] = useState(false);
const [renaming, setRenaming] = useState<string | null>(null);
const [renameValue, setRenameValue] = useState('');
const dropdownRef = useRef<HTMLDivElement>(null);
const refresh = useCallback(() => setBookmarks(getBookmarks()), []);
useEffect(() => {
refresh();
}, [refresh]);
// Close dropdown on outside click
useEffect(() => {
const handler = (e: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
setOpen(false);
}
};
document.addEventListener('mousedown', handler);
return () => document.removeEventListener('mousedown', handler);
}, []);
const isBookmarked = bookmarks.some((b) => b.url === pathname);
const toggleBookmark = () => {
if (isBookmarked) {
const existing = bookmarks.find((b) => b.url === pathname);
if (existing) {
deleteBookmark(existing.id);
refresh();
}
} else {
const pageName =
pathname === '/'
? 'Overview'
: pathname.slice(1).replace(/-/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase());
saveBookmark({ name: pageName, url: pathname });
refresh();
}
};
const handleRename = (id: string) => {
if (renameValue.trim()) {
renameBookmark(id, renameValue.trim());
refresh();
}
setRenaming(null);
setRenameValue('');
};
return (
<div ref={dropdownRef} style={{ position: 'relative' }}>
{/* Star toggle */}
<button
type="button"
onClick={toggleBookmark}
title={isBookmarked ? 'Remove bookmark' : 'Bookmark this page'}
className={`flex h-8 w-8 items-center justify-center rounded-lg border transition-colors ${
isBookmarked
? 'border-amber-300 bg-amber-50 text-amber-500 dark:border-amber-700 dark:bg-amber-950 dark:text-amber-400'
: 'border-gray-200 bg-gray-50 text-gray-500 hover:bg-gray-100 hover:text-gray-700 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700'
}`}
aria-label={isBookmarked ? 'Remove bookmark' : 'Bookmark this page'}
>
<StarIcon filled={isBookmarked} />
</button>
{/* Dropdown trigger */}
{bookmarks.length > 0 && (
<button
type="button"
onClick={() => setOpen((v) => !v)}
title="Saved bookmarks"
className="ml-1 flex h-8 items-center gap-1 rounded-lg border border-gray-200 bg-gray-50 px-2 text-xs font-semibold text-gray-600 transition-colors hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700"
aria-haspopup="true"
aria-expanded={open}
>
{bookmarks.length}
<svg width={12} height={12} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2.5}>
<polyline points="6 9 12 15 18 9" />
</svg>
</button>
)}
{/* Dropdown panel */}
{open && (
<div
role="menu"
style={{
position: 'absolute',
top: 'calc(100% + 8px)',
right: 0,
zIndex: 60,
minWidth: 280,
background: '#ffffff',
borderRadius: 16,
border: '1px solid #e2e8f0',
boxShadow: '0 16px 40px rgba(15,23,42,0.14)',
padding: '10px 0',
}}
>
<div
style={{
padding: '6px 14px 10px',
fontSize: 11,
fontWeight: 700,
textTransform: 'uppercase',
letterSpacing: '0.08em',
color: '#94a3b8',
}}
>
Saved views
</div>
{bookmarks.map((bookmark) => (
<div
key={bookmark.id}
role="menuitem"
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '8px 14px',
}}
>
{renaming === bookmark.id ? (
<input
autoFocus
value={renameValue}
onChange={(e) => setRenameValue(e.target.value)}
onBlur={() => handleRename(bookmark.id)}
onKeyDown={(e) => {
if (e.key === 'Enter') handleRename(bookmark.id);
if (e.key === 'Escape') { setRenaming(null); setRenameValue(''); }
}}
style={{
flex: 1,
borderRadius: 8,
border: '1px solid #6366f1',
padding: '4px 8px',
fontSize: 13,
outline: 'none',
}}
/>
) : (
<a
href={bookmark.url}
style={{
flex: 1,
fontSize: 13,
fontWeight: 600,
color: '#1e293b',
textDecoration: 'none',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
}}
onClick={() => setOpen(false)}
>
{bookmark.name}
</a>
)}
{/* Rename */}
<button
type="button"
title="Rename"
onClick={() => { setRenaming(bookmark.id); setRenameValue(bookmark.name); }}
style={{ background: 'none', border: 'none', cursor: 'pointer', color: '#94a3b8', padding: 2 }}
>
<svg width={13} height={13} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2}>
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7" />
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z" />
</svg>
</button>
{/* Delete */}
<button
type="button"
title="Delete"
onClick={() => { deleteBookmark(bookmark.id); refresh(); }}
style={{ background: 'none', border: 'none', cursor: 'pointer', color: '#f87171', padding: 2 }}
>
<svg width={13} height={13} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2}>
<polyline points="3 6 5 6 21 6" />
<path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6" />
<path d="M10 11v6M14 11v6" />
</svg>
</button>
</div>
))}
</div>
)}
</div>
);
}