fix: refresh favorites after unfavorite

This commit is contained in:
TerryM
2026-06-05 18:16:33 +08:00
parent 486c09dd39
commit a9ec46e008
5 changed files with 88 additions and 21 deletions

View File

@@ -25,7 +25,7 @@ type FavoritesContextValue = {
pendingIds: Set<string>;
statusFor: (resourceId: string) => FavoriteStatus;
ensureFavoriteIds: (resourceIds: string[]) => Promise<void>;
toggleFavorite: (resourceId: string) => Promise<void>;
toggleFavorite: (resourceId: string) => Promise<boolean | null>;
markFavorite: (resourceId: string, favorited: boolean) => void;
};
@@ -158,17 +158,19 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
);
const runFavoriteMutation = useCallback(
async (resourceId: string) => {
if (!token) return;
async (resourceId: string): Promise<boolean | null> => {
if (!token) return null;
const currentlyFavorite = favoriteIds.has(resourceId);
const nextFavorited = !currentlyFavorite;
setPendingIds((prev) => new Set(prev).add(resourceId));
markFavorite(resourceId, !currentlyFavorite);
markFavorite(resourceId, nextFavorited);
try {
if (currentlyFavorite) await removeFavorite(token, resourceId);
else await addFavorite(token, resourceId);
showToast(
currentlyFavorite ? t("favoriteRemoved") : t("favoriteAdded"),
);
return nextFavorited;
} catch (error) {
markFavorite(resourceId, currentlyFavorite);
if (isFavoritesAuthError(error)) handleAuthError();
@@ -191,9 +193,9 @@ export function FavoritesProvider({ children }: { children: ReactNode }) {
pendingAfterLoginRef.current = resourceId;
openLoginModal();
showToast(t("favoriteLoginRequired"));
return;
return null;
}
await runFavoriteMutation(resourceId);
return runFavoriteMutation(resourceId);
},
[openLoginModal, runFavoriteMutation, showToast, status, t, token],
);