forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmarks.ts
More file actions
36 lines (31 loc) 路 1.12 KB
/
Copy pathbookmarks.ts
File metadata and controls
36 lines (31 loc) 路 1.12 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
const STORAGE_KEY = 'gistpin-bookmarks';
const MAX_BOOKMARKS = 10;
export interface Bookmark {
id: string;
name: string;
url: string;
timestamp: number;
}
export function getBookmarks(): Bookmark[] {
if (typeof window === 'undefined') return [];
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]') as Bookmark[];
} catch {
return [];
}
}
export function saveBookmark(bookmark: Omit<Bookmark, 'id' | 'timestamp'>): Bookmark | null {
const bookmarks = getBookmarks();
if (bookmarks.length >= MAX_BOOKMARKS) return null;
const next: Bookmark = { ...bookmark, id: crypto.randomUUID(), timestamp: Date.now() };
localStorage.setItem(STORAGE_KEY, JSON.stringify([...bookmarks, next]));
return next;
}
export function deleteBookmark(id: string): void {
const bookmarks = getBookmarks().filter((b) => b.id !== id);
localStorage.setItem(STORAGE_KEY, JSON.stringify(bookmarks));
}
export function renameBookmark(id: string, name: string): void {
const bookmarks = getBookmarks().map((b) => (b.id === id ? { ...b, name } : b));
localStorage.setItem(STORAGE_KEY, JSON.stringify(bookmarks));
}