Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/llms-txt-hub / apps/web/components/favorites/favorites-context.tsx |
| Date | Not specified in source |
| Repository | Janady13/llms-txt-hub |
| Revision | e0f4bf978d4df168369c7601d9c5f5ea89e84377 |
| Branch | main |
| Path or archive member | apps/web/components/favorites/favorites-context.tsx |
| Original SHA-256 | 35b897868664a02de5d27b56c2b782825f591acf9bc20a514789dda4703e6259 |
| Git blob SHA | d03d447eac7ba0dbac53e825d5dc5761de2dce56 |
| Reading-copy SHA-256 | 35b897868664a02de5d27b56c2b782825f591acf9bc20a514789dda4703e6259 |
Recovered source transcript
Download complete source text ↓110 lines · 2.8 KB
'use client'
import { useAuth } from '@thedaviddias/auth'
import { logger } from '@thedaviddias/logging'
import { createContext, useCallback, useContext, useEffect, useState } from 'react'
import {
getFavoritesFromStorage,
saveFavoritesToStorage,
toggleFavorite as toggleFavoriteLogic
} from '@/lib/favorites'
interface FavoritesContextType {
favorites: Set<string>
favoritesCount: number
isFavorite: (slug: string) => boolean
toggleFavorite: (slug: string) => void
isLoading: boolean
}
const FavoritesContext = createContext<FavoritesContextType | undefined>(undefined)
/**
* Provides favorites state and actions to child components
*/
export function FavoritesProvider({ children }: { children: React.ReactNode }) {
const [favorites, setFavorites] = useState<Set<string>>(new Set())
const [isLoading, setIsLoading] = useState(true)
const { user } = useAuth()
// Initialize favorites from localStorage on mount
useEffect(() => {
const storedFavorites = getFavoritesFromStorage()
setFavorites(new Set(storedFavorites))
setIsLoading(false)
}, [])
// Sync with user profile when user changes
useEffect(() => {
if (!user) return
/**
* Syncs local favorites with the user's server-side profile
*/
const syncWithProfile = async () => {
try {
// TODO: Implement user profile sync
// For now, we rely on localStorage as the primary storage
logger.info('User profile sync not implemented yet')
} catch (error) {
logger.error('Failed to sync with user profile:', {
data: error,
tags: { type: 'component' }
})
}
}
syncWithProfile()
}, [user])
const isFavorite = useCallback((slug: string) => favorites.has(slug), [favorites])
const toggleFavorite = useCallback(
(slug: string) => {
setFavorites(prev => {
const current = Array.from(prev)
const updated = toggleFavoriteLogic(slug, current)
const newSet = new Set(updated)
// Save to localStorage
saveFavoritesToStorage(updated)
// If user is logged in, update their profile
if (user) {
// TODO: Update user profile
// This would require calling a server action or API route
}
return newSet
})
},
[user]
)
return (
<FavoritesContext.Provider
value={{
favorites,
favoritesCount: favorites.size,
isFavorite,
toggleFavorite,
isLoading
}}
>
{children}
</FavoritesContext.Provider>
)
}
/**
* Hook that provides access to the favorites context
*/
export function useFavorites() {
const context = useContext(FavoritesContext)
if (context === undefined) {
throw new Error('useFavorites must be used within a FavoritesProvider')
}
return context
}