Skip to main content

Overview

Tag utilities provide functions for AI-powered tag generation, extracting available tags from bookmarks, and creating sorted tag lists with counts.

generateTags

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

bookmarkData
string
required
The bookmark content to analyze (typically a combination of URL, title, and description)
availableTags
string[]
required
Array of existing tags to consider when generating new tags
session
Session
required
Supabase session object containing the user’s access token for authentication

Return Value

tags
string[] | null
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

getAvailableTags

Extracts all unique tags from a collection of bookmarks.

Function Signature

const getAvailableTags = (bookmarks: Bookmark[]): string[]

Parameters

bookmarks
Bookmark[]
required
Array of bookmark objects to extract tags from

Return Value

tags
string[]
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

bookmarks
Bookmark[]
required
Array of bookmark objects to create tag counts from

Return Value

tags
Tag[]
Array of Tag objects sorted by count in descending order

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:
  1. Retrieves the current time period filter from the bookmark store (“all”, “week”, or “month”)
  2. Filters bookmarks based on the selected time period
  3. Counts occurrences of each tag across filtered bookmarks
  4. Adds a special “all” tag representing the total count of filtered bookmarks
  5. Returns tags sorted by count in descending order

Source Location

~/workspace/source/src/utils/createTagList.ts:19

Build docs developers (and LLMs) love