fixed start page and scroll in modal window
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// src/hooks/useFeedActions.js
|
||||
import { useState, useCallback } from "react";
|
||||
import { useState, useCallback, useRef } from "react";
|
||||
import { useDeletePost } from "./useDeletePost";
|
||||
import { useFavorites } from "./useFavorites";
|
||||
import { usePostStatus } from "./usePostStatus";
|
||||
@@ -22,12 +22,18 @@ import { usePostStatus } from "./usePostStatus";
|
||||
* @param {Function} onAuthRequired — вызывается когда нужна авторизация
|
||||
* @param {boolean} isAuthenticated
|
||||
*/
|
||||
export function useFeedActions({ refresh, loadMyPosts, onAuthRequired, isAuthenticated, t }) {
|
||||
export function useFeedActions({ refresh, loadMyPosts, onAuthRequired, isAuthenticated, t, setLiked, setLikeCountAdj }) {
|
||||
const { deletePost } = useDeletePost();
|
||||
const { toggleFavorite } = useFavorites();
|
||||
const { markAsFound } = usePostStatus();
|
||||
|
||||
const [error, setError] = useState("");
|
||||
const likedRef = useRef(new Set());
|
||||
|
||||
const syncLikedRef = useCallback((prev) => {
|
||||
likedRef.current = prev instanceof Set ? prev : new Set(prev);
|
||||
return prev;
|
||||
}, []);
|
||||
|
||||
const afterMutation = useCallback(() => {
|
||||
refresh();
|
||||
@@ -39,13 +45,35 @@ export function useFeedActions({ refresh, loadMyPosts, onAuthRequired, isAuthent
|
||||
onAuthRequired();
|
||||
return;
|
||||
}
|
||||
const result = await toggleFavorite(id);
|
||||
if (result.success) {
|
||||
afterMutation();
|
||||
} else {
|
||||
setError(result.error);
|
||||
if (setLiked) {
|
||||
const wasCurrentlyLiked = likedRef.current.has(id);
|
||||
const delta = wasCurrentlyLiked ? -1 : 1;
|
||||
|
||||
setLiked(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) next.delete(id); else next.add(id);
|
||||
likedRef.current = next;
|
||||
return next;
|
||||
});
|
||||
if (setLikeCountAdj) {
|
||||
setLikeCountAdj(prev => ({ ...prev, [id]: (prev[id] || 0) + delta }));
|
||||
}
|
||||
|
||||
const result = await toggleFavorite(id);
|
||||
if (!result.success) {
|
||||
setLiked(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(id)) next.delete(id); else next.add(id);
|
||||
likedRef.current = next;
|
||||
return next;
|
||||
});
|
||||
if (setLikeCountAdj) {
|
||||
setLikeCountAdj(prev => ({ ...prev, [id]: (prev[id] || 0) - delta }));
|
||||
}
|
||||
setError(result.error);
|
||||
}
|
||||
}
|
||||
}, [isAuthenticated, onAuthRequired, toggleFavorite, afterMutation]);
|
||||
}, [isAuthenticated, onAuthRequired, toggleFavorite, setLiked, setLikeCountAdj]);
|
||||
|
||||
const handleDelete = useCallback(async (postId) => {
|
||||
if (!window.confirm(t('feed.confirmDelete'))) return;
|
||||
@@ -75,6 +103,7 @@ export function useFeedActions({ refresh, loadMyPosts, onAuthRequired, isAuthent
|
||||
handleMarkFound,
|
||||
handleNewAd: afterMutation,
|
||||
handleEditSave: afterMutation,
|
||||
syncLiked: syncLikedRef,
|
||||
error,
|
||||
clearError,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user