diff options
Diffstat (limited to 'app/src/lib/bookmark-storage.ts')
-rw-r--r-- | app/src/lib/bookmark-storage.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/app/src/lib/bookmark-storage.ts b/app/src/lib/bookmark-storage.ts new file mode 100644 index 0000000..2c4f397 --- /dev/null +++ b/app/src/lib/bookmark-storage.ts @@ -0,0 +1,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'); + } + } +}
\ No newline at end of file |