From 0d745e21744b5cdb29ddb50da55698740f118ee8 Mon Sep 17 00:00:00 2001 From: syui Date: Fri, 18 Jul 2025 15:06:21 +0900 Subject: [PATCH] Add favorite cards display on homepage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fetch and display users' favorite cards at the top of homepage - Filter users with fav \!== '0' to avoid empty display - Use useQueries to fetch multiple users' cards in parallel - Display cards in grid layout with owner username below - Cards are shown above the user list section 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/components/pages/DocsPage.tsx | 14 +-------- src/components/pages/HomePage.tsx | 49 +++++++++++++++++++++++++++++-- src/utils/api.ts | 10 +++++++ 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/src/components/pages/DocsPage.tsx b/src/components/pages/DocsPage.tsx index 2afa9c9..38806d9 100644 --- a/src/components/pages/DocsPage.tsx +++ b/src/components/pages/DocsPage.tsx @@ -85,18 +85,6 @@ export default function DocsPage({ isEnglish = false, page }: DocsPageProps) { -

{isEnglish ? 'Battle' : '対戦について'}

-

@yui.syui.ai /card -b

-

{isEnglish ? 'Random match, one of the top 3 cards on hand will be chosen at random' : 'ランダムマッチ、手持ちの上位3枚のうち1枚がランダムで選ばれます'}

- -

Mastodon

-

- - - @yui@syui.ai - /card - -

); }; @@ -112,4 +100,4 @@ export default function DocsPage({ isEnglish = false, page }: DocsPageProps) { ); -} \ No newline at end of file +} diff --git a/src/components/pages/HomePage.tsx b/src/components/pages/HomePage.tsx index 51353c2..4e8691b 100644 --- a/src/components/pages/HomePage.tsx +++ b/src/components/pages/HomePage.tsx @@ -1,6 +1,8 @@ -import { useQuery } from '@tanstack/react-query'; +import { useQuery, useQueries } from '@tanstack/react-query'; import Navigation from '../common/Navigation'; -import { fetchUsers } from '../../utils/api'; +import { fetchUsers, fetchUserCards } from '../../utils/api'; +import SpecialCard from '../card/SpecialCard'; +import type { Card } from '../../types'; export default function HomePage() { @@ -9,6 +11,30 @@ export default function HomePage() { queryFn: () => fetchUsers() }); + // Get all users with fav cards + const usersWithFav = users?.data?.filter(user => user.fav && user.fav !== '0') || []; + + // Fetch cards for each user with fav + const favCardQueries = useQueries({ + queries: usersWithFav.map(user => ({ + queryKey: ['userCards', user.id], + queryFn: () => fetchUserCards(user.id), + enabled: !!user.id + })) + }); + + // Extract fav cards + const favCards: { user: typeof usersWithFav[0], card: Card }[] = []; + usersWithFav.forEach((user, index) => { + const userCards = favCardQueries[index]?.data?.data; + if (userCards && user.fav) { + const favCard = userCards.find(card => card.id === parseInt(user.fav)); + if (favCard) { + favCards.push({ user, card: favCard }); + } + } + }); + if (isLoading) { return (
@@ -28,6 +54,25 @@ export default function HomePage() { help
+ {/* Favorite Cards Section */} + {favCards.length > 0 && ( +
+

Favorite Cards

+
+ {favCards.map(({ user, card }) => ( +
+ + +
+ ))} +
+
+ )} + {users?.data && Array.isArray(users.data) && users.data.length > 0 && (
diff --git a/src/utils/api.ts b/src/utils/api.ts index f146d36..430e630 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -52,6 +52,16 @@ export const fetchUserCards = async (userId: number, itemsPerPage = 8000): Promi } }; +export const fetchCardById = async (cardId: number): Promise => { + try { + const response = await api.get(`cards/${cardId}`); + return response.data; + } catch (error) { + console.error('Failed to fetch card by ID:', error); + return null; + } +}; + export const fetchUser = async (userId: number): Promise<{ data: User }> => { const response = await api.get(`users/${userId}`); return response.data;