perf: Optimize recommendation system and improve error handling

- Fix N+1 query problem in preset package fetching
  * Search with limit 5 to find best matches
  * Case-insensitive package name matching
  * Fallback to most popular when no exact match

- Improve category filtering accuracy
  * Update category mapping to match database schema
  * Remove duplicate packages in category fetching
  * Better distribution across categories

- Add version control for localStorage profile
  * Support future schema migrations
  * Auto-migrate old profiles to new version
  * Persist version in localStorage

- Enhance error handling
  * Specific error messages for 400/500/404 responses
  * Network error detection
  * User-friendly error messages in UI

- Fix OS detection fallback
  * Default to ubuntu when detection returns unknown
  * Prevent empty OS selection in onboarding

- Add better API validation
  * Explicit limit range validation (1-50)
  * Clear error messages for invalid inputs
This commit is contained in:
ersaayan
2025-11-22 17:47:52 +03:00
parent bcf8652728
commit f8fffdce83
6 changed files with 88 additions and 30 deletions
+12
View File
@@ -57,11 +57,14 @@ function detectOS(): string {
return "unknown";
}
const CURRENT_PROFILE_VERSION = 1;
/**
* Get default user profile
*/
function getDefaultProfile(): UserProfile {
return {
version: CURRENT_PROFILE_VERSION,
categories: [],
detectedOS: detectOS(),
selectedOS: undefined,
@@ -86,6 +89,13 @@ export function useRecommendationProfile() {
if (stored) {
const parsed = JSON.parse(stored) as UserProfile;
// Handle version migration
if (!parsed.version || parsed.version < CURRENT_PROFILE_VERSION) {
console.log("Migrating profile from version", parsed.version || 0, "to", CURRENT_PROFILE_VERSION);
// Add migration logic here when schema changes in the future
parsed.version = CURRENT_PROFILE_VERSION;
}
// Update detectedOS if it changed
const currentOS = detectOS();
if (parsed.detectedOS !== currentOS) {
@@ -93,6 +103,8 @@ export function useRecommendationProfile() {
}
setProfile(parsed);
// Save migrated profile
localStorage.setItem(STORAGE_KEY, JSON.stringify(parsed));
} else {
// First time user - save default profile
const defaultProfile = getDefaultProfile();