refactor(favorites): replace localStorage with centralized favorite store

This commit is contained in:
zetaloop
2026-02-22 08:29:37 +08:00
parent 8ce3b8a8b5
commit 237cf90f5e
3 changed files with 72 additions and 34 deletions
+9 -25
View File
@@ -1,10 +1,10 @@
"use client"
import { Heart } from "lucide-react"
import { useEffect, useMemo, useState } from "react"
import { Button } from "@/components/ui/button"
import { useRequireAuth } from "@/lib/use-require-auth"
import { useAuthStore } from "@/store/auth"
import { useFavoriteStore } from "@/store/favorites"
interface FavoriteButtonProps {
initialFavorited: boolean
@@ -13,26 +13,13 @@ interface FavoriteButtonProps {
}
export function FavoriteButton({ initialFavorited, targetType, targetId }: FavoriteButtonProps) {
const [favorited, setFavorited] = useState(initialFavorited)
const { requireAuth } = useRequireAuth()
const userId = useAuthStore((s) => s.user?.id)
const storageKey = useMemo(
() => `favorite:${userId ?? "guest"}:${targetType}:${targetId}`,
[userId, targetType, targetId],
const favoritedInStore = useFavoriteStore((state) =>
userId ? state.isFavorited(userId, targetType, targetId) : false,
)
useEffect(() => {
const stored = window.localStorage.getItem(storageKey)
if (stored === "1") {
setFavorited(true)
return
}
if (stored === "0") {
setFavorited(false)
return
}
setFavorited(initialFavorited)
}, [storageKey, initialFavorited])
const toggleFavorite = useFavoriteStore((state) => state.toggleFavorite)
const favorited = userId ? favoritedInStore : initialFavorited
return (
<Button
@@ -40,13 +27,10 @@ export function FavoriteButton({ initialFavorited, targetType, targetId }: Favor
size="sm"
className="gap-1.5"
onClick={() =>
requireAuth(() =>
setFavorited((prev) => {
const next = !prev
window.localStorage.setItem(storageKey, next ? "1" : "0")
return next
}),
)
requireAuth(() => {
if (!userId) return
toggleFavorite(userId, targetType, targetId)
})
}
>
<Heart className={`h-4 w-4 ${favorited ? "fill-current" : ""}`} />
+6 -4
View File
@@ -1,15 +1,17 @@
import { mockFavorites } from "@/lib/mock"
import { useFavoriteStore } from "@/store/favorites"
export function listFavorites() {
return mockFavorites
return useFavoriteStore.getState().favorites
}
export function listFavoritesByUser(userId: string) {
return mockFavorites.filter((favorite) => favorite.userId === userId)
return useFavoriteStore.getState().favorites.filter((favorite) => favorite.userId === userId)
}
export function isFavorited(userId: string, targetType: "player" | "shop", targetId: string) {
return mockFavorites.some(
return useFavoriteStore
.getState()
.favorites.some(
(favorite) =>
favorite.userId === userId &&
favorite.targetType === targetType &&
+52
View File
@@ -0,0 +1,52 @@
import { create } from "zustand"
import { generateId } from "@/lib/id"
import { mockFavorites } from "@/lib/mock"
import type { Favorite } from "@/lib/types"
interface FavoriteState {
favorites: Favorite[]
listFavoritesByUser: (userId: string) => Favorite[]
isFavorited: (userId: string, targetType: "player" | "shop", targetId: string) => boolean
toggleFavorite: (userId: string, targetType: "player" | "shop", targetId: string) => boolean
}
export const useFavoriteStore = create<FavoriteState>((set, get) => ({
favorites: mockFavorites,
listFavoritesByUser: (userId) => get().favorites.filter((favorite) => favorite.userId === userId),
isFavorited: (userId, targetType, targetId) =>
get().favorites.some(
(favorite) =>
favorite.userId === userId &&
favorite.targetType === targetType &&
favorite.targetId === targetId,
),
toggleFavorite: (userId, targetType, targetId) => {
const state = get()
const existing = state.favorites.find(
(favorite) =>
favorite.userId === userId &&
favorite.targetType === targetType &&
favorite.targetId === targetId,
)
if (existing) {
set((prev) => ({
favorites: prev.favorites.filter((favorite) => favorite.id !== existing.id),
}))
return false
}
const next: Favorite = {
id: generateId("fav"),
userId,
targetType,
targetId,
createdAt: new Date().toISOString(),
}
set((prev) => ({
favorites: [next, ...prev.favorites],
}))
return true
},
}))