Overview
Tag utilities provide functions for AI-powered tag generation, extracting available tags from bookmarks, and creating sorted tag lists with counts.
Generates relevant tags for a bookmark using AI based on the bookmark’s content and existing tags.
Function Signature
const generateTags = async (
bookmarkData: string,
availableTags: string[],
session: Session
): Promise<string[] | null>
Parameters
The bookmark content to analyze (typically a combination of URL, title, and description)
Array of existing tags to consider when generating new tags
Supabase session object containing the user’s access token for authentication
Return Value
Returns an array of generated tag strings on success, or null if the generation fails
Usage Example
import generateTags from "../utils/generateTags"
import getAvailableTags from "../utils/getAvailableTags"
import { showErrorToast, showSuccessToast, showLoadingToast } from "../utils/showToast"
const generateBookmarkTags = async (bookmark: Bookmark, session: Session) => {
const toastId = showLoadingToast("Crafting the perfect tags for you...")
// Get all available tags from existing bookmarks
const availableTags = getAvailableTags(bookmarks)
// Generate tags based on bookmark content
const newTags = await generateTags(
`${bookmark.url} ${bookmark.title} ${bookmark.description}`,
availableTags,
session
)
if (!newTags) {
return showErrorToast("Failed to generate tags. Please try again later.", { id: toastId })
}
// Update bookmark with generated tags
const updatedBookmark = { ...bookmark, tags: newTags }
const response = await updateBookmark(bookmark.id, updatedBookmark)
if (!response.success) {
return showErrorToast(response.data, { id: toastId })
}
showSuccessToast("Tags generated successfully!", { id: toastId })
}
Source Location
~/workspace/source/src/utils/generateTags.ts:4
Extracts all unique tags from a collection of bookmarks.
Function Signature
const getAvailableTags = (bookmarks: Bookmark[]): string[]
Parameters
Array of bookmark objects to extract tags from
Return Value
Array of unique tag strings found across all bookmarks
Usage Example
import getAvailableTags from "../utils/getAvailableTags"
const availableTags = getAvailableTags(bookmarks)
// Returns: ["react", "typescript", "tutorial", "design", ...]
Source Location
~/workspace/source/src/utils/getAvailableTags.ts:1
createTagList
Creates a sorted list of tags with their usage counts, filtered by the current time period.
Function Signature
const createTagList = (bookmarks: Bookmark[]): Tag[]
Parameters
Array of bookmark objects to create tag counts from
Return Value
Array of Tag objects sorted by count in descending order
Number of bookmarks with this tag (filtered by time period)
Usage Example
import createTagList from "../utils/createTagList"
const tagList = createTagList(bookmarks)
// Returns:
// [
// { name: "all", count: 150 },
// { name: "react", count: 45 },
// { name: "typescript", count: 32 },
// { name: "design", count: 28 },
// ...
// ]
Implementation Details
The function:
- Retrieves the current time period filter from the bookmark store (“all”, “week”, or “month”)
- Filters bookmarks based on the selected time period
- Counts occurrences of each tag across filtered bookmarks
- Adds a special “all” tag representing the total count of filtered bookmarks
- Returns tags sorted by count in descending order
Source Location
~/workspace/source/src/utils/createTagList.ts:19