feat: 앱 오늘 피드를 웹과 맞춤 — 히어로 오늘/내일 전환·좋아요·댓글·보내기·점수 근거 창, 오늘의 각 카드(gak_post), 날씨각 카드(그림 슬라이드), 행사·TOP5 띠 없앰, 좋아요·댓글 로그인 없이 (GA hero_day_switch, like hero_post·gak_post)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
e8b8cc79b6
commit
01b5f917b9
16 files changed
+449
-156
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { likePostMutation, unlikePostMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
||||
import type { PostReaction, ResFeed } from '@nalssigak/shared/api/client/types.gen';
|
||||
import type { PinnedKey } from '@nalssigak/shared/lib/analytics';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useState } from 'react';
|
||||
|
||||
import { track } from '@/lib/analytics';
|
||||
|
||||
/** 히어로·오늘의 각처럼 포스트 틀이 아닌 카드의 좋아요 (웹 pinnedLike.ts). 로그인 없이 된다. 화면을 먼저 바꾸고 실패하면 되돌린다 */
|
||||
export function usePinnedLike(key: PinnedKey, r?: PostReaction | null) {
|
||||
const queryClient = useQueryClient();
|
||||
const like = useMutation(likePostMutation());
|
||||
const unlike = useMutation(unlikePostMutation());
|
||||
const [failed, setFailed] = useState(false);
|
||||
const liked = !!r?.liked;
|
||||
const count = r?.like_count ?? 0;
|
||||
const patch = (values: Partial<PostReaction>) =>
|
||||
queryClient.setQueriesData<ResFeed>({ predicate: (q) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'getFeed' }, (feed) =>
|
||||
feed?.[key] && feed[key].post_id === r?.post_id ? { ...feed, [key]: { ...feed[key], ...values } } : feed,
|
||||
);
|
||||
const toggle = () => {
|
||||
if (!r || like.isPending || unlike.isPending) return;
|
||||
const on = !liked;
|
||||
setFailed(false);
|
||||
patch({ liked: on, like_count: count + (on ? 1 : -1) });
|
||||
const revert = () => {
|
||||
patch({ liked, like_count: count });
|
||||
setFailed(true);
|
||||
};
|
||||
(on ? like : unlike).mutate(
|
||||
{ path: { post_id: r.post_id } },
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
if (!res.result?.success) return revert();
|
||||
patch({ liked: res.liked ?? on, ...(res.like_count != null ? { like_count: res.like_count } : {}) });
|
||||
if (on) track('like', { content_type: key, item_id: r.post_id });
|
||||
},
|
||||
onError: revert,
|
||||
},
|
||||
);
|
||||
};
|
||||
return { liked, count, failed, toggle };
|
||||
}
|
||||
Reference in new issue
Block a user