diff --git a/src/app/(main)/character/[id]/page.tsx b/src/app/(main)/character/[id]/page.tsx
index 3d345b5..934eeb9 100644
--- a/src/app/(main)/character/[id]/page.tsx
+++ b/src/app/(main)/character/[id]/page.tsx
@@ -5,7 +5,7 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { IconButton } from '@/components/ui/button';
import Link from 'next/link';
import { getTranslations } from 'next-intl/server';
-import LikedIcon from '@/components/features/LikedIcon';
+import LikedButton from '@/components/features/LikeButton';
import { LikeTargetType } from '@/services/editor/type';
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
@@ -20,11 +20,7 @@ export default async function Page({ params }: { params: Promise<{ id: string }>
-
+
{/* 内容区 */}
diff --git a/src/app/(main)/chat/[id]/Drawer/index.tsx b/src/app/(main)/chat/[id]/Drawer/index.tsx
index fcf3d5e..ce8f427 100644
--- a/src/app/(main)/chat/[id]/Drawer/index.tsx
+++ b/src/app/(main)/chat/[id]/Drawer/index.tsx
@@ -23,7 +23,7 @@ import IconFont from '@/components/ui/iconFont';
import MaskCreate from './MaskCreate';
import { useTranslations } from 'next-intl';
import { useParams } from 'next/navigation';
-import LikedIcon from '@/components/features/LikedIcon';
+import LikedButton from '@/components/features/LikeButton';
import { LikeTargetType } from '@/services/editor/type';
type SettingProps = {
@@ -71,7 +71,7 @@ export default function SettingDialog({ open, onOpenChange }: SettingProps) {
{activeTab === 'profile' ? (
-
+
) : (
titleMap[activeTab]
)}
diff --git a/src/components/features/LikedIcon.tsx b/src/components/features/LikeButton.tsx
similarity index 53%
rename from src/components/features/LikedIcon.tsx
rename to src/components/features/LikeButton.tsx
index 70e2609..dc14f6b 100644
--- a/src/components/features/LikedIcon.tsx
+++ b/src/components/features/LikeButton.tsx
@@ -4,29 +4,31 @@ import { LikeTargetType, LikeType } from '@/services/editor/type';
import { IconButton } from '../ui/button';
import { useThmubObject } from '@/hooks/services/common';
import React from 'react';
+import { cn } from '@/lib/utils';
type LikedIconProps = {
objectId: string;
objectType: LikeTargetType;
- iconProps?: React.ComponentProps;
-};
+} & React.ComponentProps;
-const LikedIcon = React.memo((props: LikedIconProps) => {
- const { objectId, objectType, iconProps } = props;
+const LikedButton = React.memo((props: LikedIconProps) => {
+ const { objectId, objectType, ...iconProps } = props;
const { thumb, handleThumb, loading } = useThmubObject({ objectId, objectType });
+ const isLiked = thumb === LikeType.Liked;
return (
{
if (loading) return;
- handleThumb(thumb === LikeType.Liked ? LikeType.Canceled : LikeType.Liked);
+ handleThumb(isLiked ? LikeType.Canceled : LikeType.Liked);
}}
/>
);
});
-export default LikedIcon;
+export default LikedButton;
diff --git a/src/hooks/services/common.ts b/src/hooks/services/common.ts
index 817d2e6..f9b18e7 100644
--- a/src/hooks/services/common.ts
+++ b/src/hooks/services/common.ts
@@ -1,30 +1,35 @@
import { getLikeStatus, thmubObject } from '@/services/editor';
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
-import { useState } from 'react';
+import { useEffect, useState } from 'react';
import { authKeys } from '@/lib/query-keys';
import { LikeObjectParamsType, LikeType } from '@/services/editor/type';
import { useAsyncFn } from '../tools';
+import { useCurrentUser } from '../auth';
export function useThmubObject(props: Pick) {
- const queryClient = useQueryClient();
const [thumb, setThumb] = useState();
- const user = queryClient.getQueryData(authKeys.currentUser()) as any;
+ const { data: user } = useCurrentUser();
- useQuery({
- queryKey: ['likeStatus', props.objectId, user.userId],
- enabled: !!props.objectId && !!user.userId,
- queryFn: () => {
- return getLikeStatus({ objectId: props.objectId, userId: user.userId });
+ const { data: likeStatus, refetch: refetchLikeStatus } = useQuery({
+ queryKey: ['likeStatus', props.objectId, user?.userId],
+ enabled: !!props.objectId && !!user?.userId,
+ queryFn: async () => {
+ const { data } = await getLikeStatus({ objectId: props.objectId, userId: user?.userId });
+ return data.likeStatus;
},
});
+ useEffect(() => {
+ if (typeof likeStatus === 'number') {
+ setThumb(likeStatus);
+ }
+ }, [likeStatus]);
+
const { run: handleThumb, loading } = useAsyncFn(async (likeType: LikeType) => {
setThumb(likeType);
- const user = queryClient.getQueryData(authKeys.currentUser()) as any;
- const { data } = await thmubObject({ ...props, likeType, userId: user?.userId });
- if (data.code === 200) {
- } else {
- setThumb(undefined);
+ const { code } = await thmubObject({ ...props, likeType, userId: user?.userId });
+ if (code === 200) {
+ refetchLikeStatus();
}
});
diff --git a/src/services/editor/index.ts b/src/services/editor/index.ts
index e8d6f96..b48f44a 100644
--- a/src/services/editor/index.ts
+++ b/src/services/editor/index.ts
@@ -23,6 +23,5 @@ export async function thmubObject(params: LikeObjectParamsType) {
}
export async function getLikeStatus(params: Pick) {
- const { data } = await editorRequest('/api/like/getLikeStatus', { method: 'POST', data: params });
- return data;
+ return editorRequest('/api/like/getLikeStatus', { method: 'POST', data: params });
}