feat: persist pinned libraries per user with cross-device sync (#20)

Store pinned library IDs on the user record and expose GET/PUT
/me/pinned-libraries endpoints. The web client now loads and saves pins
through the API, migrates legacy localStorage data once, and applies
pinned ordering on both the libraries and home pages.

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: truewhile <truewhile@users.noreply.github.com>
This commit is contained in:
truewhile
2026-09-03 08:47:06 +08:00
committed by GitHub
parent 1025e3c693
commit 1e291df647
12 changed files with 367 additions and 15 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useCallback, useEffect, useState } from 'react'
import {
loadPinnedLibraryIds,
savePinnedLibraryIds,
togglePinnedLibraryId,
} from '../utils/pinnedLibraries'
export function usePinnedLibraries() {
const [pinnedIds, setPinnedIds] = useState<string[]>([])
const [loading, setLoading] = useState(true)
const [syncing, setSyncing] = useState(false)
useEffect(() => {
let cancelled = false
setLoading(true)
loadPinnedLibraryIds()
.then((ids) => {
if (!cancelled) setPinnedIds(ids)
})
.catch(() => {
if (!cancelled) setPinnedIds([])
})
.finally(() => {
if (!cancelled) setLoading(false)
})
return () => {
cancelled = true
}
}, [])
const togglePin = useCallback(async (libraryId: string) => {
let previous: string[] = []
let optimistic: string[] = []
setPinnedIds((current) => {
previous = current
optimistic = togglePinnedLibraryId(current, libraryId)
return optimistic
})
setSyncing(true)
try {
const saved = await savePinnedLibraryIds(optimistic)
setPinnedIds(saved)
} catch {
setPinnedIds(previous)
} finally {
setSyncing(false)
}
}, [])
return { pinnedIds, loading, syncing, togglePin }
}