- Add NODE_ENV=production to .env for environment configuration - Enhance product API route with JSON cache manager for local storage and retrieval - Introduce cache-only mode and detailed logging for better performance and offline support - Update UI to display cache status and storage statistics
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { Product } from "./openFoodFactsApi";
|
|
|
|
/**
|
|
* Calcula score de relevância de um produto em relação à query
|
|
*/
|
|
export function calculateRelevanceScore(
|
|
product: Product,
|
|
searchTerm: string
|
|
): number {
|
|
const productName = product.product_name?.toLowerCase() || "";
|
|
const lowerSearchTerm = searchTerm.toLowerCase();
|
|
|
|
// Nome exato = maior prioridade
|
|
if (productName === lowerSearchTerm) {
|
|
return 3;
|
|
}
|
|
|
|
// Nome começa com a query = alta prioridade
|
|
if (productName.startsWith(lowerSearchTerm)) {
|
|
return 2;
|
|
}
|
|
|
|
// Nome contém a query = média prioridade
|
|
if (productName.includes(lowerSearchTerm)) {
|
|
return 1;
|
|
}
|
|
|
|
// Correspondência em marca ou categoria = baixa prioridade
|
|
if (
|
|
product.brands?.toLowerCase().includes(lowerSearchTerm) ||
|
|
product.categories?.toLowerCase().includes(lowerSearchTerm)
|
|
) {
|
|
return 0.5;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Ordena produtos por relevância em relação à query
|
|
*/
|
|
export function sortByRelevance(
|
|
products: Product[],
|
|
searchTerm: string
|
|
): Product[] {
|
|
return products
|
|
.map((product) => ({
|
|
product,
|
|
relevanceScore: calculateRelevanceScore(product, searchTerm),
|
|
}))
|
|
.sort((a, b) => b.relevanceScore - a.relevanceScore)
|
|
.map(({ product }) => product);
|
|
}
|