Files
otunes/apps/mobile/contexts/persistent-cache.tsx
T
joshuafhigginsandClaude Opus 5.5 Verified 22cb6b6ccc refactor: share logic duplicated across web, mobile, and Convex
Move queue rules, stream planning, stream URLs, share links, song search,
favorites optimistic updates, token parsing, and format badge marks into
@otunes/convex so each rule has one implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:22:19 -05:00

38 lines
972 B
TypeScript

import { useAuthToken } from "@convex-dev/auth/react"
import { userIdFromToken } from "@otunes/convex/client/auth-token"
import { createContext, useContext, useMemo, type ReactNode } from "react"
import { loadPersistentCache } from "@/lib/persistent-cache"
export const ANONYMOUS_CACHE_SCOPE = "anon"
const CacheScopeContext = createContext(ANONYMOUS_CACHE_SCOPE)
void loadPersistentCache()
/** Namespaces persisted client data by server and signed-in user. */
export function PersistentCacheProvider({
serverUrl,
children,
}: {
serverUrl: string
children: ReactNode
}) {
const token = useAuthToken()
const userId = userIdFromToken(token)
const scope = useMemo(
() => (userId ? `${serverUrl}|${userId}` : ANONYMOUS_CACHE_SCOPE),
[serverUrl, userId]
)
return (
<CacheScopeContext.Provider value={scope}>
{children}
</CacheScopeContext.Provider>
)
}
export function useCacheScope() {
return useContext(CacheScopeContext)
}