Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat: add like action in community detail (#10971)

Shinji-Lic11d802d2606
diff --git a/locales/zh-CN/discover.json b/locales/zh-CN/discover.json
index 8dfc296483b..9381566619a 100644
--- a/locales/zh-CN/discover.json
+++ b/locales/zh-CN/discover.json
@@ -2,9 +2,14 @@
"assistant.favorite": "收藏",
"assistant.favoriteFailed": "收藏失败",
"assistant.favoriteSuccess": "已收藏",
+ "assistant.like": "点赞",
+ "assistant.likeFailed": "点赞失败",
+ "assistant.likeSuccess": "已点赞",
"assistant.unfavorite": "取消收藏",
"assistant.unfavoriteFailed": "取消收藏失败",
"assistant.unfavoriteSuccess": "已取消收藏",
+ "assistant.unlike": "取消点赞",
+ "assistant.unlikeSuccess": "已取消点赞",
"assistants.addAgent": "添加助理",
"assistants.addAgentAndConverse": "添加助理并会话",
"assistants.addAgentSuccess": "添加成功",
diff --git a/src/app/(backend)/market/social/[[...segments]]/route.ts b/src/app/(backend)/market/social/[[...segments]]/route.ts
index aaf4b60e4a5..e0f089d65b8 100644
--- a/src/app/(backend)/market/social/[[...segments]]/route.ts
+++ b/src/app/(backend)/market/social/[[...segments]]/route.ts
@@ -80,20 +80,20 @@ export const POST = async (req: NextRequest, context: RouteContext) => {
// Like actions
case 'like': {
- const { targetType, targetId } = body;
- await market.likes.like(targetType, targetId);
+ const { targetType, targetId, identifier } = body;
+ await market.likes.like(targetType, identifier ?? targetId);
return NextResponse.json({ success: true });
}
case 'unlike': {
- const { targetType, targetId } = body;
- await market.likes.unlike(targetType, targetId);
+ const { targetType, targetId, identifier } = body;
+ await market.likes.unlike(targetType, identifier ?? targetId);
return NextResponse.json({ success: true });
}
case 'toggle-like': {
- const { targetType, targetId } = body;
- const result = await market.likes.toggleLike(targetType, targetId);
+ const { targetType, targetId, identifier } = body;
+ const result = await market.likes.toggleLike(targetType, identifier ?? targetId);
return NextResponse.json(result);
}
@@ -226,6 +226,18 @@ export const GET = async (req: NextRequest, context: RouteContext) => {
}
// Like queries
+ case 'like-status': {
+ const targetType = segments[1] as 'agent' | 'plugin';
+ const targetIdOrIdentifier = segments[2];
+ if (!accessToken) {
+ return NextResponse.json({ isLiked: false });
+ }
+ const isNumeric = /^\d+$/.test(targetIdOrIdentifier);
+ const targetValue = isNumeric ? Number(targetIdOrIdentifier) : targetIdOrIdentifier;
+ const result = await market.likes.checkLike(targetType, targetValue as number);
+ return NextResponse.json(result);
+ }
+
case 'liked-agents': {
const userId = Number(segments[1]);
const result = await market.likes.getUserLikedAgents(userId, paginationParams);
diff --git a/src/app/[variants]/(main)/community/(detail)/assistant/features/Header.tsx b/src/app/[variants]/(main)/community/(detail)/assistant/features/Header.tsx
index 55c5f8fd91e..2a702814471 100644
--- a/src/app/[variants]/(main)/community/(detail)/assistant/features/Header.tsx
+++ b/src/app/[variants]/(main)/community/(detail)/assistant/features/Header.tsx
@@ -70,6 +70,7 @@ const Header = memo<{ mobile?: boolean }>(({ mobile: isMobile }) => {
const { mobile = isMobile } = useResponsive();
const { isAuthenticated, signIn, session } = useMarketAuth();
const [favoriteLoading, setFavoriteLoading] = useState(false);
+ const [likeLoading, setLikeLoading] = useState(false);
// Set access token for social service
if (session?.accessToken) {
@@ -85,6 +86,15 @@ const Header = memo<{ mobile?: boolean }>(({ mobile: isMobile }) => {
const isFavorited = favoriteStatus?.isFavorited ?? false;
+ // Fetch like status
+ const { data: likeStatus, mutate: mutateLike } = useSWR(
+ identifier && isAuthenticated ? ['like-status', 'agent', identifier] : null,
+ () => socialService.checkLikeStatus('agent', identifier!),
+ { revalidateOnFocus: false },
+ );
+
+ const isLiked = likeStatus?.isLiked ?? false;
+
const handleFavoriteClick = async () => {
if (!isAuthenticated) {
await signIn();
@@ -111,6 +121,32 @@ const Header = memo<{ mobile?: boolean }>(({ mobile: isMobile }) => {
}
};
+ const handleLikeClick = async () => {
+ if (!isAuthenticated) {
+ await signIn();
+ return;
+ }
+
+ if (!identifier) return;
+
+ setLikeLoading(true);
+ try {
+ if (isLiked) {
+ await socialService.unlike('agent', identifier);
+ message.success(t('assistant.unlikeSuccess'));
+ } else {
+ await socialService.like('agent', identifier);
+ message.success(t('assistant.likeSuccess'));
+ }
+ await mutateLike();
+ } catch (error) {
+ console.error('Like action failed:', error);
+ message.error(t('assistant.likeFailed'));
+ } finally {
+ setLikeLoading(false);
+ }
+ };
+
const categories = useCategory();
const cate = categories.find((c) => c.key === category);
diff --git a/src/locales/default/discover.ts b/src/locales/default/discover.ts
index 0fb8afd54af..be5e6f70b61 100644
--- a/src/locales/default/discover.ts
+++ b/src/locales/default/discover.ts
@@ -2,9 +2,14 @@ export default {
'assistant.favorite': 'Save',
'assistant.favoriteFailed': 'Failed to save',
'assistant.favoriteSuccess': 'Saved',
+ 'assistant.like': 'Like',
+ 'assistant.likeFailed': 'Failed to like',
+ 'assistant.likeSuccess': 'Liked',
'assistant.unfavorite': 'Unsave',
'assistant.unfavoriteFailed': 'Failed to unsave',
'assistant.unfavoriteSuccess': 'Unsaved',
+ 'assistant.unlike': 'Unlike',
+ 'assistant.unlikeSuccess': 'Unliked',
'assistants.addAgent': 'Add Agent',
'assistants.addAgentAndConverse': 'Add Agent & Chat',
'assistants.addAgentSuccess': 'Added',
diff --git a/src/services/_url.ts b/src/services/_url.ts
index 09f9bf96b2c..b1866521924 100644
--- a/src/services/_url.ts
+++ b/src/services/_url.ts
@@ -96,6 +96,10 @@ export const MARKET_ENDPOINTS = {
like: withElectronProtocolIfElectron('/market/social/like'),
unlike: withElectronProtocolIfElectron('/market/social/unlike'),
toggleLike: withElectronProtocolIfElectron('/market/social/toggle-like'),
+ likeStatus: (targetType: 'agent' | 'plugin', targetIdOrIdentifier: number | string) =>
+ withElectronProtocolIfElectron(
+ `/market/social/like-status/${targetType}/${encodeURIComponent(targetIdOrIdentifier)}`,
+ ),
likedAgents: (userId: number) =>
withElectronProtocolIfElectron(`/market/social/liked-agents/${userId}`),
likedPlugins: (userId: number) =>
diff --git a/src/services/social.ts b/src/services/social.ts
index e93909a72dc..12ee80796cc 100644
--- a/src/services/social.ts
+++ b/src/services/social.ts
@@ -16,6 +16,10 @@ export interface FavoriteStatus {
isFavorited: boolean;
}
+export interface LikeStatus {
+ isLiked: boolean;
+}
+
export interface ToggleLikeResult {
liked: boolean;
}
@@ -262,23 +266,56 @@ class SocialService {
// ==================== Like ====================
- async like(targetType: SocialTargetType, targetId: number): Promise<void> {
+ async like(
+ targetType: SocialTargetType,
+ targetIdOrIdentifier: number | string,
+ ): Promise<void> {
+ const body =
+ typeof targetIdOrIdentifier === 'string'
+ ? { identifier: targetIdOrIdentifier, targetType }
+ : { targetId: targetIdOrIdentifier, targetType };
+
await this.request(MARKET_ENDPOINTS.like, {
- body: JSON.stringify({ targetId, targetType }),
+ body: JSON.stringify(body),
method: 'POST',
});
}
- async unlike(targetType: SocialTargetType, targetId: number): Promise<void> {
+ async unlike(
+ targetType: SocialTargetType,
+ targetIdOrIdentifier: number | string,
+ ): Promise<void> {
+ const body =
+ typeof targetIdOrIdentifier === 'string'
+ ? { identifier: targetIdOrIdentifier, targetType }
+ : { targetId: targetIdOrIdentifier, targetType };
+
await this.request(MARKET_ENDPOINTS.unlike, {
- body: JSON.stringify({ targetId, targetType }),
+ body: JSON.stringify(body),
method: 'POST',
});
}
- async toggleLike(targetType: SocialTargetType, targetId: number): Promise<ToggleLikeResult> {
+ async checkLikeStatus(
+ targetType: SocialTargetType,
+ targetIdOrIdentifier: number | string,
+ ): Promise<LikeStatus> {
+ return this.request(MARKET_ENDPOINTS.likeStatus(targetType, targetIdOrIdentifier), {
+ method: 'GET',
+ });
+ }
+
+ async toggleLike(
+ targetType: SocialTargetType,
+ targetIdOrIdentifier: number | string,
+ ): Promise<ToggleLikeResult> {
+ const body =
+ typeof targetIdOrIdentifier === 'string'
+ ? { identifier: targetIdOrIdentifier, targetType }
+ : { targetId: targetIdOrIdentifier, targetType };
+
return this.request(MARKET_ENDPOINTS.toggleLike, {
- body: JSON.stringify({ targetId, targetType }),
+ body: JSON.stringify(body),
method: 'POST',
});
}