summaryrefslogtreecommitdiff
path: root/app/src/lib/bookmark-storage.ts
blob: 2c4f3976d3cbb46fd7cdcf723bf14cda664285b5 (plain)
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
import { TwitterBookmark, ProcessedBookmark, BookmarkSyncStatus } from './bookmark-models';

export class BookmarkStorageService {
  private static readonly STORAGE_KEYS = {
    BOOKMARKS: 'twitter_bookmarks',
    PROCESSED_BOOKMARKS: 'processed_bookmarks',
    SYNC_STATUS: 'sync_status',
  };

  static saveBookmarks(bookmarks: TwitterBookmark[]): void {
    if (typeof window === 'undefined') return;
    
    const existing = this.getBookmarks();
    const newBookmarks = bookmarks.filter(
      (newBookmark) => !existing.some((existing) => existing.id === newBookmark.id)
    );
    
    const allBookmarks = [...existing, ...newBookmarks];
    localStorage.setItem(this.STORAGE_KEYS.BOOKMARKS, JSON.stringify(allBookmarks));
  }

  static getBookmarks(): TwitterBookmark[] {
    if (typeof window === 'undefined') return [];
    const data = localStorage.getItem(this.STORAGE_KEYS.BOOKMARKS);
    return data ? JSON.parse(data) : [];
  }

  static getBookmarkById(id: string): TwitterBookmark | null {
    if (typeof window === 'undefined') return null;
    const bookmarks = this.getBookmarks();
    return bookmarks.find((bookmark) => bookmark.id === id) || null;
  }

  static clearAll(): void {
    if (typeof window === 'undefined') return;
    localStorage.removeItem(this.STORAGE_KEYS.BOOKMARKS);
    localStorage.removeItem(this.STORAGE_KEYS.PROCESSED_BOOKMARKS);
    localStorage.removeItem(this.STORAGE_KEYS.SYNC_STATUS);
  }

  static exportBookmarks(): string {
    if (typeof window === 'undefined') return '';
    const bookmarks = this.getBookmarks();
    const data = {
      bookmarks,
      exportedAt: new Date().toISOString(),
    };
    return JSON.stringify(data, null, 2);
  }

  static importBookmarks(jsonData: string): void {
    if (typeof window === 'undefined') return;
    try {
      const data = JSON.parse(jsonData);
      if (data.bookmarks) {
        localStorage.setItem(this.STORAGE_KEYS.BOOKMARKS, JSON.stringify(data.bookmarks));
      }
    } catch (error) {
      throw new Error('Invalid bookmark data format');
    }
  }
}