From 0e09e9e0277fa3d13f976e4a88fc4a39307a67b4 Mon Sep 17 00:00:00 2001 From: Yihao Wang Date: Sun, 13 Jul 2025 22:50:26 +1200 Subject: [PATCH] Add bookmark helper functions --- src/utils/bookmark.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils/bookmark.ts diff --git a/src/utils/bookmark.ts b/src/utils/bookmark.ts new file mode 100644 index 0000000..125e752 --- /dev/null +++ b/src/utils/bookmark.ts @@ -0,0 +1,32 @@ +import { DefinedTool } from '@tools/defineTool'; + +const bookmarkedToolsKey = 'bookmarkedTools'; + +export function getBookmarkedTools(): string[] { + return localStorage.getItem(bookmarkedToolsKey)?.split(',') ?? []; +} + +export function isBookmarked(tool: DefinedTool): boolean { + return getBookmarkedTools().some((path) => path === tool.path); +} + +export function toggleBookmarked(tool: DefinedTool) { + if (isBookmarked(tool)) { + unbookmark(tool); + } else { + bookmark(tool); + } +} + +function bookmark(tool: DefinedTool) { + localStorage.setItem( + bookmarkedToolsKey, + tool.path + localStorage.getItem(bookmarkedToolsKey) + ); +} + +function unbookmark(tool: DefinedTool) { + const bookmarked = localStorage.getItem(bookmarkedToolsKey)?.split(',') ?? []; + const unbookmarked = bookmarked.filter((path) => path != tool.path); + localStorage.setItem(bookmarkedToolsKey, unbookmarked.join(',')); +}