From 17bbe54cb1e3cbbea8c7e042dc4eeeed15161f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C4=B0pek?= Date: Sun, 23 Nov 2025 13:08:25 +0300 Subject: [PATCH] refactor: Pass user profile as a prop to the recommendations section and remove the manual refresh button. --- src/components/RecommendationsSection.tsx | 46 +++++++++++------------ src/components/RepoHubApp.tsx | 23 ++++++------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/components/RecommendationsSection.tsx b/src/components/RecommendationsSection.tsx index 0d97c5f..0165db1 100644 --- a/src/components/RecommendationsSection.tsx +++ b/src/components/RecommendationsSection.tsx @@ -3,7 +3,7 @@ import { useState, useEffect, useMemo } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' -import { Sparkles, RefreshCw, Settings, Package as PackageIcon, Star, Grid3x3, List, TrendingUp, Award, ChevronDown, ChevronUp } from 'lucide-react' +import { Sparkles, Settings, Package as PackageIcon, Star, Grid3x3, List, TrendingUp, Award, ChevronDown, ChevronUp } from 'lucide-react' import { RecommendedPackage, UserCategory } from '@/types/recommendations' import { Package } from '@/types' import { useLocale } from '@/contexts/LocaleContext' @@ -12,10 +12,13 @@ import { CATEGORY_ICONS } from '@/constants/categoryIcons' import { RecommendationCard } from './RecommendationCard' import { RecommendationListItem } from './RecommendationListItem' +import { UserProfile } from '@/types/recommendations' + interface RecommendationsSectionProps { onPackageToggle: (pkg: Package) => void selectedPackages: Package[] onCustomizeClick: () => void + profile: UserProfile } type ViewMode = 'grid' | 'compact' @@ -25,17 +28,20 @@ type FilterCategory = 'all' | string export function RecommendationsSection({ onPackageToggle, selectedPackages, - onCustomizeClick + onCustomizeClick, + profile }: RecommendationsSectionProps) { const { t } = useLocale() - const { profile, getEffectiveOS, isProfileComplete } = useRecommendationProfile() + const { getEffectiveOS, isProfileComplete } = useRecommendationProfile() + // Override profile from hook with prop + const effectiveProfile = profile const [recommendations, setRecommendations] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [viewMode, setViewMode] = useState('grid') const [sortMode, setSortMode] = useState('recommended') const [filterCategory, setFilterCategory] = useState('all') - const [isExpanded, setIsExpanded] = useState(true) + const [isExpanded, setIsExpanded] = useState(false) const fetchRecommendations = async () => { if (!isProfileComplete()) { @@ -53,8 +59,8 @@ export function RecommendationsSection({ }, body: JSON.stringify({ platform_id: getEffectiveOS(), - categories: profile.categories, - experienceLevel: profile.experienceLevel, + categories: effectiveProfile.categories, + experienceLevel: effectiveProfile.experienceLevel, limit: 12 }) }) @@ -74,7 +80,8 @@ export function RecommendationsSection({ } const data = await response.json() - setRecommendations(data.recommendations || []) + const recs = data.recommendations || [] + setRecommendations(recs) } catch (err) { console.error('Error fetching recommendations:', err) @@ -92,9 +99,11 @@ export function RecommendationsSection({ // Fetch recommendations on mount and when profile changes useEffect(() => { if (isProfileComplete()) { + // If profile just changed (e.g. from customization), we might want to force refresh + // But for now, let's rely on the cache key changing which includes profile data fetchRecommendations() } - }, [profile.categories, profile.selectedOS, profile.experienceLevel]) + }, [effectiveProfile.categories, effectiveProfile.selectedOS, effectiveProfile.experienceLevel]) const isPackageSelected = (pkg: RecommendedPackage) => { return selectedPackages.some(selected => selected.id === pkg.id) @@ -196,18 +205,7 @@ export function RecommendationsSection({ ? (t('common.deselect_all') || 'Deselect All') : (t('common.select_all') || 'Select All')} - + - {profile.categories.map(cat => { + {effectiveProfile.categories.map(cat => { const count = getCategoryCount(cat) const Icon = CATEGORY_ICONS[cat] return ( @@ -368,7 +366,7 @@ export function RecommendationsSection({ {loading && (
- +

{t('recommendations.loading')}

)} @@ -376,7 +374,7 @@ export function RecommendationsSection({ {error && (

{error}

-
diff --git a/src/components/RepoHubApp.tsx b/src/components/RepoHubApp.tsx index 85cea16..574b31e 100644 --- a/src/components/RepoHubApp.tsx +++ b/src/components/RepoHubApp.tsx @@ -124,10 +124,10 @@ function RepoHubAppContent({ cryptomusEnabled }: { cryptomusEnabled: boolean }) if (!platformToUse && hasCompletedOnboarding) { // Get effective OS from profile and find matching platform from loaded platforms const effectiveOS = profile.selectedOS || detectedOS - + if (effectiveOS && availablePlatforms.length > 0) { platformToUse = availablePlatforms.find(p => p.id === effectiveOS) || null - + if (!platformToUse) { console.warn(`Platform not found for OS: ${effectiveOS}`) } @@ -179,6 +179,7 @@ function RepoHubAppContent({ cryptomusEnabled }: { cryptomusEnabled: boolean }) onPackageToggle={handlePackageToggle} selectedPackages={selectedPackages} onCustomizeClick={handleCustomizePreferences} + profile={profile} /> )} @@ -210,15 +211,15 @@ function RepoHubAppContent({ cryptomusEnabled }: { cryptomusEnabled: boolean }) p.id === generatedScript.platform) || - { - id: generatedScript.platform, - name: generatedScript.platform.charAt(0).toUpperCase() + generatedScript.platform.slice(1), - description: '', - icon: '', - packageManager: '' - } + selectedPlatform={selectedPlatform || + availablePlatforms.find(p => p.id === generatedScript.platform) || + { + id: generatedScript.platform, + name: generatedScript.platform.charAt(0).toUpperCase() + generatedScript.platform.slice(1), + description: '', + icon: '', + packageManager: '' + } } onClose={handleCloseScriptPreview} />